Unity shader to achieve masking effect

Time:2019-4-18

In this paper, we share the specific code of Unity shader to achieve the masking effect for your reference. The details are as follows.

Effect:

Unity shader to achieve masking effect

Shader code:

Shader "Custom/Mask" {
 Properties {
 _ MainTex ("Base (RGB), 2D) ="white"{}// target image, that is, the image that needs to be masked
 _ MaskLayer ("Culling Mask", 2D) = "white" {}// mixed image, set to white image, any color mixed with white, its color unchanged
 _Cutoff("Alpha cutoff",Range(0,1)) = 0
 }
 SubShader {
 Tags { 
  "Queue"="Transparent" 
 }// The rendering queue is set to render transparent objects in backward-forward order
 Lighting off//Turn off the light
 ZWrite off // Close deep cache
 Blend off//close mixing
 AlphaTest GEqual [_Cutoff]// Enable alpha testing

 Pass{
  SetTexture [_MaskLayer]{combine texture}// Mixed Mapping
  // Mixed Mapping. Previous is placed in the previous sequence so that this image is used as the main part for mixing in AlphaTest.
  SetTexture[_MainTex]{combine texture,previous}
 } 
 }
}

Create a new material ball, then give the target picture and occlusion picture to see the effect.

Unity shader to achieve masking effect

Xiaobian will share another piece of code for you: Unity shader serrated masking effect, forget the author’s name, thank this friend for sharing.

This Shader can be used in UGUI to make headframe masks, no jagged, very nice.


Shader "Custom/CircleMask" {
  Properties {
    _MainTex ("MainTex", 2D) = "white" {}
    _MaskTex ("MaskTex", 2D) = "white" {}
    [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    //MASK SUPPORT ADD
    _StencilComp ("Stencil Comparison", Float) = 8
    _Stencil ("Stencil ID", Float) = 0
    _StencilOp ("Stencil Operation", Float) = 0
    _StencilWriteMask ("Stencil Write Mask", Float) = 255
    _StencilReadMask ("Stencil Read Mask", Float) = 255
    _ColorMask ("Color Mask", Float) = 15
    //MASK SUPPORT END
  }
  SubShader {
    Tags {
      "IgnoreProjector"="True"
      "Queue"="Transparent"
      "RenderType"="Transparent"
    }
    //MASK SUPPORT ADD
    Stencil
    {
      Ref [_Stencil]
      Comp [_StencilComp]
      Pass [_StencilOp]
      ReadMask [_StencilReadMask]
      WriteMask [_StencilWriteMask]
    }
    ColorMask [_ColorMask]
    //MASK SUPPORT END
    Pass {
      Name "FORWARD"
      Tags {
        "LightMode"="ForwardBase"
      }
      Blend SrcAlpha OneMinusSrcAlpha
      ZWrite Off
      CGPROGRAM
      #pragma vertex vert
      #pragma fragment frag
      #define UNITY_PASS_FORWARDBASE
      #include "UnityCG.cginc"
      #pragma multi_compile_fwdbase
      #pragma only_renderers d3d9 d3d11 glcore gles
      #pragma target 3.0
      uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
      uniform sampler2D _MaskTex; uniform float4 _MaskTex_ST;
      struct VertexInput {
        float4 vertex : POSITION;
        float2 texcoord0 : TEXCOORD0;
      };
      struct VertexOutput {
        float4 pos : SV_POSITION;
        float2 uv0 : TEXCOORD0;
      };
      VertexOutput vert (VertexInput v) {
        VertexOutput o = (VertexOutput)0;
        o.uv0 = v.texcoord0;
        o.pos = UnityObjectToClipPos( v.vertex );
        return o;
      }
      float4 frag(VertexOutput i) : COLOR {
////// Lighting:
        float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
        float3 finalColor = _MainTex_var.rgb;
        float4 _MaskTex_var = tex2D(_MaskTex,TRANSFORM_TEX(i.uv0, _MaskTex));
        return fixed4(finalColor,_MaskTex_var.a);
      }
      ENDCG
    }
  }
  FallBack "Diffuse"
}

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.