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 jilleJr · Feb 25, 2016 at 01:38 PM · shadershader writingstencilztest

Stencils and zTest be buggin

What is going on?!?!? I like shaders 'cause they're cool and all, but gosh they're annoying to debug.

Now. I have a simple stencil shader setup, where I have the player writing to the stencil when it's behind something, like so:

 (Image this being inside the shader, I cut out the boring parts)
 Properties
 {
     _MainTex ("Texture", 2D) = "white" {}
 }
 SubShader
 {
     // Just write to the stencil
     Pass {
         Cull Off
         ZWrite Off
         ZTest Greater
         ColorMask 0
         Stencil {
             Ref 1
             Comp always
             Pass replace
         }
     }
 
     // Draw normally ...
     2nd pass {


Then I have an image effect that writes with a color if stencil=1, real simple. Reason I am doing it this way is that via an image effect shader I can easily do the silhouette as postprocessing, without having to wonder about draw order and such. Also, this way I can have a transparent silhouette, and it won't stack so there will only be one "layer" of transparent color on top. Just the way I like it.

Good system, I'ma noob at shaders so I'm glad it works. But then stuff starts acting weird.

How I understand it, when the ZTest says the pixel is behind something, the stencil can do it's magic, but for some reason it doesn't always work(?). Inspect the following gif:

stencils-be-buggin.gif

It may be unclear at first, but the top half is the scene view, and the bottom half is the game view. Sideeffect of doing the silhouettes the stencil way is that I can only see the effect in the game view. Butt. In the scene view, I'm dragging a plane in and out of the big tree in the first shot and the ground in the second, and the plane has a shader with ONLY the "write-to-stencil" pass, no second pass (Also, could someone pls tell me why the plane is visible in the first shot, it's transparent black, for some reason?? There's literally zero code that actually makes it draw, wtf). Now in the tree shot, the stencil part only works when the plane is like >50% inside, and in the ground shot, the stencil part is kinda clipped.

Now I should note that both the tree and the ground has an unlit texture shader, tho the tree has a slight modification where I added Cull Off (yes it's necessary, workaround is not what I'm looking for) and the ground is actually a terrain, believe those renderes differently, not sure tho.

If I change the tree from Cull Off shader to an unlit texture shader, i.e. same as before but w/o the Cull Off part (so it defaults to Cull Back), it works perfectly, without activating half into the tree for no apparent reason.

My question here is do anyone here know what is going on? And if it's fix'able, how? Is there maybe something I missed 'cause as I've said I'm pretty new to this shader thing so errors are bound to happen. Please tell me if you need more info/code and I'll supply it.

Here's a small showcase of one time I got it to work long enough for the gif, so that's what I am aiming for: silhouette

Please reply as I enjoy a good discussion with someone who knows what they're talking about.

Ty for ur time, peace! ✌

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Performance of looping compute shader in Update() 2 Answers

Custom Surface shader don't work with fresnel reflection and normal map at the same time 0 Answers

Shader error in 'libS/Lit': redefinition of 'SurfaceData' at 0 Answers

extend Mobile-Bumped and Mobile-BumpedSpec.shader with color 1 Answer

Shader metallic effect 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