按距离单位而不是 THREE.js 中的因素对 2 Vector3 进行线性插值
Linear Interpolation of 2 Vector3 by unit of distance rather than factor in THREE.js
对任何措辞和不正确的引用表示歉意,3 维数学对我来说是新的!
问题:
- 我在 3D 中有两个点 space A:
(1,2,3)
和 B: (6,5,4)
- 我有距离L:10
- 定义为 A TOWARDS B 在 UNIT 处的 3D 点 C 是什么L?
备注:
- L 的距离不是 A 或 B 之间距离的一个因素,
.lerp()
这里的工具不正确
- L 可以大于也可以小于 A 和 B 之间的距离。
我正在使用 THREE.js
,但欢迎使用任何语言示例!
到目前为止的工作:
- 计算 A 和 B 之间的距离 -> D
- 获取 D 与我们的目标值 L 相关的因子 -> R
- 将带有 A 的 .lerp() 应用到带有因子 R 的 B
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
将这个新的平行向量移回它的初始位置,得到最终位置。它会落在由 A 和 B 创建的线段内部或外部的某个地方
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))
对任何措辞和不正确的引用表示歉意,3 维数学对我来说是新的!
问题:
- 我在 3D 中有两个点 space A:
(1,2,3)
和 B:(6,5,4)
- 我有距离L:10
- 定义为 A TOWARDS B 在 UNIT 处的 3D 点 C 是什么L?
备注:
- L 的距离不是 A 或 B 之间距离的一个因素,
.lerp()
这里的工具不正确 - L 可以大于也可以小于 A 和 B 之间的距离。
我正在使用 THREE.js
,但欢迎使用任何语言示例!
到目前为止的工作:
- 计算 A 和 B 之间的距离 -> D
- 获取 D 与我们的目标值 L 相关的因子 -> R
- 将带有 A 的 .lerp() 应用到带有因子 R 的 B
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
将这个新的平行向量移回它的初始位置,得到最终位置。它会落在由 A 和 B 创建的线段内部或外部的某个地方
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))