How do you determine the depth of a fragment?
I'm working on a game that uses the sprite stacking technique for both sprites and lighting, it works kind of like this:
- Turn on gpu depth testing.
- Draw all the non-light sprite stacks, passing the depth of each slice into the shader as a uniform and using a vertex shader to set the z coordinates of each vertex to this depth.
- Copy the fully rendered scene to a backup surface for later reference.
- Turn on subtractive blending and call
gpu_set_zfunc(cmpfunc_equal) - Draw all the lights over the scene (lights are stacks of sprites that form spheres or cones that approximate the spread of a light source). Because we've set the z testing function to cmpfunc_equal this result in darkening of pixels where a sprite and a light overlap and are on the same z-level in the world.
- Feed the darkened version of the scene, and the backup copy we took into a shader that reverses the darkening and applies shadows to everything else instead.
The result is pseudo-3D lighting and looks like this:
The issue I'm having is with incorporating billboard-style sprites into the rendering in addition to the voxel-like sprite stacks.
Here's an example:
The pacman sprite nearest the car has been z-tilted - the top of the sprite is closer to the camera than the bottom. This lets it clip through the car as though it existed in 3D space, but makes it impossible to light (Ignore the lighting on the other pacmans, I got that by turning off z-testing for lights completely and flattening the lighting down - it's not a workable solution).
Rather than having a single depth value like everything else in the scene pacman has a different depth value for each row of pixels, interpolated between the depth of the object and the depth of the object minus the offset I applied when I tilted it.
Crucially: none of these depth values match the depths of the lights in the scene, so the lights will always ignore pacman.
I'm not even sure how to begin solving this problem. My instincts tell me there must be a set of z values I can apply to the vertices such that the computed depth of each fragment will work out to exactly match the depth values of other things in the scene, but I have no idea how to figure that out.
Step one was to try and visualise the depth values my fragments are ending up with, but there doesn't seem to be a way to do that. gl_Fragcoord.z doesn't actually vary from fragment to fragment, and GLSL ES doesn't support gl_Fragdepth. There also doesn't seem to be a way to set the final depth of a fragment, you just have to take whatever the vertex shader's interpolation gives you.
Has anyone else ever tried something like this? Any help would be greatly appreciated.
Posted Jul 5, 2020, 1:28 PM UTC