본문 바로가기
셰이더 (Shader)/유니티 쉐이더 스타트업 - 정종필

[Unity/Shader] 파트14-7 : Diffuse Wraping 기법

by Minkyu Lee 2023. 8. 22.

팀포트리스2에서 사용된 Wrapped Diffuse 기법에 대해 알아본다.

가볍고 응용이 편리하다. 지금도 많이 사용한다.

 

노멀과 라이트 벡터의 내적값을 UV로 사용하는 기법이다.

 

텍스쳐 세팅

Clamp : 마지막 색상이 처음 부분에 묻어나는 문제를 방지한다.

None : 압축하면 컬러의 손실이 생길 수 있다. 따라서 압축 해제하여 방지한다.

 

코드

Shader "Custom/part14-7"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _BumpMap ("NormalMap", 2D) = "bump" {}
        _RampTex ("RampTex", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf warp noambient
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _BumpMap;
        sampler2D _RampTex;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_BumpMap;
        };

        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }

        float4 Lightingwarp(SurfaceOutput s, float3 lightDir, float atten)
        {
            float ndotl = dot(s.Normal, lightDir) * 0.5 + 0.5; // 0 ~ 1
            float4 ramp = tex2D(_RampTex, float2(ndotl, 0.5)); // 가로 램프 텍스쳐의 y축은 모든 위치가 다 동일한 색이다. 따라서 중간인 0.5를 넣는다.

            float4 final;
            final.rgb = s.Albedo.rgb * ramp.rgb;
            final.a = s.Alpha;
            return final;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

댓글