unity噪声消融效果Shader实现

这个shader可以做慢慢消失的效果,就是消融效果

shader代码

Shader "Kaima/Dissolve/EdgeColor"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _NoiseTex("Noise", 2D) = "white" {}
        _Threshold("Threshold", Range(0.0, 1.0)) = 0.5
        _EdgeLength("Edge Length", Range(0.0, 0.2)) = 0.1
        _EdgeColor("Edge Color", Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "Queue"="Geometry" "RenderType"="Opaque" }

        Pass
        {
            Cull Off //要渲染背面保证效果正确

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uvMainTex : TEXCOORD0;
                float2 uvNoiseTex : TEXCOORD1;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _NoiseTex;
            float4 _NoiseTex_ST;
            float _Threshold;
            float _EdgeLength;
            fixed4 _EdgeColor;
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uvMainTex = TRANSFORM_TEX(v.uv, _MainTex);
                o.uvNoiseTex = TRANSFORM_TEX(v.uv, _NoiseTex);
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                //镂空
                fixed cutout = tex2D(_NoiseTex, i.uvNoiseTex).r;
                clip(cutout - _Threshold);

                //边缘颜色
                if(cutout - _Threshold < _EdgeLength)
                    return _EdgeColor;

                fixed4 col = tex2D(_MainTex, i.uvMainTex);
                return col;
            }
            ENDCG
        }
    }
}

创建材质球选择这个着色器,最后样子就是这样的

Texture贴物体的图片,   Noise贴噪声贴图

Threshold 控制消失或出现(具体实现看需求)

EdgeLength控制噪声贴图的白边(看自己喜好调节)

下面附上一张噪声贴图

猜你喜欢

转载自www.cnblogs.com/qq2351194611/p/11976647.html