在使用CatmullRomCurve3的getLength()计算长度时,如果曲线的points长度尝过了200,一定要修改arcLengthDivisions属性,因为three.js中getLength()方法计算长度时是通过arcLengthDivisions的值确定分段的。
getLength()源码:
getLength() {
const lengths = this.getLengths();
return lengths[lengths.length - 1];
}
getLengths(divisions = this.arcLengthDivisions) {
if (this.cacheArcLengths &&
(this.cacheArcLengths.length === divisions + 1) &&
!this.needsUpdate) {
return this.cacheArcLengths;
}
this.needsUpdate = false;
const cache = [];
let current, last = this.getPoint(0);
let sum = 0;
cache.push(0);
for (let p = 1; p <= divisions; p++) {
current = this.getPoint(p / divisions);
sum += current.distanceTo(last);
cache.push(sum);
last = current;
}
this.cacheArcLengths = cache;
return cache; // { sums: cache, sum: sum }; Sum is in the last element.
}