If you want to use a Raw Image to mask what’s behind the Canvas (like a hole revealing objects behind it), you need to use Stencil Buffers or Alpha Clipping.
Unity’s UI system doesn’t support masking by default in a way that lets objects behind the Canvas be visible. However, stencil buffers can be used to cut out the area covered by the Raw Image.
- The Raw Image writes to the stencil buffer, marking the area as masked.
- Other objects behind the Canvas are only rendered where the mask exists.
This shader masks out everything except where the Raw Image exists.
Shader "Custom/StencilMask"
_MainTex ("Texture", 2D) = "white" {}
Tags { "Queue"="Overlay" "RenderType"="Transparent" }
ColorMask 0 // Don't draw any color (just modify the stencil buffer)
Ref 1 // Reference value in stencil buffer
Comp Always // Always write to stencil
Pass Replace // Replace existing stencil value with '1'
✔️ This Raw Image material will write to the stencil buffer but won’t render any color.
This shader will only render objects behind the Canvas where the stencil value is 1.
Shader "Custom/StencilReveal"
_MainTex ("Texture", 2D) = "white" {}
Tags { "Queue"="Overlay" "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
Ref 1 // Must match the mask stencil value
Comp Equal // Render only where the stencil is 1
float4 vertex : POSITION;
float4 vertex : SV_POSITION;
o.vertex = UnityObjectToClipPos(v.vertex);
fixed4 frag (v2f i) : SV_Target
return tex2D(_MainTex, i.uv); // Render normally
✔️ This only renders where the stencil value is 1, meaning it appears only inside the masked area.
-
Create a Material for the Mask (Raw Image)
- Create a new Material in Unity.
- Set its shader to “Custom/StencilMask”.
- Assign it to the Raw Image component.
-
Create a Material for Background Objects
- Create a new Material for background objects.
- Set its shader to “Custom/StencilReveal”.
- Assign it to the objects behind the Canvas.
-
**Make sure the Canvas is in World Space Mode:
- Select your
Canvas → Set Render Mode = World Space.
- Adjust the Canvas size and positioning to fit your scene.
If you want to fade out areas behind the Raw Image instead of a hard mask, use an Alpha Clipping shader.
🔹 Shader for Alpha Clipping (Soft Mask)
Shader "Custom/AlphaMask"
_MainTex ("Texture", 2D) = "white" {}
_MaskTex ("Mask Texture", 2D) = "white" {}
Tags { "Queue"="Overlay" "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
float4 vertex : POSITION;
float4 vertex : SV_POSITION;
o.vertex = UnityObjectToClipPos(v.vertex);
fixed4 frag (v2f i) : SV_Target
float mask = tex2D(_MaskTex, i.uv).r; // Read red channel of mask
fixed4 col = tex2D(_MainTex, i.uv);
col.a *= mask; // Multiply alpha by the mask
✔️ This shader will apply transparency based on a mask texture, allowing you to fade out objects behind the Raw Image.
| Use Case | Best Solution |
|---|
| Hard mask (like a hole) | Stencil Buffer (Solution 1) |
| Soft transparency mask | Alpha Clipping (Solution 2) |
| Masking a single UI element | Unity’s built-in UI Mask |
✔ Stencil Buffer works great for cutting holes in UI elements.
✔ Alpha Clipping is better for soft masks.
✔ Make sure to use World Space Canvas for 3D masking effects.
Let me know if you need any refinements! 🚀🎨