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

[Unity/Shader] 파트5-2 : 텍스쳐 입출력 해석

by Minkyu Lee 2023. 8. 7.

텍스쳐 받아서 그대로 출력하는 쉐이더이다.

추가된 코드는 없다. 단지 해석할 뿐이다.

 

코드

Shader "Custom/part5-2"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {} // 2D 텍스쳐를 받는다. 기본값은 흰색 텍스쳐이다.
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

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

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

        sampler2D _MainTex; // 입력받은 텍스쳐를 sampler로 받게 된다. UV와 만나야 비로소 float4가 되기 때문에 sampler라고 한다.

        struct Input
        {
            float2 uv_MainTex; // uv를 받아온다.
            // uv는 버텍스가 가지고 있다. 이렇게 엔진에게 달라고 할 때 input 구조체 안에 쓴다.
            // 임의로 만든 변수는 넣을 수 없다. 규칙에 따라 작성한다.
        };

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            // float (32비트) > half (16비트) > fixed (11비트)
            // 대부분 텍스쳐 컬러는 채널당 8비트 이하이다. 따라서 fixed로도 충분하다.
           
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

댓글