Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
three.js Object3D.clone()是否会创建几何体的深度副本?_Three.js - Fatal编程技术网

three.js Object3D.clone()是否会创建几何体的深度副本?

three.js Object3D.clone()是否会创建几何体的深度副本?,three.js,Three.js,我用collada装载机装载模型。加载程序返回一个Object3D“dae”,其中包含多个子网格。我想多次实例化父“dae”对象,而不复制网格。我可以只使用dae.clone()吗 换句话说:我想制作浅拷贝,它们都有自己的变换矩阵,但共享相同的几何体。执行此操作最有效的方法是什么?默认情况下Object3D.clone()会创建深度副本。让我们来看看 下面是我用来深入克隆对象材质的两个函数。你可以根据自己的需要修改它 也考虑这个信息: /**为object3D提供了递归克隆材质的能力(普通克隆不

我用collada装载机装载模型。加载程序返回一个Object3D“dae”,其中包含多个子网格。我想多次实例化父“dae”对象,而不复制网格。我可以只使用dae.clone()吗


换句话说:我想制作浅拷贝,它们都有自己的变换矩阵,但共享相同的几何体。执行此操作最有效的方法是什么?

默认情况下
Object3D.clone()
会创建深度副本。让我们来看看


下面是我用来深入克隆对象材质的两个函数。你可以根据自己的需要修改它

也考虑这个信息:

/**为object3D提供了递归克隆材质的能力(普通克隆不克隆材质)*/
THREE.Object3D.prototype.GdeepCloneMaterials=函数(){
var object=this.clone(新的三个.Object3D(),false);
for(var i=0;i
您可以参考
Object3D
中的
复制
克隆
方法来深度克隆网格材质

首先,在三个方面扩展了两种新方法:

THREE.Object3D.prototype.deepClone = function ( recursive ) {

    return new this.constructor().deepCopy( this, recursive );

},
THREE.Object3D.prototype.deepCopy = function( source, recursive ) {

        if ( recursive === undefined ) recursive = true;

        this.name = source.name;

        this.up.copy( source.up );

        this.position.copy( source.position );
        this.quaternion.copy( source.quaternion );
        this.scale.copy( source.scale );

        this.matrix.copy( source.matrix );
        this.matrixWorld.copy( source.matrixWorld );
        if(source.material){
            //changed
            this.material = source.material.clone()
        }
        if(source.geometry){
            //changed
            this.geometry = source.geometry.clone()
        }
        this.matrixAutoUpdate = source.matrixAutoUpdate;
        this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;

        this.layers.mask = source.layers.mask;
        this.visible = source.visible;

        this.castShadow = source.castShadow;
        this.receiveShadow = source.receiveShadow;

        this.frustumCulled = source.frustumCulled;
        this.renderOrder = source.renderOrder;

        this.userData = JSON.parse( JSON.stringify( source.userData ) );

        if ( recursive === true ) {

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

                var child = source.children[ i ];
                this.add( child.deepClone() ); //changed

            }

        }

        return this;

    }
THREE.Object3D.prototype.deepClone=函数(递归){
返回新的this.constructor().deepCopy(this,递归);
},
THREE.Object3D.prototype.deepCopy=函数(源,递归){
如果(递归===未定义)递归=true;
this.name=source.name;
this.up.copy(source.up);
此.position.copy(source.position);
this.quaternion.copy(source.quaternion);
此.scale.copy(source.scale);
此.matrix.copy(source.matrix);
this.matrixWorld.copy(source.matrixWorld);
如果(来源材料){
//改变
this.material=source.material.clone()
}
if(源.几何体){
//改变
this.geometry=source.geometry.clone()
}
this.matrixAutoUpdate=source.matrixAutoUpdate;
this.matrixWorldNeedsUpdate=source.matrixWorldNeedsUpdate;
this.layers.mask=source.layers.mask;
this.visible=source.visible;
this.castShadow=source.castShadow;
this.receiveShadow=source.receiveShadow;
this.frustumCulled=source.frustumCulled;
this.renderOrder=source.renderOrder;
this.userData=JSON.parse(JSON.stringify(source.userData));
if(递归===true){
for(var i=0;i

第二,当您想要深度克隆名为
originalObj
的Object3D或场景时,只需执行
var newObj=originalObj.deepClone()
three.js
Object3D.clone()
即可创建几何体和材质的深度副本。它将引用克隆对象的几何体和材质

您可以在three.module.js中看到这一点:


请记住,这是一个性能问题,因为three.js必须为每个object3D计算单独的几何体,包括所有网格。

源代码是公开的,为什么不查看?或者你可以创建一个或两个测试并尝试。是的,我看到了它和标志。然而,我仍然不能完全确定这是我正在寻找的答案。孩子们都是孩子。我确实想复制那些。我只是不想复制网格。网格应该由所有实例共享,而对象层次结构应该保持不变。克隆目前不接受任何参数(Three.js release 73)@michael-p如何使用它?将
object.clone()
替换为
object.GdeepCloneMaterials()
将永远不会调用
mesh。GdeepCloneMaterials()
mesh是Object3D的一个子类,因此它将
THREE.Mesh.prototype.clone = function ( object ) {

    if ( object === undefined ) object = new THREE.Mesh( this.geometry, this.material );

    THREE.Object3D.prototype.clone.call( this, object );

    return object;

};
    /** Gives the aptitude for an object3D to clone recursively with its material cloned (normal clone does not clone material)*/

    THREE.Object3D.prototype.GdeepCloneMaterials = function() {
        var object = this.clone( new THREE.Object3D(), false );

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

            var child = this.children[ i ];
            if ( child.GdeepCloneMaterials ) {
                object.add( child.GdeepCloneMaterials() );
            } else {
                object.add( child.clone() );
            }

        }
        return object;
    };

    THREE.Mesh.prototype.GdeepCloneMaterials = function( object, recursive ) {
        if ( object === undefined ) {
            object = new THREE.Mesh( this.geometry, this.material.clone() );
        }

        THREE.Object3D.prototype.GdeepCloneMaterials.call( this, object, recursive );

        return object;
    };
THREE.Object3D.prototype.deepClone = function ( recursive ) {

    return new this.constructor().deepCopy( this, recursive );

},
THREE.Object3D.prototype.deepCopy = function( source, recursive ) {

        if ( recursive === undefined ) recursive = true;

        this.name = source.name;

        this.up.copy( source.up );

        this.position.copy( source.position );
        this.quaternion.copy( source.quaternion );
        this.scale.copy( source.scale );

        this.matrix.copy( source.matrix );
        this.matrixWorld.copy( source.matrixWorld );
        if(source.material){
            //changed
            this.material = source.material.clone()
        }
        if(source.geometry){
            //changed
            this.geometry = source.geometry.clone()
        }
        this.matrixAutoUpdate = source.matrixAutoUpdate;
        this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;

        this.layers.mask = source.layers.mask;
        this.visible = source.visible;

        this.castShadow = source.castShadow;
        this.receiveShadow = source.receiveShadow;

        this.frustumCulled = source.frustumCulled;
        this.renderOrder = source.renderOrder;

        this.userData = JSON.parse( JSON.stringify( source.userData ) );

        if ( recursive === true ) {

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

                var child = source.children[ i ];
                this.add( child.deepClone() ); //changed

            }

        }

        return this;

    }
Mesh.prototype = ... {

    ...,

    copy: function ( source ) {
        Object3D.prototype.copy.call( this, source );

        ...

>>>>>>> this.material = source.material; <<<<<<<
>>>>>>> this.geometry = source.geometry; <<<<<<<

        return this;

    }

    ...

}
this.material = source.material.clone();
this.geometry = source.geometry.clone();