유니티/쉐이더

05/06 유니티 쉐이더 라이트 lambert와 half-lambert 연습

박준희 2021. 5. 6. 13:06

좌 : lambert 우 : half-lambert

텍스쳐 한장을 받는 쉐이더 생성
라이트이름 설정 : Test
Lighting + Test 이름으로 함수 선언
반환타입은 float4
매개변수 (surfaceOutput s,
float3 lightDir, //단위벡터, 뒤집힌 방향
float atten) //빛의 감쇠 : Directional Light (x), Point Light (o)

램버트 공식 : 표면벡터와 조명벡터를 내적
옵션 : saturate(표면벡터와 조명벡터를 내적)
0 ~ 1까지를 표현하고 그 값을 반환


하프 램버트 공식 : 표면벡터와 조명벡터를 내적 * 0.5 + 0.5

라이트 함수 내에서 SurfaceOutput 구조체 값 사용가능
예) s.Albedo

_LightColor0 내장변수 : 조명의 색상, 강도 저장된 내장변수

 

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

        CGPROGRAM
        #pragma surface surf Test noambient

        sampler2D _MainTex;
        sampler2D _BumpMap;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_BumpMap;
        };


        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 LightingTest(SurfaceOutput s, float3 lightDir, float3 atten) {
            //lambert
            //float ndotl = saturate(dot(s.Normal, lightDir));

            //half-lambert
            float ndotl = dot(s.Normal, lightDir) * 0.5 + 0.5;

            float4 final;
            final.rgb = ndotl* s.Albedo* _LightColor0.rgb* atten;
            final.a = s.Alpha;
            return final;
        }

        ENDCG
    }
    FallBack "Diffuse"
}