BufferGeometry
Object3D
Raycaster
Camera
CubeCamera
PerspectiveCamera
OrthographicCamera
StereoCamera
Clock
Curve
CurvePath
Path
Shape
ShapePath
ArrowHelper
AxesHelper
BoxHelper
Box3Helper
CameraHelper
DirectionalLightHelper
GridHelper
PolarGridHelper
HemisphereLightHelper
PlaneHelper
PointLightHelper
SkeletonHelper
SpotLightHelper
Light
PointLight
RectAreaLight
SpotLight
DirectionalLight
HemisphereLight
LightShadow
PointLightShadow
AnimationLoader
AudioLoader
BufferGeometryLoader
CompressedTextureLoader
CubeTextureLoader
DataTextureLoader
FileLoader
ImageBitmapLoader
ImageLoader
Loader
LoaderUtils
MaterialLoader
ObjectLoader
TextureLoader
LoadingManager
Material
Box2
Box3
Color
Cylindrical
Euler
Frustum
Interpolant
Line3
MathUtils
Matrix3
Matrix4
Plane
Quaternion
AnimationAction
AnimationClip
AnimationMixer
AnimationObjectGroup
AnimationUtils
keyframeTrack
PropertyBinding
PropertyMixer
BooleanKeyframeTrack
QuaternionKeyframeTrack
StringKeyframeTrack
Audio
AudioAnalyser
AudioContext
AudioListener
PositionalAudio

CurvePath.getCurveLengths()

简介

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]来获取,因为最后一个元素为路径的总长度。