Will three.js Object3D.clone() create a deep copy of the geometry?

前端 未结 4 2080
轮回少年
轮回少年 2021-01-03 04:12

I\'m loading models using the collada loader. The loader returns an Object3D, \"dae\", with many child meshes. I\'d like to instantiate the parent \"dae\" object many times

4条回答
  •  暖寄归人
    2021-01-03 04:41

    By default Object3D.clone() does create a deep copy. Let's take a look at the source

    clone: function ( object, recursive ) {
    
        if ( object === undefined ) object = new THREE.Object3D();
        if ( recursive === undefined ) recursive = true;
    
        object.name = this.name;
    
        object.up.copy( this.up );
    
        object.position.copy( this.position );
        object.quaternion.copy( this.quaternion );
        object.scale.copy( this.scale );
    
        object.renderDepth = this.renderDepth;
    
        object.rotationAutoUpdate = this.rotationAutoUpdate;
    
        object.matrix.copy( this.matrix );
        object.matrixWorld.copy( this.matrixWorld );
    
        object.matrixAutoUpdate = this.matrixAutoUpdate;
        object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;
    
        object.visible = this.visible;
    
        object.castShadow = this.castShadow;
        object.receiveShadow = this.receiveShadow;
    
        object.frustumCulled = this.frustumCulled;
    
        object.userData = JSON.parse( JSON.stringify( this.userData ) );
    
        if ( recursive === true ) {
    
            for ( var i = 0; i < this.children.length; i ++ ) {
    
                var child = this.children[ i ];
                object.add( child.clone() );
    
            }
    
        }
    
        return object;
    
    }
    

    As we can see the clone function accepts two optional arguments:

    1. the object to clone the Object3D into.
    2. a flag to indicate whether or not to recursively clone the children.

    So yes it is possible to make shallow copies with respect to the Object3D.children, but that's not what you want (based on your comment).

    I believe you can actually use the default behavior of Object3D.clone() to get what you are after. Mesh.clone() does not clone the Geometry and Material properties.

    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;
    
    };
    

提交回复
热议问题