Unity/수업 내용

[Unity 2020.3] Refraction Shader

JSH1 2021. 11. 26. 17:56

Shader "Custom/Refraction"
{
    Properties
    {
        _MainTex ("Main Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent" }

        ZWrite Off

        GrabPass {}

        CGPROGRAM
        #pragma surface surf _MyLight alpha:fade

        sampler2D _GrabTexture;
        sampler2D _MainTex;

        struct Input
        {
            float4 color:COLOR;
            float4 screenPos;
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {   
            float4 ref = tex2D(_MainTex, IN.uv_MainTex);

            //IN.screenPos.a;  //카메라 거리에 상관 없이 UV를 유지
            //https://forum.unity.com/threads/what-is-screenpos-w.616003/
            //https://forum.unity.com/threads/w-component-of-in-screenpos.242314/
            
            float2 screenUV = float2(IN.screenPos.r, IN.screenPos.g) / IN.screenPos.w;

            float4 c = tex2D(_GrabTexture, screenUV.xy + ref.r * 0.05);
            o.Emission = c.rgb;
        }

        float4 Lighting_MyLight (SurfaceOutput o, float3 lightDir, float atten)
        {
            return float4(0, 0, 0, 1);
        }
        ENDCG
    }
    FallBack "Diffuse"
}