3D Object/Mesh behind a Sprite? Sorting Layer?...

Hello,
it is possible using SortingLayer on a Mesh with perhaps a Mobile/Diffuse shader?
I have found 1-2 solution but he only working when the Mesh use a Transparent or Fade shader.
But when i use Transparent/Fade, i get a graphic glitch on the 3d object.

So i would like now ask, search i unsuccessfully then it is not possible or only not found the right way? o.o
Anyone know a way?
Perhaps create a own shader they respect sorting layer and can use a diffuse without transparent/fade mode?
Or anyone know a Shader/Script from the Asset Store they can handle this?

What i need is, that the mesh is behind a sprite. But this is only working when the mesh have a transparent shader. With the other all time the mesh is in the front of the sprite :confused:
Thanks.

Sorting Layer does work exactly the same way as it does on sprites, but how meshes and sprites are rendered is very different thus the results look completely different or like the sorting layer isn’t working.

The main difference is z buffer depth sorting.

For sprites the order they’re rendered is all that matters, but sprites don’t intersect, or have different parts that can overlap within themselves. 3D meshes can, but the order each triangle is rendered in isn’t sorted by depth! They’re intentionally not sorted for efficiency reasons, and even if you did you could still have two polygons intersecting each other which just sorting per-triangle wouldn’t solve.

The solution that graphics programmers came up with for this is the zbuffer, which records the depth from the screen something was rendered at each pixel. As each object gets rendered it tests if each pixel being rendered is further away than the previously rendered objects. If it is, it gets skipped, if it’s closer it renders and overwrites that recorded depth. This means no matter what order you render opaque objects the end result is the same! This also means when you want to change the order things render in … it doesn’t appear to do anything.

However since only one depth is recorded at a time transparent objects tend to not write to the zbuffer. If they did then transparent objects would have seemingly random parts of the mesh hidden when you would expect them to show up behind other parts, basically the inverse of what happens now with parts rendering on “top” when they shouldn’t. However drawing in the wrong order is slightly less ugly then not rendering at all. The result is all the weird sorting problems that the zbuffer solves still happen with transparent objects, which is what’s happening when you enable fade or transparency on the standard shader.

The solutions to allow you to use 3D models that can be sorted like sprites are somewhat less than obvious, and none that simple. Some fight games do this, like Guilty Gear Xrd and Street Fighter 4 & 5. I can’t say exactly the method they used, but the “easiest” method is using a custom shader that “clears” the zbuffer after each object renders. Another method is to render each object into an off screen texture each frame and then actually render it into the scene as a sprite. The last method I know of is to artifically “squish” 3d meshes so they’re nearly flat when rendering to the zbuffer, not too much that they don’t properly sort with themselves, but flat enough you have some room to manipulate multiple objects by adjusting the distance they flatten to.

All of these methods mean they’ll sort oddly with other 3d things in the scene, only that first option would work with just the built in sorting layer stuff.

5 Likes

I respect your answer and thank you for this but its not really help me. I dont know how i can create the shader or… :frowning:
Its dont must use sorting layer. But have this problem since months and think perhaps before i now do nothing, i stay this to the background, do it later. Now i have use particles and found the sorting layer for handle sprite and particles and have again try it with the 3d mesh. Script for set the sorting layer… but not working, only when transparent, jepp. zpuffer -.-

Okay, i know the exact point when the 3d mesh must go in the background and the sprite to the front over this.
So really sorting layer dont must use. But, what i can do, it is going to the front and the 3d mesh to the background?
Its ok when i set it every frame, thats not a problem. Have you perhaps a solution for this? Then i dont really know for what i must search for find this :S

First time, i have try set the ocean y -1 and the ships y -1, then is the ship behind the terrain hills. But i can´t do it on the land. When people or animals walking arround the terrain, the walking not in the background from a hill, he walking over it. And here i can´t set a other high :frowning:

The other answer to sorting 3D meshes and sprites is to treat sprites like 3D objects and move both in Z so they actually are in front or behind each other in 3D space rather than rely on sorting layers and order.

2 Likes

Thats what i mean with the y -1, i must use y and not z but in the end its the same what you say too. Think thats is really the one solution what i can do. Don´t know what i have create the last time i have try it but now i have again try this what you say and now its looks thats its working :slight_smile:

Thank you very much. Then with this last post from you i have really try again this with the other high for get front/back.
Gras is slow and dont go in a other sprite so i dont must change anything.
Mountain is high and go into a other sprite.
I can´t change the position from all sprites then i need it, its sector… i add the mountain gameobject a child with the sprite and set the child +1 and then the z a little bit lower so it is from the camera the same position as the parent. (when only high change, the position looks wrong). And now? On the other sprite (the mountain picture is into), the 3d object is in the background from the mountain. Finish. Now i must only make script thats can then handle this but thats not really a problem.

Hope is ok that i have try explained what i now try doing.
I´m was really on a point for give up -.-
Thank you very much bgolus.

how to set the order for quads as sorting layer

public string sortingLayerName = string.Empty; //initialization before the methods
public int orderInLayer = 0;
public Renderer MyRenderer;

Now add this below Method

void SetSortingLayer()
{
if (sortingLayerName != string.Empty)
{
MyRenderer.sortingLayerName = sortingLayerName;
MyRenderer.sortingOrder = orderInLayer;
}
}

Call this above method in start() method
and set the order in editor
This Works:):):slight_smile:

5 Likes

YEA!!! WORKS!!!

I was searching for this for like 4-5 hrs everwhere on internet. Thakyou so much.