Cleaning up unwanted objects in THREE.js webGL

Our app utilizes THREE.js to showcase 3D body meshes. We have a special object named MeshViewer that manages the rendering process; within the initialize method, we establish

this.renderer = new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: true })
    

We developed a script to verify that this.renderer is not deallocated.

<script>
        var count = 0;
        function loop () {
            if (count >= 25) { 
                return; 
            }
            else {
                count++;
                var viewer = new MeshViewer(
                    'mesh_viewer',
                    's3_assets/textured_mean_scape_female.obj',
                    []
                );
    
                viewer.cleanup();
    
                  setTimeout(function () {
                    loop();
                 }, 500);
               }
         }
       loop();
     </script>
    

In this scenario, 'mesh_viewer' represents the id of the HTML element where we aim to insert the viewer. Our cleanup procedure assigns

this.renderer = null
    

The cleanup operation functions as expected to prevent an error related to exceeding WebGL context limits when it is executed. The intriguing issue arises when attempting to understand why calling viewer.cleanup just before the setTimeout loop causes failure, while placing cleanup outside and prior to setTimeout ensures success. This dilemma may lean more towards JavaScript intricacies rather than being solely tied to THREE.js or WebGL concepts.

Answer №1

One potential explanation could be the intricate way in which browsers handle the execution of JavaScript code within a single thread.

If you insert viewer.cleanup(); before calling setTimeout, it allows either a browser's internal function or a deferred THREE function to properly release the renderer before executing loop();.

Without placing viewer.cleanup(); before loop(), both functions will be executed one after the other without any delay, potentially causing issues.

Disclaimer: No actual testing has been conducted to verify this theory


As a helpful suggestion: consider encapsulating your THREE.WebGLRenderer instance within a singleton for reusability rather than constant releasing and recreating.

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

Only one bootstrap collapse is visible at a time

Currently, I am using Bootstrap's collapse feature that displays content when clicking on a specific button. However, the issue I am facing is that multiple collapses can be open at the same time. I want to ensure that only one collapse is visible whi ...

Mastering the Art of Content Swapping in SPA

Hey there! I'm in the process of creating a webpage using tornado io and incorporating various graphs. To add some single page app magic, I decided to swap out content within a div like so: <div id="chartType"> Chart goes here</div> <a ...

Utilizing JavaScript for validation on an ASPX page within Visual Studio

After entering an email ID in a textbox, the email validation process is initiated. However, it seems that there is an issue where the entire function may not be executed properly. <head runat="server"> <title></title> ...

Submitting form data including file uploads using AJAX

Currently, the file is being sent via AJAX using the following code: var fd = new FormData(); //additional actions to include files var xhr = new XMLHttpRequest(); xhr.open('POST', '/Upload/' + ID); xhr.send(fd); In ...

Is it possible to create a cylinder in three.js that emits light, giving it the appearance of a tube light?

As a beginner in Blender and three.js, I am eager to find resources that can help me master the art of creating scenes in Blender and then exporting them to three.js. I've noticed that in Blender it's possible to make any mesh emit light - how ca ...

Randomize checkbox selection using Javascript and Bootstrap

I'm in a bit of a bind with my webpage and could really use some help to figure it out. Here's the issue: On my website, there's a table of players. Each player has two checkboxes on their line: one to show if they are active (checked), and ...

Struggling with jQuery and the "hash" functionality?

I am encountering issues with jQTouch. The problem arises when I try to use this link: <a href="#site_map" class="swap">Map</a> and initialize jQTouch like this: var jQT = new $.jQTouch({ icon: 'jqtouch.png', ...

What is the best way to fit a non-square texture onto a geometric plane in Three.js using "background-size:cover"?

I am seeking to replicate the behavior of the "background-size:cover" CSS property for my texture. My approach involves working with UV coordinates. After consulting this answer, I began developing a solution: Three.js Efficiently Mapping UVs to Plane I ...

Browserify does not provide access to the require function in the browser environment

I am currently in the process of developing a web application using node.js along with express. My goal is to leverage Browserify in order to expose my local modules to the browser. Here is the structure of my application: ├── app.js ├── lib ...

React Alert: "Unable to modify while in the middle of a state transition"

Currently, I am working on developing an application that involves utilizing a toastr component. However, I am encountering an error when attempting to dispatch a redux action within this particular component. The error message displayed in the console is ...

Save the webpage source code to a file using JavaScript and Grunt

I am facing an issue and need assistance with my project. I have developed an app using GruntJs and now I need to download the source code of a webpage using GruntJs. For instance, let's assume I have a webpage at: example.com/index.html. What I wou ...

Issue with exporting Three.js to Maya

I've been attempting to utilize the Three.js exporter for Maya found here. However, when I try to load the threeJsFileTranslator.py plug-in from the plug-ins manager in Maya, I encounter an error in the Script Editor: // Error: line 1: invalid synta ...

Tips for utilizing global functions in VUE 2 CLI crowd

I have multiple components that require the same functions. Is there a way to avoid duplicating the code in each component and instead use it globally...? Even if I put the function in the App.vue, it still isn't accessible in the components. ...

Strange symbols keep appearing in my output from PHP

My current task involves generating a SQL query based on some inputs. I have a predefined SQL statement, in which I perform certain replacements, that will use these inputs to generate the required SQL through an ODBC connection. For now, I have stored th ...

Error in parsing JSON: An unexpected token < was encountered at the beginning of the JSON input, causing a SyntaxError at position 0 when parsing with

I need to transfer an array from a PHP file to JavaScript and save it in a JavaScript array. Below is the snippet of JavaScript code: xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 ...

What is the best way to respond to a hashchange event within AngularJs?

I am new to AngularJs and I am trying to update the page on hashchange events. Currently, I have this code which I know is not the proper way to do it: <!DOCTYPE html> <html> <head> <style> #hashdrop { display:block; ...

Is there a method by which I can access information from a linked document in Firebase?

I am relatively new to firebase and I am attempting to retrieve data from a referenced document in another collection to display. Link 1 Link 2 This is how I add a student and the parent's ID: const newStudent = { name: req.body.name, grade: ...

NavigAuth - NativeScript Vue's Innovative Authentication-driven Navigation

After spending hours trying to figure this out, I need to ask for help. How can I create a simple Auth-based Navigation within my App? I have successfully set up a Firebase auth user inside my Vuex using an auth listener. Now, all I want is to display th ...

What happens to the npm package if I transfer ownership of a github repository to a different user?

I was considering transferring a GitHub repository to another user or organization, but I have concerns about what will happen to older versions of the npm package associated with it. Let's say my Node.js package is named node-awesome-package. Versi ...

Dynamic banner featuring animated text, automatically resizing divs based on width while maintaining body width

Currently, I am working on creating a banner with moving text. While I have managed to come up with a solution for implementing this effect, there are two significant issues that I am facing. The width values in pixels need to be manually adjusted based ...