Search Unity

Visual glitch with URP material. Transparent, Alpha and Both Sides combination

Discussion in 'Shaders' started by everalan10, Jun 7, 2021.

  1. everalan10

    everalan10

    Joined:
    Mar 8, 2014
    Posts:
    10
    I need a transparent both sides sphere. But appears glitched.

    This visual glitch appears when I use a default URP material with the next parameters: transparent, alpha and both sides properties activated.

    Can I solve this or simply it doesnt exist that posibilty.

    Unity default shader problem.png

    This is a gif

    Unity Shader Transparent.gif
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Correct and efficient sorting of real time transparency is an unsolved problem.
    https://forum.unity.com/threads/render-mode-transparent-doesnt-work-see-video.357853/#post-2315934
    https://realtimevfx.com/t/transparency-issues-in-unity/4337/2?u=bgolus

    TLDR version: Single transparent meshes are rendered based on the order of the triangles in the mesh. These are not sorted based on the camera position, because that's very expensive, so it's just whatever the order was when the mesh was imported. The first triangle to render shows up behind the triangles rendered afterward. And that's it.

    For a simple case like a sphere or similarly mostly convex object you can get away with rendering using two passes. Render all interior faces in one pass, then render all exterior faces in a second pass. Unfortunately URP doesn't support shaders with more than one lit pass, so you have to do this as two materials, one set to Render Face > Back, and the other set to Render Face > Front. Then set the "back face" material to render before the "front face" material. For the built in render path this would have been done with the render queue, but for the URP you'll want to use the Priority settings under Advanced.

    For more complex shapes ... there's no perfect solution. Just a handful of workarounds and hacks with different pros and cons. Some of which are detailed in the second link above, though all of them are basically impossible to do in the URP without at the very least handwritten custom shaders, and possibly significant modifications to the URP itself.
     
    nbsdp likes this.