Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Intersection shader via depth compare breaks in deferred rendering mode?

Discussion in 'Shaders' started by Deleted User, May 10, 2017.

  1. Deleted User

    Deleted User

    Guest

    SOLUTION TO PROBLEM CAN BE FOUND HERE:
    https://forum.unity3d.com/threads/h...ly-runs-in-deferred-mode.470542/#post-3068567


    I'm writing a custom shader which detects intersecting geometry by comparing the fragment depth to the scene depth like so:

    Code (CSharp):
    1.  
    2. // ...
    3. Tags {
    4.     "Queue" = "Overlay"
    5.     "RenderType" = "Overlay"
    6.     "IgnoreProjector" = "True"
    7. }
    8.  
    9.     CGPROGRAM
    10.     #pragma surface surf Standard vertex:vert
    11.     #pragma target 3.0
    12. // ...
    13.  
    14. void vert(inout appdata_full v, out Input o) {
    15.     UNITY_INITIALIZE_OUTPUT(Input, o);
    16.     COMPUTE_EYEDEPTH(o.eyeDepth);
    17. }
    18.  
    19. void surf (Input i, inout SurfaceOutputStandard o) {
    20.     float depth = i.eyeDepth;
    21.     float sceneDepth =  inearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture,UNITY_PROJ_COORD(i.screenPos)));
    22.     float depthBlend = clamp((sceneDepth - depth) / 0.1, 0, 1);    
    23.  
    24.     o.Emission = depthBlend;
    25. }
    26.  
    It works great in Forward rendering (top) but breaks in Deferred mode (bottom):



    I end up with black color and weird white artifacts near edges it seems.

    Do I have to do something special for this to work in Deferred?
     
    Last edited by a moderator: May 15, 2017
  2. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,492
    Deferred render depth layer once and then apply shader at image level after, you are making a shader per object basis, it wouldn't work.