Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Special opacity shader wanted

Discussion in 'Shaders' started by Cascho01, Sep 10, 2012.

  1. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Hi,

    I am developing an AugmentedReality Projekt where parts of the mesh are located under the floor (Imagine a house with a basement level).

    To get a realistic visual effect I need to "open" the floor over that basement area.

    Are there any ideas how to do that?

    Thanks
    Carsten

    Another way would be a shader that makes an object (a fake floor mesh with a hole in the middle) full transparent and culls all objects behind it.
    Would this be possible?
     
    Last edited: Sep 10, 2012
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    The technique is known as Z masking.

    Create a mesh that represents the floor and give it a material with this shader (remember and cut out a hole for the bit you want to see through).

    It'll write to the ZBuffer, so it'll occlude any geometry that's behind the polygons of the floor (because a depth test will tell it your floor mesh is in front of it, so don't render there) but it will be otherwise invisible.

    The Queue of Geometry-50 that this shader will make it draw before the rest of the geometry in the scene.

    The ColorMask 0 means this shader doesn't render to either the RGB or A channels of your screen output (but WILL still render to the depth buffer).

    Code (csharp):
    1.  
    2. Shader "Custom/ZMask" {
    3.  
    4.     Properties {
    5.     }
    6.  
    7.     SubShader {
    8.         LOD 100
    9.    
    10.         Tags { "Queue"="Geometry-50" "IgnoreProjector"="True" "RenderType"="Opaque" }
    11.        
    12.         Pass {
    13.             ColorMask 0
    14.         }
    15.     }
    16. }
    17.  
     
    Last edited: Sep 21, 2012
    Frolky, plasmanunchucks and AliBuck like this.
  3. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    WOW, that´s it!
    Thanks so much!!!

    Carsten
     
  4. benb

    benb

    Joined:
    Aug 9, 2012
    Posts:
    20
    Any idea how to recreate this using a fragment and vertex customised shader program? I need to apply this effect along with a couple of others and am struggling to understand how to output this
     
  5. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    The important part is that tag of "Queue" = "Geometry-50" - this ensures it draws before things with "Geometry" as it's queue.

    So anything you write - either custom vert/frag or surface shader - that has a tag saying "Queue" = "Geometry" will be hidden by a mesh with the above shader applied.

    For more on what the Queue tag is, read up on this; http://docs.unity3d.com/Documentation/Components/SL-SubshaderTags.html
     
  6. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Now I run into clearing-problems when I use a skybox.
    Could you help me once again?
     
  7. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Could you post pics? It shouldn't affect the skybox, that's the first thing that's rendered.
     
  8. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
  9. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    I've had a look at this and honestly I'm a bit stumped.

    As far as I can tell, the skybox shader should render before everything else, so the ZMask shouldn't affect it at all.

    I dunno how to fix that :(
     
  10. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
  11. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    For what it's worth, I was testing it with the default skybox shaders (the regular 6-image skybox and the cubemapped skybox) and the mobile one (which is the same as the 6-image one but without a tint, I think).

    Also created a custom skybox shader based on the 6-image one and messed with it's render queue and ZTest settings... to no avail.

    Camera has to have Clear Flags set to Skybox otherwise it doesn't render it at all.


    I guess you could do a Source Engine thing and have a second camera in the centre of an actual box, with it's rotation slaved to the main camera, and set it to render first.
     
  12. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Hmmm, ok - so I have to wait until someone find´s a solution.
    Anyway, thanks for helping!
     
  13. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    It's rather difficult to fix problems based on screen shots only.

    I think you should prepare a simple Unity project that shows the problem and make that project available. (We are good here, but we aren't wizards. ;) )
     
  14. Spikeh

    Spikeh

    Joined:
    Jun 7, 2013
    Posts:
    24
    I appreciate this is an old thread - but this is exactly the shader I'm looking for at the moment. I'm getting the clearing issue as well. I've put a unity project together to see if anyone can help us get this working.

    The sphere in the test scene has the shader above attached to it, and I've extrapolated the unity skybox shader into a modifiable shader file. The skybox just reuses a single texture, just to save zip file size. Looks naff, but does the job.

    Weird thing is, the scene view is rendering the shader properly, but the game view is displaying the artefacts.

    Any help is appreciated, I've been reading about Shaders / Cg for days, but I'm still yet to figure out what I'm doing - it's sinking in very slowly :(
     

    Attached Files:

  15. TheShane

    TheShane

    Joined:
    May 26, 2013
    Posts:
    136
    I just wrote a similar shader for a different thread, and I noticed it suffered from the same bug after reading your post.

    I fixed it by creating a dummy camera (culling mask set to "Nothing") with a lower depth that the main camera. Clear the dummy camera to "Skybox" and set the main camera to "Don't Clear".

    This seems to work properly. I don't know why this would be necessary. It looks fine in the editor, but doesn't clear properly in-game. Strange.
     
  16. Spikeh

    Spikeh

    Joined:
    Jun 7, 2013
    Posts:
    24
    Hmm... luckily, in my main game project I already do this (or at least something similar that I can play around with) as I have different cameras for rendering things at different distances. I'll give it a go... though you're right, I'm not sure why I need to!
     
  17. Spikeh

    Spikeh

    Joined:
    Jun 7, 2013
    Posts:
    24
    I can confirm that this works. It's a bit annoying you have to go to this extra step to get it to work, though :(
     
    AliBuck likes this.
  18. Spikeh

    Spikeh

    Joined:
    Jun 7, 2013
    Posts:
    24
    Something else I just noticed - you have to have "Receive Shadows" set on the mesh renderer of the object in order for it to even be visible.
     
    plasmanunchucks and AliBuck like this.
  19. AliBuck

    AliBuck

    Joined:
    Aug 22, 2020
    Posts:
    30
    2022 and it still works! Thanks :)