three Plane 平面

166 阅读5分钟

Plane 有三个属笥 十八个方法

Plane( normal : Vector3, constant : Float ) normal - (可选参数) 定义单位长度的平面法向量Vector3。默认值为 (1, 0, 0)。 constant - (可选参数) 从原点到平面的有符号距离。 默认值为 0.

属性

  • isPlane : Boolean 判断是Plane 类
  • normal : Vector3 法向量(normal)在三维图形学中扮演着非常重要的角色。它不仅定义了平面的方向,还在碰撞检测、光照计算、变换和面向判断等方面发挥着关键作用。通过有效地使用法向量,您可以创建更加真实和动态的三维场景。
  • constant : Float 常数

方法

  • applyMatrix4 ( matrix : Matrix4, optionalNormalMatrix : Matrix3 ) : this matrix - 要应用的四维矩阵(Matrix4)。 optionalNormalMatrix - (可选参数) 预先计算好的上述Matrix4参数的法线矩阵 Matrix3。 在平面上应用矩阵。矩阵必须是仿射齐次变换。 如果提供一个optionalNormalMatrix,可以这样创建const optionalNormalMatrix = new THREE.Matrix3().getNormalMatrix( matrix ); 法向量的处理: 如果平面进行的变换只包括平移,则法向量的方向不会改变。但是如果有旋转或缩放,您需要提供 optionalNormalMatrix 以确保法向量正确。
    // 创建法向量和常数
    const normal = new THREE.Vector3(0, 1, 0);
    const constant = 0;
    // 创建平面
    const plane = new THREE.Plane(normal, constant);
    // 创建一个旋转矩阵
    const rotationMatrix = new THREE.Matrix4().makeRotationY(Math.PI / 4); // 旋转 45 度
    // 计算法向量的变换矩阵
    const normalMatrix = new THREE.Matrix3().getNormalMatrix(rotationMatrix);
    // 应用变换到平面
    plane.applyMatrix4(rotationMatrix, normalMatrix);
    console.log(plane); // 输出变换后的平面,法向量也会更新
  • clone () : Plane 返回一个与当前平面有相同法线 normal,常量 constant 距离的平面。
  • coplanarPoint ( target : Vector3 ) : Vector3 target — 结果会拷贝到该向量中 返回一个共面点,通过原点的法向量在平面上投影算得。
  • copy ( plane : Plane ) : this 拷贝给定平面,将其中的法线 normal,距离常量 constant属性拷贝给该对象。
  • distanceToPoint ( point : Vector3 ) : Float 返回点point到平面的有符号距离。
    // 定义一个平面,法向量指向 Y 轴,常数为 0
    const normal = new THREE.Vector3(0, 1, 0);
    const constant = 0;
    const plane = new THREE.Plane(normal, constant);
    // 定义一个点
    const pointAbove = new THREE.Vector3(0, 1, 0); // 在平面上方
    const pointBelow = new THREE.Vector3(0, -1, 0); // 在平面下方
    const pointOnPlane = new THREE.Vector3(0, 0, 0); // 在平面上
    // 计算距离
    const distanceAbove = plane.distanceToPoint(pointAbove); // 正值
    const distanceBelow = plane.distanceToPoint(pointBelow); // 负值
    const distanceOnPlane = plane.distanceToPoint(pointOnPlane); // 零
    console.log(distanceAbove);   // 输出: 1 (正值,表示在平面上方)
    console.log(distanceBelow);   // 输出: -1 (负值,表示在平面下方)
    console.log(distanceOnPlane); // 输出: 0 (零,表示在平面上)
  • distanceToSphere ( sphere : Sphere ) : Float 返回球面 sphere 的边缘到平面的最短距离。
  • equals ( plane : Plane ) : Boolean 检查两个平面是否相等。(法线 normal 以及常量 constant 都相同)。
  • intersectLine ( line : Line3, target : Vector3 ) : Vector3 line - 检测是否相交的三维几何线段 Line3。 target — 结果将会写入该向量中。 返回给定线段和平面的交点。如果不相交则返回null。如果线与平面共面,则返回该线段的起始点。
  • intersectsBox ( box : Box3 ) : Boolean box - 检查是否相交的包围盒 Box3。 确定该平面是否与给定3d包围盒Box3相交。
  • intersectsLine ( line : Line3 ) : Boolean line - 检查是否相交的三维线段 Line3。测试线段是否与平面相交。
  • intersectsSphere ( sphere : Sphere ) : Boolean sphere - 检查是否相交的球体 Sphere。 确定该平面是否与给定球体 Sphere 相交。
  • negate () : this 将法向量与常量求反(乘以-1)。
  • normalize () : this 归一化法向量 normal ,并相应的调整常量 constant数值。

1728958243627.png

    // 作用
    // -   **标准化法向量**: 将平面的法向量调整为单位长度,即长度为 1。这有助于确保在后续计算中使用的法向量是正确的。
    // -   **调整常数**: 在标准化法向量的同时,常数值也会被更新,以反映标准化后的法向量。这保证了从原点到平面的距离与新的法向量一致。
    // 创建一个法向量和常数
    const normal = new THREE.Vector3(2, 2, 0); // 非单位法向量
    const constant = 4;
    // 创建平面
    const plane = new THREE.Plane(normal, constant);
    // 标准化平面
    plane.normalize();
    console.log(plane.normal);   // 输出标准化后的法向量 { "x": 0.7071067811865475, "y": 0.7071067811865475, "z": 0 }
    console.log(plane.constant);  // 输出更新后的常数 1.414213562373095
  • projectPoint ( point : Vector3, target : Vector3 ) : Vector3 point - 需要投射到该平面的点。 target — 在该平面上离投射点最近的点。
    // 创建一个法向量和常数定义一个平面
    const normal = new Vector3(0, 1, 0); // 法向量指向 Y 轴
    const constant = 0; // 平面位于 Y = 0
    const plane = new Plane(normal, constant);
    // 定义需要投影的点
    const point = new Vector3(2, 3, 4); // 这个点在平面上方
    // 定义一个目标点用于存储结果
    const target = new Vector3();
    // 计算点在平面上的投影
    const projectedPoint = plane.projectPoint(point, target);
    console.log(projectedPoint); // 输出: Vector3 { x: 2, y: 0, z: 4 }
    console.log(target);          // 同样输出: Vector3 { x: 2, y: 0, z: 4 }
  • set ( normal : Vector3, constant : Float ) : this normal - 单位长度的向量表示平面的法向量。 constant - 原点到平面有符号距离。 设置平面 normal 的法线和常量 constant 属性值。
  • setComponents ( x : Float, y : Float, z : Float, w : Float ) : this x - 单位长度法向量的x值。 y - 单位长度法向量的y值。 z - 单位长度法向量的z值。 w - 原点沿法向量到平面常量 constant 距离。 设置定义平面的各个变量。
  • setFromCoplanarPoints ( a : Vector3, b : Vector3, c : Vector3 ) : this a - 用于确定平面的第一个点。 b - 用于确定平面的第二个点。 c - 用于确定平面的第三个点。 根据给定的三个点确定平面。如果三个点共线将会抛出错误。通过右手螺旋规则确定(向量叉乘)法向量 normal。
    // 创建一个平面对象
    const plane = new THREE.Plane();
    // 定义三个共面点
    const pointA = new THREE.Vector3(1, 1, 0);
    const pointB = new THREE.Vector3(2, 0, 0);
    const pointC = new THREE.Vector3(0, 0, 1);
    // 使用这三个点设置平面
    plane.setFromCoplanarPoints(pointA, pointB, pointC);
    console.log(plane.normal);   // 输出法向量
    console.log(plane.constant);  // 输出常数
  • setFromNormalAndCoplanarPoint ( normal : Vector3, point : Vector3 ) : this normal - 平面单位法向量 point - 平面上的点 通过参数提供的法线 normal 和 平面上的一个点 point 来设置该平面。
    // 创建一个平面对象
    const plane = new THREE.Plane();
    // 定义法向量和一个平面上的点
    const normal = new THREE.Vector3(0, 1, 0); // 法向量指向 Y 轴
    const point = new THREE.Vector3(0, 2, 0); // 平面上的点
    // 使用法向量和点设置平面
    plane.setFromNormalAndCoplanarPoint(normal, point);
    console.log(plane.normal);   // 输出法向量: (0, 1, 0)
    console.log(plane.constant);  // 输出常数: -2
  • translate ( offset : Vector3 ) : this offset - 平移量 将平面平移给定向量大小,注意:这只会影响平面的常量不会影响平面的法向量。