如何将three.js网格合并为一个网格?

时间:2015-08-11 13:09:13

标签: javascript three.js

在Three.js中是否可以合并两个或多个具有不同材质的网格?

我找到的解决方案,仅合并几何图形,或者只将网格放入一个Object3DGroup

1 个答案:

答案 0 :(得分:5)

是:亲切(见问题所附的评论及此答复帖):

var blueMaterial = new THREE.MeshPhongMaterial( {color: 0x0000FF } );
var redMaterial = new THREE.MeshPhongMaterial({ color:0xFF0000 });
var meshFaceMaterial = new THREE.MeshFaceMaterial( [ blueMaterial, redMaterial ] );

var boxGeometry = new THREE.BoxGeometry( 10, 10, 10 );

for ( var face in boxGeometry.faces ) {
    boxGeometry.faces[ face ].materialIndex = 0;
}

var sphereGeometry = new THREE.SphereGeometry( 5, 16, 16 );
sphereGeometry.applyMatrix( new THREE.Matrix4().makeTranslation(0, 5, 0) );

var mergeGeometry = new THREE.Geometry();
mergeGeometry.merge( boxGeometry, boxGeometry.matrix );
mergeGeometry.merge( sphereGeometry, sphereGeometry.matrix, 1 );

var mesh = new THREE.Mesh( mergeGeometry, meshFaceMaterial );
scene.add( mesh );

我选择了一个立方体和一个球体,因为例如一个盒子想要知道每个面孔的材质ID。

http://jsfiddle.net/L0rdzbej/16/