2

我试图在加载后克隆一个 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 );

            }


        } );

///////更新

好的,我已经找到了针对我的特定问题的解决方案,但是无法弄清楚如何正确克隆具有任何类型子组的 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);
            }

            });
4

0 回答 0