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

[Shader] 이미지 한장 받아서 흑백으로 만들기

치명적흑형 2021. 11. 21. 02:46

 

 

grayscale intensity : 0
grayscale intensity : -0.5
grayscale intensity : 0.5


Shader "Custom/Grayscale"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Intensity ("grayscale intensity", Range(-0.5, 0.5)) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Standard

        struct Input
        {
            float2 uv_MainTex;
        };

        sampler2D _MainTex;
        float _Intensity;

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            float4 c = tex2D(_MainTex, IN.uv_MainTex);
            o.Emission = (c.r + c.g + c.b)/3 + _Intensity;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
간단한 회색조 공식
-원래는 복잡한 식이 있지만 아래의 방법으로 간단하게 가능하다

float4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Emission = (c.r + c.g + c.b)/3

-o.Emission = (c.rrr +  c.ggg + c.bbb)/3 와 같다


------------------------------------------------------------------------

 _Intensity ("grayscale intensity", Range(-0.5, 0.5)) = 0
회색조 조절용
Range는 유니티의 인스펙터 창에서 슬라이더로 표시된다
수치가 정해진 것은 아니다