The Three.js animation mixer won't refresh unless it's actively running in the animation loop

Encountering an odd quirk in JavaScript, where a simple AnimationMixer.update(delta) function only works when accompanied by a console.log statement:

// Here's one that works as expected
function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
  // Updating mixers...
  Object.values(sandbox.models).forEach(x => (x.mixer && console.log(x.mixer.update(clock.getDelta()))));
}

// These ones do not work properly
function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
  // Updating mixers...
  Object.values(sandbox.models).forEach(x => (x.mixer && (x.mixer.update(clock.getDelta()))));
}

function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
  // Updating mixers...
  Object.values(sandbox.models).forEach(x => (x.mixer && void(x.mixer.update(clock.getDelta()))));
}

Furthermore, when attempting to play the animation using

AnimationMixer.clipAction(animation).play()
, it doesn't work unless I include the logging statement.

This behavior is quite perplexing - why does the mixer only update when its return value is logged?

I am unable to share all of the code due to its length and dependencies on external files.

Logging excessively is not a viable solution, as it can negatively impact game performance.

Answer №1

It is best practice to refrain from calling Clock.getDelta() multiple times within your animation loop. Doing so can result in delta values that are close to 0, as it calculates the elapsed time between each call to getDelta(). To optimize this, you can structure your code as follows:

function animate() {
  requestAnimationFrame(animate);
  const delta = clock.getDelta();
  renderer.render(scene, camera);
  // Update mixers...
  Object.values(sandbox.models).forEach( x => {
    if (x.mixer) x.mixer.update(delta);
  } );
}

Additionally, try to minimize the use of Object.values() within the loop, as it creates a new array with each invocation leading to unnecessary overhead on garbage collection and performance issues.

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

Implementing dynamic routing to manage the path /a/b-c-d-:e-:f

Here is a sample route: server.get("/something/best-shoes-in-india-:brand-:location", (req, res) => { res.send(JSON.stringify(req.params)) }) For example, if brand name = adidas and location = Delhi: If the URL is => "/something/best-shoes-in- ...

How come this JavaScript isn't triggering a confirmation box to display?

I've inherited a project and I'm currently fixing some bugs. There's a snippet of JavaScript that is supposed to highlight certain boxes and prompt a confirmation box. However, what actually happens is the boxes change color, there's a ...

Angular2 allows for the creation of global variables that can have their values updated throughout the

During the bootstrap process, I added a service to make use of global variables: bootstrap(AppComponent, [ GlobalService ]); global.service.ts import {Http} from 'angular2/http'; import {Observable} from 'rxjs/Observable'; import {O ...

What are the different ways to format a .SQL file in real-time when it is located on an internal website?

In order to upload files to my local server for future reference and training purposes, I am looking for a way to easily format SQL and C# code. I believe there must be a JavaScript library available that can meet these specific needs. My ideal solution ...

Issue with Google Charts JSON data parsing

I'm working on creating a Google chart using my JSON object, but I keep encountering an error when executing var data = new google.visualization.DataTable(Chartdata); Below is my code, any assistance would be greatly appreciated. var Addresses = JS ...

What could be causing the User object in the auth0/nextjs useUser hook to have missing properties?

The user object retrieved using the useUser hook from auth0/nextjs package seems to be missing some important properties: { "nickname": "example", "name": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bedbc6dfd3ced2dbfec7 ...

What could be causing my JavaScript try/catch block to not trigger?

I'm having trouble getting any code within my function to execute unless I completely comment out the try/catch block. Why could this be happening? I've tried adding code before and after the try/catch, as well as within each block, but nothing s ...

Input Error in ASP.NET JavaScript

Within my ASP.NET website, I have implemented an <asp:TextBox /> where the text input is encoded using HttpUtility.HtmlEncode(); Furthermore, the page is equipped with validation controls like <asp:RequiredFieldValidator /> and <asp:CustomV ...

Issue: failure of child element - the provided path is incorrect: "users/[object Object]", when implementing Firebase with Next.js

Having trouble with the identityNumber variable, which is giving an error message stating that a string is required. However, my identity number is already a string. Any assistance would be greatly appreciated. Additionally, the aim is to make the identity ...

What is the best way to create a child process in a separate directory?

Currently, I am developing a browser application using Electron.js that connects to a Flask back-end located in a separate folder within the directory. Right now, I am utilizing a temporary solution to run the back-end: const bat = cp.exec("cd api &am ...

Using JavaScript to dynamically render a variable amount of tiles

Let's say you have between 4 and 6 images or tiles that you need to display on a webpage. Depending on the specific number of tiles, different formatting is required. For example, if you have five images, they need to be arranged in two rows with two ...

Tips on incorporating an external JSON file into a javascript variable for global accessibility

I have been utilizing a JSON file in my project with the following structure: <script src="js/main.js"></script> <script> var data = {"bills":[{"id":1,"name":"DStv","reference":"SmartCard Number","logo":"bill\/logo\/24 ...

What steps can be taken to convert this function into a more concise, dry function?

I'm attempting to customize a typewriter effect on my webpage, and while it successfully displays predefined data, I am struggling with converting it into a function that can receive values and then display those values. I have made attempts to modif ...

Struggling to resolve a common issue in Migration? It seems that a raw query in Sequelize is adding backslashes that are causing errors when inserting values into the database

During migration, data is taken from one table and inserted into another. However, an issue arises when Sequelize adds backslash escape characters to the 'v_occupation' value, causing insertion errors. I have attempted various replacements and m ...

Retrieve a comprehensive roster of currently logged-in Keycloak users using Javascript

Currently, I have successfully retrieved a list of all Keycloak users using JavaScript. However, this list includes all users without considering their online status. Is there a method to specifically fetch a list of users who are currently online (or logg ...

Can I update a label using ajax from the controller?

Hello everyone, I am facing a challenge in changing the text label coming from my Controller's JsonResult. There are two specific issues that I am encountering: 1) I am having difficulty displaying the text sent from my controller onto my view... ...

Concentrating on a Div Element in React

I'm trying to set up an onKeyPress event that will be triggered when a key is pressed while a 'Level' element is displayed. I know that the div needs to be focused for events to register, but I'm not sure how to do this with functional ...

Is it possible to retrieve data from a database using jQuery and store it in separate variables?

I am able to print out one field from a table, but I want to display all fields in separate tables. How can I achieve this? Below is my save/load code: // Save/Load data. $('').ready(function() { if($.cookie('code')) { $.aj ...

Updated the object in an array retrieved from an API by adding a new key-value pair. Attempted to toggle the key value afterwards, only to find that

Essentially, I am retrieving products from an API and including a new key value isAdded in the object within the products array. I utilized a foreach loop to add that key and it was successfully added. Now, when I click the Add to cart button, the product ...

Only the first row of the table is responsive to the jQuery click listener

My issue lies in the fact that the code snippet below only triggers the pop-up box for the first delete button selected. Subsequent buttons do not respond. How can I adjust it so that any delete button clicked will activate the script? <a class="btn bt ...