Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question URP Lit Shader with Transparency not working as intended

Discussion in 'Universal Render Pipeline' started by glutzer, Oct 4, 2023.

  1. glutzer

    glutzer

    Joined:
    Nov 18, 2017
    Posts:
    5
    Dear Forum,

    I have a problem with displaying my Terrain Tiles correctly.
    I am using LTS 2022.3.5f1
    The Shader is the standart URP, Lit, Specular, Transparent, Alpha, Front, Auto, LEqual
    In the shader I leave the Alpha channel unconneted with the value 1
    Wehen I switch from Transparent to Opaque it works
    -The Problem is that Faces further away are drawn infront of Faces closer with a strange square pattern
    -The Problem seems only to occur within a single Tile of 32x32 Faces (different Tiles dont seem to interfere with each other)
    -When I place a (red) Cube inbetween it blocks the Problem

    I have no idea where to start looking to fix this issue. I would appreciate some Keywords or direction to help me start fixing this. I gladly provide additional information if it helps. Finally a sample picture:

    transparency.jpg
     
  2. Jonas-Mortensen

    Jonas-Mortensen

    Unity Technologies

    Joined:
    Jan 3, 2020
    Posts:
    98
    Hey! This is a classic issue with rasterized graphics :)
    By default, transparent shaders do not write to depth and so there is no telling if a given triangle is in front of another or not. This basically means that triangles are drawn in the order they are sent to the GPU- Something that often gives unwanted results. You could try and set Depth Write to ForceEnabled in the shadergraph but that would only fix it for alpha=1.
    I guess my question is: why do you want your terrain to be transparent?
     
  3. glutzer

    glutzer

    Joined:
    Nov 18, 2017
    Posts:
    5
    Dear Jonas,

    when I Force the Depth one Layer LOD of my terrain works well. But when i have overlapping LOD of the terrain it looks like swiss cheese.

    transparency3.jpg

    I want to blend two LOD of my terrain with the Alpha Channel. I dont know if that is a smart way to go? The Blending is done circular, the Player is the center of the circle. (I started with enabeling and disabeling tiles by script but it got complicated very fast and without blending. The shader solution seemed very elegant and the result, apart from the issue is very good)

    Here is the higher quality LOD of the terrrain to the right and a liftet tile of the lower LOD to the left. One can see the cirular blending.

    transparency2.jpg
     
  4. Jonas-Mortensen

    Jonas-Mortensen

    Unity Technologies

    Joined:
    Jan 3, 2020
    Posts:
    98
    Are you using Unity's terrain system or regular meshes?

    Have you tested performance to see if you actually gain anything from doing this LOD Transition? Alpha blending happens in the fragment shader so you'd still be running vertex shaders for both LODs. You probably also have overdraw for the entire terrain so I wouldn't be surprised if doing a LOD like this is actually more expensive than purely rendering the high detail mesh.

    There really isn't an easy way to fix triangle order issues when rendering transparents. You could look into order-independent transparency but for your specific problem I would do something else. A tessellation shader would be the obvious solution but those aren't trivial to write either.
     
  5. glutzer

    glutzer

    Joined:
    Nov 18, 2017
    Posts:
    5
    Hello Jonas,
    Mesh: I use a regular mesh (but i ordered the vertex indices in Blender to be able to address them easy. In Unity I had to disable opimize mesh the to make it work, but the rasterized graphics problem still happens If I let Unity optimize the mesh.)

    Performance: I assumed that the transparent parts of the Terrain will cost the same as the not transparent parts. I am planning in 4 LODs resulting in tile scales of: 1,3,9,27 therefore I am sure I need a LOD system.

    What I am not sure is if its needed for my game, to be reasonable rather not. If there is no easy fix for that, I'll just continue my project. I am very confident that I can find a terrain solution that suits my needs. I just wanted to know if there is an easy fix for that.

    Is there any other way to blend different terrain LOD's? The tessellation approach looks like a lot of headache :(

    Best Regards Martin
     
  6. Jonas-Mortensen

    Jonas-Mortensen

    Unity Technologies

    Joined:
    Jan 3, 2020
    Posts:
    98
    It's safe to assume that rendering transparents is more expensive than rendering opaques. Some optimizations (e.g. early depth test) are only possible for opaque geometry and transparent geometry is almost always blended on top of (rendered after) opaque geometry resulting in overdraw.

    I don't think this will work very well as a LOD solution for terrain unfortunately.

    I would swap the LODs as they were needed (on the CPU side) and if the pop-in is too obvious, hide it with a dither fade (not transparent) that fades the entire tile. That's also how the Unity LOD system works.

    That is if tessellation is out of the question.
     
  7. glutzer

    glutzer

    Joined:
    Nov 18, 2017
    Posts:
    5
    Dear Jonas,
    thank you for input and guidance. As soon I spend more time on this topic i feel confident to find a solution with the cpu LOD Swap, combined with some sort of fade mecanism. As for now, I dont want to spend more time here, as long the rest of the game is not further developped. (Its so easy to get lost within the possibilities of unity, i need to focus)
    Best Regards Martin
     
    Jonas-Mortensen likes this.