computeBoundingBox vs computeBoundingSphere in Three.js

In Three.js, calculating boundary volumes is essential for frustum culling, raycasting, and collision detection. This article analyzes the performance differences between computeBoundingBox and computeBoundingSphere, explaining how they work under the hood, their computational overhead during creation, and their efficiency during runtime rendering tests.

Mathematical and Computational Complexity

To understand the performance difference, we must look at how Three.js calculates these two spatial boundaries over a 3D mesh’s geometry.

1. computeBoundingBox()

This method calculates an Axis-Aligned Bounding Box (AABB) by finding the minimum and maximum X, Y, and Z coordinates among all vertices in the geometry.

2. computeBoundingSphere()

This method calculates a bounding sphere that encapsulates all vertices of the geometry. It determines a center point and a radius.

Creation Phase Winner: computeBoundingBox is computationally cheaper to generate than computeBoundingSphere because it relies solely on simple comparison operations rather than arithmetic distance calculations.


Runtime Performance (Usage Phase)

While generating a bounding box is faster than generating a bounding sphere, the primary performance difference lies in how Three.js uses these boundaries during the render loop.

By default, Three.js uses these boundaries for frustum culling—the process of ignoring objects that are outside the camera’s field of view to save GPU processing power.

Frustum Culling with Spheres

Testing whether a sphere is inside the camera frustum is incredibly fast. The engine only needs to calculate the distance from the sphere’s center to the six frustum planes and compare it to the sphere’s radius. This involves simple dot products and addition.

Frustum Culling with Boxes

Testing an Axis-Aligned Bounding Box against the camera frustum is mathematically more complex. It requires testing the eight corners of the box against the frustum planes, which involves significantly more conditional checks and floating-point operations.

Rendering Phase Winner: Bounding spheres are significantly faster for real-time frustum culling and collision detection checks.


Best Practices for Optimization

To ensure your Three.js application runs at a fluid 60 FPS or higher, follow these optimization guidelines: