three.js - 获取曲线上点的位置
问题描述
我有一个从 Vector3 点数组创建的 CatmullRomCurve3。
const curve = new THREE.CatmullRomCurve3(points);
我想知道曲线上这些相同点的位置(范围从 0 到 1)。.getPointAt()
基本上与第一个点在 0 的位置相反,最后一个点在 1 的位置。
我想这样做是为了在我用来创建曲线的每个点之间将曲线细分为相等数量的段。因此,例如在点 0 和点 1 之间获得 10 个细分,在点 1 和点 2 之间获得 10 个细分,依此类推。
解决方案
我确定您的问题有一种数学方法,但一个简单的解决方法是:
- 使用数组或
Vector3
s 创建原始曲线。 - 创建具有相同数组的重复曲线,该曲线停在
Vector3
您正在搜索的位置。 - 取第二条曲线的长度,除以原始曲线的长度,你会得到你的 [0, 1] 位置。
在代码中:
// Create original curve
var curve = new THREE.CatmullRomCurve3( [
new THREE.Vector3( -10, 0, 10 ),
new THREE.Vector3( -5, 5, 5 ),
new THREE.Vector3( 0, 0, 0 ),
new THREE.Vector3( 5, -5, 5 ),
new THREE.Vector3( 50, 0, 50 )
], false );
var searchPoint = new THREE.Vector3( 5, -5, 5 ); // Point we're looking for
var searchArray = []; // Array for second curve
var uPosition = null; // Result is null for now
// Loop through curve.points to find our final point
for(var i = 0; i < curve.points.length; i++){
searchArray.push(curve.points[i]);
// Exit loop once point has been found
if(searchPoint.equals(curve.points[i])){
// Create shorter curve that stops at desired point
var curve2 = new THREE.CatmullRomCurve3(searchArray);
// Result is short length / original length
uPosition = curve2.getLength() / curve.getLength();
break;
}
}
// Result is null if position not found
console.log(uPosition);
推荐阅读
- ios - 以编程方式在垂直堆栈视图中添加多个水平堆栈视图
- floating-point - IEEE-754 复合函数范围限制的基本原理
- r - 将日期格式从数据框转换为时间序列的问题
- python - calendar.monthrange() 函数显示错误
- xml - 当我在布局上放置背景时,我的应用程序无法运行并出现错误
- php - 通过两个自定义字段过滤自定义帖子类型
- javascript - 如何找出节点进程中的内存泄漏
- javascript - Vuejs关于选择更改组件属性
- javascript - Javascript:无法识别对象属性
- php - 无法从 MySQL 数据库中删除条目 - PHP