具有一个几何和不同纹理的多个网格。错误

时间:2012-11-13 21:08:40

标签: javascript geometry textures three.js mesh

我有一个循环,我创建一个具有不同几何的多个网格,因为每个网格都有一个纹理:

var geoCube = new THREE.CubeGeometry(voxelSize, voxelSize, voxelSize);
var geometry = new THREE.Geometry();

for( var i = 0; i < voxels.length; i++ ){
  var voxel = voxels[i];
  var object;
  color = voxel.color; 
  texture = almacen.textPlaneTexture(voxel.texto,color,voxelSize); 
  //Return the texture with a color and a text for each face of the geometry
  material = new THREE.MeshBasicMaterial({ map: texture });                       
  object = new THREE.Mesh(geoCube, material);

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

//Add geometry merged at scene
mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial() );
mesh.geometry.computeFaceNormals();
mesh.geometry.computeVertexNormals();
mesh.geometry.computeTangents();

scene.add( mesh );

但是现在我在javascript代码Three.js中有这个错误

未捕获的TypeError:无法读取未定义的属性“map”

在功能中:

function bufferGuessUVType ( material ) {
}

更新

最后,我删除了合并的解决方案,我可以为所有体素使用独特的几何体。我认为如果我使用合并网格,应用程序会有更好的性能。

2 个答案:

答案 0 :(得分:1)

对于r53 +,代码应该是这样的:

var geoCube = new THREE.CubeGeometry(voxelSize, voxelSize, voxelSize);
var geometry = new THREE.Geometry();
var materials = [];

for( var i = 0; i < voxels.length; i++ ){

  for ( var j = 0; j < geoCube.faces.length; j ++ ) {
    geoCube.faces[ j ].materialIndex = i;
  }

  var object = new THREE.Mesh( geoCube );
  // here I assume you'll me positioning the object.
  THREE.GeometryUtils.merge( geometry, object );

  var voxel = voxels[i];
  var texture = almacen.textPlaneTexture(voxel.texto,voxel.color,voxelSize);
  materials.push( new THREE.MeshBasicMaterial( { map: texture } ) );

}

// Add geometry to scene

var mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
mesh.geometry.computeFaceNormals();
mesh.geometry.computeVertexNormals();

scene.add( mesh );

答案 1 :(得分:0)

某些方法期望材质数组位于网格面材质中。例如:

function getBufferMaterial( object, geometryGroup ) {

    return object.material instanceof THREE.MeshFaceMaterial
        ? object.material.materials[ geometryGroup.materialIndex ]
        : object.material;

};

可以通过在几何体和网格面材料中引用材质数组来处理此问题。因此,创建网格的行看起来像:

mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial(geometry.materials) );

并在循环处理体素:

geometry.materials.push( new THREE.MeshBasicMaterial({ map: texture }));

几何体中的每个面也需要指定一个materialIndex,否则会出现错误。如果你知道每个体素有多少面,那么循环通过geometry.faces []并指定materialIndex可能是一个好方法。