Unity/게임 그래픽 프로그래밍(Shader)

[Shader] SimpleColor Shader

치명적흑형 2021. 11. 19. 10:33

쉐이더의 컬러는 RGBA의 fixed4로 표현된다.

컬러프로퍼티의 수치는 0~255로 표현됨 255로 나누어 주면 0~1로 변환이 가능하다. 반대의 경우 곱해주면 된다.

fixed4(1, 1, 1, 1) = white

fixed4(0, 0, 0, 1) = black

fixed4(1, 0, 0, 1) = red

fixed4(0, 1, 0, 1) = green

fixed4(0, 0, 1, 1) = blue

 

 

연산이 가능하다

-화면에는 1까지만 표현이 되지만 수치는 저장이 된다

fixed4(1, 0, 0, 1) + fixed4(0, 1, 0, 1) = fixed4(1, 1, 0, 2) = yellow

fixed4(1, 0, 0, 1) + fixed4(1, 1, 0, 1) = fixed4(2, 1, 0, 2) = yellow

 

+, -, *, / 모두 가능

 

.rgba / .xyzw로 각 속성에 접근이 가능하다


Shader "Custom/SimpleColor"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM

        //#pragma surface surfaceFunction lightModel [optionalparams]
        #pragma surface surf Standard

        #pragma target 3.0

        struct Input
        {
            //사용하지 않지만 하나 이상 받아야 하기 때문에 임의로 넣는다.
            float3 viewDir;
        };

        fixed4 _Color;

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            //빛을 반사해서 색상 표현
            o.Albedo = _Color.rgb;

            //표면에서 빛의 색상을 방출하여 색을 표현
            //o.Emission = _Color.rgb;

            //랜더 타입이 Opaque기 때문에 넣어주지 않아도 된다.
            o.Alpha = _Color.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

 


Albedo

빛을 반사해서 색상 표현


Emission

표면에서 빛의 색상을 방출하여 색을 표현