Particles behind meshes?

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.

2 Likes

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).

tomvds, that’s a good point. I will check that!

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.

654014–23345–$TransparencyIssue.unitypackage (969 KB)

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.

The problem appears to be the “Receive Shadows” option. Try to deactivate it. Also, you should use sorted billboards.

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 :slight_smile:
Edit: Bug reported (Case 414572)

654175–23349–$TransparencyIssueWorkaround.unitypackage (973 KB)

Well, it worked for me. :slight_smile:

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.

Edit: I added it to the bug report.

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. :slight_smile: 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.)

The bug is getting more and more interesting :slight_smile:

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.

OK, I wasn’t aware of that: I just deactivated it for the particle system. :slight_smile:

It is even stranger, if you modify the distance to the camera, there is a certain point at which it may switch between correct and incorrect.

To the Unity employee who has to fix this bug: I am going to offer you a drink if we ever meet us! :slight_smile: