Три. js - Связь частиц с линиями - Как сделать более эффективным? - PullRequest
0 голосов
/ 18 января 2020

Я играл с Three. js, и я пытался сделать что-то вроде этого https://www.openprocessing.org/sketch/824319 Это было сделано с помощью p5 и составляет 2d.

Я пытался реализовать дополнительный метод в методе класса Boid update() код, с которым я играл, от https://github.com/juanuys/boids, с живой демонстрацией на https://juanuys.com/boids/

Это это моя первая попытка

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?
        }

    })
}

Но она крайне неэффективна, плюс она не работает так, как должна. Он даже удаляет сами Boids :)

У вас есть указания, как сделать код более эффективным?

...