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

[Unity/Shader] 파트18-5 : Matcap

by Minkyu Lee 2023. 10. 12.

Matcap은 Material Capture의 준말이다.

텍스쳐를 이용해 조명을 대신하는 방식이다.

 

ViewDir을 기반으로 구현한다.

Matcap 텍스쳐를 사용하기 위한 UV를 연산하는 것이 핵심이다.

뷰 노말을 UV로 사용한다.

 

코드

Shader "Custom/part18-5"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Matcap("Matcap", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

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

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

        sampler2D _MainTex;
        sampler2D _Matcap;

        struct Input
        {
            float2 uv_MainTex;
            float3 worldNormal;
        };

        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);
            float3 viewNormal = mul((float3x3)UNITY_MATRIX_V, IN.worldNormal.rgb); // 월드 노말을 행렬을 이용해 뷰 노말로 변환한다.
            float2 MatcapUV = viewNormal.xy * 0.5 + 0.5; // -1~1 -> 0~1 범위로 만들기
            o.Emission = tex2D(_Matcap, MatcapUV) * c.rgb;
            o.Alpha = c.a;
        }

        float4 Lightingnolight(SurfaceOutput s, float3 lightDir, float atten) {
            return float4(0,0,0,s.Alpha);
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

추가정보

노말맵을 사용하는 Matcap 구현

노말맵이 사용된 worldNomal을 만들어야한다.

 

댓글