Rendering objects without mesh intersections

For a comic-style maze game, I need to make sure that characters are rendered “above” each other without the meshes intersecting while at the same time using the z-buffer properly to render the characters in front of or behind the maze walls.

The effect I want to achieve between characters is the same you would get if you put each character onto their own layer and use one camera per character with different depths. Unfortunately it seems that this doesn’t work with the maze because the characters will be rendered either always in front of the maze or always behind the maze.

One possible solution might be rendering the characters into a render texture using some sort of billboards, plus a little z-offset per character/ billboard. But I was hoping that there is a simpler approach.

So … how could this be achieved?

I’ve seen a couple of somewhat similar questions but in those cases it’s either about making sure the meshes don’t intersect (won’t work in my case) or intentionally rendering meshes above everything else (not the effect I’m trying to achieve).

1

… seems like the image isn’t shown, so here’s a link: http://www.ramtiga.com/Portals/1/img/funstuff/MeshIntersections.png

Render one player before everything but your background. Render the foreground player afterwards, using ZTest Always. You can stack as many players as you want, using ZTest Always on all of them but the furthest one back. (You could use it there too, but it’s not strictly necessary.) Here are the simplest shaders that do this; edit whatever shaders you’re actually using, with the Queue and ZTest modifications.

Shader “Background Dude” {

Subshader {
	Tags {"Queue"="Geometry-2"}
	Pass {Color(1,0,0)}	
}

}

Shader "Foreground Dude" {

Subshader {
	Tags {"Queue"="Geometry-1"}
	ZTest Always
	Pass {Color(0,0,1)}	
}

}

If I correctly understood the problem, I suppose you could get this effect by using some transparent shader (diffuse, specular etc. - use alpha=1) for the characters and setting their rendering order with material.renderQueue: transparent shaders read but don’t write to the z-buffer, thus the characters would overlap each other without ignoring the maze walls.

I think that if you have multiple cameras in the same position with different clipping planes and depths overlaid on the same screen, you’ll be able to render one object as being “in front” of another without it “phasing” into another object… assuming that it’s a consistent distance from the main camera.