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

前端 未结 4 2085
轮回少年
轮回少年 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:37

    three.js Object3D.clone() will not create a deep copy of geometry and material. It will reference geometry and material of the cloned object instead.

    You can see this in three.module.js:

    Mesh.prototype = ... {
    
        ...,
    
        copy: function ( source ) {
            Object3D.prototype.copy.call( this, source );
    
            ...
    
    >>>>>>> this.material = source.material; <<<<<<<
    >>>>>>> this.geometry = source.geometry; <<<<<<<
    
            return this;
    
        }
    
        ...
    
    }
    

    So, regarding the OP's question: Yes, you can just use dae.clone().

    For a deep clone including geometry and material, just change two lines of the above Mesh.prototype.copy() function:

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

    Keep in mind this is a performance issue because three.js has to calculate separate geometries for each object3D, including all Meshes.

提交回复
热议问题