Javascript Three.js-连接粒子和线条-如何使其更有效?
我在玩Three.js,我试着做一些类似的东西,这是用p5做的,是2d的 我试图在Boid类的方法Javascript Three.js-连接粒子和线条-如何使其更有效?,javascript,canvas,three.js,Javascript,Canvas,Three.js,我在玩Three.js,我试着做一些类似的东西,这是用p5做的,是2d的 我试图在Boid类的方法update()中实现一个额外的方法,我正在使用的代码来自,在 这是我第一次尝试 drawConnections(delta, neighbours) { neighbours.forEach(neighbour => { // skip same object if (neighbour.id === this.id) return;
update()
中实现一个额外的方法,我正在使用的代码来自,在
这是我第一次尝试
drawConnections(delta, neighbours) {
neighbours.forEach(neighbour => {
// skip same object
if (neighbour.id === this.id) return;
const distance = neighbour.mesh.position.distanceTo(this.mesh.position)
let range = 50;
if (distance <= range) {
if (!this.connections) {
this.connections = {};
this.connections.material = new THREE.LineBasicMaterial(
{
color: 0xffffff,
linewidth: 3 //? not working?
}
);
}
if (!this.connections[neighbour.id]) {
this.connections[neighbour.id] = {};
this.connections[neighbour.id].geometry = new THREE.Geometry();
this.connections[neighbour.id].geometry.vertices.push(neighbour.mesh.position );
this.connections[neighbour.id].geometry.vertices.push(this.mesh.position);
this.connections[neighbour.id].line = new THREE.Line(this.connections[neighbour.id].geometry, this.connections.material);
this.scene.add(this.connections[neighbour.id].line);
} else {
this.connections[neighbour.id].geometry.vertices[ 0 ].set(neighbour.mesh.position );
this.connections[neighbour.id].geometry.vertices[ 1 ].set(this.mesh.position );
this.connections[neighbour.id].geometry.verticesNeedUpdate = true;
}
} else { // neighbour not in range
this.connections == null; // or hide somehow until back in range?
}
})
}
drawConnections(三角形、相邻){
邻居。forEach(邻居=>{
//跳过同一对象
if(nexter.id==this.id)返回;
const distance=nexter.mesh.position.distanceTo(this.mesh.position)
设量程=50;
如果(距离)将线顶点写入一个大的缓冲区,请尝试在一次绘图调用中渲染它们。@manthrax,谢谢。我会研究的。您可以查看源代码。它似乎非常有效。@Marquizzo,哇。这很正确!谢谢。