Depth errors on generated meshes

Hey. I built my own ribbon trail engine a little while ago, because Unity’s built-in one didn’t do what I needed it to. My engine works pretty well, except for one issue - the depth. In most cases, the depth is fine, but it occasionally renders in the wrong order when put behind objects with transparent materials (alpha blended and additive materials both cause this issue). The only thing I can think is that, for some reason, the depth is being read from only the centre point of the mesh, rather than separately for each vertex, though I’m not sure why this would be. If anyone has any ideas, I’d be grateful to hear them.

This is a screenshot of the error occurring, with a trail being rendered behind alpha blended particles:


And my code can be found in this post here: Trail rendering for car skid marks

Thanks in advance for any suggestions.

Your analysis of the problem is exactly right. See this page about transparency sorting modes for cameras:

Messing with the TransparencySortMode won’t solve your problem for you, but the descriptions do illustrate that the cause of the problem is what you thought. :slight_smile: Both sortmodes work from the center of the object. With transparent shaders there is no consideration of depth at the vertex level (let alone at the pixel level) because transparent shaders are not required to undergo depth testing as that would prevent alphablending. Instead, they implement the painter’s algorithm by rendering the entire objects one by one in back-to-front order.

This doesn’t automatically work in your case, because the elongated shape of your tire trails makes the object’s center be further away than the center of your dust particles, so the trail in its entirety gets rendered before them (back-to-front). Usually, one would give the shader for the trail a higher Queue number than the standard Transparent (“Queue” = “Transparent”) tag in the shader to force it behind normal transparent objects. But you don’t necessarily need to write a custom shader to add this. You can override the queue number from standard C#/JS/Boo scripts by accessing the material’s renderQueue property, i.e. go to the trail’s script, and add, eg., this line of code to it:

renderer.material.renderQueue = 3001;

That’ll make your trails always be drawn after all other normal transparent objects, which appears to be what you want.