Hi all!
I am working on the following application:
This is a globe made up of cells. Each cell is a mesh of 7 vertices (1 center and 6 on the edge). I am using the MeshStandardMaterial
with different color codes. When the user zooms in an update is triggered and a new layer (= smaller cells) will be loaded for a smaller area, not the entire globe. The previous cells will be disposed()
like this.
As you can guess, this is not very effective as I am essentially running,
dataFromServer.map( ( cell ) => {
var geometry = createRegionGeom( cell.verticies );
var material = createRegionMaterial( cell.color );
scene.add(new THREE.Mesh( geometry, material ));
})
render();
where dataFromServer
contains the information for thousands of cells coming from a webworker every time there is an update.
The Problem with that is, that scene.add()
and the subsequent render()
cause a pretty significant lag which I want to avoid.
My Idea is to pre-create, letās say 10000 meshes, at the world center (each mesh with 7 vertices) reusing the same MeshStandardMaterial
in #FFFFFF
and storing their UUIDs in an array.
var material = createRegionMaterial( '#FFFFFF' );
var geometry = createRegionGeom( SomeSevenVerticies );
geometry.verticesNeedUpdate = true;
geometry.dynamic = true;
var arrayOfPrecreatedMeshes = [ ];
for ( var i = 0 ; i < 10000 ; i++ ){
var mesh = new THREE.Mesh( geometry, material );
arrayOfPrecreatedMeshes.push( mesh.uuid );
scene.add( mesh );
}
render();
At this point all the meshes that will ever be displayed are part of the scene.
Question: Could I use this method to update verticies, and also set the color of the mesh as shown below?
dataFromServer( ( cell ) => {
const precreatedCell = scene.getObjectByProperty( 'uuid', arrayOfPrecreatedMeshes[idx] );
for ( var v = 0; v < 7 ; v++ ){
precreatedCell.geometry.vertices[ v ] = cell.verticies[ v ];
}
precreatedCell.material.color.setStyle( cell.color );
});
Or is my idea non-sense and will not improve performance?
Any other ideas or pointers are much appreciated. Thank you.