Skip to main content

Box3

Represents an axis-aligned bounding box (AABB) in 3D space.

Code Example

const box = new THREE.Box3(); const mesh = new THREE.Mesh( new
THREE.SphereGeometry(), new THREE.MeshBasicMaterial() ); // ensure the
bounding box is computed for its geometry // this should be done only once
(assuming static geometries) mesh.geometry.computeBoundingBox(); // ... // in
the animation loop, compute the current bounding box with the world matrix
box.copy( mesh.geometry.boundingBox ).applyMatrix4( mesh.matrixWorld );

Constructor

Box3

function Box3( min: Vector3, max: Vector3 ): void;  

min - (optional) Vector3 representing the lower (x, y, z) boundary of the box. Default is ( + Infinity,

  • Infinity, + Infinity ).
    max - (optional) Vector3 representing the upper (x, y, z) boundary of the box. Default is ( - Infinity,
  • Infinity, - Infinity ).

Creates a Box3 bounded by min and max.

Properties

isBox3

isBox3: Boolean;  

Read-only flag to check if a given object is of type Box3.

min

min: Vector3;  

Vector3 representing the lower (x, y, z) boundary of the box.
Default is ( + Infinity, + Infinity, + Infinity ).

max

max: Vector3;  

Vector3 representing the upper (x, y, z) boundary of the box.
Default is ( - Infinity, - Infinity, - Infinity ).

Methods

applyMatrix4

function applyMatrix4( matrix: Matrix4 ): this;  

matrix - The Matrix4 to apply

Transforms this Box3 with the supplied matrix.

clampPoint

function clampPoint( point: Vector3, target: Vector3 ): Vector3;  

point - Vector3 to clamp.
target — the result will be copied into this Vector3.

Clamps the point within the bounds of this box.

clone

function clone( ): Box3;  

Returns a new Box3 with the same .min and .max as this one.

containsBox

function containsBox( box: Box3 ): Boolean;  

box - Box3 to test for inclusion.

Returns true if this box includes the entirety of box. If this and box are identical,
this function also returns true.

containsPoint

function containsPoint( point: Vector3 ): Boolean;  

point - Vector3 to check for inclusion.

Returns true if the specified point lies within or on the boundaries of this box.

copy

function copy( box: Box3 ): this;  

box - Box3 to copy.

Copies the .min and .max from .ox3 to this box.

distanceToPoint

function distanceToPoint( point: Vector3 ): Float;  

point - Vector3 to measure distance to.

Returns the distance from any edge of this box to the specified point. If the point lies inside of this box, the distance will be 0.

equals

function equals( box: Box3 ): Boolean;  

box - Box to compare with this one.

Returns true if this box and box share the same lower and upper bounds.

expandByObject

function expandByObject( object: Object3D, precise: Boolean ): this;  

object - Object3D to expand the box by.
precise - (optional) expand the bounding box as little as necessary at the expense of more computation. Default is false.

Expands the boundaries of this box to include object and its children, accounting for the object's, and children's, world transforms. The function may result in a larger box than strictly necessary (unless the precise parameter is set to true).

expandByPoint

function expandByPoint( point: Vector3 ): this;  

point - Vector3 that should be included in the box.

Expands the boundaries of this box to include point.

expandByScalar

function expandByScalar( scalar: Float ): this;  

scalar - Distance to expand the box by.

Expands each dimension of the box by scalar. If negative, the dimensions of the box will be contracted.

expandByVector

function expandByVector( vector: Vector3 ): this;  

vector - Vector3 to expand the box by.

Expands this box equilaterally by vector. The width of this box will be expanded by the x component of vector in both directions. The height of this box will be expanded by the y component of vector in both directions. The depth of this box will be expanded by the z component of vector in both directions.

getBoundingSphere

function getBoundingSphere( target: Sphere ): Sphere;  

target — the result will be copied into this Sphere.

Gets a Sphere that bounds the box.

getCenter

function getCenter( target: Vector3 ): Vector3;  

target — the result will be copied into this Vector3.

Returns the center point of the box as a Vector3.

getParameter

function getParameter( point: Vector3, target: Vector3 ): Vector3;  

point - Vector3.
target — the result will be copied into this Vector3.

Returns a point as a proportion of this box's width, height and depth.

getSize

function getSize( target: Vector3 ): Vector3;  

target — the result will be copied into this Vector3.

Returns the width, height and depth of this box.

intersect

function intersect( box: Box3 ): this;  

box - Box to intersect with.

Computes the intersection of this and box, setting the upper bound of this box to the lesser of the two boxes' upper bounds and the lower bound of this box to the greater of the two boxes' lower bounds. If there's no overlap, makes this box empty.

intersectsBox

function intersectsBox( box: Box3 ): Boolean;  

box - Box to check for intersection against.

Determines whether or not this box intersects box.

intersectsPlane

function intersectsPlane( plane: Plane ): Boolean;  

plane - Plane to check for intersection against.

Determines whether or not this box intersects plane.

intersectsSphere

function intersectsSphere( sphere: Sphere ): Boolean;  

sphere - Sphere to check for intersection against.

Determines whether or not this box intersects sphere.

intersectsTriangle

function intersectsTriangle( triangle: Triangle ): Boolean;  

triangle - Triangle to check for intersection against.

Determines whether or not this box intersects triangle.

isEmpty

function isEmpty( ): Boolean;  

Returns true if this box includes zero points within its bounds.
Note that a box with equal lower and upper bounds still includes one point, the one both bounds share.

makeEmpty

function makeEmpty( ): this;  

Makes this box empty.

set

function set( min: Vector3, max: Vector3 ): this;  

min - Vector3 representing the lower (x, y, z) boundary of the box.
max - Vector3 representing the upper (x, y, z) boundary of the box.

Sets the lower and upper (x, y, z) boundaries of this box.
Please note that this method only copies the values from the given objects.

setFromArray

function setFromArray( array: Array ): this;  

array -- An array of position data that the resulting box will envelop.

Sets the upper and lower bounds of this box to include all of the data in array.

setFromBufferAttribute

function setFromBufferAttribute( attribute: BufferAttribute ): this;  

attribute - A buffer attribute of position data that the resulting box will envelop.

Sets the upper and lower bounds of this box to include all of the data in attribute.

setFromCenterAndSize

function setFromCenterAndSize( center: Vector3, size: Vector3 ): this;  

center, - Desired center position of the box.
size - Desired x, y and z dimensions of the box.

Centers this box on center and sets this box's width, height and depth to the values specified
in size

setFromObject

function setFromObject( object: Object3D, precise: Boolean ): this;  

object - Object3D to compute the bounding box of.
precise - (optional) compute the smallest world-axis-aligned bounding box at the expense of more computation. Default is false.

Computes the world-axis-aligned bounding box of an Object3D (including its children), accounting for the object's, and children's, world transforms. The function may result in a larger box than strictly necessary.

setFromPoints

function setFromPoints( points: Array ): this;  

points - Array of Vector3s that the resulting box will contain.

Sets the upper and lower bounds of this box to include all of the points in points.

translate

function translate( offset: Vector3 ): this;  

offset - Direction and distance of offset.

Adds offset to both the upper and lower bounds of this box, effectively moving this box offset units in 3D space.

union

function union( box: Box3 ): this;  

box - Box that will be unioned with this box.

Computes the union of this box and box, setting the upper bound of this box to the greater of the two boxes' upper bounds and the lower bound of this box to the lesser of the two boxes' lower bounds.

Source

src/math/Box3.js