我正在尝试在加载后克隆dae模型。我尝试了几种解决方案,但我认为我的问题是该模型在组内有组。 它有一个包含所有资产的主要父组。这里没有几何。 它的子元素是一个具有几何体的球体和另外4个父组(每个组中没有几何体)。 每组有3个几何形状:2个圆锥和一个圆柱。
我正在使用以下代码,但您可以在链接上使用: http://caostar.com/3d/PABLOwebgl_loader_collada2.html 克隆的对象不保留位置和变换。将网格添加到新对象只是将它们全部放在一起,就像它们都具有相同的矩阵一样。如果你滚动缩放并让相机看到奇怪的物体内部,你会发现球体和锥体都在变形的穹顶内。
关于我如何达到预期的效果,有100个克隆而不是100个变形的3D对象?
这是我正在使用的代码,在整个组中循环并获取其网格。
var dae;
var loader = new THREE.ColladaLoader();
loader.options.convertUpAxis = true;
loader.load( './models/collada/caostar/estrela4.dae', function ( collada ) {
dae = collada.scene;
dae.scale.set(16,16,16);
var piece = collada.scene.children[0];
for (var i = 0; i < 100; i++) {
var newPiece = new THREE.Object3D();
//looping through the groups. If a group has children, loop again
for (var j = 0; j < piece.children.length; j++) {
if(piece.children[j].children.length > 0){
for (var j2 = 0; j2 < piece.children[j].children.length; j2++) {
var newMesh = new THREE.Mesh(piece.children[j].children[j2].geometry, piece.children[j].children[j2].material);
//this applyMatrix doesnt do anything :/
if( piece.children[j].children[j2].geometry.matrix )newMesh.geometry.applyMatrix( piece.children[j].children[j2].geometry.matrix );
newPiece.add(newMesh);
}
}else{
var newMesh = new THREE.Mesh(piece.children[j].geometry, piece.children[j].material);
//this applyMatrix doesnt do anything :/
if(piece.children[j].geometry.matrix)newMesh.geometry.applyMatrix( piece.children[j].geometry.matrix );
newPiece.add(newMesh);
}
}
newPiece.position.set(Math.random()*10-5,Math.random()*10-5,Math.random()*10-5);
//newPiece.scale.set(sizes,sizes,sizes);
newPiece.updateMatrix();
scene.add( newPiece );
}
} );
/////// UPDATE
好的,我找到了针对我的具体问题的解决方案但是,无法弄清楚如何正确克隆任何类型子组的collada。
这是我的代码。希望它可以帮助别人。
var dae;
var loader = new THREE.ColladaLoader();
loader.options.convertUpAxis = true;
loader.load( './models/collada/caostar/estrela4.dae', function ( collada ) {
dae = collada.scene;
dae.scale.set(16,16,16);
var piece = collada.scene.children[0];
//
//I will keep the parent names to avoid meshe being added twice
var parentsNames = [];
for (var i = 0; i < 100; i++) {
var newPiece = new THREE.Object3D();
piece.traverse( function ( child ) {
//check if child has children but exclude the main group. This is custom part and must be addpted for each DAE to be clonned
if(child.children.length > 0 && child.children.length < 5){
//create the group
var newPieceChildren = new THREE.Object3D();
//
parentsNames.push(child.name);
//
child.traverse( function ( child ) {
var newMesh = new THREE.Mesh(child.geometry, child.material);
//this will apply all transformations to the piece
newMesh.applyMatrix( child.matrix)
newPieceChildren.add(newMesh);
});
//this will apply all transformations to the group
newPieceChildren.applyMatrix( child.matrix);
newPiece.add(newPieceChildren);
//se if it is an isolated child with no children and if it has not been included in the subgroups
}else if(child.children.length == 0 && parentsNames.indexOf(child.parent.name) == -1){
var newMesh = new THREE.Mesh(child.geometry, child.material);
//this will apply all transformations to the piece
newMesh.applyMatrix( child.matrix)
newPiece.add(newMesh);
}
} );
//
scene.add(newPiece);
}
});