Search Unity

Multiple transparent objects with same material hiding each other

Discussion in 'Shaders' started by DonPepe, Feb 17, 2020.

  1. DonPepe

    DonPepe

    Joined:
    Jun 5, 2013
    Posts:
    11
    Hey,

    I've got an issue and the feeling this could be relativley easy be solved with a stencil or use of the depth buffer. But I can't manage to achieve what I want.

    I need some transparent lines which are very likely to stack at some points. Under those lines are additional solid tubes. I want the lines to be only drawen once where they overlap, but always show the tubes under them.

    upload_2020-2-17_17-5-23.png

    Right now I have a transparent shader writing to zbuffer. What happens is the lines are z-fighting. It often looks like the red circeled one. But I wan't it to always look like the green circeled parts.

    Is there any easy way (without modifying the line meshes) to achieve this?

    Best, Jan
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    You probably want to use stencils instead of zwrite for this. Zwrite is still very draw order dependant, so if something doesn't draw in exactly the correct order, or if two surfaces are co-planar, then you'll get the issues you're seeing. Stencils don't care about that at all (for this specific use case).

    Try this:
    Code (csharp):
    1. Stencil {
    2.     Ref 1
    3.     Comp NotEqual
    4.     Pass Replace
    5. }
    That's saying check if the stencil is set to 1 (Ref 1). If it isn't (Comp NotEqual) then draw the object and set the stencil to 1 (Pass Replace). If it is 1, skip drawing.
     
    OzgurGurbuz and DonPepe like this.
  3. DonPepe

    DonPepe

    Joined:
    Jun 5, 2013
    Posts:
    11
    Awesome, I thought I tried this. But, whatever ‍:rolleyes: Thank you very much!
     
    Last edited: Feb 18, 2020