three Box3 3D包围盒

210 阅读8分钟

上一节点介绍了 Box2 Box3与Box2 使用方法大约相同,就不做过多的示例

expandByObject 在使用这个方法时需要注意传入的3DOBJECT 设置位置时在某些情况下会出现计算偏差。

Box3 有三个属性 三十三个方法

属性

  • isBox3 判断当前是否是Box3
  • min Vector3 表示包围盒的下边界。 默认值是( + Infinity, + Infinity, + Infinity )。
  • max Vector3 包围盒的(x, y, z)上边界。 默认值是 ( - Infinity, - Infinity, - Infinity ).

方法

  • applyMatrix4 ( matrix : Matrix4 ) : this THREE.Box3.applyMatrix4(matrix) 是一个方法,用于将一个 4x4 矩阵(Matrix4)应用于 Box3 对象。这会根据给定的矩阵变换更新 Box3 的边界,通常用于在对三维对象应用变换(如平移、旋转和缩放)时更新其边界框。
    // 创建一个 Box3 对象
    const box = new THREE.Box3(new THREE.Vector3(1, 1, 1), new THREE.Vector3(3, 3, 3));
    // 创建一个平移矩阵
    const translationMatrix = new THREE.Matrix4().makeTranslation(2, 2, 2);
    // 输出初始的 Box3
    console.log("max x y ", box.max.x,box.max.y, "min x y ", box.min.x,box.min.y);
    // 应用矩阵变换
    box.applyMatrix4(translationMatrix);
    // 输出变换后的 Box3
    console.log("Transformed Box3:", box); // Box3(3, 3, 3) 到 Box3(5, 5, 5)
    // 创建 OrbitControls
    const controls = new OrbitControls(camera, renderer.domElement);
  • clampPoint ( point : Vector3, target : Vector3 ) : Vector3 THREE.Box3.clampPoint(point, target) 和 THREE.Box2.clampPoint(point, target) 类似,它用于将二维点限制在 Box3 的范围内。假如该点位于 Box3 外部,clampPoint 会返回包围盒边界上距离该点最近的点。如果该点已经在 Box3 内,则保持原点不变。
  • clone () : Box3 返回一个与该包围盒子有相同下边界min 和上边界 max的新包围盒。
  • containsBox ( box : Box3 ) : Boolean box - 需要检测是否在当前包围盒内的 Box3。传入的 box 整体都被包含在该对象中则返回true。如果他们两个包围盒是一样的也返回true。
  • containsPoint ( point : Vector3 ) : Boolean point - 需要检测是否在当前包围盒内的 Vector3。 当传入的值 point 在包围盒内部或者边界都会返回true。
  • copy ( box : Box3 ) : this box - 需要复制的包围盒 Box3 。将传入的值 box 中的 min 和 max 拷贝到当前对象。
  • distanceToPoint ( point : Vector3 ) : Float point - 用来测试距离的点 Vector3。 返回这个box的边缘到指定点的最小距离。如果这个点位于这个盒子里,距离将是0。
  • equals ( box : Box3 ) : Boolean box - 将box与当前对象做比较。 返回true如果传入的值与当前的对象 box 有相同的上下边界。
  • expandByObject ( object : Object3D ) : this object - 包裹在包围盒中的3d对象 Object3D。 扩展此包围盒的边界,使得对象及其子对象在包围盒内,包括对象和子对象的世界坐标的变换。 该方法可能会导致一个比严格需要的更大的框。
    // 创建一个 Box3 对象
    const box = new THREE.Box3(new THREE.Vector3(1, 1, 1), new THREE.Vector3(3, 3, 3));
    // 创建一个 Object3D 对象
    const object = new THREE.Object3D();
    // 为 Object3D 添加一个几何体
    const geometry = new THREE.BoxGeometry(4, 4, 4);
    const material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
    const mesh = new THREE.Mesh(geometry, material);
    object.add(mesh);
    // 扩展 Box3 以包含 Object3D 的边界
    box.expandByObject(object);
    console.log("Expanded Box3:", box);
  • expandByPoint ( point : Vector3 ) : this THREE.Box3.expandByPoint(point) 是一个用于扩展 Box3 边界的方法。它通过检查一个给定的点是否位于 Box3 外部,如果是,则扩展 Box3 的最小点和最大点来包含该点。如果点已经在 Box3 内部,边界保持不变。
  • expandByScalar ( scalar : Float ) : this THREE.Box3.expandByScalar(scalar) 是一个方法,用于以均匀的方式扩展 Box3 的边界。它会将 Box3 的最小点减去标量值,将最大点加上标量值,从而在所有方向上扩展边界。 如果你使用负数作为标量值,Box3 会相应地缩小。
  • expandByVector ( vector : Vector3 ) : this THREE.Box3.expandByVector(vector) 方法用于根据给定的向量扩展 Box3 的边界。这意味着您可以通过添加或减去一个向量来增加 Box3 的大小,向量的每个分量(x、y 和 z)都将用于扩展相应方向的边界。
    // 创建一个 Box3 对象
    const box = new THREE.Box3(
        new THREE.Vector3(1, 1, 1), // 最小点
        new THREE.Vector3(3, 3, 3)  // 最大点
    );
    // 创建一个扩展向量
    const expandVector = new THREE.Vector3(1, 1, 1); // 每个方向扩展 1 个单位
    // 使用 expandByVector 扩展 Box3
    box.expandByVector(expandVector);
    // 输出扩展后的 Box3
    console.log("Expanded Box3:", box);
  • getBoundingSphere ( target : Sphere ) : Sphere THREE.Box3.getBoundingSphere(target) 方法用于计算包含 Box3 的最小包围球,并将结果存储在提供的目标 Sphere 对象中。这个方法通常用于确定与三维空间中的 Box3 相关的最小球体,以便进行碰撞检测或其他计算。
    // 创建一个 Box3 对象
    const box = new THREE.Box3(
        new THREE.Vector3(1, 1, 1), // 最小点
        new THREE.Vector3(3, 3, 3)  // 最大点
    );
    // 创建一个 Sphere 对象作为目标
    const boundingSphere = new THREE.Sphere();
    // 计算 Box3 的包围球并存储在 boundingSphere 中
    box.getBoundingSphere(boundingSphere);
    // 输出计算出的包围球的中心和半径
    console.log("Bounding Sphere Center:", boundingSphere.center);
    console.log("Bounding Sphere Radius:", boundingSphere.radius);
  • getCenter ( target : Vector3 ) : Vector3 THREE.Box3.getCenter(target) 方法用于计算 Box3 的中心点,并将结果存储在提供的目标 Vector3 对象中。这个方法可以帮助您获取 Box3 的几何中心,用于场景中的位置计算或其他目的。
    // 创建一个 Box3 对象
    const box = new THREE.Box3(
        new THREE.Vector3(1, 1, 1), // 最小点
        new THREE.Vector3(5, 5, 5)  // 最大点
    );
    // 创建一个 Vector3 对象作为目标
    const center = new THREE.Vector3();
    // 计算 Box3 的中心并存储在 center 中
    box.getCenter(center);
    // 输出计算出的中心点坐标
    console.log("Box3 Center:", center);
  • getParameter ( point : Vector3, target : Vector3 ) : Vector3 THREE.Box3.getParameter(point, target) 方法用于计算给定点在 Box3 边界中的参数位置,并将结果存储在指定的目标 Vector3 中。这个方法返回的是一个归一化坐标,表示该点在 Box3 的位置。 1728443336775.png
    // 创建一个 Box3 对象
    const box = new THREE.Box3(
        new THREE.Vector3(1, 1, 1), // 最小点
        new THREE.Vector3(5, 5, 5)  // 最大点
    );
    // 创建一个 Vector3 对象作为目标
    const parameter = new THREE.Vector3();
    // 定义要计算参数位置的点
    const point = new THREE.Vector3(3, 3, 3);
    // 计算给定点的参数位置并存储在 parameter 中
    box.getParameter(point, parameter);
    // 输出计算出的参数位置
    console.log("Parameter Position:", parameter);
  • getSize ( target : Vector3 ) : Vector3 target — 如果指定了target ,结果将会被拷贝到target。 返回包围盒的宽度,高度,和深度。
  • intersect ( box : Box3 ) : this box - 与包围盒的交集 计算此包围盒和 box 的交集,将此框的上界设置为两个框的max的较小部分, 将此包围盒的下界设置为两个包围盒的min的较大部分。如果两个包围盒不相交,则清空此包围盒。
  • intersectsBox ( box : Box3 ) : Boolean box - 用来检测是否相交的包围盒 确定当前包围盒是否与传入包围盒box 相交。
  • intersectsPlane ( plane : Plane ) : Boolean plane - 用来检测是否相交的 Plane。 确定当前包围盒是否与平面 plane 相交。
  • intersectsSphere ( sphere : Sphere ) : Boolean sphere - 用来检测是否相交的球体 Sphere。 确定当前包围盒是否与球体 sphere 相交。
  • intersectsTriangle ( triangle : Triangle ) : Boolean triangle - 用来检测是否相交的三角形 Triangle。 确定当前包围盒是否与三角形 triangle 相交。
  • isEmpty () : Boolean 如果这个盒子包含0个顶点,则返回true。 注意,下界和上界相等的方框仍然包含一个点,即两个边界共享的那个点。
  • makeEmpty () : this 清空包围盒。
  • set ( min : Vector3, max : Vector3 ) : this min - Vector3 表示下边界每个纬度(x,y,z)的值。 max - Vector3 表示上边界每个纬度(x,y,z)的值。 设置包围盒上下边界每个纬度(x,y,z)的值。 请注意,此方法仅复制给定对象的值。
  • setFromArray ( array : Array ) : this array - 数组当中的所有的点都将被包围盒包裹。 设置包围盒的上下边界使得数组 array 中的所有点的点都被包含在内。
  • setFromBufferAttribute ( attribute : BufferAttribute ) : this attribute - 位置的缓冲数据,包含在返回的包围盒内。 设置此包围盒的上边界和下边界,以包含 attribute 中的所有位置数据。
  • setFromCenterAndSize ( center : Vector3, size : Vector3 ) : this center, - 包围盒所要设置的中心位置。 size - 包围盒所要设置的x、y和z尺寸(宽/高/长)。
  • setFromObject ( object : Object3D ) : this object - 用来计算包围盒的3D对象 Object3D。 计算和世界轴对齐的一个对象 Object3D (含其子对象)的包围盒,计算对象和子对象的世界坐标变换。 该方法可能会导致一个比严格需要的更大的框。
  • setFromPoints ( points : Array ) : this points - 计算出的包围盒将包含数组中所有的点 Vector3s 设置此包围盒的上边界和下边界,以包含数组 points 中的所有点。
  • translate ( offset : Vector3 ) : this offset - 偏移方向和距离。 给包围盒的上下边界添加偏移量 offset,这样可以有效的在3D空间中移动包围盒。 偏移量为 offset 大小。
  • union ( box : Box3 ) : this box - 将被用于与该盒子计算并集的盒子。 在 box 参数的上边界和已有box对象的上边界之间取较大者,而对两者的下边界取较小者,这样获得一个新的较大的联合盒子。