The efficiency decreases while attempting to completely reset the scene using Three.js

In my current project, I am utilizing Threejs to construct a level with various meshes. All the visual components such as camera, scene, projector, renderer, etc., are encapsulated within one object. For testing purposes, I need to reset the entire scene with different parameters, like changing the size of the level.

Because I need to accurately measure the time taken by an algorithm, I require a complete reset. Currently, my approach involves deleting the div-box that contains the scene/canvas and removing the object containing the Threejs code. Then, I create a new object for the graphical level. However, repeating this process multiple times in succession leads to significant performance degradation.

I have attempted other methods like removing all meshes in the scene using scene.delete() or deleting elements like the scene and renderer before getting rid of the main object. Despite these efforts, performance issues persist.

How can I successfully achieve a complete reset of all WebGL components without sacrificing performance?

Thank you in advance.

Answer №1

Simply deleting all references to three won't fix the issue at hand. When you delete the WebGLRenderer, the WebGL context it holds onto is not released, leading to multiple WebGL contexts running concurrently. This will result in degraded performance as more live contexts are created, eventually reaching a limit.

For a workaround on releasing the context, check out this helpful question.

Three.js does not officially support releasing the context because ideally, there should be no need to recreate it. In my experience working with Angular and different application phases involving WebGL usage, I store an instance of the renderer in the page-level controller to avoid recreating the WebGLRenderer or the context when switching between controllers.

Answer №2

If you're looking to boost your performance, consider utilizing these two functions to reset: iterate through each object labeled obj within your scene and experiment with the following:

scene.remove( obj );
renderer.deallocateObject( obj );

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 combination of Spring Boot and Angular's routeProvider is a powerful

I have been working on a project using Spring Boot, REST, and AngularJS. While I successfully implemented the back end with REST, this is my first time using AngularJS. Despite watching numerous tutorials and following them step by step, I am still facing ...

`How can I update a textbox's value using JQuery Ajax?`

Currently in the process of implementing an ajax call to update the value of a textbox: <div id="PNIncrementDiv" style="position:absolute; left: 730px; top: 15px; width: 350px;" class=""> <input id="TBIncrementTextBox" class="textbox" type="te ...

Error: Next.js is throwing a SyntaxError due to encountering an unexpected token 'export'

I encountered an issue when trying to render the following code: SyntaxError: Unexpected token 'export' (project path)/node_modules/react-syntax-highlighter/dist/esm/styles/prism/index.js Everything seems to work as expected initially, but then ...

The outcome of comparing with Bcrypt is consistently a negative result

bcrypt.compare() is consistently returning false when used in conjunction with this code within the user model. It appears that this issue is specific to bcrypt-nodejs. User.pre('save', function (callback) { this.password = bcrypt.hashSync(thi ...

Having troubles with the xml2json jQuery plugin's functionality?

I've implemented the jQuery XML to JSON Plugin by Fyneworks.com. Any idea why my code isn't functioning as expected? index.html <!DOCTYPE html> <html> <head> <script src="build/jquery.xml2json.js"></script> <sc ...

Can Javascript templates help improve server performance compared to PHP templates?

I'm in the process of developing a website that heavily relies on client-side technologies like Symfony2, Backbone.js, and Underscore. Symfony2 is used for the backend, while Backbone.js powers the frontend. In Symfony2, Twig templates are used and co ...

Firefox browser does not display flashing titles for tabs

Every second, I want to display "New message..." in the title when the browser tab is inactive or the user is in another tab. Here's the code I used: <script> var mytimer; function log() { document.title = document.title == "" ...

Add and condense sub-row

I am struggling to make the child row collapse and append when clicking on the parent row. It seems like my code is not working properly. Can anyone offer some guidance or assistance? This is the code for the parent row: $j(document).ready(function(){ $ ...

Observing an item with a numerical identifier in Vue

Is there a method to monitor a specific value within an object that uses a numeric key in Vue2? If the key were a standard string like obj = {keyName: []}, I am aware that you can achieve this by: watch: { 'obj.keyName'() { //Handle arra ...

Transforming a decimal number into binary base 2 manually in Javascript without using pre-defined functions

As a newcomer to coding and Javascript, I have been given an assignment that requires me to convert base 10 numbers to binary without using specific built-in methods in Javascript (such as alert(a.toString(16))). The constraints are that I can only use loo ...

attempting to merge two arrays into a single union

I am looking for a way to create a new array that contains only unique values from both the first and second arrays, maintaining their original order. This is my current approach: function union(first, second) { return first.filter(function(value) { ret ...

Firebase Storage Metadata Disappearing Act

Despite trying to add custom metadata to my image in Firebase, it doesn't seem to be accepting it. I expect to see my metadata listed under 'Other Metadata' after I upload the image. Currently, this is how it appears for me: https://i.sstat ...

Folding without extending

My button has a div inside it with content. I've set it up so that when the div is clicked, the collapsed content expands. The hover effect changes color and pointer as expected. But for some reason, clicking on the div doesn't expand the content ...

The CSS and JS codes are not successfully integrating into the webpage

I am encountering an issue with loading CSS and JS files onto my page. My project involves PHP and Xampp. The file structure is as follows: My Site - CSS - index.css - JS - index.js - Index.php (Apologies for the lack of a folder tre ...

Struggling to integrate an audio player specifically for mp3 files and feeling quite perplexed by the process

I'm struggling with implementing an audio player for a file sharing platform, specifically only for .mp3 files. I have successfully implemented a function for images (.jpeg and others), but I am unsure how to do the same for mp3 files. function is_au ...

Sending the id as a prop in react-router-dom

Is it possible to pass an ID in props to a React component using react-router-dom? Take a look at my app.js file below: <Switch location={this.props.location}> <Route exact path="/" component={Home} /> <Route path= ...

Generating dynamic input field values using jQuery in a CodeIgniter PHP framework application

I am facing an issue where I am unable to display the value from a dynamically created input field in the page controller. Below is the jQuery code used to append the dynamic input fields: var tableRow='<tr>'; tableRow+=' ...

What is the best way to connect a JavaScript file to an HTML file in an Express app?

I have my NodeJS server set up with Express, utilizing Handlebars as the rendering engine. app.use(express.static(publicDirPath)) (...) app.engine("hbs",hbs({ extname: "hbs", defaultView: "main", layoutsDir: path.join(srcDirP ...

Separate and add on the change validity status of an AngularJS form

If you want to test it out, check this jsFiddle link: HERE (it's recommended to view on a new jsFiddle, refer to the EDIT section of this post) I've noticed what seems like a bug in AngularJS or at least an unexpected behavior. When I detach a f ...

Merge multiple Javascript files into one consolidated file

Organizing Files. /app /components /core /extensions - array.js - string.js /services - logger.js /lib - core.js Core.js (function() { 'use strict'; an ...