Presenting a fully equipped GLTF model prior to incorporating it into the environment

I've been experimenting with different methods to accomplish this task: I'm looking to load a model and then have the function return it.

var loader = new THREE.GLTFLoader();
loader.crossOrigin = true;

var loadedModel = loader.load('model.gltf', function (data) {
    var object = data.scene;
    return object;
}

Next, I want to call another function and pass loadedModel:

loadToScene(loadedModel);

Additionally, I would like it to load to the scene multiple times if needed. Let's hardcode it for twice in this example:

function loadToScene(model){
model.position.set(0,0,0);
scene.add(model);
model.position.set(0,0,5);
scene.add(model);
}

I struggled to find a way to retain the data.scene once the scope of loader ends. Is there a solution to achieve this?

Answer №1

If you want to execute the loadToScene function after your .gltf file has finished loading, make sure to place it within the callback function:

var loader = new THREE.GLTFLoader();
loader.crossOrigin = true;

loader.load('model.gltf', function (data) {
    var object = data.scene;

    // Ensure that loadToScene function is called *inside* the callback
    // timing of when the loading completes is uncertain
    loadToScene(object);
}

To include the model twice in the scene, you must first clone it. Simply duplicating the same object won't have any effect:

function loadToScene(model){
    // Add original object
    model.position.set(0,0,0);
    scene.add(model);

    // Clone a copy
    var model2 = model.clone(true);
    model2.position.set(0,0,5);
    scene.add(model2);
}

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

Steps for adding a texture to a unique shape using Three.js

I managed to successfully apply a texture to a cube geometry using the following code snippet: const geometry = new THREE.CubeGeometry(10, 10, 10); const meshMaterial = new THREE.MeshPhongMaterial({ transparent: false, map: THREE.ImageUtils.loadTexture(&a ...

Tips for styling text in a textarea for blog or news articles

After developing a simple blog application using php, I have incorporated an html form with the main blog article written within a <textarea>. The functionality works well as the data is stored in a database and can be accessed by other pages. Despi ...

Exploring the TypeScript compiler API to read and make updates to objects is an interesting

I'm delving into the world of the typescript compiler API and it seems like there's something I am overlooking. I am trying to find a way to update a specific object in a .ts file using the compiler API. Current file - some-constant.ts export co ...

Employing regular expressions within Notepad++ to insert whole numbers into img src within table elements

I have a large table filled with numerous items that require individual images to be added. Let's take a look at the code snippet below. <tr><td><img src=".jpg" width=100 height=100/></td></tr> Although I could manually ...

Tips for resizing an image to perfectly fit on a compact HTML5 canvas without sacrificing its quality

I need assistance with my code. I'm trying to draw an image on the canvas while maintaining its quality. const canvas = document.getElementById("canvas"); const context = canvas.getContext("2d"); canvas.width = 360px; canvas.height = 360px; const img ...

Having issues with the jQuery .css function?

I need help with hiding and showing tabs on my webpage based on a successful login. I have tried various methods, such as setting the tab to be hidden by default in the HTML code and then using JavaScript to display it upon login. However, none of these me ...

Retrieve the country code of an IP address using getJSON

I am currently facing an unusual issue. My goal is to extract the country code (like US for United States) from an IP address using free APIs. After some research, I came across ipify for retrieving the IP address (which works fine), and then attempted to ...

Angular URL changes causing template flickering

Currently, I am in the process of developing a small application using Angular, but I am encountering an issue with template flickering. This problem occurs when one template is displayed briefly and then immediately switches to another. In my base templa ...

The ng-View feature appears to be malfunctioning within AngularJS

I'm extremely new to AngularJS and am having trouble with ng-view. Here is the code from AngularFormsApp.js: var angularFormsApp = angular.module('angularFormsApp', ["ngRoute"]); angularFormsApp.config(function ($routeProvider) { $routeP ...

Adjust the clarity of the elements within my HTML document

I have been working on creating a login page that will appear in front of the main webpage. Through my online research, I discovered a technique to blur the main webpage background until the user logs in. Below is the code snippet: HTML: <div id="logi ...

Executing a JavaScript function at a specified interval

I am a beginner in the world of programming and javascript. What I aim to accomplish : Executing a JavaScript function on page load, specifically showVideo(). I would like this function to run for approximately 10 seconds before moving on to the next func ...

Pop-up message on card selection using JavaScript, CSS, and PHP

I have a total of 6 cards displayed in my HTML. Each card, when clicked, should trigger a modal window to pop up (with additional information corresponding to that specific card). After spending a day searching for a solution online, I've come here s ...

Replacing text using regex results in whitespace

How can I eliminate text and spaces? For instance: http://www.exampleweb.com/myprogram.rar http://www.examplebackup.com/mybackups.rar http://www.exampleweb.com/myprogram1.rar I have used something similar to remove the second line: http://www.exampleba ...

Tips for avoiding deleting content within a span element when using contenteditable

I am working on an HTML 5 editor that utilizes the contenteditable tag. Inside this tag, I have a span. The issue arises when all text is removed as the span also gets removed. I want to prevent the removal of the span, how can I achieve this? Here is a s ...

Creating a rotating phone effect on scroll using Three.js and react-three/fiber

I have crafted this impressive threejs model using react-three/fiber https://i.sstatic.net/QSgRNfCn.gif https://i.sstatic.net/LrKYEFdr.png Visit this link for the model code Check out the live model here import React, { Suspense, useRef } from "re ...

What is a method to omit elements within a nested child element from a selection without relying on the children() function

Here is an example of an element: <div id="foo"> <a href="#" class="some">click me</a> <div id="bar"> <a href="#" class="some">click me too</a> </div> </div> I am facing the challenge of selectin ...

Error: The 'replace' property of null cannot be read in select2

In my Node Express app, I am incorporating select2, and encountering an error when supplying an array as the data source with data: dataBase. The issue arises as Uncaught TypeError: Cannot read property 'replace' of null. Although using an ajax ...

Using the $ sign to choose a subdocument in mongoose

Here is the structure of my document: "event": { "attendants": { "seekers": [ { "$oid": "5bcdabd27e51de001576d289" }, { "$oid": "5bc9b39c1a48dd0d7d521924" } ...

Fiddle demonstrates HTML functionality, while local testing is unsuccessful

Currently, I am in the process of creating an image slider and attempting to run it on my personal computer. However, upon doing so, I have encountered a problem where the page is not rendering correctly. Additionally, I receive an ActiveX warning message ...

Guide for implementing async/await in conjunction with the eval() function within JavaScript

I'm currently using the eval function to evaluate strings and adding await to it to ensure all values are obtained, but unfortunately the await is not functioning correctly. Here is a snippet of my code: if (matchCard.card.status != "notstarted& ...