- Home /
Overlapping transparency Shader fighting.
I have 2 Transparent meshes that start fighting on who gets the overhand.
I have 3 picts, the first picts shows how the layer should look without fighting, the 2nd pict shows layer 2 mesh got the overhand the 3rd pict shows the Atmosphere layer is back in place.
1st layer Titan Satellite Sphere, Bumped Specular, Size of 1.
2nd layer Clouds. Transparent Bumped Specular, Size 1.005.
3rd layer Atmosphere. Atmosphere Shader, Size 1.03.
When I turn half way around the 2nd layer kinder blended the 3rd layer out, another half way around the 3rd layer was correctly shown. So 50/50.
Can I please get some insights how to solver this shader fighting? When I turn clouds off, it works 100%. This will also fix my other planets and satellites. Also will fix my planet ring transparency which has a transparency way in the background.
Answer by duck · Nov 18, 2013 at 09:02 PM
Depth sorting for semi-transparent objects is an ongoing challenge in realtime engines. For a quick summary of why, see this article: http://www.opengl.org/wiki/Transparency_Sorting
So the problem you have, is that the transparent objects are being sorted against each other by their position only, not per screen pixel, because there's no way for the depth buffer to record a semi-transparent depth measurement at the screen pixels that your object is occupying.
Because your three concentric spheres are (I'm guessing) at exactly the same position in world space, Unity can't sensibly decide which to draw first, and the decision is probably coming down to arbitrary inaccuracies and variations in the floating point math when examining their distance from the camera, which is why you probably aren't seeing a sensible pattern as to when they pop back and forth.
There are a couple of workarounds that spring to mind.
Modify the source code for the transparent shader that you want to be in front, and change the part where it says "Queue"="Transparent" to "Queue"="Transparent+1". If it's a built-in unity shader, to do this you'll need to download the source code for the shader that you're using from here. Duplicate it into your project, give it a name such as "priority transparent shader" or something. Then use this shader instead.
An alternative approach could be to have some code on the transparent sphere objects that runs in Update, which constantly keeps the spheres to be drawn in front slightly closer to the camera than the other ones. This will require calculating the direction to the camera and offsetting the position in that direction by a small amount.
Both methods should work. Give whichever one you feel like a try!
Thankyou so much, I asked this question about a year ago and not did you only answer the question you also solved my z-fighting problem for all my shaders. I only had to change the Atmosphere shader, so if I understand correctly if I would have like another transparent shader I would to like +1 and then other would be +2 correct. Like layer depth.
Your answer
Follow this Question
Related Questions
How to a 3d character transparent? 2 Answers
Can I draw Opaque geometry on top of Sprites, cheaply? 0 Answers
Transparent shader decreases frame rate. Which material i should use for transparent object? 3 Answers
How to paint transparent texture on terrain? 0 Answers
Transparent object makes other objects act strange 0 Answers