Is there a way to duplicate a GLTF model that has been imported into the Autodesk Viewer?

I encountered an issue while trying to dynamically clone a loaded GLB model and allow the user to position it in the scene. Despite using the model.clone() method, the cloned model ends up appearing at the same position as the original, causing changes in position to affect both models. Moreover, I noticed that both the original and cloned models share the same ID. This made me question whether my understanding of the clone() method is correct, prompting me to consider alternative approaches. I came across the SceneBuilder extension, but extracting the geometry of the GLB model for it remains a challenge.

Here's the code snippet:

class FireExtinguisher extends Autodesk.Viewing.Extension {

    constructor(viewer, options){
        super(viewer, options)
        this.model = null
    }

    load(){

        const modelUrl = './GLTF/fireExtinguisher.glb'
        const loadOptions = {
            keepCurrentModels: true,
            applyRefPoint: true,
        }
        this.viewer.loadModel(modelUrl, loadOptions, this.onSuccessCallback.bind(this), this.onErrorCallback)
        return true
    }

    unload(){
        return false
    }

    onSuccessCallback(model){
        
        this.model = model

        const cloned1 = model.clone()
        cloned1.id = 14

        const clonedPosition1 = { x: 10, y: 0, z: 0 };
        const clonedMatrix1 = new THREE.Matrix4();
        const clonedTranslation1 = new THREE.Matrix4().makeTranslation(clonedPosition1.x, clonedPosition1.y, clonedPosition1.z);
        clonedMatrix1.multiply(clonedTranslation1);
        cloned1.setPlacementTransform(clonedMatrix1);

        this.viewer.impl.addModel(cloned1);
        this.viewer.impl.invalidate(true);


        const cloned2 = model.clone()

        const clonedPosition2 = { x: -10, y: 3, z: 0 };
        const clonedMatrix2 = new THREE.Matrix4()
        const clonedTranslation2 = new THREE.Matrix4().makeTranslation(clonedPosition2.x,clonedPosition2.y,clonedPosition2.z)

        clonedMatrix2.multiply(clonedTranslation2)
        cloned2.setPlacementTransform(clonedMatrix2)

        this.viewer.impl.addModel(cloned2);
        this.viewer.impl.invalidate(true);

        const newModel = this.clone(model)
        console.log(newModel);

    }

    clone(obj){
        var clone = {};
        for(var i in obj) {
            if(typeof(obj[i])=="object" && obj[i] != null)
                clone[i] = this.clone(obj[i]);
            else
                clone[i] = obj[i];
        }
        return clone;
    }
}

I attempted various methods to break the reference between translation matrices and models, but encountered no success. Efforts to sever ties between the original and cloned models led to other errors.

Your assistance is greatly appreciated.

Answer №1

The documentation states that the clone method merely creates a shallow copy that shares all inner state with the original model. Therefore, if you require multiple independent instances in the viewer, this may not be the most effective approach.

If you are dealing with gltf/glb data, you could utilize the standard three.js gltf loader to load these objects (bearing in mind that APS Viewer is based on three.js version R71) and then incorporate the objects into the viewer either as an overlay, or by leveraging the capabilities of the SceneBuilder extension.

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

The issue with the full postback in the updatepanel is triggered by utilizing JavaScript on the button's onclick event within

During my testing, I encountered an issue with buttons inside a repeater within an update panel. When adding asyncpostback triggers for the buttons using <Trigger></Trigger>, an error is generated indicating that the button could not be found. ...

Mixing pipe operators with Angular Observables in an array

In my project, I have an interesting scenario where I am using Observables and piping them to multiple functions as shown below. getOrders(filters: any, page: any = 1, orderBy: string = 'deliver_on', sort: string = 'asc') { const op ...

What is the best way to save the outcomes of several asynchronous $.get calls into an array?

I have a challenge where I need to retrieve data from an API for each item in an array, and then store that data in another array for further processing. However, I suspect the issue lies in the asynchronous nature of the requests, as the data may not be ...

Utilizing Sails.js: Invoking a YouTube service through a controller

I am facing an issue while trying to integrate Youtube Data API with Node.js in Sails.js. The problem lies with the "fs.readFile" function. Upon launching the service, it returns "undefined". Below is the code snippet for YoutubeService : module.exports ...

Looking to increase the resolution of a 512x512 heightmap array of pixels in a png image to 2017x2017 using JavaScript for implementation as a heightmap in Unreal Engine 4?

I have a task of converting a 512 x 512 32-bit RGB PNG image with encoded height values into a 16-bit grayscale PNG representing height values for a height map. Below is the code used for this conversion: The image conversion process utilizes image-js li ...

Using various textures on a single geometry with multiple meshes

In my code, I have a loop that generates multiple Mesh objects with different geometries, as each mesh is associated with one texture. for( var i = 0; i < voxels.length; i++ ){ texture = almacen.textPlaneTexture(voxel.texto,color,voxelSize); materi ...

What is the proper way to utilize the "Long" type when declaring a schema in MongoDB?

Forgive my ignorance, but I am trying to achieve something like this: var mongoose = require('mongoose'); var Long = require("long"); var UserSchema = new mongoose.Schema({ id: Long(), name: String, completed: Long(), ...

The xmlhttp object parameter is not defined. JavaScript AJAX

As a newcomer to javascript and ajax, I am trying my best to understand it all. I followed a tutorial to write this code, but I can't seem to figure out what I did wrong. If you click here, you can see the live version. The error message that Firebug ...

JavaScript tabs function malfunctioning upon page load

I attempted to implement tabs on my website using this example: http://www.w3schools.com/howto/howto_js_tabs.asp Although everything is functioning correctly, the default tab isn't opening as expected. The tab opens, but the header color does not get ...

JavaScript constructor functions may trigger ReSharper warnings for naming convention

When it comes to JavaScript coding, I personally prefer using PascalCase for constructor functions and camelCase for other functions. It seems like my ReSharper settings are aligned with this convention. However, when I write code like the following: func ...

Using jQuery to generate nested lists

I have attempted various solutions for creating nested ul/li elements without success. I am a beginner in JavaScript and struggling with this task. The data is retrieved after the page has loaded, and it looks like this: data = { "key1": ["value1", va ...

Error encountered in TypeScript's Map class

When working with TypeScript, I keep encountering an error message that reads "cannot find name Map." var myMap = new Map(); var keyString = "a string", keyObj = {}, keyFunc = function () {}; // assigning values to keys myMap.set(keyString, "val ...

"Step-by-step guide on associating JSON data with <li> elements using AngularJS

Currently, I am working on creating an application using AngularJS that involves retrieving data from a database and populating a list item with that data. To achieve this, I have written a WebMethod as shown below: [WebMethod] public static string g ...

What is the best way to trigger an event from a child component to a parent component in

parent.component.ts public test(){ //some code..... } parent.component.html <app-child (eventfire)="test($event)"></app-child> In this scenario, the child component button is displayed within the parent component. However, there i ...

Form a bond with the latest SignalR library to initiate communication

After attempting to connect to an asp.net core signalR server using the code below, I encountered some issues. Can you spot where I might have gone wrong? Here is the error message that I received: Error: The "promise" option must be a Promise v ...

Using Emission Material in Three.js Tutorial

Anyone have a sample of how to use the emissive property with meshLambertMaterial and meshPhongMaterial? I'm thinking along the lines of a sphere emitting light like a sun. This example is close to what I want to achieve: http://www.youtube.com/watc ...

Using jQuery to populate an array with selected table items

Issue Description: I am working on an HTML page with two add buttons and two tables. Currently, when the add button is clicked, rows are appended to their respective tables. However, in the backend, I need to gather all row elements when a specific "button ...

Unable to connect to the cloud firestore backend specifically on the deployed version

When deploying the project using Vercel, I included my Firebase security and project details as environment variables on the Vercel dashboard. Authentication works fine during deployment, but there is an error in the console: @firebase/firestore: Firesto ...

Removing a Div with Dynamic Parameters

I'm struggling to implement a feature in my form that allows the user to add multiple entries, but I'm having trouble with the removal aspect. Here is the JavaScript code: var i = 1; var divContent = document.getElementById ...

Some models showcase crisp textures with Thee.js, while others appear hazy (especially custom designs)

As a beginner in the realm of Three.js, I have been following a tutorial on texturing custom geometric shapes. The tutorial used a classic head OBJ file as a test case, which worked perfectly fine. However, when I tried to tweak the script to render my own ...