Shader "UIEffect/Dissolve" { Properties { _Color("Main Color",Color) = (1,1,1,1) _MainTex("Main Texture", 2D) = "defaulttexture" {} _NoiseTex("Noise", 2D) = "defaulttexture" {} _DissolveTex ("Edge Color", 2D) = "defaulttexture" {} _dissolve("Dissolve", Range(0, 1)) = 1 } SubShader { Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" } Cull Off ZWrite Off ZTest Always Blend One OneMinusSrcAlpha Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } sampler2D _MainTex; sampler2D _DissolveTex; sampler2D _NoiseTex; float _dissolve; fixed4 _Color; fixed4 frag (v2f i) : SV_Target { fixed4 MainCol = tex2D(_MainTex,i.uv); MainCol.rgb *= MainCol.a; // 随机阈值 fixed randValue = tex2D(_NoiseTex, i.uv).r; float cutout = 0.6 - 1.2 * _dissolve + randValue; clip(cutout - 0.5); float weight = 1.0 - clamp(8 * cutout - 4, 0.0, 1.0); float2 burnUV = float2(weight, 0); fixed3 edgeColor = weight * tex2D(_DissolveTex, burnUV ).xyz; edgeColor = step(0.05, MainCol.a) * edgeColor; // edgeColor.rgb *= MainCol.a; // 需要剔除的部分 fixed3 finalCol = (MainCol.rgb + edgeColor)*_Color.a; return fixed4(finalCol, MainCol.a); } ENDCG } } }
时间: 2024-02-10 07:30:01 浏览: 140
Advanced Dissolve 2022.6.1.zip
这是一段 Unity Shader 代码,实现了一个 Dissolve 效果,可以用于 UI 元素的渲染。具体实现是:根据一个随机阈值和 Dissolve 参数,计算出一个 cutout 值,然后根据这个值确定哪些像素需要被剔除,同时根据 DissolveTex 来确定剔除部分的颜色。最后将剔除部分和原始颜色进行混合,得到最终的颜色。
阅读全文