Move object’s DEPTH value relative to camera
Modifies the fragment’s DEPTH value so it appears closer to the camera than it actually is in world space.
This only works for orthogonal cameras. I’m pretty sure it’s because perspective cameras use a non-linear depth value and I couldn’t figure out the math to make that conversion.
render_mode skip_vertex_transform
is necessary. Not sure why.
It’s worth mentioning that doing this isn’t free. By manually writing to DEPTH, you force the GPU to skip some early z testing and optimizations.
Shader code
shader_type spatial;
render_mode skip_vertex_transform;
void vertex() {
VERTEX = (MODELVIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
}
void fragment() {
float change_in_depth = 1.0;
vec4 new_ndc = PROJECTION_MATRIX * vec4(0, 0, change_in_depth , 1.0);
DEPTH = FRAGCOORD.z + new_ndc.z;
}