在 Three.js 中,LineCurve
是用于描述二维线段的基础曲线类,它是 Curve
的子类,能够提供点的插值、切线计算等功能。尽管看似简单,LineCurve
是许多复杂几何体或动画轨迹的基础构建块。
本文将详细讲解 LineCurve
的所有属性和方法,并通过示例演示它在实际开发中的各种应用场景。
LineCurve
简介LineCurve
是一个表示直线段的二维曲线类。它接受两个点作为参数,并能够计算它们之间的任意插值点、切线方向等。
const curve = new THREE.LineCurve(v1, v2);
v1
: 起始点,为 THREE.Vector2
对象。v2
: 结束点,为 THREE.Vector2
对象。LineCurve
的属性与方法type
: 曲线类型,始终为 'LineCurve'
。v1
: 曲线的起点(Vector2
)。v2
: 曲线的终点(Vector2
)。getPoint(t)
返回曲线上参数 ( t ) (范围 ( [0, 1] ))对应的点。
const point = curve.getPoint(0.5);
console.log(point); // Vector2 { x: ..., y: ... }
getPoints(divisions)
将曲线划分为若干段,返回分割点的数组。
const points = curve.getPoints(10);
console.log(points); // [Vector2, Vector2, ...]
getTangent(t)
获取曲线在 ( t ) 处的切线方向(始终为常量,因为是直线)。
const tangent = curve.getTangent(0.5);
console.log(tangent); // Vector2 { x: ..., y: ... }
clone()
克隆当前曲线对象。
const clonedCurve = curve.clone();
console.log(clonedCurve);
toJSON()
和 fromJSON(json)
序列化和反序列化曲线对象,用于存储和恢复曲线数据。
LineCurve
的基本用法以下代码展示如何创建并绘制一个简单的线段:
// 创建二维点
const start = new THREE.Vector2(0, 0);
const end = new THREE.Vector2(10, 10);
// 创建 LineCurve
const curve = new THREE.LineCurve(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);
LineCurve
连接复杂路径可以通过 CurvePath
将多个 LineCurve
连接起来,形成复杂的路径。
const path = new THREE.CurvePath();
path.add(new THREE.LineCurve(new THREE.Vector2(0, 0), new THREE.Vector2(5, 0)));
path.add(new THREE.LineCurve(new THREE.Vector2(5, 0), new THREE.Vector2(5, 5)));
path.add(new THREE.LineCurve(new THREE.Vector2(5, 5), new THREE.Vector2(0, 5)));
path.add(new THREE.LineCurve(new THREE.Vector2(0, 5), new THREE.Vector2(0, 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);
LineCurve
的进阶应用让一个物体沿直线段移动:
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, 0);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
LineCurve
生成三维几何体可以将 LineCurve
投影到三维空间,通过结合 TubeGeometry
或自定义形状生成 3D 模型。
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);
Shape
结合绘制封闭形状将多个 LineCurve
添加到 Shape
中,可以创建填充的几何体:
const shape = new THREE.Shape();
shape.moveTo(0, 0);
shape.lineTo(5, 0);
shape.lineTo(5, 5);
shape.lineTo(0, 5);
shape.lineTo(0, 0);
const geometry = new THREE.ShapeGeometry(shape);
const material = new THREE.MeshBasicMaterial({ color: 0x00ffff, side: THREE.DoubleSide });
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
结合 PointsMaterial
,使用 LineCurve
生成点云效果:
const points = curve.getPoints(50);
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.PointsMaterial({ size: 0.1, color: 0xff9900 });
const pointsMesh = new THREE.Points(geometry, material);
scene.add(pointsMesh);
当绘制大规模路径时,可以控制 getPoints
中的 divisions
数量以优化性能:
const points = curve.getPoints(5); // 较少的插值点
LineCurve
支持序列化和反序列化,便于保存和动态加载路径:
const json = curve.toJSON();
// 存储 JSON 到文件或服务器
const restoredCurve = new THREE.LineCurve().fromJSON(json);
LineCurve
是 Three.js 中最简单但非常重要的曲线类型之一。它在路径绘制、动画轨迹、几何构造等场景中广泛应用。通过结合其他组件如 CurvePath
和 Shape
,可以构建复杂的图形和动画效果。希望本文能帮助你深入理解和应用 LineCurve
,让你的 Three.js 项目更加灵活和丰富。