BufferGeometry
Object3D
Raycaster
Camera
CubeCamera
PerspectiveCamera
OrthographicCamera
StereoCamera
Clock
Curve
CurvePath
Path
Shape
ShapePath
ArrowHelper
AxesHelper
BoxHelper
Box3Helper
CameraHelper
DirectionalLightHelper
GridHelper
PolarGridHelper
HemisphereLightHelper
PlaneHelper
PointLightHelper
SkeletonHelper
SpotLightHelper
Light
PointLight
RectAreaLight
SpotLight
DirectionalLight
HemisphereLight
LightShadow
PointLightShadow
AnimationLoader
AudioLoader
BufferGeometryLoader
CompressedTextureLoader
CubeTextureLoader
DataTextureLoader
FileLoader
ImageBitmapLoader
ImageLoader
Loader
LoaderUtils
MaterialLoader
ObjectLoader
TextureLoader
LoadingManager
Material
Box2
Box3
Color
Cylindrical
Euler
Frustum
Interpolant
Line3
MathUtils
Matrix3
Matrix4
Plane
Quaternion
AnimationAction
AnimationClip
AnimationMixer
AnimationObjectGroup
AnimationUtils
keyframeTrack
PropertyBinding
PropertyMixer
BooleanKeyframeTrack
QuaternionKeyframeTrack
StringKeyframeTrack
Audio
AudioAnalyser
AudioContext
AudioListener
PositionalAudio

Frustum.intersectsSphere()

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.

Syntax

frustum.intersectsSphere( sphere : Sphere ) : Boolean

Parameter

  • sphere – A THREE.Sphere() object representing the sphere to test.

Return value

  • Returns true if the frustum intersects with the given sphere, false otherwise.

Description

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.

Example

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.

References