What is the process for duplicating a group containing loaded .obj models?

Initially, I created a new THREE.Object3D() and named it groupChair. I then loaded 3 obj files and added them to groupChair within the callback function. After that, I added the groupChair to the scene and it worked perfectly. However, I encountered an issue when trying to clone the groupChair object.

Below is the code snippet:

        var groupChair = new THREE.Object3D();
        var cloneChair;
        var cushion;
        var backrest;
        var frame;

        var loader = new THREE.OBJLoader( manager );

        // Loading the first obj file
        loader.load('model-stuff/chair/obj/cushion.obj', function( object ) {
            object.traverse( function ( child ){
                if ( child instanceof THREE.Mesh ) {
                    child.material = new THREE.MeshStandardMaterial({
                        map: textureColorForCushion, 
                        roughness: 3,
                        metalness:0.6,
                        blending: THREE.NormalBlending,
                        shading: THREE.SmoothShading,
                        envMap: textureCube,
                    });
                    child.receiveShadow = true;
                    child.castShadow = true;
                }
            });
            cushion = object;
            groupChair.add( cushion );
        }, onProgress, onError);

        // Loading the second obj file
        loader.load('model-stuff/chair/obj/backrest.obj', function( object ) {
            object.traverse( function ( child ){
                if ( child instanceof THREE.Mesh ) {
                    child.material = new THREE.MeshStandardMaterial({
                        map: textureColorForCushion, 
                        roughness: 3,
                        metalness:0.6,
                        blending: THREE.NormalBlending,
                        shading: THREE.SmoothShading,
                        envMap: textureCube,
                    });
                    child.receiveShadow = true;
                    child.castShadow = true;
                }
            });
            backrest = object;
            groupChair.add( backrest );
        }, onProgress, onError);

        // Loading the third obj file
        loader.load('model-stuff/chair/obj/frame.obj', function( object ) {
            object.traverse( function ( child ){
                if ( child instanceof THREE.Mesh ) {
                    child.material = new THREE.MeshStandardMaterial({
                        map: textureColorForFrame, 
                        roughness: 0.9,
                        metalness:0.4,
                        blending: THREE.NormalBlending,
                        shading: THREE.SmoothShading,
                        envMap: textureCube,
                    });
                    child.receiveShadow = true;
                    child.castShadow = true;
                }
            });
            frame = object;
            groupChair.add( frame );
        }, onProgress, onError);

        scene.add( groupChair );

        // Attempting to clone the groupChair object, but faced an issue
        cloneChair = groupChair.clone();
        cloneChair.position.set(30,0,0);
        scene.add( cloneChair );

Answer №1

When dealing with the code example, it is essential to clone the groupChair object only after ensuring that all objects are fully loaded. To achieve this, the THREE.LoadingManager can be utilized effectively. Moreover, it is crucial to incorporate a separate loader for each object being loaded.

    var groupChair = new THREE.Object3D();
    var cloneChair;
    var cushion;
    var backrest;
    var frame;

    var loader1 = new THREE.OBJLoader( manager );

    //load the 1st obj file
    loader1.load('model-stuff/chair/obj/cushion.obj', function( object ) {
        object.traverse( function ( child ){
            if ( child instanceof THREE.Mesh ) {
                child.material = new THREE.MeshStandardMaterial({
                    map: textureColorForCushion, 
                    roughness: 3,
                    metalness:0.6,
                    blending: THREE.NormalBlending,
                    shading: THREE.SmoothShading,
                    envMap: textureCube,
                });
                child.receiveShadow = true;
                child.castShadow = true;
            }
        });
        cushion = object;
        groupChair.add( cushion );
    }, onProgress, onError);

    var loader2 = new THREE.OBJLoader( manager );
    //load the 2nd obj file
    loader2.load('model-stuff/chair/obj/backrest.obj', function( object ) {
        object.traverse( function ( child ){
            if ( child instanceof THREE.Mesh ) {
                child.material = new THREE.MeshStandardMaterial({
                    map: textureColorForCushion, 
                    roughness: 3,
                    metalness:0.6,
                    blending: THREE.NormalBlending,
                    shading: THREE.SmoothShading,
                    envMap: textureCube,
                });
                child.receiveShadow = true;
                child.castShadow = true;
            }
        });
        backrest = object;
        groupChair.add( backrest );
    }, onProgress, onError);

    var loader3 = new THREE.OBJLoader( manager );
    //load the 3th obj file
    loader3.load('model-stuff/chair/obj/frame.obj', function( object ) {
        object.traverse( function ( child ){
            if ( child instanceof THREE.Mesh ) {
                child.material = new THREE.MeshStandardMaterial({
                    map: textureColorForFrame, 
                    roughness: 0.9,
                    metalness:0.4,
                    blending: THREE.NormalBlending,
                    shading: THREE.SmoothShading,
                    envMap: textureCube,
                });
                child.receiveShadow = true;
                child.castShadow = true;
            }
        });
        frame = object;
        groupChair.add( frame );
    }, onProgress, onError);

    scene.add( groupChair );

    manager.onLoad = function(){

        //clone groupChair object, but it doesn't work
        cloneChair = groupChair.clone();
        cloneChair.position.set(30,0,0);
        scene.add( cloneChair );

    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with Three.js: GLTF model not positioned correctly at origin point

After attempting to load a glTF model with a 0,0,0 position, I noticed that it appears far from the origin. Upon trying to rotate the glTF model, I observed that it spins around (indicated by blue dots) the origin instead of spinning from its center. Thi ...

Struggling to make the switch operational

I'm struggling to make this switch statement work. I want to have multiple conditions like in an if-else statement, but I can't seem to figure out how to add more than two conditions. function spriteAI1() { var posX = c2Sprite.p ...

Use Angular and JavaScript to fetch HTML from a mySQL database and dynamically render it on a webpage

I'm currently utilizing Angular for the front end and Node for the back end. The data is retrieved from a MySql database where it's manually stored in text format with HTML tags. For example: <ul> <li>item1</li> <li> ...

Switch between class and slider photos

I am currently having an issue toggling the class. Here is the code I am working with: http://jsfiddle.net/h1x52v5b/2/ The problem arises when trying to remove a class from the first child of Ul. I initially set it up like this: var len=titles.length, ...

"NODEJS: Exploring the Concept of Key-Value Pairs in Object

I am facing a challenge with accessing nested key/value pairs in an object received through a webhook. The object in req.body looks like this: {"appId":"7HPEPVBTZGDCP","merchants":{"6RDH804A896K1":[{"objectId&qu ...

Error: The function res.json is not recognized. Despite searching through related inquiries, I cannot find a solution to my specific issue

Struggling to find a solution and avoiding repetitive questions, I am facing an issue with my bug tracker. After submitting the form and sending it to the server side, the bug is created in the database. However, when I save the bug using await bug.save() ...

Error: Node-Sass - Unable to download win32-x64-57_binding.node

Currently in the process of getting a retired colleague's program up and running, but when attempting to execute meteor run I encounter this error. While loading package materialize:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" dat ...

Disable Chrome's suggestions bar on your Android phone

I'm having trouble disabling spelling suggestions for an input box and everything I've tried so far hasn't worked. I've already used attributes like autocomplete="off", autocapitalize="off", and spellcheck="off" for the input field, bu ...

Leverage the power of puppeteer alongside webpack

Struggling to make puppeteer work with webpack? Despite adding it to package.json and configuring webpack.dev, the 'launch' function still throws errors. Here's what I've tried: Installed dependency in package.json: npm i puppeteer In ...

Developing a bespoke React component library - encountering an issue with 'react module not found' during Jest testing, as well as using testing-library

I am in the process of creating a bespoke react component library to be shared across various applications. To build this library, I am utilizing rollup and referencing resources such as this blog post along with others: https://dev.to/alexeagleson/how-to- ...

Unique Javascript Library Focused on AJAX

Looking for a specific JavaScript library that focuses solely on AJAX functionality, such as a basic XMLHttp wrapper. ...

Can you make two columns in CSS that are left floated and maintain their original order?

While the title may not provide much clarity, I am struggling to put into words exactly what I am trying to achieve. I have created a layout in Photoshop which I have shared below to help illustrate my goal. Essentially, I have a blog that displays my sto ...

Creating a custom autocomplete search using Angular's pipes and input

Trying to implement an autocomplete input feature for any field value, I decided to create a custom pipe for this purpose. One challenge I'm facing is how to connect the component displaying my JSON data with the component housing the autocomplete in ...

The database was successfully updated; however, the API encountered a 500 error when trying to access

Currently, I am working on a project that utilizes Javascript alongside Node.js, Express, SuperAgent, and KnexJS for database management with Sqlite3. The issue I am facing is as follows: Upon submitting data for updates through my API route using the PUT ...

The mysterious case of the missing currentUserObj in Angular with rxjs Subject

I've encountered an issue while trying to pass data from my login component to the user-profile component using an rxjs subject. Despite calling the sendUser method in the login component and subscribing to the observable in the user-profile component ...

Error: EPERM -4048 - the system is unable to perform the operation specified

Every time I attempt to install a package using npm install, I encounter an error message (usually referring to a different path). I have attempted running npm cache clean, restarting my computer, and checking for any processes that may be interfering with ...

Is it possible to refrain from using the object variable name in an ng-repeat loop?

When utilizing an ng-repeat directive to loop through an array, the syntax requires ng-repeat="friend in friends". Inside the template, you can then use the interpolation operator like this {{friend.name}}. Is there a way to directly assign properties to ...

What steps can be taken to halt an ongoing AJAX call and initiate a new one with different parameters?

I recently implemented an ajax call using the jQuery.everyTime() function. The setup involves a combo box where users can select different graph names, triggering dynamic calls to an ajax function that returns JSON data and populates a chart in the View ev ...

Navigate to a different webpage while employing sweetalert2 and extracting data using the GET method

Is there a way to use sweetalert2 for redirecting to deleting.php with a specific ID parameter? How can I include this dynamic data in the code snippet below, which is used to display options for editing and purging data from an SQL database? echo' &l ...

Encountering issues with Vue routing while utilizing webpack. The main page is functional, however, subpaths are resulting in

Before implementing webpack, my vue routing was functioning properly. However, I encountered several loader issues and decided to use webpack. After setting up webpack, the main page loads correctly, but all of my routes now result in a 404 error. I have ...