Rendering the initial frame in Three.js is lagging behind due to extended processing time

I am currently using Three.js with WegGL to render a series of alternating scenes consisting of thousands of image tiles moving in space. The animations are managed by Tween.js and I conduct testing on Chrome.

In order to enhance the loading process of the images, I preload all texture images before the initial scene is displayed. These textures are then stored in memory as THREE.Texture. When setting up a scene for display, I execute the following steps:

let tile = null, tweens = [], cameraZ = 1000;
for (let y =  0; y < rows; y++){
    for (let x =  0; x < columns; x++){
        tile = await this.createTile(x, y, [textureSize]);
        tile.position.x = x * this.tileWidth - this.picWidth / 2;
        tile.position.y = -y * this.tileHeight + this.picHeight / 2;
        tile.position.z = cameraZ * 1.1;

        tweens.push(new TWEEN.Tween(tile.position)
            .to({z: 0}, 4000)
            .delay(200 + x * 120 + Math.random() * 1000)
            .easing(TWEEN.Easing.Cubic.InOut));
        this.scene.add(tile);
    }
}
tweens.map(t => t.start());

The setup of the scene also consists of the camera and a point light, taking approximately 400 ms to finish.

Subsequently, the rendering of the scene is carried out in the following manner:

function render(){
    requestAnimationFrame(render);
    TWEEN.update();
    renderer.render(this.scene, this.camera);
}
render();

Although everything is being displayed correctly, upon measuring processing durations, it was observed that the initial rendering call requires around 1400 ms! Subsequent calls range between 70 to 100 ms.

My ultimate objective is to seamlessly transition between multiple scenes like this without any lag. Given that all necessary assets have been preloaded, what could be causing this issue and how can I optimize it?

Thank you

Answer №1

At the onset of rendering, your assets and shaders undergo compilation and are sent to the GPU for processing. To bypass this process, you can implement some behind-the-scenes strategies. One option is to render each object once after loading by adding it to a single scene and invoking renderer.render on it. The effectiveness of this approach depends on whether shader compilation or asset uploading presents the bottleneck. By pre-rendering objects individually, you can stagger the uploads to the graphics card instead of executing them all at once.

In addition, a previous comment pointed out an error in your render loop.

The correct syntax should be requestAnimationFrame(render);

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

Confirming the information before submitting the form

My submit button is set up like this: <input class="create_button" name="commit" onclick="return validate_activity();" type="submit" value="Save"> I have noticed that this button always sends a request to the server regardless of wh ...

The issue of Elasticsearch results not being correctly parsed as JSON objects by JavaScript when retrieved from a Python client

I am facing an issue with extracting a Javascript object from the Elasticsearch(2.1.1) response received through my Python (2.7) client. Python code: es=Elasticsearch(); @app.route('/output') def findSpots(): lat = request.args.get('la ...

inject spinner during the call and verify status post-call

How can I make the loading icon display during the save function call and then switch to the check mark icon after the function resolves instead of mocking it on a timeout? function save() { successMessage() new Promise((resolve, reject) => { setTim ...

What methods can I use to avoid unnecessary array elements from being generated in Javascript?

When working in PHP, performing the following code: $var = array(); $var[5000] = 1; echo count($var); Will result in the output of 1. However, in JavaScript, missing elements are created. <script type="text/javascript"> var fred = []; f ...

Troubleshooting problems with the CSS code for a progress bar in Gmail

I recently came across a unique progress bar design on Gmail and wanted to implement something similar. I found some code snippets on this webpage: . However, I encountered an issue where the progress bar was only displaying in Internet Explorer but not in ...

One way to achieve the same functionality as onclick in an Ajax call using

I have two JSPs. In the first JSP, I am making an ajax call and receiving data (Ajax body) from the second JSP. However, I am unsure of how to execute jQuery when an argument is present in the function. Everything works fine without an argument. In the ...

Which is the better approach for performance: querying on parent selectors or appending selectors to all children?

I currently have 2 mirror sections within my DOM, one for delivery and another for pickup. Both of these sections contain identical content structures. Here's an example: <div class="main-section"> <div class="description-content"> ...

What is the best way to customize column width in AG-Grid?

I am looking for a way to dynamically set column width in my table. I have provided a stackblitz example which demonstrates that when changing the screen size, only the table border adjusts, but not the column widths. Is there a way to also change the col ...

What is the best way to implement a callback function in JavaScript?

I am looking to update this code to incorporate a callback function. My goal is for the program to first call DynamicLoadScriptForEdit(), followed by calling ExecuteScriptForEdit. Unfortunately, my current implementation is not working as expected. window ...

Having trouble displaying values from nested JSON in a datatable

Response from server : ["{\"CLIENT\":[{\"tranche\":\"1-4\",\"prix\":\"65.96\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"1-4\",\"prix\ ...

Tips for declaring a data variable in Vue with the help of APIs

I'm currently working on a project where I am integrating movie data from an API (TMDb) using Vue Routers. I have a component named 'MovieList' where I am fetching data from the API. Below is the data structure: data() { return { mov ...

Reading the final element in the series with an IF statement

Something strange is happening with my code. I have a variable called racks_value that gets updated based on calculations performed on the page. Despite manually setting racks_value to 2 and confirming it with a console log, after running a series of IF st ...

"Exploring the Passage of Regular Expression Objects in JavaScript: A Deep Dive into how they

Exploring the depths of JavaScript/OOP, I am intrigued by the way regular expression argument parameters are handled in JavaScript. While I have a good understanding of regular expressions themselves, my interest lies in how they are processed by JavaScrip ...

Why aren't the correct error messages being shown when requesting from the network?

Struggling to figure out how to display informative error messages on the client side instead of generic ones? For instance, when attempting to register with an email that is already taken, users should see a message saying "<a href="/cdn-cgi/l/email-pr ...

If the duration is 24 hours, Moment.js will display 2 days

Looking for a way to allow users to input specific timeframes? For example, 1 week or 5 days and 12 hours. I found that using Duration from Moment.js seemed like the best solution. The snippet of code below is currently giving me 2 00:00, indicating 2 day ...

Is it possible to personalize the Facebook like box appearance?

Is there a way to modify the number of feeds and enable auto scroll feature in a Facebook likebox? I've been experimenting with the code snippet below but have hit a roadblock. <div id="fb-root"></div><script>(function(d, s, id) {va ...

What is the best way to initially hide a div using slide toggle and then reveal it upon clicking?

Is there a way to initially hide the div tag and then animate it in a slide toggle effect? I attempted using display:none on my '.accordion_box' followed by show() in slideToggle, but this results in adding the CSS property display: block. My goa ...

How can we send an array using JavaScript and Ajax without relying on JQuery?

When I call a PHP function using Ajax: var ajax = new XMLHttpRequest(); ajax.open("POST", "file.php", true); ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); ajax.onreadystatechange = function(){ if(ajax.readyState == 4 &am ...

What is the best way to set up jest to generate coverage reports for Selenium tests?

I recently made the switch to using jest for testing my JavaScript project after encountering coverage issues with mocha and chai. While my unit tests are providing coverage data, my Selenium tests are not. I've tried various solutions found in outdat ...

Error on node: The type of error encountered is TypeError. It appears to be related to req.query.acct_stmt_id not being

I encountered a puzzling issue where a particular route works flawlessly with an input array of size 10, but throws the error TypeError: req.query.acct_stmt_id.map is not a function when the input array size increases to 100. To run node, I am utilizing - ...