Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by erfan · Sep 15, 2016 at 10:48 AM · spriteshader programmingmaskstencil

UI mask doesn't work with custom sprite material

Hi. I use two shader for stencil mask. the mask are:

 Shader "Sprites/Stencil Mask"
 {
     Properties
     {
         [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
     _Color("Tint", Color) = (1,1,1,1)
         [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
 
         // 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
     {
         "Queue" = "Transparent"
         "IgnoreProjector" = "True"
         "RenderType" = "Transparent"
         "PreviewType" = "Plane"
         "CanUseSpriteAtlas" = "True"
     }
 
         Cull Off
         Lighting Off
         ZWrite Off
         Fog{ Mode Off }
         Blend One OneMinusSrcAlpha
 
         Pass
     {
         Stencil
     {
         Ref 1
         Comp always
         Pass replace
 
     }
 
         CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #pragma multi_compile DUMMY PIXELSNAP_ON
 #include "UnityCG.cginc"
 
     struct appdata_t
     {
         float4 vertex   : POSITION;
         float4 color    : COLOR;
         float2 texcoord : TEXCOORD0;
     };
 
     struct v2f
     {
         float4 vertex   : SV_POSITION;
         fixed4 color : COLOR;
         half2 texcoord  : TEXCOORD0;
     };
 
     fixed4 _Color;
 
     v2f vert(appdata_t IN)
     {
         v2f OUT;
         OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
         OUT.texcoord = IN.texcoord;
         OUT.color = IN.color * _Color;
 #ifdef PIXELSNAP_ON
         OUT.vertex = UnityPixelSnap(OUT.vertex);
 #endif
 
         return OUT;
     }
 
     sampler2D _MainTex;
 
     fixed4 frag(v2f IN) : SV_Target
     {
         fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
     if (c.a<0.001) discard;            //Most IMPORTANT working Code
     c.rgb *= c.a;
     return c;
     }
         ENDCG
     }
     }
 }

and the draw in mask are:

 Shader "Sprites/Stencil Draw In Mask"
 {
     Properties
     {
         [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
     _Color("Tint", Color) = (1,1,1,1)
         [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
         // 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
     {
         "Queue" = "Transparent+1"
         "IgnoreProjector" = "True"
         "RenderType" = "Transparent"
         "PreviewType" = "Plane"
         "CanUseSpriteAtlas" = "True"
     }
 
         Cull Off
         Lighting Off
         ZWrite Off
         Fog{ Mode Off }
         Blend One OneMinusSrcAlpha
         Pass
     {
         Stencil
     {
         Ref 1
         Comp Equal
         
     }
 
         CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #pragma multi_compile DUMMY PIXELSNAP_ON
 #include "UnityCG.cginc"
 
     struct appdata_t
     {
         float4 vertex   : POSITION;
         float4 color    : COLOR;
         float2 texcoord : TEXCOORD0;
     };
 
     struct v2f
     {
         float4 vertex   : SV_POSITION;
         fixed4 color : COLOR;
         half2 texcoord  : TEXCOORD0;
     };
 
     fixed4 _Color;
 
     v2f vert(appdata_t IN)
     {
         v2f OUT;
         OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
         OUT.texcoord = IN.texcoord;
         OUT.color = IN.color * _Color;
 #ifdef PIXELSNAP_ON
         OUT.vertex = UnityPixelSnap(OUT.vertex);
 #endif
 
         return OUT;
     }
 
     sampler2D _MainTex;
 
     fixed4 frag(v2f IN) : SV_Target
     {
         fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
     c.rgb *= c.a;
     return c;
     }
         ENDCG
     }
     }
 }

it works great. but the problem is that this material doesn't work with UI Mask component.

I think this is a related work: UI mask with shader but i can't fix my shader. any help?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by zac_tangible · Nov 09, 2016 at 06:08 PM

Unity identifies a clip rectangle to pass to graphics, you need to properly handle that information. Note, this will not work with a SpriteRenderer, you need to be using a Graphics component of some sort (ie Image)

 //scope variables
 #include "UnityUI.cginc"
 float4 _ClipRect;
 
 struct v2f
      {
          float4 vertex   : SV_POSITION;
          fixed4 color : COLOR;
          half2 texcoord  : TEXCOORD0; 
          float4 worldPosition : TEXCOORD1; //we need to pass world pos to the fragment shader for clipping
      };
 
 //vertex shader
 o.worldPosition = i.vertex;
 o.pos = UnityObjectToClipPos(o.worldPosition);
 
 //fragment shader
 output_color.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect);
                 
 
   
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew_pearce · Dec 02, 2018 at 09:30 AM 0
Share

Works perfect, however under Unity 2018, I had to add also

inline float UnityGet2DClipping(in float2 position, in float4 clipRect) { float2 inside = step(clipRect.xy, position.xy) step(position.xy, clipRect.zw); return inside.x inside.y; }

otherwise Unity was not able to find that function. Thanks!

avatar image
1

Answer by crocodilepp · Sep 12, 2017 at 06:19 AM

Ran into exactly the same issue for my project and finally found this thread Masked UI Element's Shader Not Updating. Using image.materialForRendering.SetFloat to change shader property at runtime perfectly solved the problem.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by johnstablencsoft · May 12, 2017 at 12:15 PM

If you want to modify the material AFTER mask is applied, you need to get the Mask component, and call GetModifiedMaterial().

 Mask myMask = _image.GetComponent<Mask>();
 Material modifiedMat = myMask.GetModifiedMaterial( _image.material );
 modifiedMat.SetFloat( "_SomeProp", 3.0f );
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Invisible transition mask 0 Answers

Default Sprite Mask 2D not working on mobile 0 Answers

sprites / diffuse does not support rectmask 1 Answer

Why my c# for load sprite in spritemask does'n work? 0 Answers

How can I make a sprite mask from some dynamic coordinates? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges