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

Shader outline not visible over the terrain

Discussion in 'Shaders' started by niv_kor, Dec 16, 2020.

  1. niv_kor

    niv_kor

    Joined:
    Aug 31, 2019
    Posts:
    13
    Hi,
    I'm using "NPR Cartoon Effect" shader with a black outline for one of my objects, and it worked really well until I converted my terrain to Micro Splat terrain.
    Now I can only see the object's outline when the terrain is not behind it.

    The object with nothing behind its top half:


    The object with the terrain behind it:


    What causes this behaviour? Do I need to change some settings in the Micro Splat terrain?
     
  2. niv_kor

    niv_kor

    Joined:
    Aug 31, 2019
    Posts:
    13
    If anyone else encounters an issue like this, I fixed it by changing the setting on the terrain's MicroSplat material.
    Under 'Splats' tab, change 'Render Queue' to 'Geometry'.


     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,208
    I'd say this is more a limitation of the toon shader, though it is a common issue with the technique being used by it.

    For modern GPUs, opaque objects are sorted per pixel using a z depth buffer. This ensures the final image is always the same regardless of what order the opaque objects on screen are rendered. When an object is rendered, it also renders how far away it is from the camera. If something is rendered after that object, but further away, the GPU doesn't update those pixels it overlaps with the closer object. If something is rendered closer, it overwrites the color and the depth with the new closer value.

    Transparent objects can't do this, because they can be semi-transparent, so they need to be pre-sorted before they're drawn.

    The issue here is similar. The way the outline for this shader is working is it's rendering the object slightly larger and with a solid color, but not rendering it to the depth buffer. Then rendering the object afterwards, which since the outline didn't write to the depth, it overwrites it no matter what. The problem is if anything else renders after the object, it too will render over the outline, which is what you were seeing.

    You fixed this by changing the terrain material to use the Geometry queue (2000) instead of what I'm guessing was defaulted to Alpha Test (2450), which the mine material may have also been using, or something else between 2000 and 2450. You could have also fixed this by making sure the mine's material was after 2450, like 2451, which may be what you want to do anyway since if you have any other alpha tested objects in the scene at some point, they too will draw over the outlines.
     
  4. AlexTorbin

    AlexTorbin

    Joined:
    Dec 12, 2019
    Posts:
    48
    A noob question out of curiosity. If outline pass is not writing depth, why it wasn't erased by skybox on the first screenshot? I'm guessing its URP and I believe it renders skybox after opaque geometry..
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,208
    The answer is ... it should have, and probably will in the Game view.

    However in the Scene view Unity always renders the skybox first.