I’m a bit new to Unity, and currently I’m working with multiple transparent objects using the “Transparent/Diffuse” shader, and I noticed that you can sometimes still see through objects even when the opacity is set to 1, depending on the angle the camera is facing. I still want the option for the objects to be transparent, so is there a way to fix this bug without simply using an opaque shader? Thanks!
There are multiple stages involved in the rendering process. In terms of order-of-operations, a Shader that is capable of having transparency will be rendered after a Shader that only supports opaque objects.
The “Transparent/Diffuse” Shader supports transparency. Therefore, it does not render at the same time as a basic opaque shader, regardless of the opacity it’s being drawn with.
Furthermore, transparent object ordering will generally be simplified by using the origin point of each GameObject to determine whether it’s assumed to be closer or further away than others. By having multiple GameObjects in the same location, it’s largely luck and coincidence that decides which ones render in front of which other ones.
All in all, the short answer is that there aren’t really super-duper-simple ways of layering multiple transparent objects and getting them to always look the way you want them to. Mainly, this is to keep the rendering pipeline more efficient. If you had 1000 objects in the same location, all slightly different sizes, all carefully calculating which pixels are on top of which others, that’s up to 1000 passes per pixel (or more, if the same logic held true for self-overlapping transparent meshes), which is woefully inefficient/computationally expensive.
So, I managed to find a solution that works for me. All I needed to do was find the order I wanted my objects to be layered as (higher values will be rendered on top of lower values), set material.renderQueue = value; when creating the object (value being the render order number I prefer), and then making sure that whenever I replace the materials of one of these objects, I make sure to include newMaterial.renderQueue = oldMaterial.renderQueue;.
I didn’t have to do this, but if someone wants to change the render order depending on where the camera is, they could have an update function that updates the materials of the objects with the new render order whenever the camera moves to a new location (say, on the other side of the objects when their ordering changes relative to the camera).
I’m not sure if this is the “proper” method, and it would get a bit cumbersome for a large amount of transparent objects scattered through a scene, but it works.
