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;
}
Tags
depth, orthogonal
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from KarlTheCool

Pixel Perfect Sprite Stacking Edges in 3D

Related shaders

Depth Buffer Object Edge Dissolve

3D Pixel art outline & highlight Shader (Post-processing/object)

Sobel Edge Outline shader per-object

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments