unity-shader 템플릿 테스트 - 마스크

unity-shader 템플릿 테스트 - 마스크
실험 효과
장면에 어떤 인물 a가 있는데 숨겨서 보이지 않는다. 평면 b를 사용하여 화면을 표시하고 평면 b가 인물 a와 중첩될 때 평면 b 범위 내의 인물을 표시한다.
의 원리
  • 먼저 평면 b를 그리고 템플릿 버퍼에 참고값 1(Ref 1, 값은 마음대로 할 수 있음)
  • 인물 a를 다시 그리고 참고값을 1로 설정하고 템플릿 버퍼의 참고값이 같아야 통과합니다.
  • 그리는 선후 순서에서 알 수 있듯이 평면 b의 RenderQueue 값은 <인물 a.(크면 클수록 나중에 그리기).shader 코드
  • 투명도
    평면 b의 RenderQueue는 3000으로 설정된 투명도 유형 위에 있습니다.알파 값을 0으로 맞추면 돼요.
    인물 a의 경우 3000+, 가설은 3001
    효과
    shader 코드
    평면 b의 shader - testStencilByMaskPlane.shader
    Shader "ITS/test/testStencilByMaskPlane" {
        Properties {
            _MainTex ("Base (RGB)", 2D) = "white" {}
            _MainColor ("Color", Color) = (1, 1, 1, 1)
        }
    
        SubShader {
            // Tags { "RenderType"="Transparent" }
            Tags {
        "Queue"="Transparent" "RenderType"="Transparent"}
            LOD 100
            Blend SrcAlpha OneMinusSrcAlpha 
    
            Pass {  
                Stencil
                {
                    Ref 1
                    Comp Always
                    Pass Replace
                    ZFail Replace
                }
    
                CGPROGRAM
                    #pragma vertex vert
                    #pragma fragment frag
                    #pragma multi_compile_fog
    
                    #include "UnityCG.cginc"
    
                    struct appdata_t {
                        float4 vertex : POSITION;
                        float2 texcoord : TEXCOORD0;
                    };
    
                    struct v2f {
                        float4 vertex : SV_POSITION;
                        half2 texcoord : TEXCOORD0;
                        UNITY_FOG_COORDS(1)
                    };
    
                    sampler2D _MainTex;
                    float4 _MainTex_ST;
                    float4 _MainColor;
    
                    v2f vert (appdata_t v)
                    {
                        v2f o;
                        o.vertex = UnityObjectToClipPos(v.vertex);
                        o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                        UNITY_TRANSFER_FOG(o,o.vertex);
                        return o;
                    }
    
                    fixed4 frag (v2f i) : SV_Target
                    {
                        fixed4 col = tex2D(_MainTex, i.texcoord);
                        col *= _MainColor;
                        UNITY_APPLY_FOG(i.fogCoord, col);
                        // UNITY_OPAQUE_ALPHA(col.a);
                        return col;
                    }
                ENDCG
            }
        }
    }
    

    인물 a의 shader - test StencilBy MaskChar.shader
    Shader "ITS/test/testStencilByMaskChar" {
        Properties {
            _MainTex ("Base (RGB)", 2D) = "white" {}
            _MainColor ("Color", Color) = (1, 1, 1, 1)
        }
    
        SubShader {
            Tags { "RenderType"="Opaque" }
            LOD 100
    
            Pass {  
                ZTest LEqual
                Stencil
                {
                    Ref 1
                    Comp Equal
                }
    
                CGPROGRAM
                    #pragma vertex vert
                    #pragma fragment frag
                    #pragma multi_compile_fog
    
                    #include "UnityCG.cginc"
    
                    struct appdata_t {
                        float4 vertex : POSITION;
                        float2 texcoord : TEXCOORD0;
                    };
    
                    struct v2f {
                        float4 vertex : SV_POSITION;
                        half2 texcoord : TEXCOORD0;
                        UNITY_FOG_COORDS(1)
                    };
    
                    sampler2D _MainTex;
                    float4 _MainTex_ST;
                    float4 _MainColor;
    
                    v2f vert (appdata_t v)
                    {
                        v2f o;
                        o.vertex = UnityObjectToClipPos(v.vertex);
                        o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                        UNITY_TRANSFER_FOG(o,o.vertex);
                        return o;
                    }
    
                    fixed4 frag (v2f i) : SV_Target
                    {
                        fixed4 col = tex2D(_MainTex, i.texcoord);
                        col *= _MainColor;
                        UNITY_APPLY_FOG(i.fogCoord, col);
                        UNITY_OPAQUE_ALPHA(col.a);
                        return col;
                    }
                ENDCG
            }
        }
    }

    좋은 웹페이지 즐겨찾기