- Home /
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).
... seems like the image isn't shown, so here's a link: http://www.ramtiga.com/Portals/1/img/funstuff/MeshIntersections.png
I may be able to help, but don't understand. :-( http://en.wikipedia.org/wiki/A_picture_is_worth_a_thousand_words
I've edited the question to contain an image - but I don't see the image, so here's a link: http://www.ramtiga.com/Portals/1/img/funstuff/$$anonymous$$eshIntersections.png
Edits don't appear for like an hour now. This site is rather unusable at the moment.
Answer by Jessy · Mar 13, 2012 at 10:40 PM
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)}
}
}
Sorry - I'm afraid my description saying "like with different cameras" was a bit misleading. Your solution always renders Background Dude in the background, and Foreground Dude in the foreground (just like it would be with the different cameras).
But what I really need is Background Dude to "pop forward" when he passes by Foreground Dude. So sometimes DudeA would be the background dude, and sometimes DudeB would be the background dude.
A shader can't handle that for you. You need to either script $$anonymous$$aterial.renderQueue or switch shaders.
Actually, with ZWrite Off this seems to work quite nicely. I still got some issues with my models when using that approach but those can be fixed. Basically, what I get now is that ins$$anonymous$$d of intersecting / overlapping, the whole model "pops to front" when DudeA comes closer to the camera than DudeB.
The "popping" should be predictable, based on your scripting. I recommend queues 2500-2999 for this effect, if you're using ZWrite Off ins$$anonymous$$d of ZTest Always.
Answer by aldonaletto · Mar 13, 2012 at 09:11 PM
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 should do it ... the only problem I have at the moment is that the models I'm using look pretty messed up when they are being used with transparent shaders (the problem is that there's e.g. hair on top of a head, and using the transparent shaders, the head "pops out"). But that's a problem that can be fixed ;-)
There's no reason the shaders have to be "transparent" i.e. blending. The key is the ZWrite off. That's a viable solution; I offer another in my answer.
I suggested the transparent shaders only as a starting point - the necessary shaders could be edited to only read the ZBuffer. But the "local" depth (relative depth of different model parts) should be solved somehow.
Answer by Christopher K. · Mar 13, 2012 at 09:37 PM
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.
With "consistent distance" you mean the rrelative distances remain the same? If so, that's where this approach fails: As it's a maze game, the characters (spheres in the linked image) move back and forth, so having a static set up wouldn't work.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065107im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to make an horizontal mesh? c# 0 Answers
Weapon system 0 Answers
Rendering part of the mesh 0 Answers
Do I need a program like Blender3D or 3DSMAX to create objects and characters? 2 Answers
How does Mesh.CreateVBO work? 1 Answer