Search Unity

Shader set to Transparent see self through where Alpha is white.

Discussion in 'General Graphics' started by tasticad, Oct 8, 2019.

  1. tasticad

    tasticad

    Joined:
    Dec 4, 2017
    Posts:
    10
    Hello, well this might be something stupid but I'm stuck with this for hours without figuring it out.

    My project uses Unity 2018.4.10f1 and Lightweight RP. I need part of my object to be transparent then I set Surface type in that mode. But the object now sees through itself when it shouldn't.

    I hope my picture as all the details needed: at the top of the Inspector is the used Shader and under the png image I use with Alpha channel.

    Is there a reason why I see through this pant when the Alpha there is completly white? Help would be very appreciated.
     

    Attached Files:

  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Unfortunately, there is no convenient way for draw concave transparent mesh.
    The existing hack is to draw the model twice, the first pass is written only to the depth buffer, the second - as always, but I do not know how to do this in LWRP.
     
    Last edited: Oct 9, 2019
    tasticad likes this.
  3. tasticad

    tasticad

    Joined:
    Dec 4, 2017
    Posts:
    10
    Thank you mouurusai for your reply. Indeed one of the LWRP specificities is the direct single pass rendering, is that then what explains the present problem?
     
  4. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    No it's not, the source of problem is fundamental way how transparent geometry is drawing.
    As it turns out, you can use the draw to depth hack even in LWRP, just assign 2 materials to single renderer, (z only) as first.
     
    tasticad likes this.
  5. tasticad

    tasticad

    Joined:
    Dec 4, 2017
    Posts:
    10
    I'm not sure to get this ^^' although I understand it's all related to the draw to depth.

    The quick n' dirty way I'd get away with it is to separate my model on two pieces (one with opaque material, the other with transparent) but I'd like to understand your hack as I'm getting deeper into custom shaders lately.
     
  6. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Triangles are drawn in the same order in which they are written in the model — not sorted relative to the camera, so it's may cause a situation when a farther triangle draws on top of a closer one.
    When opaque objects are drawn, they write to the depth buffer, so the pixel that passed the z test (the closest by default) will be drawn last.
    Transparent objects do not write to the depth buffer, this why the last drawn pixel can be further away than the one already drawn in the screen buffer.
    The hack allows you to get around this problem, but when you use it, you need to draw such an object after all the transparent geometry, because it will occlude other objects otherwise.

    Some articles about transparency:
    https://en.wikibooks.org/wiki/Cg_Programming/Unity/Transparency
    https://en.wikibooks.org/wiki/Cg_Programming/Unity/Order-Independent_Transparency
    https://docs.unity3d.com/Manual/SL-CullAndDepth.html

    Can't get rid of transparent surface shader artefacts
     
    Last edited: Nov 3, 2019
    tasticad likes this.
  7. tasticad

    tasticad

    Joined:
    Dec 4, 2017
    Posts:
    10
    Oh waw, this is a huge clarification for me! Thank you so much for your explaination on the difference between opaque and transparent rendering :)

    Thank you so much for the links too, those last two ones might be of a big help to a very actual problem I have!
    You've been of a great help there mouurusai, wish I could offer a drink :)
     

    Attached Files: