Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by Twice-Circled · Jul 29, 2014 at 06:07 PM · shadersstencil

Is it possible to AlphaTest prior to writing to the Stencil Buffer?

I want to use the stencil buffer to store mask information for a 2D game. The idea is that first I draw a sprite only to the stencil buffer (no RGB) e.g. value = 1 and then any sprites/pixels drawn on top are tested against this and masked if the value is 1.

This is relatively simple but the problem I'm having is only writing to the stencil buffer in pixels where the src alpha is above a certain value rather than across the entire quad. I want the mask to follow the shape of the sprite i.e. by pixel rather than geometry.

I've tried including "AlphaTest Greater 0.5" but it doesn't seem to be working.

Here's my masking shader:

 Shader "Custom/StencilMask" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
     }
      SubShader {
         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
         ZWrite Off Lighting Off Cull Off Fog { Mode Off } Blend Zero One
         LOD 110
         Pass {
             // Only write to stencil where alpha is greater than 50%
             AlphaTest Greater 0.5
         
             Stencil {
                 // Set stencil value to one if we pass
                 Ref 1
                 // Don't compare, we are only interesting in writing
                 Comp always
                    // This tells it to set the value, this should always pass because nothing is writing to the depth buffer.
                 Pass replace
             }
         }
     }
 }

Then the sprite to be masked has the following inside it's pass:

     Stencil {
         // Compare zero with the stencil value.
         Ref 0
         // Passes if it's equal. Masked areas have value 1
         Comp equal
     }

This works as intended however the stencil buffer seems to be written into regardless of the alpha value of the pixel.

To restate the title question: Is it possible to AlphaTest prior to writing to the Stencil Buffer?

I have already found a work around which is to write to the depth buffer instead to mask pixels drawn below. However I would prefer to use the stencil buffer if possible as it gives me more fine tuned control over which sprites mask which other sprites.

Thanks in advance,

Tim

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
7

Answer by CardTrick · Jan 08, 2015 at 11:06 PM

This is possible if you put an explicit discard statement in your shader.

For example:

 fixed4 color(...): COLOR {
     fixed4 col = ...;
     if (col.a < 0.5) discard;
     return col;
 }

I've tested this and it definitely works. I don't know how efficient it is though - I know it's often a bad idea to use discards. Still, it might be OK in your use-case/on your platform.

Hope it helps.

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

23 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

Related Questions

Problem with transparency and stencil shader 1 Answer

Shader that renders pixel with highest alpha value? 0 Answers

Multi-shader Stencil Buffer not working as expected,Multi-shader Stencil not working as expected 0 Answers

Adding a Stencil to Particle Standard Surface shader 0 Answers

Standard Shader Still Visible through Stencil Shader 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