- Home /
Mixing 2D and 3D - Issues with clipping and the depth buffer
Hi Unity community,
I'm working on a project that mixes 2D sprites with 3D world geometry. Unsurprisingly, I'm running into undesired clipping behavior.
Below is an example...
In this scene, both the white walls and the green grid are all true 3D objects. The red cube is a sprite textured to a camera-facing quad.
The desired behavior is for the cube to appear as if it were sitting behind the front wall (just like a normal 3D cube would). Unfortunately, the sprite's quad extends through the wall and thus I get the clipping visible in the image above.
So now the question; how would you achieve sprites that clip properly when positioned near 3D geometry?
One idea I had was to define a 3D bounding box that is correctly positioned in the world. I could then (theoretically) use this bounding box to provide depth information for rendering the sprite. The caveat is that I have no idea how to do this in Unity. I'm guessing it has something to do with customizing a shader.
Therefore, I throw myself to the genius of the Unity community. Please let me know what you think of the above approach, or if you would do something completely different.
Also, pointers to any resources where I could read more about this topic are much appreciated.
Thanks for your help!
I'm having a similar problem and I think the "correct" way to do it will involve writing a shader and a second texture for the "depth" of the sprites.
Depending on your game, you might be able to get away with just moving the sprite backwards (ie: directly backwards from the camera) and clipping through the floor and fixing that with layers, but you'll have the same problem with "real" walls behind the sprite. However, this will work if in your game you only care about the floor and back walls (the green ones)
I'd be interested in any solution/workaround you came up with?
Hey Tarlius, an update on what I've learned/devised... First, it seems this is a non-trivial problem, and while we can't be the first to encounter it, there doesn't seem to be a lot that is publicly available.
The solution I came up with is to create a mesh that (at least to some degree) mimics the geometry of the sprite it contains. The complexity of the mesh is entirely dependent upon how the sprite interacts with its surroundings (does the sprite just sit in-front-of/behind other sprites, do they occupy the same space, etc...). You can then modify the z-value (from the camera's frame of reference) of the mesh's vertices such that you get proper clipping.
Hope this helps!
I actually did some reading about this myself and stumbled upon info from how they did it in the original Sims. They apparently had a separate texture that contained depth information for each pixel (quite similar to what I suggested, actually!)
I think messing around with the mesh is simpler though, and that was the workaround I implemented too, although it sounds like your implementation is less basic than $$anonymous$$e was.
I would model the object, but not store traditional UVs. You can construct your own texture coordinates in the shader, based on view space coordinates. $$anonymous$$aybe use the UV coordinate for every vertex, and use them as a scalar for local to texture space.