three Sphere 球

268 阅读4分钟

Sphere 是数学库中常见的几何对象,用于定义三维空间中的球体。在你描述的 Sphere 对象中,包含两个关键属性

Sphere( center : Vector3, radius : Float ) center - 球心的位置,默认值是一个位于(0, 0, 0)的Vector3。 radius - 球的半径,默认值是-1。

Sphere 有三个属性十八个方法

属性

  • center : Vector3 A Vector3定义了球心的位置,默认值位于(0, 0, 0)。
  • isSphere : Boolean 只读属性判断是球类
  • radius : Float 球的半径,默认值为-1。

方法

  • pplyMatrix4 ( matrix : Matrix4 ) : this 在 3D 数学库中,Sphere.applyMatrix4(matrix: Matrix4): this 是一种对球体进行变换的方式。这个方法的作用是通过传入的 Matrix4 矩阵对球体进行几何变换,如旋转、平移、缩放等。
    const sphere = new THREE.Sphere(new THREE.Vector3(1, 1, 1), 2);
    const matrix = new THREE.Matrix4();
    matrix.makeTranslation(3, 0, 0); // 创建一个平移矩阵,沿 X 轴移动 3 个单位
    sphere.applyMatrix4(matrix);
    console.log(sphere.center); // 输出: Vector3 { x: 4, y: 1, z: 1 }
    console.log(sphere.radius); // 半径保持不变,仍为 2
  • clampPoint ( point : Vector3, target : Vector3 ) : Vector3 point - Vector3 将要夹取的点。 target — 结果将被复制到这个Vector3中。 从球中夹取一个点。若这一点位于球外,则将会夹取到该点球边缘最近的点。已位于球中的点将不会受到影响。
  • clone () : Sphere 返回一个新的球,新的球与这个球具有相同的center和radius。
  • containsPoint ( point : Vector3 ) : Boolean 检查球体中是否包含所传入的point点,包括球的表面。
  • copy ( sphere : Sphere ) : this 复制所传入的球的center和radius到这个球上。
  • distanceToPoint ( point : Vector3 ) : Float 返回球的边界到所传入的point点的最近距离。 若这个点位于球内,则距离将为负值。
  • expandByPoint ( point : Vector3 ) : this 这个方法的主要功能是扩展球体的范围,确保它包含传入的 point。具体做法是: 如果 point 已经在球体内部或位于球表面,球体不需要做任何调整。 如果 point 在球体外部,球体的半径将被增大,以确保 point 位于新的球体范围内,同时球心可能会发生移动。
  • isEmpty () : Boolean 检查球是否为空(球半径为负值)。半径为 0 的球体仅包含其中心点,并不会被视为空。
  • makeEmpty () : this 将该球修改为空,即中心点 center 为 (0,0,0),半径 radius 为 -1。
  • equals ( sphere : Sphere ) : Boolean 检查这两个球的球心与半径是否相等。
  • getBoundingBox ( target : Box3 ) : Box3 target — 结果将被复制到这个Box3中。 返回这个球的Minimum Bounding Box(最小包围盒)。
  • intersectsBox ( box : Box3 ) : Boolean box - 将被用于测试是否与这个球有交集的Box3。 检测这个球与所传入的box是否有交集。
  • intersectsPlane ( plane : Plane ) : Boolean plane - 将被用于测试是否与这个球有交集的Plane。 检测这个球与所传入的plane是否有交集。
  • intersectsSphere ( sphere : Sphere ) : Boolean sphere - 将被用于测试是否与这个球有交集的Sphere。 检测两球之间是否有交集。
  • set ( center : Vector3, radius : Float ) : this center - 球心位置。 radius - 球的半径。 设置球的center和radius属性。 请注意此,方法使用复制的方式来设置中心值。
  • setFromPoints ( points : Array, optionalCenter : Vector3 ) : this 是一个几何函数,用于根据一组给定的点来定义或重新设置球体的中心和半径。该函数会计算这些点的分布,确定一个最小包围球体,使所有点都位于球体内部或边界上。points: 这是一个包含多个 Vector3 对象的数组,每个 Vector3 代表一个三维点。optionalCenter: 可选参数。如果提供了此参数,则球体将使用该点作为球心,而只调整球的半径来确保包含所有点。如果不提供,则球心将自动根据所有点的位置计算。
    const points = [
        new THREE.Vector3(1, 0, 0),
        new THREE.Vector3(0, 1, 0),
        new THREE.Vector3(0, 0, 1)
    ];
    const sphere = new THREE.Sphere();
    sphere.setFromPoints(points);
    console.log(sphere.center); // 自动计算的球心,输出: Vector3 { "x": 0.5, "y": 0.5, "z": 0.5 }
    console.log(sphere.radius); // 计算得到的最小半径,输出: 0.8660254037844386
  • translate ( offset : Vector3 ) : this 使用所给定Vector3 offset(偏移量)平移球心。
  • union ( sphere : Sphere ) : this sphere - 将与该球体即将结合的边界球体。 扩展此球体以包含原始球体和给定球体。