- Home /
Is it possible for transparent spheres to look like they're intersecting?
This is an interesting problem I'm having, and I'm not sure there's going to be an easy solution to it.
For my game, it's necessary for the user to be able to see the intersecting line between transparent spheres. If I use an opaque material, you can see what I'm talking about:
Notice how the line between the two spheres is clearly visible? That's the effect I'm looking for, only with transparent spheres instead of opaque ones (so you can see part of one sphere overlapping inside of the other).
However, when I make the sphere materials transparent, this is what happens instead:
The spheres do not appear to intersect at all. Instead, it always seems as if one sphere is completely in front of the other one. (Note that the only difference between these two shots is that the position of the camera has shifted slightly, which causes one sphere to take priority over the other one.) It's not just in the editor either; this effect also applies when the game is actually in runtime.
Is there any way to fix this? Is the effect I'm trying to even achieve even possible within Unity?
(These images were taken with Legacy Specular and Legacy Transparent Diffuse, though it didn't seem like any of the other default transparent shaders worked any better)
Answer by Bunny83 · Oct 13, 2017 at 06:34 AM
Uhm, do the sphere's actually have any alpha value smaller than 1? It looks like you still render them opaque.
Almost all transparent shaders do not write into the depth buffer to prevent unwanted clipping. However that only makes sense then the objects are actually transparent. One solution is to create a shader that writes to the depth buffer, however keep in mind that transparent geometry is sorted from back to front based on the distance of objects origin from the camera.
That however means when your shader writes to the depth buffer only one sphere can be seen inside the other. The one that is drawn first will be visible inside the other. The one that is drawn last won't be visible in the inside of the first sphere as the depth buffer prevents that. Though that's actually what forms the intersection line.
The only proper solution would be to split the two sphere meshes at the intersection circle into two parts each and render them in the right order. Which means the two smaller caps which are inside should be drawn first and the larger part of the spheres would be drawn last. This is of course not really feasible if those spheres should move / intersect dynamically.
Another way would be to use a multipass shader and some stencil operations
I had the spheres at a nearly opaque level so the effect could be seen more clearly. The problem still persists at different alpha levels.
Shame about that depth buffer stuff. It's pretty much necessary for the spheres to intersect dynamically, so I suppose splitting the spheres isn't a feasible option. I'm curious about that multipass approach, though. Could you elaborate on it a bit more?
Your answer
![](https://koobas.hobune.stream/wayback/20220612140914im_/https://answers.unity.com/themes/thub/images/avi.jpg)