Three.js Object3D.clone() 会创建几何的深层副本吗? [英] Will three.js Object3D.clone() create a deep copy of the geometry?

查看:27
本文介绍了Three.js Object3D.clone() 会创建几何的深层副本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 collada 加载器加载模型.加载程序返回一个 Object3D,dae",带有许多子网格.我想多次实例化父dae"对象而不复制网格.我可以只使用 dae.clone() 吗?

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 without duplicating the meshes. Can I just use dae.clone()?

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

Put another way: I'd like to make shallow copies that all have their own transformation matrix but share the same geometry. What's the most efficient way to do this?

推荐答案

默认情况下 Object3D.clone() 确实会创建一个深层副本.我们来看看源码

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;

}

正如我们所见,clone 函数接受两个可选参数:

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

  1. Object3D 克隆到的对象.
  2. 用于指示是否递归克隆子项的标志.

所以是的,可以对 Object3D.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).

我相信你实际上可以使用 Object3D.clone() 的默认行为来获得你想要的.Mesh.clone() 不会克隆 GeometryMaterial 属性.

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;

};

这篇关于Three.js Object3D.clone() 会创建几何的深层副本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆