经纬志
首页 / three.js / API 文档

Sphere

先看结论

球体范围。由一个中心点和半径描述,是 three.js 视锥剔除与射线粗测使用的默认包围体。

最后验证 2026-11-03 · three.js 0.160.0 · 更新 2026-10-29 · 引用本文
new THREE.Sphere(center, radius)

构造参数

参数默认说明
center(0, 0, 0)球心
radius-1半径。负值表示空球,isEmpty() 返回 true

属性

属性说明
center球心,Vector3
radius半径
isSphere恒为 true

方法

方法说明
set(center, radius)设置球心与半径
setFromPoints(points, optionalCenter)由点集合求最小包围球
makeEmpty()重置为空球
isEmpty()半径是否小于 0
containsPoint(point)点是否在球内
distanceToPoint(point)到球面的距离,点在内部时为负
intersectsSphere(sphere)与另一个球是否相交
intersectsBox(box)与包围盒是否相交
intersectsPlane(plane)与平面是否相交
clampPoint(point, target)把点夹到球面上最近的位置
getBoundingBox(target)转成轴对齐包围盒
applyMatrix4(matrix)用变换矩阵改写,非均匀缩放会让半径变大
translate(offset)平移球心
clone() / copy(sphere)复制

示例:视锥剔除与点击粗测

// 几何体的包围球,剔除判断依赖它
geometry.computeBoundingSphere();
const bound = geometry.boundingSphere.clone().applyMatrix4(mesh.matrixWorld);

// 相机视锥
const frustum = new THREE.Frustum();
frustum.setFromProjectionMatrix(
  new THREE.Matrix4().multiplyMatrices(
    camera.projectionMatrix,
    camera.matrixWorldInverse
  )
);

console.log('是否可见', frustum.intersectsSphere(bound));

// 点击粗测:先用球排除远处对象,再对候选做精确射线检测
const sphere = new THREE.Sphere();
const hit = raycaster.ray.intersectsSphere(sphere.set(mesh.position, 3.2));
if (hit) {
  raycaster.intersectObject(mesh);
}

半径从哪来

geometry.computeBoundingSphere() 取几何体所有顶点的 最小包围球,结果与顶点分布有关,不一定等于最长边的一半。 对象带缩放时,applyMatrix4 会按最大缩放轴放大半径, 非均匀缩放下这个球会偏大,属于保守估计。

动态几何要重算

每帧修改顶点后,包围球不会自动更新。物体在屏幕边缘抖动消失, 通常就是旧包围球把物体判为不可见。修改顶点后调用 computeBoundingSphere(),或暂时关闭 frustumCulled。

空球会让剔除逻辑失效。半径默认 -1,忘记赋值就参与判断时, 所有相交测试都返回 false,物体会整体消失。手工构造后先检查 isEmpty()。

引用本文

经纬志(2026)。《Sphere》。https://www.jingweizhi.com/three/api/sphere.html(访问日期 2026-11-03)。