Sorting pixels from multiple overlapping sprites by using depth maps.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By facepalm()

What would be the best process for creating a shader to compare the depth maps of multiple overlapping sprites and only drawing the pixel that is highest?

Currently I can do this on a per-sprite basis, but I don’t know how to compare multiple sprites. This is what I have so far, it simply compares the depth map to an arbitrary threshold and draws the pixel if it is above it:

shader_type canvas_item;

uniform sampler2D depthmap : hint_black;


void fragment() {
	
	float threshold = cos(TIME);
	
	if (texture(depthmap,UV).r >= threshold){
		COLOR = texture(TEXTURE, UV);
	}
	else {
		COLOR = vec4(0.0,0.0,0.0,0.0);
	}
	}

This is my second week learning shaders.

Thanks.