如何确定点是否在框内(three.js)?

How to determine if point is inside box (three.js)?

如果我们有非轴对齐的框,我们如何才能最好地检查一个点是否位于其中? (我正在使用 three.js,因此那里的任何实用程序都可以提供帮助。Three.js 包含边界框概念,但这是轴对齐的边界框)

如果你的盒子是一个 THREE.BoxGeometry 旋转,平移和缩放,那么你可以使用它的变换矩阵 m 来确定它是否与你的点相交 v:

  • 通过m
  • 的逆变换v和方框
  • 检查变换后的 v 是否在变换后的框内(现在是轴对齐的)

代码如下:

var box = <Your non-aligned box>
var point = <Your point>

box.geometry.computeBoundingBox(); // This is only necessary if not allready computed
box.updateMatrixWorld(true); // This might be necessary if box is moved

var boxMatrixInverse = new THREE.Matrix4().getInverse(box.matrixWorld);

var inverseBox = box.clone();
var inversePoint = point.clone();

inverseBox.applyMatrix(boxMatrixInverse);
inversePoint.applyMatrix4(boxMatrixInverse);

var bb = new THREE.Box3().setFromObject(inverseBox);

var isInside = bb.containsPoint(inversePoint);

这是一个 运行 演示:https://jsfiddle.net/holgerl/q0z979uy/