UI mask with shader

Hi,

I want to use a UI mask with a shader instead of a texture. However, not only it doesn’t work, but I get this warning:

Material UnmoveableMask (UnityEngine.Material) doesn’t have color mask
UnityEngine.Canvas:SendWillRenderCanvases()

I tried playing with stencil stuff directly in my shader, but didn’t get any satisfying result.

So, is the UI Mask component supposed to work with custom shaders?

Thanks,

Maxime

1 Like

yes, just need to copy the part the Stencil and ColorMask from the build in shader “UI-Default.shader”

Shader "Custom/Opaque"
 {  
	Properties
	{
		[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
		_Color ("Tint", Color) = (1,1,1,1)
		
		// required for UI.Mask
		_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
	}
	SubShader
	{
		Tags 
		{ 
			// ...
		}
		
		// required for UI.Mask
		Stencil
		{
			Ref [_Stencil]
			Comp [_StencilComp]
			Pass [_StencilOp] 
			ReadMask [_StencilReadMask]
			WriteMask [_StencilWriteMask]
		}
 		ColorMask [_ColorMask]
		
		Pass
		{
			// ...
		}
	}
 }

Hi,

Just a little visual update for 5.4.1 to get a Shaderforge-shader working with stencil UI Mask component. I hope it helps…

Look at this: UI Mask override my shaders custom property ? - Unity Answers
Store the new Material in a variable and update that anytime with your usual scripts. The GetModifiedMaterial() gets called only when the canvas layout should be updated.

Hey! I dig this topic up because I encountered the same issue today and none of the top google answers helped me.

I checked every parameters, and Instantiated the new material at runtime and yet, it didn’t work properly.

In my case, the problem was the Image’s scale. If it was (1,1,1), the shader worked, if it’s (-1,1,1) it didn’t.

Hope this will help some wanderers