Three.js Object3D.clone() 会创建几何的深层副本吗?

编程入门 行业动态 更新时间:2024-10-24 06:26:09
本文介绍了Three.js Object3D.clone() 会创建几何的深层副本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用 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:

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

所以是的,可以对 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 16:09:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1396268.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:副本   几何   js   Object3D   clone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!