I have a spherical globe adorned with pins on its surface, each pin representing an object and labeled accordingly with DOM elements. These labels are calculated based on the positioning of the pin in the 2D world.
The issue arises when the pins move behind the globe either through mouse dragging or animation. In such cases, I need to ensure that the corresponding labels in the DOM are hidden, making them invisible without their associated pins.
To tackle this problem, my approach involves determining whether a pin in the 3D world is positioned behind the globe so I can then hide its label.
For a comprehensive view of the code, visit Codepen.
Here is the function I have been exploring:
function checkPinVisibility() {
var startPoint = camera.position.clone();
for (var i = 0; i < pins.length; i++) {
var direction = pins[i].position.clone();
var directionVector = direction.sub(startPoint);
raycaster.set(startPoint, directionVector.clone().normalize());
var intersects = raycaster.intersectObject(pins[i]);
if (intersects.length > 0) {
// ?
}
}
}
Despite delving into multiple resources, including:
- ThreeJS: How to detect if an object is rendered/visible
- Three.js - How to check if an object is visible to the camera
I have managed to make it work using mouse XY position as a ray, but a sustainable solution for continuous rendering of all pins still eludes me.