Using Three JS to recycle the geometry of a previously imported object

Is there a way to efficiently re-use imported object geometries in Three JS without duplicating them in memory? I've tried writing a loader but it doesn't seem to update the geometry once loaded.

var tmpGeo = geometries[ID];
if (!tmpGeo) {
    tmpGeo = new THREE.BufferGeometry();
    geometries[ID] = tmpGeo;
    objLoader.load("/models/" + ID + ".obj", function (mesh) {
        tmpGeo = mesh.children[0].geometry;
    });
}
obj.add(new THREE.Mesh(tmpGeo, tmpMat));

I want to be able to import an object only once and then re-use its geometry across multiple objects. Any suggestions on how to achieve this?

Answer №1

It's important to keep in mind that the model is loaded asynchronously, so when you're generating the mesh without waiting for the model to finish loading, you are essentially updating the empty BufferGeometry with new data. To avoid this issue, consider implementing the following solution:

var geometry = geometries[ID];
if (!geometry) {
  geometry = new THREE.BufferGeometry();
  geometries[ID] = geometry;
  objLoader.load("/models/" + ID + ".obj", function (mesh) {
      // Update the existing BufferGeometry using loaded data.
      geometry.copy( mesh.children[0].geometry );
  });
}
obj.add(new THREE.Mesh(geometry, material));

Alternatively, you can postpone creating the mesh until the geometry has finished loading.

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 AdminLTE Bootstrap Table Search Bar vanishes when extra <th> tags are added

Currently facing difficulties with the table functionality in AdminLTE. By default, AdminLTE includes a search and organization feature in its table structure. When I insert some table data using PHP, everything looks fine. However, when I attempt to add ...

Display <tr> tag when button is clicked without refreshing the page

I have a specific requirement to hide a certain tag initially. If the user clicks the forward button without selecting any radio buttons or checkboxes, the tag should be displayed and the page should not refresh. However, there seems to be an issue with ...

Creating a tooltip using JQuery for a text input field - a complete guide

Is there a way to create a tooltip for a textbox? I'm looking to achieve something similar to the image below: I would like the tooltip to be located near the right of the textbox, with some added animation. Any ideas or suggestions are welcome. ...

What is the best way to manage the "checked" state of an input checkbox using React?

I'm currently developing an application that features a form with radio buttons. One of the radio button options can be toggled on/off using a checkbox, but I want to ensure that the checkbox is disabled if the corresponding radio button is not selec ...

There was an issue with the RouteHandler function at line 21 in the ./.cache/root.js

Attempting to create a Gatsby project without using the CLI to gain insight into how the CLI-generated components work. However, encountering the following runtime error popup in the browser: Unhandled Runtime Error One unhandled runtime error has been de ...

Assistance required for activating an unidentified function or plugin within a Chrome extension

I am currently working on a project involving a chrome extension designed to automate tasks on a specific website. My main focus right now is trying to trigger the click event of a link that has an event handler set up through an anonymous function as show ...

Utilizing the array result obtained from the fetch function

Seeking assistance with a coding issue. I am puzzled as to why I am unable to utilize the data returned from an API call outside of its function, even though there are no errors occurring. The fetchUser function successfully retrieves the data from the API ...

Interacting with MongoDB in a React application using Redux and Redux Thunk

Welcome to the Mock Library website, where you can view books (author title genre) from the database and even add a new book. After checking, it appears that the data is properly fetched from my react component. It is then passed on correctly to the actio ...

Associative TypeScript Arrays

I'm attempting to organize reservations based on business ID in order to achieve a specific end result. Here is the desired output: [ [businessID1] => [Object1,Object2, Object3], [businessID2] => [Object1,Object2], [businessID3] => [Obje ...

Utilizing Mantine dropzone in conjunction with React Hook Form within a Javascript environment

Can Mantine dropzone be used with React hook form in JavaScript? I am currently working on a modal Upload using Tailwind components like this import { useForm } from 'react-hook-form'; import { Group, Text, useMantineTheme } from '@mantine/c ...

Guide on integrating an HTML and CSS register form in Django

I have successfully created a responsive register and login using HTML and CSS. Instead of utilizing the standard register form and login provided by Django upon configuration, I want to apply my own custom template. To summarize, while I am familiar with ...

Unable to retrieve the regex value with an alternate label

Here is a RegExp expression that I am currently using: const regex = new RegExp('<(.*)>' + text + '<(.*)>'); renderer.setProperty(node, 'innerHTML', node.innerHTML.replace(regex, '<$1>' + replaceTe ...

What could be the reason for the lack of error handling in the asynchronous function?

const promiseAllAsyncAwait = async function() { if (!arguments.length) { return null; } let args = arguments; if (args.length === 1 && Array.isArray(args[0])) { args = args[0]; } const total = args.length; const result = []; for (le ...

What is the best way to connect the imagemap shape with the checkbox?

I need assistance with synchronizing an imagemap collection of shapes and checkboxes. How can I ensure that clicking on a shape selects the corresponding checkbox, and vice versa? Imagemap: <div class="map_container"> <%= image_tag("maps/main ...

I am facing an issue with properly linking my jQuery

After searching through numerous posts on this website, I have yet to find a solution to my problem. My issue involves trying to implement a simple jQuery function that is not functioning as expected. It appears that the jQuery link may not be properly set ...

JavaScript/Typescript is throwing an error because it is unable to access the property 'username' of an undefined object

In my project, I am attempting to compile a list of Profile objects and then extract specific elements from each object in the list. To accomplish this, I have defined a public interface named Profile, imported it into my component, and instantiated a new ...

PHP form functioning correctly on one domain but not on the other

After successfully developing my client's website on my test domain and resolving any issues with the help of the stackoverflow community, I uploaded it to my client's domain. Surprisingly, the form stopped working once it was transferred, despit ...

Performing an AJAX request to a form within a CSS modal

Greetings from France, and please excuse any language errors in my English. I am currently coding in Symfony 3 and have an entity called "book" which can have different attributes (client, student, organizer, and/or speaker) based on the selected "type" a ...

Retrieve the div element from the webpage

I've scoured the internet in search of a suitable technique, but unfortunately, I haven't had much luck. Essentially, I am interested in using JavaScript or jQuery (potentially with Ajax) to extract a specific word from a div on a page within my ...

Using Javascript to dynamically enable or disable an input field depending on the choice made in another field

I attempted to implement the solution provided in this answer for my code: How do I disable input field when certain select list value is picked but unfortunately, it is not working as expected. The scenario involves an HTML select field with the ID &apos ...