//方式一
const lineGeometry = new THREE.BufferGeometry();
const points = [];
const point = new THREE.Vector3();
const direction = new THREE.Vector3();
for ( let i = 0; i < 50; i ++ ) {
direction.x += Math.random() - 0.5;
direction.y += Math.random() - 0.5;
direction.z += Math.random() - 0.5;
direction.normalize().multiplyScalar( 10 );
point.add( direction );
points.push( point.x, point.y, point.z );
}
lineGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( points, 3 ) );
// 方式二
let geometry = new THREE.BufferGeometry();
let lineMaterial = new THREE.LineBasicMaterial({
color:"#ff0000"
})
let vertices = new Float32Array([
20,20,0,
20,-20,0,
-20,-20,0,
-20,20,0
])
geometry.setAttribute('position',new THREE.BufferAttribute(vertices,3))
// let line = new THREE.Line(geometry,lineMaterial) //连续的线
// let line = new THREE.LineLoop(geometry,lineMaterial)//会自动闭合的线
let line = new THREE.LineSegments(geometry,lineMaterial)//两个为一组的线
scene.add(line)