- Home /
Opaque object still being transparent glitch
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!
Answer by Quantanaut · Jul 08, 2021 at 08:30 PM
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.
Answer by Eno-Khaon · Jul 08, 2021 at 06:16 AM
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.
That makes sense, but would there be a way I can fix it for this case? I'm not working with many objects, so would there be some way I could manually tell the shader which object to render first / last so the inner layers will be properly hidden?
EDIT: I think my workaround will be to create a new custom shader copied from "Transparent/Diffuse" and set ZTest LEqual to ZTest Always, then manually change their position in the Rendering Queue. I'll update this when I try that. Currently I can't seem to find the Transparent/Diffuse shader in the Unity 2020 2020.3.12 built-in shaders file...