Search Unity

Help Wanted Question about transparency and rear faces

Discussion in 'Shader Graph' started by alanmthomas, Nov 8, 2019.

  1. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    I am working on a transparent shader and am coming across an issue I'm hoping there is an easy solution to that I am simply not aware of. I am fading an object, but would like to essentially take the opaque view of the object and fade that. The transparent view shows rear faces and other geometry that I'd prefer to be hidden. In the screen shot, you can see the orange bricks coming through. Some of them are on the back of the object. Does anyone have an ideas?
     

    Attached Files:

  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    590
    Sorry I'm not sure exactly what I'm looking at as it looks a bit abstract, but could you maybe use alpha clip to fade your objects? Then they would pretty much fade like opaque objects... Without sorting artifacts. But I'm not sure if that's the whole thing you are fading or what. It's usually better to show a clear example case.

    Could something like this work for you? (the fading object is the standard Unity sphere mesh)
    ditherfade_demo.PNG
     
  3. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    I think that is what I am talking about. Here are more clear example. First is the object rendered as opaque. Next the same view with transparency. In the transparent view, you can see the little rectangular shape showing through. This is what I'm hoping to avoid.
    Screen Shot 2019-11-08 at 1.19.09 PM.png Screen Shot 2019-11-08 at 1.19.23 PM.png
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    590
    Which renderer are you using? Standard, URP, HDRP? It would be good nowadays to tell that when posting graphics related issues as there's so many things done in different ways on different renderers.
     
  5. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    Good point. I'm using URP.
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    590
    Try this graph, something similar should work. I just put it together quickly, so...
    Expose the parameter to interface. On HDRP you have to enable alpha clip in that gear (settings) menu of the Master node but I think URP had it by default visible, if I remember correctly.

    ditherfade.PNG
     
  7. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    5,344
    if it is one single mesh you may try to enable depth write.
     
  8. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    Thanks for this. I'm thinking that I'm overcomplicating this. I eliminated the excess geometry and got something that is close to the effect I'm going for. Doing the alpha threshold dithering seems to work somewhat, but eliminating the geometry definitely gets the job done.
     
  9. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    How does one go about enabling that?
     
  10. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    5,344
    when it comes to shader graph - hmm, get the shader code from the master node, create a new shader and paste it there.
     
  11. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    5,344
    but sorry, if it one mesh it won't work...
     
  12. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    Okay. Thanks.
     
  13. LandonTownsend

    LandonTownsend

    Unity Technologies

    Joined:
    Aug 21, 2019
    Posts:
    21
    This might help for Universal RP.
    How to add a Z-test pass to any transparent object:
    1. Create a layer for your Z-tested transparent objects:
    upload_2019-11-25_15-26-9.png
    2. Create an unlit transparent material with 0 alpha called ZTestOnly
    upload_2019-11-25_15-27-7.png
    3. Select your Universal Pipeline Renderer asset:
    upload_2019-11-25_15-36-42.png
    4. Add a new renderer feature. Name it whatever you want, but make sure the rest is filled out like so, with the Layer Mask set to whatever your layer is that you want a prepass on, and Material set to your unlit transparent material. Make sure "Depth" and "Write Depth" is checked and "Depth Test" is set to "Less Equal"
    upload_2019-11-25_15-37-13.png
    Then put whatever transparent object you want to have a ZTest pass on that layer.
    upload_2019-11-25_15-41-23.png

    Advantages over dithering:
    No pixelated effect
    Allows for additive or multiplicative blending

    Disadvantages over dithering:
    Other transparent objects will not render behind it
    Does not work correctly with materials whose shaders have vertex modification
     
    florianhanke likes this.
  14. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    171
    I could kiss you! I was trying this approach but couldn't get it to work. This did precisely the trick. I'm not doing any vertex modification so this is ideal. Thanks so much!