Search Unity

Transparent Shader Problem

Discussion in 'Universal Render Pipeline' started by oliran, Feb 17, 2021.

  1. oliran

    oliran

    Joined:
    Sep 29, 2015
    Posts:
    49
    When I set objects to transparent with the default URP/Lit shader, I can see inside of them even though I set alpha to 1 and they have no texture attached. See attached images: they are identical except transparency toggle. What is causing this behavior? Is there a way to fix it?
     

    Attached Files:

  2. Jonas-Mortensen

    Jonas-Mortensen

    Unity Technologies

    Joined:
    Jan 3, 2020
    Posts:
    110
    Hi!
    In order to determine which surface is closest to the camera, URP uses a depth buffer. In the opaque pass, URP draws opaque objects to this buffer. In the transparent pass, the depth buffer is used to determine if a transparent surface is visible. The transparent object itself doesn't draw to the buffer and therefore doesn't know if the fragment it's drawing is the closest one.

    What you can do to solve this is to force that the transparent objects write to the depth buffer. Without writing any shaders this can be done using the render objects renderer feature:

    Start by assigning the relevant transparent objects a layer (chose a name e.g. TransparentWithDepth).

    On the renderer in use; remove the layer from the Transparent Layer Mask in the Filtering section. This will cause all objects in the layer to not be rendered by URP. This is expected as we want to take over rendering of these objects.

    Add a Render Objects renderer feature. Set the event to be after transparents, set the queue to transparents and the layer mask to only draw the TransparentWithDepth layer. In the overrides section, make sure to write to depth:
    Screenshot 2021-02-19 at 14.06.07.png

    I hope this helps! :)
     
    Drayanlia and BATTLEKOT like this.
  3. oliran

    oliran

    Joined:
    Sep 29, 2015
    Posts:
    49
    It worked, YOU ARE THE BEST!! Thank you!
     
  4. SodiiumOnYoutube

    SodiiumOnYoutube

    Joined:
    Jul 30, 2020
    Posts:
    3

    Which GameObject do I assign these scripts to?