在 Three.js 中,LineCurve3
是一种用于描述三维空间中线段的曲线类。它是 Curve
类的子类,能够精确描述三维空间中两点之间的直线段,并支持点的插值、切线计算等操作。
本文将详细讲解 LineCurve3
的使用方法,包括其属性、方法,以及与其他组件的结合使用,并通过大量示例展示其实际应用场景。
LineCurve3
简介LineCurve3
用于表示三维空间中两点之间的线段,它在构建几何体、动画路径、碰撞检测等场景中非常实用。
const curve = new THREE.LineCurve3(v1, v2);
v1
: 起始点,为 THREE.Vector3
对象。v2
: 结束点,为 THREE.Vector3
对象。LineCurve3
的属性与方法type
: 曲线类型,固定为 'LineCurve3'
。v1
: 曲线的起点(Vector3
)。v2
: 曲线的终点(Vector3
)。getPoint(t)
返回曲线上参数 ( t )(范围 ( [0, 1] ))对应的点。
const point = curve.getPoint(0.5);
console.log(point); // Vector3 { x: ..., y: ..., z: ... }
getPoints(divisions)
将曲线划分为若干段,返回分割点的数组。
const points = curve.getPoints(10);
console.log(points); // [Vector3, Vector3, ...]
getTangent(t)
获取曲线在 ( t ) 处的切线方向(始终为常量,因为是直线)。
const tangent = curve.getTangent(0.5);
console.log(tangent); // Vector3 { x: ..., y: ..., z: ... }
clone()
克隆当前曲线对象。
const clonedCurve = curve.clone();
console.log(clonedCurve);
toJSON()
和 fromJSON(json)
序列化和反序列化曲线对象,用于存储和恢复曲线数据。
LineCurve3
的基本用法以下代码展示如何创建并绘制一条简单的三维线段:
// 创建三维点
const start = new THREE.Vector3(0, 0, 0);
const end = new THREE.Vector3(10, 10, 10);
// 创建 LineCurve3
const curve = new THREE.LineCurve3(start, end);
// 获取曲线上的点
const points = curve.getPoints(10);
// 转换为几何体
const geometry = new THREE.BufferGeometry().setFromPoints(points);
// 创建线条
const material = new THREE.LineBasicMaterial({ color: 0xff0000 });
const line = new THREE.Line(geometry, material);
scene.add(line);
让一个物体沿三维线段移动:
const sphere = new THREE.Mesh(
new THREE.SphereGeometry(0.2, 16, 16),
new THREE.MeshBasicMaterial({ color: 0x0000ff })
);
scene.add(sphere);
let t = 0;
function animate() {
t += 0.01;
if (t > 1) t = 0;
const point = curve.getPoint(t);
sphere.position.set(point.x, point.y, point.z);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
LineCurve3
的进阶应用LineCurve3
构建复杂路径可以通过 CurvePath
将多个 LineCurve3
组合,形成复杂的路径:
const path = new THREE.CurvePath();
path.add(new THREE.LineCurve3(new THREE.Vector3(0, 0, 0), new THREE.Vector3(5, 0, 0)));
path.add(new THREE.LineCurve3(new THREE.Vector3(5, 0, 0), new THREE.Vector3(5, 5, 5)));
path.add(new THREE.LineCurve3(new THREE.Vector3(5, 5, 5), new THREE.Vector3(0, 5, 0)));
const points = path.getPoints(50);
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({ color: 0x00ff00 });
const line = new THREE.Line(geometry, material);
scene.add(line);
TubeGeometry
创建三维管道利用 TubeGeometry
可以将 LineCurve3
转换为三维管道几何体:
const tubeGeometry = new THREE.TubeGeometry(curve, 20, 0.1, 8, false);
const material = new THREE.MeshBasicMaterial({ color: 0xffff00 });
const tube = new THREE.Mesh(tubeGeometry, material);
scene.add(tube);
结合 PointsMaterial
,使用 LineCurve3
生成点云效果:
const points = curve.getPoints(50);
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.PointsMaterial({ size: 0.2, color: 0xff9900 });
const pointsMesh = new THREE.Points(geometry, material);
scene.add(pointsMesh);
通过动态更新 LineCurve3
的插值点,可以创建炫酷的粒子流效果:
const particleGeometry = new THREE.BufferGeometry();
const material = new THREE.PointsMaterial({ color: 0x00ffff, size: 0.1 });
let particles = curve.getPoints(100);
particleGeometry.setFromPoints(particles);
const particleSystem = new THREE.Points(particleGeometry, material);
scene.add(particleSystem);
function updateParticles() {
particles = curve.getPoints(100);
particleGeometry.setFromPoints(particles);
}
getPoints
的分割数量减少计算量。结合鼠标事件,可以实时改变 LineCurve3
的起点和终点,构建交互式场景。
document.addEventListener('mousemove', (event) => {
const newEnd = new THREE.Vector3(event.clientX / 100, event.clientY / 100, 0);
curve.v2.copy(newEnd);
updateParticles();
});
LineCurve3
是构建三维线段和路径的重要工具,它简单易用但功能强大。在结合 CurvePath
、TubeGeometry
和粒子系统后,可以实现复杂的几何体构造和动态动画效果。通过本文的详细讲解和示例,希望你能在实际项目中熟练运用 LineCurve3
,构建更加精彩的 Three.js 场景。