Occlusion for 2D Overlays in a 3D Scene
SVG outlines have no depth information, so dense cubes let rear outlines bleed through front ones — the fix wasn't smarter SVG, it was moving ordinary outlines back into the 3D layer that already has a depth buffer.
The Problem
SVG and HTML overlays are useful for outlines, labels, and interaction feedback in a 3D interface. They are often easier to style than equivalent geometry inside Three.js, especially for thin lines and text.
The limitation is that a 2D overlay has no 3D depth information. Anything drawn there sits above the rendered scene and is not automatically occluded by objects in front of it.
I ran into this in a cube-manipulation prototype. Unselected cubes used thin SVG outlines to separate them from the background. With only a few cubes, the result looked fine. Once several cubes were placed close together, outlines from cubes in the back became visible through cubes in front.
The screen coordinates were not wrong. The problem was the rendering layer: SVG knew where the projected edges were, but not which 3D object was closer to the camera.
The root-cause diagnosis and implementation approach mainly came from Claude Code. My part was validating whether the revised behavior produced the correct occlusion in dense scenes.
Choosing the Layer by Occlusion Semantics
The final solution did not add more occlusion logic to SVG. Instead, different outline states were assigned to different rendering layers.
Normal outlines for unselected objects moved into the 3D scene. Each cube uses its own EdgesGeometry. Because the lines are real 3D objects, they participate in depth testing and inherit the scene's occlusion behavior automatically.
Selection and hover outlines remain in the SVG overlay. Those states have a different purpose. Even if part of the object is hidden behind something else, the user still needs a clear indication of what is selected or currently under the pointer. Keeping those outlines above the scene is therefore intentional.