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

[Unity/Shader] 파트14-5 : 끊어지는 음영 만들기

by Minkyu Lee 2023. 8. 20.

if문을 이용해 끊어지는 음영을 만든다.

 

ceil 함수를 이용해 더 가볍게 만들 수 있다.

하지만 범위를 마음대로 설정할 수 없는 단점이 있다.

 

코드

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

        cull front

        // 1st pass 검은색 출력
        CGPROGRAM
        #pragma surface surf Nolight vertex:vert noshadow noambient

        #pragma target 3.0

        sampler2D _MainTex;

        void vert(inout appdata_full v)
        {
            v.vertex.xyz += v.normal.xyz * 0.003;
        }

        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)
        {

        }

        float4 LightingNolight(SurfaceOutput s, float3 lightDir, float atten)
        {
            return float4(0,0,0,1);
        }
        ENDCG

        cull back
        // 2nd Pass 일반 오브젝트 출력
        CGPROGRAM
        #pragma surface surf Toon

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

        float4 LightingToon(SurfaceOutput s, float3 lightDir, float atten)
        {
            float ndotl = dot(s.Normal, lightDir) * 0.5 + 0.5;
            if(ndotl < 0.7)
            {
                ndotl = 1;
            }
            else
            {
                ndotl = 0.3;
            }

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

            return final;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

댓글