Search Unity

Pixel Depth Offset

Discussion in 'Shaders' started by GuitarBro, Mar 23, 2019.

  1. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    I'm attempting to write a custom hair shader and wanted to implement something along the lines of UE4's pixel depth offset to alleviate nasty polygon intersection artifacts. Essentially I want to take a heightmap and offset the pixel's depth based on the value (not the whole polygon's depth, mind you).

    Is it possible to do such a thing in Unity (deferred rendering) without a custom render pipeline? Would it be possible with a custom render pipeline? If neither are true, is it a feature that may come sometime (hopefully soon) in the future?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,338
    Yeah, it's a standard Direct3D and OpenGL feature, available since OpenGL 2.0 or DirectX 10. The only crux is you'll need to write a custom vertex fragment shader, or modify the generated code from a Surface Shader to do it.

    Here's some data on using SV_Depth
    https://forum.unity.com/threads/wha...-modify-z-buffer-value-using-sv_depth.526406/

    Writing a custom deferred shader ... I'd stick with writing a basic Surface Shader and modifying it rather than trying to do that from scratch. Though you did mention hair ... If you were planning on using alpha blending on your hair shader, it won't be rendered using deferred regardless of if you've enabled deferred rendering for your project or camera as deferred only handles opaque objects.
     
    lielight likes this.
  3. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Oh cool, so it might be feasible then. I'm actually using temporal dithering with TAA for hair transparency so it can stay in the deferred pass and cast/receive shadows. Right now I'm just using Amplify Shader Editor for quick prototyping but I may move it over so I can modify the generated code.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,338
    The Amplify guys seem like they are really good at adding features when people ask for them, so it may be possible to ask them for it. However last I knew they build on top of the existing Surface Shader functionality. Since I don't believe you can make Surface Shaders modify the depth output per pixel, you're probably stuck modifying the generated code.