In my project, I am trying to blend perspective objects (which appear smaller as they move farther away) with orthographic objects (which maintain the same size regardless of distance). The perspective objects are a part of the rendered "world," while the orthographic objects serve as adornments like labels or icons. Unlike traditional HUD elements, I want these orthographic objects to be rendered within the world, meaning they can be obscured by other world objects, such as a plane passing in front of a label.
My approach involves using one renderer but two scenes - one with a PerspectiveCamera
and the other with an OrthographicCamera
. By rendering them sequentially without clearing the z-buffer (setting the renderer's autoClear
property to false
), I aim to achieve this effect. However, the challenge lies in synchronizing the placement of objects in each scene so that an object in one scene is assigned a proper z-position relative to objects in the opposite scene, both in front and behind it.
To tackle this problem, I have designated the perspective scene as the "leading" scene, where all object coordinates (perspective and orthographic) are set based on this scene. Perspective objects use these coordinates directly and are rendered accordingly with the perspective camera. Orthographic objects' coordinates are transformed to match those in the orthographic scene, then rendered using the orthographic camera. This transformation involves projecting the coordinates from the perspective scene onto the perspective camera's view pane and back onto the orthogonal scene with the orthographic camera:
position.project(perspectiveCamera).unproject(orthographicCamera);
However, despite my best efforts, this method is not yielding the desired results. The orthographic objects consistently render in front of the perspective objects, even when they should be positioned between them. For instance, consider an instance where a blue circle should appear behind a red square but in front of a green square (which it currently does not):
// Inserted code snippets here
The challenge persists, even though conceptually positioning orthographic objects before each render could potentially solve the issue.
UPDATE:
To address this problem, I have proposed a current solution in the form of an answer below. Despite this mitigation, the performance may not fully align with expectations compared to the orthographic camera itself. Therefore, any insights into why the orthographic camera behaves unexpectedly or alternative solutions would be greatly appreciated.