'Threejs Raycast Intersects empty with scene.children. What am I doing wrong?
Post 1) I am trying to detect all objects viewable by the camera so I can reposition them to the starting position once they are out of view. I am aiming for an infinite waterfall effect that reuses the existing objects. I really don't why intersects is empty and it works if I raycast for individual objects while looping through scene.children but this will lead to very bad performance because it is in the animation loop.
Thanks for any help
Post 2) I have changed over to frustrum culling but frustum.containsPoint(scene.children[index]) always returns true even when objects are clearly out of camera view.
Again any help is appreciated
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");
}
}
Solution 1:[1]
I am trying to detect all objects viewable by the camera
You don't need raycasting for this. Instead, setup an instance of Frustum based on the projection and view matrix of your camera via setFromProjectionMatrix(). In the next step, use intersectsObject() to detect whether a 3D object is inside the camera's view frustum (the viewable area of the 3D scene) or not.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
