유니티/게임그래픽

이미지 흑백으로 만들기, Lerp 함수

잉ㅇ잉ㅇ 2024. 2. 19. 21:05

 

fullforwardshadows는 그림자 관련 부분이다

이게 있으면 쉐이더를 사용한 오브젝트가 랜더링 상태일때 모든 라이트에서 그림자를 생성한다

이 구문을 지우게 되면 포인트나 스포트라이트에서는 그림자를 생성하지 않는다

 

흑백으로 바꾸는 방법은 매우 간단하다

출력을 (c.r + c.g + c.b) /3 으로 바꿔버리면 된다

 

이렇게 연산하면 c의 rgb값 평균이 나오게 되고 3으로 나누었으니 한자리 숫자로 나오게 된다

 

<Lerp 함수>

Lerp라는 단어 자체는 직선적인 비율로 값이 변환되는것을 의미하지만

지금은 복잡하게 알 필요없고 두 텍스쳐를 섞어주는 함수쯤으로만 이해하자

 

lerp(X, Y, s)

 

이게 기본형식으로 X와 Y는 같은 단위여야 하고 s는 float이다

이번엔 풀과 모래 텍스쳐를 lerp 함수를 이용해 합쳐보겠다

 

Shader "Custom/NewSurfaceShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _MainTex2 ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Standard
        #pragma target 3.0
        sampler2D _MainTex;
        sampler2D _MainTex2;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_MainTex2;

        };

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            fixed4 d = tex2D (_MainTex2, IN.uv_MainTex2);

            o.Albedo = c.rgb;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

우선 아까 만들어뒀던 텍스쳐를 똑같이 하나 더 만든다

입력받을수 있는 텍스쳐가 2개가 생긴것을 확인하고 두번째에 모래 텍스쳐를 넣어준다

 

 void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            fixed4 d = tex2D (_MainTex2, IN.uv_MainTex2);

            o.Albedo = lerp(c.rgb, d.rgb, 1);
        }

그리고 Albedo에 lerp 함수를 사용하여 출력해보자. s값에 1을 넣자 모래 텍스쳐가 나온다

 void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            fixed4 d = tex2D (_MainTex2, IN.uv_MainTex2);

            o.Albedo = lerp(c.rgb, d.rgb, 0);
        }

s 값에 0을 넣자 풀 텍스쳐가 나오는것을 확인할수 있다

 

그럼 이 둘을 반반 섞기 위해서는 s값에 0.5를 넣어주면 된다

 void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            fixed4 d = tex2D (_MainTex2, IN.uv_MainTex2);

            o.Albedo = lerp(c.rgb, d.rgb, 0.5);
        }

 

응용1 - 지난 글에서 range를 이용하여 밝기 조절 스위치를 만든것처럼 이번에도 섞이는 비율을 스위치로 만들수 있다

Shader "Custom/NewSurfaceShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _MainTex2 ("Texture", 2D) = "white" {}
        _Range("range", range(0,1)) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Standard
        #pragma target 3.0
        sampler2D _MainTex;
        sampler2D _MainTex2;
        float _Range;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_MainTex2;

        };

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            fixed4 d = tex2D (_MainTex2, IN.uv_MainTex2);

            o.Albedo = lerp(c.rgb, d.rgb, _Range);
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

방법은 간단하다. 스위치 하나 range로 선언해주고 s값에 스위치 변수 _Range를 넣어주면 된다