I would like to use a shader for the particle system and meshes. The problem is that the meshes are always drawn in front of the particles. Can someone tell me, how to avoid that?
Shader "TransparentIssue" {
Properties {
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
}
Category {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Alphatest Greater 0
Lighting Off
ColorMask RGB
Cull Off
ZWrite Off
Fog {Mode off}
SubShader {
Pass {
Lighting Off
BindChannels {
Bind "Vertex", vertex
Bind "Texcoord", texcoord
Bind "Color", color
}
Blend SrcAlpha OneMinusSrcAlpha
SetTexture [_MainTex] {
combine primary * texture
}
}
}
}
}
If both your particle system and your mesh are using that shader, then they will be drawn back to front. This order is imposed by virtue of being in the Transparent queue.
If you want to force draw order, you’ll have to put the mesh’s or the particle system’s material in a different Render Queue.
I also expected that behaviour. Unfortunately the particle system is always drawn behind the mesh, even if it is closer to the camera. I was looking in the reference, if something is mentioned, that the particle renderer is handled differently. But I found nothing.
Perhaps the bounding volume of your mesh is larger than you think, placing it’s center nearer to the camera than the particles. If that is the problem, you can force the correct ordering by placing the particle material in the transparent+1 queue (note that that may mess things up if you have other transparent objects besides the particles and the mesh).
No, that’s not the case. I just checked all the values and calculated the distance of the camera to the (transformed) bounding volume centers. Everything is okay.
The centers of the bounding volumes of the particle and mesh renderers are correct, too.
I reproduced the issue in a very simple example package. The scene contains two cameras. One should show the mesh in front of the particles, the other one should show the opposite. But both show draw the mesh in front of the particles. If someone confirms, that there is a problem, I would fill a bug report. Thanks.
This is a general problem with overlaying semi-transparent geometry, not a bug.
Is it necessary that your mesh uses alpha also? With solid geometry there would be no problem.
What is the effect you are trying to achieve? Maybe there is a solution…
This one was just for demonstration purposes. I am using it for a custom particle system. If the situation is like that, it can not be mixed with Unity’s particle system at all, which is not really good. So there is no solution unfortunately.
And it is not a semi-transparent geometry problem, as those objects are intended to be rendered from back to front, exactly as Daniel Brauer described it. So it actually is a bug from my point of view, as there is nothing written (or at least I could not find it), that the particle system has a special role in the rendering queue.
Deactivating “Receive Shadows” has no impact. Same for sorted billboards. The mesh is still rendered in front of the particle system, even if it is behind it.
That’s by far the coolest bug I ever found in Unity :)! If the box has a scale of (1, 1, 1), the rendering issue happens. For any other scale, no problem!
Edit: You may also change the scale of the particle system to avoid the problem.
Edit: Bug always occures, if both have the same scale
Edit: Bug reported (Case 414572)
Martin Kraus, maybe it has the same effect as changing a scale. I expect you own Unity Pro, such that it has an effect for you. I only have Unity. Both make no sense.
After seeing the project, I agree this is a bug. Deactivating receive shadows fixed it for me as well (Unity pro), but I do not see how that would make it less of a bug ;).
I agree, it doesn’t. BTW I’m using the free version of Unity. (Maybe one needs to restart the particle system (by starting the game mode) to see that it has an effect.)
Martin Kraus, you are right. I disabled it for both, the mesh and the particle system. In that case the same issue occures again. You only mentioned one. If both have the same settings it doesn’t seem to work properly.