Search Unity

Rendering Roads Without Z-Fighting in Deferred

Discussion in 'Shaders' started by CW3MH6, Apr 20, 2020.

  1. CW3MH6

    CW3MH6

    Joined:
    Jul 4, 2013
    Posts:
    14
    I am trying to render roads over the terrain without having weird z-fighting and terrain LOD issues. This is in the Built-in rendering pipeline, as we cannot yet transition to something like HDRP.

    I currently render my roads using the same triangles as the terrain, i.e. I get the bounds of the road, generate a terrain patch corresponding to this bounds, and clip any triangles outside the road outline. This works well, and so long as the terrain has no LODs it looks good. The problem occurs when the terrain LODs start to be drawn at distance, they render through the roads because of their lower resolution. I have gone so far as to remove terrain quads below the road via SetHoles in the terrain, which helps but since I can only do this to entire terrain quads, it does not give me fine enough resolution for the borders, only the inner areas.

    I can of course generate road LOD "patches" that match terrain patches and transition them out when the terrain patches transition but that seems like a lot of extra headache. So I'd rather see if I can find a shader solution.

    I have tried stencil buffer masking, which works but only when the terrain is rendered in forward (using the unused 5th stencil bit), but we need it rendered in deferred.

    I have looked into deferred decals and how they are rendered but I've only ever seen them used with simple box/quads, and I'm not sure I could apply the same techniques to a long, complicated, curving road.

    I have tried ZOffset, which causes undesirable graphical glitches, like objects on the road being cut off at the bottom, and this still does not solve the LOD issue without absurdly high ZOffsets that basically render over everything.

    I have even tried offsetting road vertices along the camera's view dir and rendering right after the terrain but disabling Z-Write which almost worked but causes its own weird lighting and graphical artifacts.

    Making the meshes "deeper" (i.e. a box instead of a plane) is undesirable and will not solve the terrain LOD issue. A box used to help in rendering i.e. the stencils is fine, but the road mesh itself should be as flat as the terrain.

    Can anyone point me in the right direction for solving this problem? Looking at other games, it seems like they simply stencil out the required areas when rendering the terrain, which is the logical solution but not one we can use in Unity's deferred built-in pipeline apparently. For example, notice in the following images how the terrain is rendered with the paths masked out.



    How might one achieve this using Unity's built-in deferred pipeline?
     
    Last edited: Apr 20, 2020
  2. Blazso

    Blazso

    Joined:
    May 19, 2015
    Posts:
    1
    ever find an answer to this? lol