user1582539
user1582539

Reputation: 11

Multiple Voxels with different texture. Performance

I need adding to a same scene geometry multiple voxels (cubes equals) but with different textures each.

I have serious errors in performance by having more than 500 voxels.

This is my code:

texture = crearTextura(voxel.text,color,voxelSize);
material = new THREE.MeshBasicMaterial({ map: texture });       
mesh = new THREE.Mesh(new THREE.CubeGeometry(voxelSize, voxelSize, voxelSize, 1, 1, 1,material),faceMaterial);
scene.add(mesh);

Upvotes: 1

Views: 621

Answers (2)

user1582539
user1582539

Reputation: 11

Finally I've created a geoemtry merge with the all cubes.

Before this change, i had worked the intersection with the mouse click in a cube.

I tryed resolve with this, but it didn't work:

Code to add geometry merge and array with all mesh:

var geometry = new THREE.Geometry();
for( var i = 0; i < voxels.length; i++ ){
  var voxel = voxels[i];
  color = voxel.color; 
  texture = textPlaneTexture(voxel.text,color,voxelSize);
  material = new THREE.MeshBasicMaterial({ map: texture });            
  mesh = new THREE.Mesh(new THREE.CubeGeometry(voxelSize, voxelSize, voxelSize, 1, 1, 1, material));

  mesh.name = voxel.name;
  mesh.position.x = voxel.x * voxelSize + offset_x;
  mesh.position.y = voxel.y * voxelSize + offset_y;
  mesh.position.z = voxel.z * voxelSize + offset_z;

  //  
  objects.push( mesh );

  THREE.GeometryUtils.merge( geometry, mesh );
}

//Add geometry to scene
mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial() );
scene.add( mesh );      
...

Double Click Mouse Event

function onDocumentMouseDoubleClick( event ) {
  if (event.target instanceof HTMLCanvasElement) {
    event.preventDefault();

    mouse2D.x = ( event.clientX / widthChart ) * 2 - 1;
    mouse2D.y = - ( (parseInt(event.clientY) - offset_mouse_y) / heightChart ) * 2 + 1;     

    mouse3D = projector.unprojectVector( mouse2D.clone(), camera );
            ray.direction = mouse3D.subSelf( camera.position ).normalize();
    var intersects = ray.intersectObjects( objects );

    if ( intersects.length > 0 ) {
      if ((intersects[ 0 ].object.name != 'undefined') && (intersects[ 0 ].object.name != '')) {
        //Object clicked
      };
    };      
  };
};

Upvotes: 0

mrdoob
mrdoob

Reputation: 19602

You need to batch all the cubes into a single geometry.

Take a look at this example: http://mrdoob.github.com/three.js/examples/webgl_geometry_minecraft.html

Upvotes: 6

Related Questions