Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Transparency Sorting Issues when rendering both faces (front & back)

Discussion in 'Shaders' started by LAKster, Sep 24, 2023.

  1. LAKster

    LAKster

    Joined:
    Aug 6, 2014
    Posts:
    2
    Hey everyone,

    I'm really new to working on Shaders in Unity so if there is an obvious reason for the behaviour I am about to describe do please let me know.

    Currently I'm working on a shader for some bottles using Shadergraph in URP using Unity version 2022.3.7f1. I would really like to be able to render both front and backfaces for stuff like labels being visible through bottles and also in the future adding liquid (for which many examples seem to use the backfaces to 'cap' the liquid. For example look at this write up; https://www.patreon.com/posts/quick-game-art-18245226 ). However I am currently running in to some unexpected behaviour which I believe might be a bug, but do please correct me if I am wrong on that front.

    I'll illustrate on both a default sphere and my own bottle model.
    So when only rendering the front faces everything is as expected:
    orb.PNG

    Same for rendering only the backfaces, again everything is as expected:
    orbbackface.PNG

    However when I render both the back and front faces the following happens:
    both.PNG

    As you can see instead of essentially 'overlapping' the front faces over the back faces like I was expecting it seems to not bother rendering chunks of the back faces. Then on top of that with specifically the bottle;
    highlighted.png
    at the highlighted spots it seems to be rendering completely non existant geometry af if it was there.

    I was thinking that this might perhaps be an issue with me having the depth write enabled, but I also tested it with the depth write disabled (which wouldn't be an ideal solution, but something I could potentially work around) and then this is the result;
    depthwrite.PNG
    It now correctly renders the backfaces, but seems to have skipped over the front faces in the same area. Almost looks like it's doing the opposite of before (so now it renders the backfaces there instead of the front faces, when I really would want it to render both of them).

    I really hope someone can offer a work around or solution to this issue I'm facing or at least shed some light as to why this happens as it is a major roadblock to what I would like to do with this shader. I have seen multiple shaders out there that seem to do what I want to do, without issue, but sadly because most of these are on the asset store and due to that I have not been able to test if this is an issue in specifically Unity version 2022.3.7f1 that would perhaps also affect their shaders. To ullistrate I'll link three examples here;
    https://assetstore.unity.com/packages/vfx/shaders/liquid-shader-173635
    https://assetstore.unity.com/packages/vfx/shaders/liquid-volume-pro-2-129967
    https://assetstore.unity.com/packages/3d/props/toontastic-modular-potion-pack-260788

    Thank you all in advance for any given help!
    -L

    Edit:
    I realised the write up I linked had it's code fully available. Threw that in to the current version of Unity and it worked, but then figured out it was in fact using an alpha mask, and then rendering the 'liquid' as fully opaque. When I modified the shader code to allow for transparency the same issues arose. This probably means that this is an engine level limitation. Really wish Shadergraph would allow for multiple passes cause that would make this twenty times easier. I'm going to keep tinkering, but any advice is more than welcome.

    The first shader on the asset store I linked for sure has see through bottles with culling correctly done, but it might work in multiple passes which is something I can't do through shadergraph.
     
    Last edited: Sep 24, 2023
  2. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,093
    Core of the issue is how gpu works
    it renders all triangles of one mesh in that order that you create if mesh file.
    Most of the time we dont care that order.

    For your case the only way is to render in 2 passes back faces then front faces.
    While shader graph dotn allow to create this automatically you can just set your material on object 2 times and set different culling modes for each instance (back faces and then front faces)