CurvePath.getCurveLengths()
是three.js中CurvePath类的一个方法,用于获取曲线路径中每一段曲线的长度,并将这些长度储存在一个数组中。
getCurveLengths(divisions?: number): number[]
divisions
(可选) - 将每一段曲线分成多少个点来计算长度,默认值为40
。一个包含每一段曲线长度的数组。数组长度为所包含曲线段的数目加一,因为最后一个元素为曲线路径的总长度。
// 创建一个CurvePath对象、两个立体曲线和一段直线路径
var curvePath = new THREE.CurvePath();
curvePath.add(new THREE.CatmullRomCurve3([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(3, 2, 0),
new THREE.Vector3(6, 1, 1),
new THREE.Vector3(10, 3, 0)
]));
curvePath.add(new THREE.CubicBezierCurve3(
new THREE.Vector3(10, 3, 0),
new THREE.Vector3(11, 4, 2),
new THREE.Vector3(9, 4, 4),
new THREE.Vector3(8, 3, 5)
));
curvePath.add(new THREE.LineCurve3(
new THREE.Vector3(8, 3, 5),
new THREE.Vector3(-2, 1, 5)
));
// 获取曲线路径中每一段曲线的长度
var lengths = curvePath.getCurveLengths();
// 打印曲线路径总长度
console.log("Total length of curve path: " + lengths[lengths.length - 1]);
// 打印每一段曲线的长度
for (var i = 0; i < lengths.length - 1; i++) {
console.log("Length of curve " + i + ": " + lengths[i]);
}
divisions
参数,默认值为40
会对大多数场景产生足够精确的长度计算,但在一些弧度比较小的场景下可能需要增加divisions
的值来增加精度。lengths[lengths.length - 1]
来获取,因为最后一个元素为路径的总长度。