Unity3D/Shader

Unity) [Shader] 2Pass 알파블랜딩으로 Dissolve, 투명 효과 구현

HSH12345 2023. 3. 22. 23:35
Shader "Custom/Dissolve"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _NoiseTex("NoiseTex", 2D) = "white"{}
        _Cut("Alpha cut", Range(0, 1)) = 0
        [HDR]_OutColor ("OutColor", Color) = (1, 1, 1, 1)
        _OutThickness ("OutThickness", Range(1, 1.5)) = 1.15
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent"}

        ColorMask 0

        CGPROGRAM
         #pragma surface surf _NoLight noambient noforwardadd nolightmap novertexlights noshadow
         #pragma target 3.0
        struct Input
        {
            float4 color:COLOR;
        };

        void surf(Input IN, inout SurfaceOutput o)
        {     
        }

        float4 Lighting_NoLight(SurfaceOutput s, float3 lightDir, float atten)
        {
            return 0;
        }

        ENDCG


        Cull Back
        Zwrite Off

        CGPROGRAM
        #pragma surface surf Lambert alpha:fade
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _NoiseTex;
        float _Cut;
        float4 _OutColor;
        float _OutThickness;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_NoiseTex;
        };


        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            fixed4 noise = tex2D(_NoiseTex, IN.uv_NoiseTex);
            o.Albedo = c.rgb;
            
            float alpha;
            if (noise.r >= _Cut) alpha = 1;
            else alpha = 0;

            float outline;
            if (noise.r >= _Cut * _OutThickness) outline = 0;
            else outline = 1;
            o.Emission = outline * _OutColor.rgb;
            o.Alpha = alpha;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

기본 텍스쳐와 노이즈 텍스쳐를 블렌딩하면서 노이즈 텍스쳐가 블렌딩된 부분에 첫 번째 패스의 투명한 부분이 적용되게 하여 Dissolve효과를 만들어낸다.