THREE.Line
是 Three.js 中用于创建线段的类。以下是 THREE.Line
的详细说明,包括构造函数参数、输出、方法和属性。
构造函数
new THREE.Line(geometry, material, mode)
- geometry (
THREE.BufferGeometry
或THREE.Geometry
): 定义线段的几何体。 - material (
THREE.Material
): 用于渲染线段的材质。通常使用THREE.LineBasicMaterial
或THREE.LineDashedMaterial
。 - mode (
Number
, 可选): 用于指定线段的绘制模式。可以是THREE.LineSegments
(默认),THREE.LineLoop
,或THREE.LinePieces
。此参数在较新版本的 Three.js 中已弃用。
方法
THREE.Line
类继承了 THREE.Object3D
类的方法,因此它拥有 THREE.Object3D
的所有方法,包括:
- clone(): 克隆该线段对象。
- copy(source): 从另一个线段对象复制属性。
- raycast(raycaster, intersects): 检测射线与线段的交点。
- updateMorphTargets(): 更新变形目标。
此外,THREE.Line
还可以调用与几何体和材质相关的方法,比如:
- computeLineDistances(): 计算线段的距离。这在使用
THREE.LineDashedMaterial
时非常有用。
属性
THREE.Line
类继承了 THREE.Object3D
类的所有属性,因此它具有 THREE.Object3D
的所有属性,例如:
- position: 该对象的位置。
- rotation: 该对象的旋转。
- scale: 该对象的缩放比例。
- visible: 该对象是否可见。
除此之外,THREE.Line
还有以下特有属性:
- geometry (
THREE.BufferGeometry
或THREE.Geometry
): 定义线段的几何体。 - material (
THREE.Material
): 用于渲染线段的材质。
示例代码
import * as THREE from 'three';
// 创建场景
const scene = new THREE.Scene();
// 创建相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 创建几何体
const geometry = new THREE.BufferGeometry();
const vertices = new Float32Array([
-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
-1.0, 1.0, 0.0,
-1.0, -1.0, 0.0
]);
geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
// 创建材质
const material = new THREE.LineBasicMaterial({ color: 0x0000ff });
// 创建线段
const line = new THREE.Line(geometry, material);
// 将线段添加到场景
scene.add(line);
// 渲染循环
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - buffergeometry - lines</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="container"></div>
<div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry - lines</div>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
// 导入three.js库和Stats库
import * as THREE from 'three';
import Stats from 'three/addons/libs/stats.module.js';
// 声明全局变量
let container, stats, clock;
let camera, scene, renderer;
let line;
const segments = 10000; // 线段数量
const r = 800; // 空间范围
let t = 0; // 时间变量
// 初始化场景
init();
animate();
function init() {
// 获取容器
container = document.getElementById( 'container' );
// 初始化相机
camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 4000 );
camera.position.z = 2750;
// 初始化场景
scene = new THREE.Scene();
// 初始化时钟
clock = new THREE.Clock();
// 创建几何体
const geometry = new THREE.BufferGeometry();
const material = new THREE.LineBasicMaterial( { vertexColors: true } );
const positions = [];
const colors = [];
for ( let i = 0; i < segments; i ++ ) {
// 随机生成点的位置
const x = Math.random() * r - r / 2;
const y = Math.random() * r - r / 2;
const z = Math.random() * r - r / 2;
// 添加位置数据
positions.push( x, y, z );
// 根据位置生成颜色
colors.push( ( x / r ) + 0.5 );
colors.push( ( y / r ) + 0.5 );
colors.push( ( z / r ) + 0.5 );
}
// 设置几何体的属性
geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
generateMorphTargets( geometry );
// 计算边界球体
geometry.computeBoundingSphere();
// 创建线条并添加到场景中
line = new THREE.Line( geometry, material );
scene.add( line );
// 初始化渲染器
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
// 将渲染器的DOM元素添加到容器中
container.appendChild( renderer.domElement );
// 初始化性能监视器
stats = new Stats();
container.appendChild( stats.dom );
// 添加窗口调整大小事件监听器
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
// 更新相机和渲染器的尺寸
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
// 动画循环
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
// 获取时间差和经过的时间
const delta = clock.getDelta();
const time = clock.getElapsedTime();
// 更新线条的旋转
line.rotation.x = time * 0.25;
line.rotation.y = time * 0.5;
// 更新morphTarget影响
t += delta * 0.5;
line.morphTargetInfluences[ 0 ] = Math.abs( Math.sin( t ) );
// 渲染场景
renderer.render( scene, camera );
}
function generateMorphTargets( geometry ) {
const data = [];
// 随机生成morphTarget的数据
for ( let i = 0; i < segments; i ++ ) {
const x = Math.random() * r - r / 2;
const y = Math.random() * r - r / 2;
const z = Math.random() * r - r / 2;
data.push( x, y, z );
}
// 创建并命名morphTarget
const morphTarget = new THREE.Float32BufferAttribute( data, 3 );
morphTarget.name = 'target1';
geometry.morphAttributes.position = [ morphTarget ];
}
</script>
</body>
</html>