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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Implementation of 2019.3 custom pass outline effect (occluded outline)

Discussion in 'High Definition Render Pipeline' started by lowbl_dan, Feb 17, 2020.

  1. lowbl_dan

    lowbl_dan

    Joined:
    Jan 22, 2019
    Posts:
    34
    Hi all,

    I'm looking for a solution to doing a world-space outline effect on objects; the outline should be occluded by other objects. I have tried converting the existing standard pipeline stencil based shaders , but it didnt work well and I have tried going with Fresnel effect nodes in shader graph but the outlines seem to appear strange for certain types of meshes, so I am trying out Unity's custom pass in 2019.3 .

    I am not exactly good at writing shaders and I am referencing https://github.com/alelievr/HDRP-Custom-Passes , but I noticed that the fullscreen selection is set to ZTested always, so the outline is always rendered on top and the Selection_Object. shader below seems to discard pixels based on distance. I'm not sure if the discarding of pixels should be done in the selection_object.shader or fullscreen pass if I want to achieve occluded outline.

    Code (csharp):
    1.  
    2. void GetSurfaceAndBuiltinData(FragInputs fragInputs, float3 viewDirection, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
    3.             {
    4.                 if( length(fragInputs.positionRWS) > _MaxDistance ) discard;
    5.                 // Write back the data to the output structures
    6.                 ZERO_INITIALIZE(BuiltinData, builtinData); // No call to InitBuiltinData as we don't have any lighting
    7.                 builtinData.opacity = 1;
    8.                 builtinData.emissiveColor = float3(0, 0, 0);
    9.                 surfaceData.color = float3(1,1,1) * _SelectionColor.rgb;
    10.             }
    11.  
    Thanks for viewing.
     
    Last edited: Feb 17, 2020
  2. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    236
    Hey,

    To achieve "world space" outline effect, it's the Z-test of the objects that needs to be used. Right now the selection object are set to ZTest LEqual which is nice because they will performe the Z-Test against the depth buffer, the problem is that the depth buffer is empty because the custom pass that draws these objects is set to custom depth buffer.
    You can change the target depth buffer to Camera so it will force objects to depth test against the camera deph buffer instead of an empty buffer. Also don't forget to update the clear flags to color only otherwise you'll discard the camera depth buffer too.
    Additionally, you may want to set the ZWrite to Off in the selection object shader so they don't write to the camera depth buffer.
     
    Last edited: Feb 17, 2020
    lowbl_dan likes this.
  3. lowbl_dan

    lowbl_dan

    Joined:
    Jan 22, 2019
    Posts:
    34
    Thanks for the help! Managed to get it to work without changing any scripts, it didn't work the last time because I didn't set clear flags to color. here's the changes made for anyone who wants to know
     

    Attached Files: