Transparent shader without transparency overlap.

Hey everyone,
I’m looking for a shader that allows transparent objects to overlap without combining the alpha values of each of them.
Basically, I need the Transparent Shader with Depth Writes from here:

However, when I use this, it doesn’t seem to work. I still get alpha overlap from objects, as seen here:

Can anyone point me in the direction of a shader that can do this? I have absolutely no experience in writing them.

The shader you are using does work, but it doesn’t do what you want it to do. You want the shader to render the face closest to the camera and ignore any face behind it, but the shader is designed to only ignore any face behind it that belongs to the same mesh. That is why it does work for the picture in the manual page you linked to: the suit of armor you see is one entire mesh. The cubes in your project are two different meshes (two instances of the same SharedMesh), so the shader isn’t going to ignore one cube just because it’s behind another cube. If you were to replace the two cubes with a single torus and you would view it from the side, you would find that you do get the effect you are looking for since the torus is a single mesh.

This isn’t a bug because the transparent shader is doing exactly what a transparent shader should do: showing partly what is behind the transparent face. If you were to disable this, you would not only stop seeing the farthest transparent cube, but also any solid objects behind the closest cube and even the background color/skybox, so effectively you would make your closest cube solid.

So a couple of possible solutions:

Depending on how many different kinds of transparent objects you want on screen and how complex these objects need to be, you could try to combine these objects into a single mesh with a single material and rigging each object to a separate bone in the same armature. You can then move the objects in relation to one another by moving the bones. This works especially well when you have two identical cubes, since you’re using the same texture with the same UV-map for both cubes and you aren’t applying animation to the cubes.

If you have two objects with different textures on them and you still want this effect using this shader, you are going to have to combine these different textures into a single texture (e.g. if you have two 1024x1024 textures you could create a 2048x1024 texture pasting the two original textures side by side) and then adjusting the UV-mapping of each object accordingly, but this means you either increase the size of your texture or decrease its resolution. Another way to use up to four different textures would be writing a new shader that uses vertex colors to determine which texture needs to be applied where.

If you are also animating the objects it gets more complex because you would have to combine all the armatures into one armature, which means you also have to create a single animator controller with different layers for each of the objects you are combining. That can be done as long as you are combining a finite number of armatures and it involves a lot of duplicating (e.g. when you combine two identical characters with a walking animation into a single mesh, but you don’t want to have them always walk in synch, you would need to put a walking animation for each character on a different layer and then create separate but identical transitions and have them controlled by separate “walking1” and “walking2” parameters). Can’t say how this will affect performance, I think it’s cheaper for the GPU but more expensive for the CPU.

The only other thing I can think of that would come close to what you are trying to achieve involves using the stencil buffer (this again involves writing your own shader). You would tell your transparent shader to only draw a fragment (a pixel belonging to a certain face) if the stencil buffer’s value is not equal to a certain value a, and then you would write the value a to the stencil buffer. That way the shader only draws a fragment the first time it is called upon and does nothing afterwards, meaning it only draws your transparent fragment once for every pixel position on your screen. The major disadvantage to this approach is that meshes are drawn from back to front (this is necessary for the transparency to work) so you will only see the farthest face instead of the closest face. Works great if you’re creating transparent silhouettes with a single color (because then the closest and farthest face look identical), but if you want to texture your objects or apply lighting to them, it’s going to be ugly.