- Home /
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
Also interrested in using a Shader on 3d object, and manage it so the UI $$anonymous$$ask affect it .... I'm trying this kind of solution but it doesn't work for now..
Answer by JeanLuc · Aug 20, 2015 at 01:21 PM
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
{
// ...
}
}
}
I am also trying to render a shader (3d mesh) and mask using the UI mask...I have a very limitid knowledge of how shaders work so please bear with me.... Should i put the above code in the standard shader to get this to work? or should i create a new shader with the above code and apply it?
Heroic. I can confirm that this still works nicely on 5.2.2.fx.
For posterity: Note that "_StencilComp" is very important and it's backing values are not documented anywhere that I could find. The float (int, really) that you assign to it tells the shader what comparison operation to use when checking the stencil buffer. In my case, I needed to write an "Equals" operation in there. I'm using my$$anonymous$$at.SetFloat("_StencilComp", 3); from C# to do it. Not sure if 3 is actually the equal operation or just one that worked because of mathematical coincidence.
So is that the text for the entire shader, then I make a material and assign this new shader to it? Also, do I modify any of the new material's properties? I used this code and did what I described, but the panel went invisible, along with its children.
Answer by Michiel-Frankfort · Oct 28, 2016 at 11:03 PM
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...
Answer by Mr-Pocket-Scientist · Jun 01, 2016 at 01:40 PM
Look at this: http://answers.unity3d.com/questions/1130203/ui-mask-override-my-shaders-custom-property.html 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.
Answer by yutuch · May 25, 2019 at 07:04 PM
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