Unity shader实现遮罩效果
程序员文章站
2024-01-27 21:15:46
本文实例为大家分享了unity shader实现遮罩效果的具体代码,供大家参考,具体内容如下
效果:
shader代码:
shader "cust...
本文实例为大家分享了unity shader实现遮罩效果的具体代码,供大家参考,具体内容如下
效果:
shader代码:
shader "custom/mask" { properties { _maintex ("base (rgb)", 2d) = "white" {}//目标图片,即需要被遮罩的图片 _masklayer("culling mask",2d) = "white"{}//混合的图片,设置为白色的图片,任何颜色与白色混合,其颜色不变 _cutoff("alpha cutoff",range(0,1)) = 0 } subshader { tags { "queue"="transparent" }//渲染队列设置为 以从后往前的顺序渲染透明物体 lighting off //关闭光照 zwrite off //关闭深度缓存 blend off //关闭混合 alphatest gequal[_cutoff] //启用alpha测试 pass{ settexture[_masklayer]{combine texture}//混合贴图 //混合贴图,previous为放置在前一序列这样在进行alphatest的时候会以这个图片为主来进行混合 settexture[_maintex]{combine texture,previous} } } }
新建一个材质球,然后将目标图片和遮挡图片赋予一下,即可看到效果
小编再为大家分享另一段代码:unity shader无锯齿遮罩效果,忘记作者名字了,感谢这位朋友的分享。
这个shader可以用于ugui制作头像框遮罩,没有锯齿,非常nice
shader "custom/circlemask" { properties { _maintex ("maintex", 2d) = "white" {} _masktex ("masktex", 2d) = "white" {} [hideininspector]_cutoff ("alpha cutoff", range(0,1)) = 0.5 //mask support add _stencilcomp ("stencil comparison", float) = 8 _stencil ("stencil id", float) = 0 _stencilop ("stencil operation", float) = 0 _stencilwritemask ("stencil write mask", float) = 255 _stencilreadmask ("stencil read mask", float) = 255 _colormask ("color mask", float) = 15 //mask support end } subshader { tags { "ignoreprojector"="true" "queue"="transparent" "rendertype"="transparent" } //mask support add stencil { ref [_stencil] comp [_stencilcomp] pass [_stencilop] readmask [_stencilreadmask] writemask [_stencilwritemask] } colormask [_colormask] //mask support end pass { name "forward" tags { "lightmode"="forwardbase" } blend srcalpha oneminussrcalpha zwrite off cgprogram #pragma vertex vert #pragma fragment frag #define unity_pass_forwardbase #include "unitycg.cginc" #pragma multi_compile_fwdbase #pragma only_renderers d3d9 d3d11 glcore gles #pragma target 3.0 uniform sampler2d _maintex; uniform float4 _maintex_st; uniform sampler2d _masktex; uniform float4 _masktex_st; struct vertexinput { float4 vertex : position; float2 texcoord0 : texcoord0; }; struct vertexoutput { float4 pos : sv_position; float2 uv0 : texcoord0; }; vertexoutput vert (vertexinput v) { vertexoutput o = (vertexoutput)0; o.uv0 = v.texcoord0; o.pos = unityobjecttoclippos( v.vertex ); return o; } float4 frag(vertexoutput i) : color { ////// lighting: float4 _maintex_var = tex2d(_maintex,transform_tex(i.uv0, _maintex)); float3 finalcolor = _maintex_var.rgb; float4 _masktex_var = tex2d(_masktex,transform_tex(i.uv0, _masktex)); return fixed4(finalcolor,_masktex_var.a); } endcg } } fallback "diffuse" }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。