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

[Unity/Shader] 파트13-3 : Gloss 맵으로 스펙큘러 강도 조절하기

by Minkyu Lee 2023. 8. 19.

이전 코드에 이어서

Gloss 텍스쳐를 프로퍼티에 받아 적용하는 법을 실습한다.

 

코드

Shader "Custom/Part13-2"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _BumpMap ("NormalMap", 2D) = "bump" {}
        _SpecCol ("Specular Color", Color) = (1,1,1,1)
        _SpecPow ("Specular Power", Range(10,200)) = 100
        _GlossTex ("Gloss Tex", 2D) = "white" {} // *
    }
    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;
        sampler2D _GlossTex; // *
        float4 _SpecCol;
        float _SpecPow;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_BumpMap;
            float2 uv_GlossTex; // *
        };

        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);
            float4 m = tex2D (_GlossTex, IN.uv_GlossTex); // *
            o.Albedo = c.rgb;
            o.Normal = UnpackNormal(tex2D(_BumpMap,IN.uv_BumpMap));
            o.Gloss = m.a; // 텍스쳐의 어떤 채널에 Gloss가 들어가있느냐에 따라 다르다. 이 예제에선 알파에 들어있어서 .a가 된다.
            o.Alpha = c.a;
        }

        float4 LightingTest(SurfaceOutput s, float3 lightDir, float3 viewDir, float atten)
        {
            float4 final;

            // Lambert term
            float3 DiffColor;
            float ndot1 = saturate(dot(s.Normal, lightDir));
            DiffColor = ndot1 * s.Albedo * _LightColor0.rgb * atten;

            // Spec term
            float3 SpecColor;
            float3 H = normalize(lightDir + viewDir);
            float spec = saturate(dot(H,s.Normal));
            spec = pow(spec, 100);
            SpecColor = spec * _SpecCol.rgb * s.Gloss; // Gloss 적용

            // final term
            final.rgb = DiffColor.rgb + SpecColor.rgb;
            final.a = s.Alpha;
            return final;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

추가정보

댓글