Getting rid of items and bringing them back

In my current three.js project, I have implemented a feature that allows users to switch between scenes containing various mesh objects. All objects are loaded simultaneously when the page loads, but the performance is affected by the textures on these objects.

To address this issue, I am looking to dynamically remove all objects from scenes that the user has not selected. For example, if the user selects scene 4 as the currentScene, then scenes 1 through 3 and 5 through 10 should be cleared of unnecessary objects that may impact performance. This action should also occur upon initial load. The objects would only be restored when the user revisits those specific scenes again.

You can view an example of my project with essential code in this FIDDLE. I am currently utilizing the remove() and dispose() methods for this functionality.

//scene to be removed and disposed
var destroyScenes = scenes[1];

//process to remove and dispose all objects in the specified scene

for (i = destroyScenes.children.length - 1; i >= 0; i--) {
            obj = destroyScenes.children[i];
            destroyScenes.remove(obj);
            obj.geometry.dispose();
            obj.material.dispose();
            obj.texture.
    }

My main question revolves around restoring these removed objects once they have been disposed of. Is this method appropriate for achieving this goal, and are there better alternatives available?

Furthermore, how can I target all scenes except for the currentScene? Any insights or suggestions on optimizing this process would be greatly appreciated.

PS: I am aware that switching between scenes may result in some loading time, but I prioritize performance optimization in this scenario.

Answer №1

What is the best method for restoring objects after they have been removed and disposed of? - If you properly dispose of an object, reloading it can be cumbersome.

Is there a more efficient approach to this process, and if so, what is it? - Hiding objects with obj.visible = false may be preferable to constantly loading and disposing them. This can improve performance, especially for elements like fog that may not be applied correctly upon re-loading.

If hiding objects is recommended, how can I exclude all scenes except for the currentScene? - It may be best to ask a separate question about excluding scenes, possibly by monitoring mouse movement.

I understand that switching between scenes may cause loading delays, but I still prefer this method. Is it feasible? - Although scenes run simultaneously, you can pause animation frames for inactive scenes. However, scenes load during initialization, not when switching between them.

Please only ask one question at a time.

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

Challenge with inserting documents in Mongoose database

I have set up a schema and now I am trying to insert data into a MongoDB collection, but I keep getting an error stating that diagramModel.insert is not defined. Can anyone help me figure out what I did wrong? app.js mongoose.connect('mongodb://lo ...

Observing modifications in the $route object - Vue Router

I am currently working on a small CRM project. Within the login form, I have implemented a component that displays an alert message when the email or password entered is incorrect. Interestingly, even after correcting the information and logging out, the m ...

Remove any elements using jQuery that do not contain any content

I need to remove any empty elements from my HTML code. Here is an example of the HTML markup: The HTML markup is stored in a jQuery variable called 'myhtml' and it may contain various tags. <h1> <u> <strong></st ...

Using the filter() function in jQuery allows for efficient sorting and

My current area of study is in jQuery, but I'm encountering a bit of confusion with the following code: var list = mylist.filter(function(f) { return $(f) .find('.anthing') .length > 0; }); I'm particularly puzz ...

What is the best way to retrieve the src value upon clicking on an image in a JavaScript function with multiple images displayed on

I have 3 images on my website. Each time I click on an image, I want to retrieve the source value. I attempted the code below, but it doesn't seem to work with multiple images. <div class="test"> <a href="#" class="part-one" ...

Exploring the possibilities of infinite scroll in JavaScript using the Backbone framework

I've been grappling with this problem for three days straight. I've been attempting to incorporate scrolling into my backbone project using the https://github.com/paulirish/infinite-scroll plugin. Despite my best efforts to find a solution throu ...

What is the best way to retrieve and save the titles of checked boxes in the Autocomplete using state with hooks?

I've implemented the React Material-UI Autocomplete feature with checkboxes in my project. Here is what I have so far in my code (check out demo.js for details): https://codesandbox.io/s/material-demo-rxbhz?fontsize=14&hidenavigation=1&theme=d ...

What is the proper way to retrieve an object from a json file?

My JSON structure looks like this: { "total": 4367, "page": 1, "per_page": 10, "paging": { "next": "/videos?query=second%20world%20war&per_page=10&access_token=XXX&page=2", "previous": null, "first": "/v ...

Tips for utilizing the Apollo cache consistently during page transitions in NextJs

In the official examples repository of NextJS, I found this apolloClient.js file: import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client' import { concatPagination } from '@apollo/client/utilities' import merge from &apos ...

The browser is preventing a cross origin request in Fetch

Currently, I am utilizing node, express, and react to build a sign-in portal. In the frontend, I have created app.js and signin.js files. The partial code snippet in the signin.js file looks like this: onSubmitSignIn = () => { fetch("http://localhost:3 ...

A guide to displaying a PDF preview using React Dropzone

I am struggling to find a way to display previews of PDF files that I'm uploading using react-dropzone. Although PNG and JPG files are working correctly, I would like to be able to show the user either the actual PDF or an image representation of it. ...

Loop through the HTML and print it repeatedly

I've been using this code to retrieve Json data and display it based on the same id. However, I've noticed that it only prints once and doesn't display all the data as expected. var requiredData; $.getJSON('http://127.0.0.1/appcpanel/ ...

Incorporate an Event into a Vue.js JavaScript string within an HTML file

I am attempting to trigger the event in a JavaScript string. Within the buffer, I have called this event: @click.prevent = "Buy($event)" However, the browser is not interpreting it correctly: https://i.sstatic.net/uHs8W.jpg Here is the code snippet: ...

Different ways to provide user feedback on a SPA website following AJAX requests

I have a single-page application website developed using React.js. What are some options for notifying the user of successful/failed/pending AJAX calls resulting from various user interactions? I am aware of Toastr-style messages that appear in the corner ...

Custom JavaScript files are not recognized by Angular 4 pages unless the page is manually refreshed

I am facing an issue with my custom JavaScript files in Angular. I have imported them all in the angular-cli.json file but the pages do not recognize them unless I refresh the page after navigating from one page to another. Here is a snippet of my angular ...

Utilizing the no-data-text attribute in v-combobox with Vuetify: Tips and tricks

My application includes a simple combobox, and I am trying to set a default text message to display when there are no entries in the items props. To achieve this goal, I utilized the no-data-text prop by passing a specific variable to it. <v-combobox ...

Relocating to reveal or conceal item

I am currently working with jQuery and trying to achieve a specific functionality. My goal is to hide and unhide an element, while also focusing on the exposed area once it becomes visible. I have a link #welcomeselect that, when clicked, should reveal t ...

Invoke cloud functions independently of waiting for a response

Attempting a clever workaround with cloud functions, but struggling to pinpoint the problem. Currently utilizing now.sh for hosting serverless functions and aiming to invoke one function from another. Let's assume there are two functions defined, fet ...

"An issue has been detected in the Entry module, which cannot be located in

My journey into JavaScript is just beginning, as I diligently follow a well-structured node tutorial available on Github. Despite my best efforts in all the modules, I keep encountering an error message whenever I run yarn dev:wds. ERROR in Entry modu ...

Implementing a feature to automatically set the datepicker value to the selected date upon page initialization

I am working with a datepicker element, identified by id="from_dt", and a button with id="fromToDate". When a date is selected from the datepicker and the button is clicked, the page will load. At that point, I want to transfer the selected date to a textb ...