- Home /
Unity 5 Standard Shader Transparent render order issues
I have a problem (obviously) with Unity 5's standard shader. I have a model that i'm trying to fade in and out, but the render order is messed up:
I know semi-transparent objects aren't compatible with the z buffer (or something like that) I know as a result, Unity's fade/transparent shader modes have this problem quite often. I know I should avoid semi-transparent objects whenever I could
The issue is in this particular case, there's no way around it. I can guarantee all the meshes I'm doing this with will be one piece, and the entire mesh will always have the same transparency, but I need the order to be correct.
When in the opaque rendering mode, the issue is gone, as you might expect:
I need the model to look exactly like this, but in the fade rendering mode with an alpha of 1
I'm good to hear any C# explanation, but I'm a complete shader noob. So if you explain any shader stuff, please talk to me like im a 5th grader lol.
Also, if there is a way around it, like I dunno, a canvas group or a camera trick or something, that would be even better
Thank you!
Answer by JaredHD · Feb 04, 2017 at 05:27 AM
I had a very similar problem a few months ago. Here is my topic and it has the solution to what i did. Basically you have to change the shader to a transparent shader, Then once you've finished with that, set the shader back. (No need to make any new shaders)
To reset the Paper plane use:
private void RevertTransparent(GameObject g)
{
for (int i = 0; i < g.renderer.materials.Length; i++)
{
if (g.renderer.materials[i].name == "Optimized Bark Material (Instance)")
g.renderer.materials[i].shader =
Shader.Find("Nature/Tree Creator Bark");
else if (g.renderer.materials[i].name ==
"Optimized Leaf Material (Instance)")
g.renderer.materials[i].shader =
Shader.Find("Nature/Tree Creator Leaves Fast");
else
g.renderer.materials[i].shader = Shader.Find("Default/Diffuse");
g.renderer.materials[i].SetColor("_Color", new Color(1,1,1));
}
for (int i = 0; i < g.transform.childCount; i++)
SetTransparent(g.transform.GetChild(i).gameObject);
}
You'd need to change the name of the shader below also. On top of that you'll come to a bug that when in a stand alone player (Built game) the shaders might turn pink. You'll need to tell unity to add the shaders in a build game because it doesn't. Go to Unity-Edit-Project settings-Graphics and you'll see the "Always included Shaders".
TADA! :P
Thanks for the response! After playing around with the script a bit, I realized I might not have explained the problem as well as I could. The Plane's alpha is at 1 at the moment. The problem is that even at an alpha of 1, the sorting is still wrong. That's my bad, and I updated the question. This ugly overlap shouldn't happen when the object is set to be fully opaque, and I have no clue how to fix it
What shader are you using? if you are using the Transparent shader I'd suggest you change between that shader when you need the plane to fade and the Standard shader when you need it to be fully opaque.
Transparency draw issues are USUALLY between different meshes so that's what I think you're saying...multiple meshes and render order.
$$anonymous$$y admittedly limited understanding is as follows: If it's multiple meshes (the usual transparency issue)...you have a draw order problem DUE TO OBJECT CENTERS. Unity sorts transparent draws by the object's center position and uses that depth to clip against the z-buffer (it has to use SO$$anonymous$$E depth value to clip with, even if it doesn't set a new value itself). It will draw your meshes in order, back to front for transparent and IF you can make the mesh centers be in the proper place for your desired order it should draw them in the proper order. Note you could have too many meshes with odd centers or, not enough meshes...lol. They don't sort by polygon, they sort by mesh. So maybe breaking it up into different meshes is what's needed, maybe one for each plane on the plane ;) . Regardless, you have to think about your models and their centers and what gets drawn in what order. Because you're using the transparent queue. Of course with backface culling on, any polygons with normals not facing the camera will cull (I think, not 100% for transparent if culling is still on.) There's info in the shader tutorials that talks about transparency and backface culling and lighting, etc. It starts to get complex rather quickly with various issues.
I hope this makes sense. You shouldn't have to swap shaders out just to get a mesh to fade (no offense to previous answers, that's a creative solution, but ideally you shouldn't HAVE to do that.) It looks to be just what you said...a sorting issue. So the render order is wrong. Check models for their center. ID$$anonymous$$ if it's using bounding box center, or mesh's origin. I've had cases with transparent queue object where I had to move the object's center somehow to get it to "fit" into the scene properly. Anyway, think about the meshes and their centers and the back-to-front draw order for some clues.
Another option may be to dynamically combine your meshes into one? https://docs.unity3d.com/ScriptReference/$$anonymous$$esh.Combine$$anonymous$$eshes.html ID$$anonymous$$ what the result will be and I hope that's not a wild-goose chase.
Not really sure what's going on since I don't have your models, so I can't do a test case and help more, sorry. Also, you may have issues with rendering in the scene itself (clipping with scene objects outside the plane meshes) so check into that too. Same issue really, zbuffer clipping vs object center. I hope I don't mislead you and this gives you some ideas, or at least things to search for. So think about it and take this as food for thought. :)
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
How come edititng "_Glossiness" through script doesn't work? 1 Answer
Represent Land 3D Area End 0 Answers
Glowing material/shader ? 1 Answer