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

[Unity/Shader] 파트11-2 : Lambert 라이트 연산

by Minkyu Lee 2023. 8. 12.

내적해서 렘버트 라이팅을 구현해본다.

 

코드

Shader "Custom/NewSurfaceShader 1"
{
    Properties
    {
        _MainTex("Albedo (RGB)", 2D) = "white"{}
        _BumpMap("NormalMap", 2D) = "bump"{}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Test noambient // 엠비언트 라이트 영향 제거

        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _BumpMap;

        struct Input
        {
            float2 uv_MainTex;
        };

        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_MainTex));
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }

        float4 LightingTest (SurfaceOutput s, float3 lightDir, float atten) {
            float ndot1 = saturate(dot(s.Normal, lightDir)); // surf함수에서 o.Normal이 없지만 기본적으로 버텍스 노말 들어가있다.
            return ndot1;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

댓글