- Home /
Render mesh in front of another mesh but not in general
Hey there, I was wondering if it is possible to let a mesh appear to be in front of another mesh even though it is behind it world space. I need it for an optical illusion.
I figured there is a Option in ShaderLab called ZTest Always.
In this example I've seperated the mesh into two pieces. The one behind the other has a shader with ZTest Always. Thats what it looks like: It kind of does what I want it to do but there is a problem with this method. If there is anything else, like a cube, on top of the mesh it appears to be behind the mesh again but it shouldn't. That is the problem. So I was wondering if there is a way to render the mesh only in front of a specific mesh but not all meshes.
I also stumbled across the option of sorting layers like it's used by the SpriteRenderer only for the MeshRenderer (here) but it didn't work for me.
I've also tried to change the "Custom Render Queue" of the Material but it didn't work too.
I also couldn't succeed with the Queue in the shader itself. I've tried something like this:
Shader "Queue"
{
SubShader
{
Tags { "Queue"="Geometry+1" }
Pass
{
....
What did I do wrong?
Thank you for reading.
This optical illusion is based upon PART of mesh 2 appearing behind mesh 1 and PART of mesh 2 appearing in front of mesh 1. Even if you make mesh 2 into two different parts, the issue remains, which of those goes in front of the other? As a human, we can deduce that if we separate mesh 2 into the CUBE (lets call it mesh 3) and the stretched-cube (lets keep calling it mesh 2), the cube is to be drawn on top of mesh 1 AND mesh 2. But, since this is an illusion, I do NOT see how to write an algorithm to figure that out dynamically.
Just to throw it out there, another way to do this would be to manipulate the mesh verticies (towards/away from the camera), forcing the z-ordering (per vertex, rather than per mesh). If you leave the mesh normals alone, this should result in the same face shading as before vertex manipulation. Though the mesh shapes wont ACTUALLY be the cubes and stretched-cubes anymore, it would still "look" that way to the camera.
Answer by tanoshimi · Jul 15, 2016 at 08:52 PM
Generally, shaders on 3d meshes write to (and cull based on) the depth buffer. That's what the ZTest
bit is for. So objects are drawn in front of others based on their distance to the camera. In contrast, the default shaders used to draw 2d sprites do not write to the depth buffer - the order in which they are drawn is instead based on the sorting layer.
But there's no reason why you can't apply the logic used in the Sprites-Default shader, say, onto a 3d mesh - disable depth testing and use sorting layers instead (you might find installing the "Sorting Layer Exposed" component" from here helpful to assign layers to meshrenderers in the inspector: www.github.com/nickgravelyn/UnityToolbag)
Thank you! I already had the sorting layer exposer but it seems like I did use it in the wrong way.
Thanks to your hint I just added ZWrite Off
to the code and the sorting layer worked.
Thanks again. it works great now
Edit: Exept for one little thing. Somehow the shading is broken. The illusion is working but the shading order is not right. $$anonymous$$aybe ZWrite Off
was the wrong conclusion afterall or did I do something wrong? Could you please help me once more @tanoshimi?
One of the problems with designing scenes around optical illusions is that it's kind of hard to figure out what the true shape of your meshes is :) Can you pull them apart a little bit to make it clear? If you're using sorting layers then the whole of one shape will be rendered on top of (or below) another - I thought that's what you wanted? - you can't have part on top and part below.
@tanoshimi sorry for the imprecise explanation of my scene.
the part rendered on top of the other mesh is "$$anonymous$$esh 2". But my problem is that "$$anonymous$$esh 1" is somehow rendered on top of itself even though it's just one mesh I hope you can see what the problem is :)
Answer by Jacky2611 · Jul 16, 2016 at 06:19 PM
I would watch this video about the making of monumentum valley. I believe they also talk a bit about pathfinding in combination with such illusions.