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

[Unity/Shader] 파트11-4 : Lambert 라이트 완성

by Minkyu Lee 2023. 8. 16.

이전 장에서 밝기만 구현된 것에서,

나머지를 덧붙여 Lambert 라이트를 완성해본다.

 

코드

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

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Test

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _BumpMap;

        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.Albedo = c.rgb;
            o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
            o.Alpha = c.a;
        }

        float4 LightingTest(SurfaceOutput s, float3 lightDir, float atten)
        {
            float ndot1 = saturate(dot(s.Normal, lightDir));
            float4 final;
            final.rgb = ndot1 * s.Albedo * _LightColor0.rgb * atten;
            // s.Albedo : 입력받은 텍스쳐이다. 이것이 Lambert와 연산되어서 Diffuse가 된다.
            // _LightColor0.rgb : 내장변수다. 라이팅의 색상과 강도를 가져온다. 곱해서 사용한다.
            /* atten : 빛의 감쇠다. 감쇠가 없으면 다음 현상이 일어나지 않는다.
            - self shadow(자기자신의 그림자)가 없다.
            - receive shadow(다른 물체의 그림자)가 없다.
            - 감쇠 효과가 없다. 즉, 멀어질수록 어두워지는 현상이 없다. */

            // 구형 문서의 경우에는 *2를 해주는 경우가 있다. 이것은 예전 유니티 버전에서 필요한 조건이다. 요즘 유니티 버전은 필요없다.

            final.a = s.Alpha;
            return final;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

댓글