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 /
avatar image
0
Question by DaiMangouDev · Sep 07, 2016 at 01:30 PM · shadershader programmingmasking

Make stencil shader target materials with same int 'ID' ?

Hi I am using a stencil shader as shown here

sorry ,I have no skill in shader scripting.

I am using the shader to cut own holes in sprites but if I have multiple objects in the scen with the same Mask and Masked shader materials on them then objects that should not cut holes into other sprites start doing so.

So how can i set some kind of ID on each material so that the Mask and Masked shader materials only affect those with the same ID ?

This is placed on materials that will do masking (will do the cutting out)

 Shader "Custom/Mask"
 {
     Properties
     {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         _Color ("Tint", Color) = (1,1,1,1)
         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
     }
  
     SubShader
     {
         Tags
         {
             "Queue"="Transparent-1"
             "IgnoreProjector"="True"
             "RenderType"="Transparent"
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
  
         Cull Off
         Lighting Off
         ColorMask 0
         ZWrite Off
         Blend One OneMinusSrcAlpha
  
         Stencil
         {
             Ref 1
             Comp always
             Pass replace
         }
  
         Pass
         {
         CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile _ 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.1) discard;
                 c.rgb *= c.a;
                 return c;
             }
         ENDCG
         }
     }
 }

This is placed on materials which will be masked (will be cut out)

 Shader "Custom/Masked"
 {
     Properties
     {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         _Color ("Tint", Color) = (1,1,1,1)
         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
     }
  
     SubShader
     {
         Tags
         {
             "Queue"="Transparent"
             "IgnoreProjector"="True"
             "RenderType"="Transparent"
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
  
         Cull Off
         Lighting Off
         ZWrite Off
         Blend One OneMinusSrcAlpha
  
         Stencil
         {
             Ref 1
             Comp notequal
             Pass keep
         }
  
         Pass
         {
         CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile _ 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
         }
     }
 }

alt text

fdwee.png (71.7 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by villevli · Sep 07, 2016 at 05:28 PM

Just changing the Ref value could be enough for your game (create a property for it) but if two masks can overlap in the game the following is required:

Add these to your shaders. The Mask Layer is a bitmask for reading and writing to the stencil buffer. (The stencil buffer is 8bit so only 8 layers are possible) Usually the value should be a power of two, e.g. 1, 2, 4, 8,... if you want to mask only one layer. You can set the Mask Layer property in your materials to make masks that mask only a certain layer or many layers. I also added the Mask Mode property to the Masked shader. You can set it to Equal or NotEqual to invert the mask.

Mask shader:

 Properties {
     _StencilMask ("Mask Layer", Range(0, 255)) = 1 
 }
 
 Stencil {
     Ref 255
     WriteMask [_StencilMask]
     Comp Always
     Pass Replace
 }


Masked shader:

 Properties {
     _StencilMask("Mask Layer", Range(0, 255)) = 1
     [Enum(CompareFunction)] _StencilComp("Mask Mode", Int) = 6
 }
 
 Stencil{
     Ref 255
     ReadMask [_StencilMask]
     Comp [_StencilComp]
 }

Comment
Add comment · Show 2 · 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 DaiMangouDev · Sep 07, 2016 at 11:27 PM 0
Share

You saved me time and even got me to learn a bit of shader script . Very awesome . Thank you very much !

avatar image calpolican · Jan 02, 2019 at 05:49 PM 0
Share

Just wanted to thank you a lot for this. I was struggling a lot with it, and this $$anonymous$$ask Layer was what I was looking for.

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

75 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

Related Questions

Image effect shader that masks spritelayer 1 Answer

Adapt alpha mask shader to gradual alpha increase with distance to object center 0 Answers

Using an object as a mask for another object (using Spine) 0 Answers

Generating a texture through Graphics.Blit with shadergraph 0 Answers

2d lighting effect 1 Answer


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