【发布时间】:2019-06-20 10:48:06
【问题描述】:
我有一个组层次结构,A -> B -> C。我希望创建这个层次结构的克隆,A2 -> B2 -> C2。
但是Object3D.clone() 删除了组的父引用。
除了在克隆后为每个子组手动设置父组,还有什么其他方法?
如果层次结构很深,这可能需要计算。
感谢您的帮助!
【问题讨论】:
标签: three.js
我有一个组层次结构,A -> B -> C。我希望创建这个层次结构的克隆,A2 -> B2 -> C2。
但是Object3D.clone() 删除了组的父引用。
除了在克隆后为每个子组手动设置父组,还有什么其他方法?
如果层次结构很深,这可能需要计算。
感谢您的帮助!
【问题讨论】:
标签: three.js
也许你可以看看这个问题Will three.js Object3D.clone() create a deep copy of the geometry?
我将Object3D 中的copy 和clone 方法扩展为深层克隆网格材料。
在你的情况下,这也应该有效。
首先,在三个方面扩展了两个新方法:
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;
}
其次,当你想深度克隆一个名为originalObj的Object3D或场景时,只需执行var newObj = originalObj.deepClone()
【讨论】: