Using 2D sprites and depth textures to fake pixel depth.

Hello everyone! I hope y’all are doing great! I need some of your wisdom since I’m not very good with shaders. What we are trying to do is using 2D sprites and a depth texture (Black & White) so we can fake depth per pixel in order to solve sorting issues in a 3D world. Kinda of what the Sims 1 did back in the day with the 2D pre-rendered objects and the 3D model of the sim. We have a shader with two passes, one is the ShadowCaster pass that what it does is read data from a depth texture in the fragment shader using uv input and returning that depth value. The other pass is just for rendering the actual sprite and color.

As you can see in the Sims 1 picture when the player moves the bed in a position that intersects with the Sim, the Sim doesn’t get fully covered by the bed sprite, is like the bed sprite only cuts the sim in half.

PD: Also that depth texture view seems to only appear in the frame debugger if I have a script that sets the camera depthTextureMode to “DepthTextureMode.Depth”.

According to that depth texture view the upper right cube should be rendered on top of the other cubes, but when rendering the opaque geometry it seems to skip the depth data. I don’t know if we are doing something wrong.

Any help is super appreciated. Thanks in advance.

2 Likes

@bgolus we need ur help

I’m uploading the shader code in this comment since I can’t upload more files in the message.

Also, in this link: http://www.simfreaks.com/tips/zbuffersbegpsp.shtml is where we found how to create the depth textures.

7877185–1001638–CustomDepth.shader (2.99 KB)

The depth texture rendered in UpdateDepthTexture is not the one that is tested against in your opaque pass (and even if it was, you’d still have to compute correct per fragment depth to do depth-testing the way you want)

You have to enable ZWrite in your opaque pass, sample your per-object depth texture and compute the correct fragment depth using it and then write to fragment depth by adding an SV_DEPTH output, something like

half4 frag(v2i i, out float depth : SV_Depth) : SV_Target
{
    half4 col = tex2D(_MainTex, i.uv);
    depth = SomeCalculation(i, SAMPLE_DEPTH_TEXTURE(_DepthTex, i.uv));
    return col;
}

Thanks for the help!