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

Help with Shader Replacement for Depth Texture

Discussion in 'Shaders' started by dvdarias, Nov 28, 2018.

  1. dvdarias

    dvdarias

    Joined:
    Sep 24, 2014
    Posts:
    12
    I am using the Unity Sprite Uber Shader: https://github.com/traggett/UnitySpriteShaders
    and its been great so far. I can 't recommend it enough.

    The problem is that i am trying to have Post Effects like Depth of field or Ambient Obscurance working with soft edged sprites.

    The developer tells you to setup an included replacement shader on the camera to get soft edged sprites but I have miserably failed to make it write to the depth texture of the camera with the post processing effects, mainly because I am new to shaders in general.

    References I found and failed to apply/understand to solve this problem:

    CameraDepthTexture (https://docs.unity3d.com/Manual/SL-CameraDepthTexture.html)

    ShaderReplacement (https://docs.unity3d.com/Manual/SL-ShaderReplacement.html)

    Second Camera: renderWithShader (https://answers.unity.com/questions/1419884/second-camera-renderwithshader.html)

    Rendering with replaced shaders (https://forum.unity.com/threads/rendering-with-replaced-shaders.81437/)

    Could someone who has some experience with replacement shaders point me in the right direction on how to make a replacement shader write only to the depth texture of the camera??
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Yep ... nope.

    The camera depth texture doesn't use replacement shaders, and you cannot write to this texture whenever you want. The only way to augment the built in depth texture is to have opaque queue objects with a shadowcaster pass, or to use command buffers to manually draw objects into the depth texture during the AfterDepth event.

    You can certainly render a depth texture using replacement shaders, but it won't be the same depth texture that the built in post process effects use. You'd have to write your own post process shaders that use your custom depth texture.

    But this doesn't solve the base problem. Depth textures record a single depth. Soft edge sprites by their very nature mean that more than one depth needs to be known at the pixels where the opacity isn't 100%. So your AO and depth of field will end up creating hard edges removing the whole point of having soft edged sprites. There's no way around this. You're better off making your sprites hard edged and using a post process AA. Semi-transparent objects will however always be excluded or wrong.
     
  3. dvdarias

    dvdarias

    Joined:
    Sep 24, 2014
    Posts:
    12
    Thanks a lot for the reply.

    I think this is my biggest problem, I would like to reuse the effects on the post-processing stack. In your opinion, there is no way a can write to the depth buffer that the post-processing will use?

    I understand your point. My objective was to write in the color buffer with soft edges and in the depth buffer in the case of an opacity different from 1 interpolate with whatever is on the depth texture at the time of the drawing. In fact, this is what the replacement shader did.

    The author of the 2d shader said in the Readme:

    I after reading I thought this was possible. :(
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    You can override the Depth Normal pass that Unity does to general the camera depth normal texture in the Graphics settings, but there's no way to override the camera depth texture as it doesn't use replacement shaders. The Depth Normal texture has very low accuracy depth, so most post process shaders don't use the depth from that texture, only the normals. Like I mentioned above, it would be plausible to render your sprites into the camera depth texture using command buffers.