Post 1) I'm currently working on a project where I need to detect all objects visible to the camera so I can reposition them back to their starting point once they go out of view. The goal is to create an infinite waterfall effect by reusing existing objects. I'm facing an issue where intersects is empty and it only works when I raycast for individual objects in a loop through scene.children, but this approach has a negative impact on performance since it's within the animation loop.
Any help on this matter would be greatly appreciated.
Post 2) I recently switched to frustum culling, but the frustum.containsPoint(scene.children[index]) method always returns true, even when the objects are clearly out of the camera's view.
Once again, any assistance or guidance on this issue would be highly valued.
camera.updateMatrix();
camera.updateMatrixWorld();
var frustum = new THREE.Frustum();
var projScreenMatrix = new THREE.Matrix4();
projScreenMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
// frustum.setFromProjectionMatrix(camera.projectionMatrix);
frustum.setFromProjectionMatrix(new THREE.Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse));
for (let index = 0; index < scene.children.length; index++) {
scene.children[index].updateMatrix(); // make sure plane's local matrix is updated
scene.children[index].updateMatrixWorld();
if (frustum.containsPoint(scene.children[index])) {
//stuff happens...
if (scene.children[index].name === "coin") {
scene.children[index].rotation.x += 0.01;
scene.children[index].position.y -= 0.1;
}
// console.log("mesh in view Frustrum");
} else {
console.log("mesh not in view Frustrum");
}
}