유니티/게임그래픽

Vertex Color Masking

잉ㅇ잉ㅇ 2024. 2. 21. 14:18

이번엔 Vertex Color를 마스킹으로 이용하여 멀티 텍스쳐 기능을 만들어보겠다

 

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

        CGPROGRAM
        #pragma surface surf Standard
        sampler2D _MainTex;
        sampler2D _MainTex2;
        sampler2D _MainTex3;
        sampler2D _MainTex4;

        struct Input
        {
           float2 uv_MainTex;
           float2 uv_MainTex2;
           float2 uv_MainTex3;
           float2 uv_MainTex4;
           float4 color:COLOR;
        };

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
            fixed4 d = tex2D(_MainTex2, IN.uv_MainTex2);
            fixed4 e = tex2D(_MainTex3, IN.uv_MainTex3);
            fixed4 f = tex2D(_MainTex4, IN.uv_MainTex4);

            o.Emission = IN.color.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

우선 텍스쳐 4개를 입력받아 우선은 Vertex color를 출력하는 스크립트를 짜준다

            o.Emission = IN.color.r;

만약 여기서 R채널만 출력하면 흑백으로 변하고 R영역만 흰색으로 출력된다 (물론 G나 B도 마찬가지)

 

 

이제 전에 배웠던 lerp함수를 이용해 텍스쳐들을 한번 섞어보자

 

14-3일차 이미지 흑백으로 만들기, Lerp 함수

fullforwardshadows는 그림자 관련 부분이다 이게 있으면 쉐이더를 사용한 오브젝트가 랜더링 상태일때 모든 라이트에서 그림자를 생성한다 이 구문을 지우게 되면 포인트나 스포트라이트에서는 그

sangeun00.tistory.com

            o.Emission = lerp(c.rgb, d.rgb, IN.color.r);

lerp를 이용하여 텍스쳐 c와 d를 color의 r값의 비율만큼 섞어준다

그럼 R의 영역에 텍스쳐 d가 섞인 모습을 볼 수 있다

 

이렇게 하나 섞었으니 이번엔 G와 B의 영역도 동시에 섞이도록 코드를 짜보겠다

 o.Emission = lerp(c.rgb, d.rgb, IN.color.r);
 o.Emission = lerp(c.rgb, e.rgb, IN.color.g);
 o.Emission = lerp(c.rgb, f.rgb, IN.color.b);

처음엔 단순하게 생각해서 이렇게 하면 될줄 알았다. 하지만 이렇게 만드니 

마지막줄에 적은 f값만 출력된다. 이렇듯 출력코드 3줄을 적으면 그 3줄을 전부 출력해주는것이 아니라

마지막 1줄만 출력한다는것을 알 수 있다

 

 o.Emission = lerp(c.rgb, d.rgb, IN.color.r);
 o.Emission = lerp(o.Emission, e.rgb, IN.color.g);
 o.Emission = lerp(o.Emission, f.rgb, IN.color.b);

결국 마지막 한줄만 적용되는거라면 첫번째, 두번째 o.Emission을 그대로 끌고와 마지막 줄에 적어주면 된다