按距离单位而不是 THREE.js 中的因素对 2 Vector3 进行线性插值

Linear Interpolation of 2 Vector3 by unit of distance rather than factor in THREE.js

对任何措辞和不正确的引用表示歉意,3 维数学对我来说是新的!

问题:

备注:

我正在使用 THREE.js,但欢迎使用任何语言示例!

到目前为止的工作:

function lerpUnit (x1,y1,z1,x2,y2,z3,distance) {
   ... ???
   return {x3,y3,z3}
}

你可以通过 THREE.Vector3()

提供的方法实现
  • 首先,您可以减去 vecB - vecA 以获得起始于 (0, 0, 0) 的平行向量。
  • 其次,使用normalize()把这个向量变成一个“单位向量”,也就是说不管它指向哪个方向,它的长度永远是1。
  • 第三,向量乘以L,所以length = 1 * L
  • 最后,用+ vecA将这个新的平行向量移回它的初始位置,得到最终位置。它会落在由 AB
  • 创建的线段内部或外部的某个地方

var vecA = new THREE.Vector3(1, 2, 3);
var vecB = new THREE.Vector3(6, 5, 4);
var L = 10;

// Create new vector
var direction = new THREE.Vector3();

// Assign positionB
direction.copy(vecB);

// Subtract positionA to get a vector starting at (0, 0, 0)
direction.sub(vecA);

// Normalize to get a vector of lengh = 1
direction.normalize();

// Multiply by new length
direction.multiplyScalar(L);

// Add positionA to return vector to initial start point
direction.add(vecA);

// Output to log to see result
console.log(direction.toArray());
<script src="https://cdn.jsdelivr.net/npm/three@0.117.1/build/three.min.js"></script>

谢谢,我只是通过使用 .lerp() 扩展了工作原理,但传递了一个比率值,例如:

function lerpUnit (vectorA, vectorB, intendedDistance ) {
   const aToBDistance = vectorA.distanceTo(vectorB)
   const adjustedFactor = intendedDistance / aToBDistance
   return new THREE.Vector3().lerpVectors(vectorA, vectorB, adjustedFactor)
}

或 1 个班轮:const vectorC = new THREE.Vector3().lerpVectors(vectorA, vectorB, intendedDistance / vectorA.distanceTo(vectorB))