Frustum.intersectsSphere()
is a method in the THREE.Frustum()
class in the three.js
library. This method determines whether the frustum intersects with a given sphere.
frustum.intersectsSphere( sphere : Sphere ) : Boolean
sphere
– A THREE.Sphere()
object representing the sphere to test.true
if the frustum intersects with the given sphere, false
otherwise.A frustum is the shape of a pyramid with the top cut off. It is a common shape used in computer graphics to represent the field of view of a camera or the viewing area of a computer screen.
In the context of three.js
, a frustum is used to represent the camera's view frustum. Frustum.intersectsSphere()
determines whether the sphere intersects with the camera's view frustum. If the sphere intersects, it means that the sphere is within the camera's viewing area and should be rendered. If not, the sphere is outside the camera's viewing area and can be culled (not rendered) to improve performance.
The method computes the distance between the center of the sphere and the closest frustum plane. If this distance is greater than the sphere's radius, the sphere does not intersect with the frustum. If the distance is less than or equal to the sphere's radius, the sphere intersects with the frustum.
This code snippet demonstrates how to use Frustum.intersectsSphere()
:
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const sphere = new THREE.Sphere( new THREE.Vector3( 0, 0, -5 ), 1 );
const frustum = new THREE.Frustum();
camera.updateMatrixWorld(); // ensure camera's world matrix is up to date
frustum.setFromProjectionMatrix( new THREE.Matrix4().multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ) );
if ( frustum.intersectsSphere( sphere ) ) {
// sphere is within camera's view frustum
// render sphere
} else {
// sphere is outside camera's view frustum
// cull sphere
}
In this example, we create a camera, a sphere, and a frustum. We then update the camera's matrix and set the frustum's projection matrix using the setFromProjectionMatrix()
method. Finally, we call intersectsSphere()
on the frustum with the sphere as the parameter. If the sphere intersects with the frustum, we render the sphere. If not, we cull the sphere.