본문 바로가기

Unity/Shader

3. 이미지를 흑백으로 만들기

흑백이미지는 두 가지 속성을 가지고 있습니다.

1. RGB 모두 동일한 숫자로 이루어져있다.

2. RGB 각 요소에 따른 강도의 평균이다.

 

이 부분을 인지하고 아래와 같이 계산하면 이미지를 흑백으로 만들 수 있습니다.

o.Albedo = (c.r + c.g + c.b) / 3;

Shader "Custom/Gray"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Standard 

        sampler2D _MainTex;

		
        struct Input
        {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = (c.r + c.g + c.b) / 3;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}