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

[Shader] 이미지 두장을 lerp 하기

치명적흑형 2021. 11. 21. 03:47
Function Prototype Profile Usage Description
lerp( a, b, f ) All Linear interpolation between a and b based on f
'f'를 기반으로 하는 'a'와 'b' 간의 선형 보간

https://developer.download.nvidia.com/CgTutorial/cg_tutorial_chapter03.html

 

LERP의 루틴은 크기가 같은 두 벡터의 가중 선형 보간을 계산합니다. 니모닉 lerp는 "선형 보간"을 나타냅니다. 루틴에는 VECTOR가 1, 2, 3 또는 4개의 구성요소가 있는 벡터이고 TYPE이 VECTOR와 동일한 수의 구성요소 및 요소 유형을 갖는 스칼라 또는 벡터인 오버로드된 프로토타입이 있습니다.

VECTOR lerp(VECTOR a, VECTOR b, TYPE weight)

lerp 루틴은 다음을 계산합니다.
result =(1-weight)xa + weight xb

0.5 의 가중치 는 균일한 평균을 제공합니다. 가중치가 0에서 1 범위 내에 있어야 한다는 요구 사항은 없습니다.

weight : 0

 

weight : 0.5

 

weight : 1


Shader "Custom/Lerp"
{
    Properties
    {
        _Texture1 ("Texture1", 2D) = "white" {}
        _Texture2 ("Texture2", 2D) = "white" {}
        _Lerp ("lerp", Range(0, 1)) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Standard

        #pragma target 3.0

        struct Input
        {
            float2 uv_Texture1;
            float2 uv_Texture2;
        };

        sampler2D _Texture1;
        sampler2D _Texture2;
        float _Lerp;

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 a = tex2D (_Texture1, IN.uv_Texture1);
            fixed4 b = tex2D (_Texture2, IN.uv_Texture2);
            o.Emission = lerp(a, b, _Lerp);
            o.Alpha = a.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

weight : a.a (a이미지의 alpha)

 

weight : 1 - a.a (a이미지의 alpha 반전)


Shader "Custom/Lerp"
{
    Properties
    {
        _Texture1 ("Texture1", 2D) = "white" {}
        _Texture2 ("Texture2", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Standard

        #pragma target 3.0

        struct Input
        {
            float2 uv_Texture1;
            float2 uv_Texture2;
        };

        sampler2D _Texture1;
        sampler2D _Texture2;

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 a = tex2D (_Texture1, IN.uv_Texture1);
            fixed4 b = tex2D (_Texture2, IN.uv_Texture2);
            o.Emission = lerp(a, b, a.a);
            //o.Emission = lerp(a, b, 1-a.a);
            o.Alpha = a.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

원하는 이미지의 색상이 안나온다면 설정해주자

 

intensity, intensity multiplier 기본값 1

 

Ennironment Reflections Lighting > intensity multiplier 기본값 1 /Ennironment Reflections > Source 기본값 Skybox / intensity multiplier 기본값 1

 


그 밖에 #pragma 옵션에 noambient 추가

noambient - 앰비언트 라이팅 또는 라이트 프로브를 적용하지 않습니다.