Accessing the current position of skeleton bones in Three.js while an animation is playing

I currently have a Three.js project with an animated scene and I am trying to obtain the positions of skeleton bones at various points in time.

If I visit, for instance, and inspect the Three.js scene:

  const modelViewer = document.querySelector("model-viewer");
  const scene = modelViewer[Object.getOwnPropertySymbols(modelViewer)[14]];

I can then retrieve the position of the LowerArmR bone which is being animated in this example:

scene.children[0].children[0].children[0].children[0].children[1].children[0].skeleton.bones.find(b => b.name == "LowerArmR").position

However, regardless of the animation progress, the retrieved position remains constant:

{x: 1.86264503820865e-10, y: 0.00575238140299916, z: -1.02445485428149e-9}

Is there a way to access real-time skeletal positions during animation?

Upon applying this method to a humanoid avatar undergoing animation, I only see the default T-pose rather than the actual bone positions as they change over time: https://i.sstatic.net/MILfq.png

According to this source:

Skeletal Animations are managed on the GPU, making it challenging to access this data directly through JavaScript code in real-time.

Although potential workarounds may exist, none are widely known or established.

Therefore, I am seeking information on any workaround or standard approach to accomplishing this task.

Answer №1

When it comes to skeletal animation (also known as "skinning"), the individual mesh vertices undergo transformations on the GPU1. This approach is taken because there are typically more vertices than bones, making it more efficient to update them on the GPU rather than the CPU.

In three.js, however, the bone transformations themselves are calculated on the CPU. It's important to understand some key distinctions here:

  1. The .position attribute of a bone, which is inherited from THREE.Object3D's .position attribute, represents a local position relative to its parent and ancestors.
  2. Skeletal animation primarily involves rotating bones rather than translating them. For instance, rotating the shoulder bone will both translate and rotate its descendants, such as the arm.

To determine the world position rather than the local position of a specific bone, you can use the object.getWorldPosition method provided by the Object3D parent class:

const position = new THREE.Vector3();

bone.getWorldPosition( position );

It's worth noting that this method incurs a slight performance overhead as it updates the bone's world matrix by traversing its ancestors before computing the current world position. If you need the positions of multiple bones or other properties like world rotation and scale, it's recommended to ensure all global transforms are up to date once using scene.updateMatrixWorld() if necessary, and then decompose the object's .matrixWorld:

const position = new THREE.Vector3();

position.setFromMatrixPosition( bone.matrixWorld );

1 In case you do require computing the position of a specific vertex on the CPU, the SkinnedMesh class in three.js offers a helpful method for this: .boneTransform().

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

Is React-Apollo encountering a 404 network error?

I am currently exploring React-apollo and attempting to implement Server-side rendering with apollo, but I keep encountering a 404 error. Despite confirming that my graphQL endpoint is functional. Here is the specific error message: { Error: Network erro ...

How do I change the 'CSS Theme' of my website?

With Halloween approaching, I am eager to transform my website's BODY css into a spooky Halloween theme. The challenge is that my .Net pages are Templates. Is there a way for me to ensure that the body class checks for an existing CSS theme override? ...

Shadows in Three.js are malfunctioning

Exploring the realm of shadows in Three.js led me to stumble upon this intriguing example on jsfiddle. Yet, a perplexing mystery presents itself when I attempt to lower the y axis of the light source to 65, as shown here: light.position.set(20, 65, 0); ...

I keep encountering errors with TypeGuard

Looking for some guidance with typescript. Even after implementing a type guard (and including the '?' symbol), I'm still encountering errors in the code snippet below. Is the syntax correct or should I make changes to the tsconfig file? int ...

Issues with the functionality of the shopping cart are causing a disruption

This is the coding for the online shopping cart: <!DOCTYPE html> <html lang="en-us"> <head> <meta charset="UTF-8" /> <title>Online Shopping Cart</title> <script src="jquery-3.1.1.min.js"></script> ...

Suspend features for moving between pages in history

In the application, we have two main pages - Home.vue and Statistics.vue. The Home.vue page renders the TableFields.vue component. On the Home page, there are fields with numbers that have an initial value of "3" set upon page load. An interval running a c ...

I am encountering issues with getting jquery.multiple.select.js to properly validate and send the checked boxes to PHP

I'm having trouble with the validation and output in my PHP mailer using jquery.multiple.select.js v1.1.0. My goal is to allow visitors to select multiple options to request a quote on my site. Currently, I am able to receive the email with all other ...

Updating objects in Angular 8 while excluding the current index: a guide

this.DynamicData = { "items": [ { "item": "example", "description": "example" }, { "item": "aa", "description": "bb" }, ...

The 'id' key is causing issues in React when used for mapping

While working on my simple messaging app, I encountered an issue where updating the same div with a new message was successful, but adding a new div for each message was not. Initially, I tried using index while mapping the messages to display, but it did ...

Issue encountered with the carousel plugin while transitioning to a different page (utilizing Durandal)

I am currently working on a project using the Durandal SPA template, and I have integrated a carousel element into my page: var compositionComplete = function () { $('.testimonials-carousel').carousel({ namespace: "mr-rotato" // ...

Is there a way to store my collection data in a variable and access it externally from the file?

Topic of Interest : Working with Discord.js I am seeking advice on how to save collector data in a variable and access it outside of the file, for example in index.js. I aim to create a setup command that prompts users with questions when they type -setup ...

Attempting to convert the code from react documentation into class components within a react-create-app, but encountering unexpected behavior

Hey there! I'm currently diving into the world of React and challenging myself by rewriting all the code samples from the documentation in a local react-create-app. However, I've hit a roadblock with this section on the Doc, you can find the code ...

Use Javascript to display or conceal events on a fullcalendar interface

I am in the process of creating a religious calendar as part of a project that displays various events from major religions. Using the full calendar, I have included a "religion key" at the top of the calendar to differentiate between different faiths. My ...

What is the purpose of adding "/dist" to the library import statement?

Currently, I am in the process of developing a react component library using vite as my primary build tool. After successfully compiling the project and deploying it to the npm registry, I encountered an issue when importing it into my client app. Specifi ...

Transform JSON data into a JavaScript object

There is a JSON string in a specific format: [{"A":"SomeStringData","B":1}, {"A":"SomeStringData","B":2}, ... ... ...] It should be noted that this JSON string passes through all online parsers successfully and is considered valid. An attempt is being ...

Assist with JavaScript Programming

Can someone assist me in creating functionality for the "Show Picture" button to toggle the visibility of the picture when clicked? I've been trying to achieve this using an If/Else statement for a school project but have been struggling with it. Any ...

Aggregate values through an onclick event using a get request and iterate through each using

I am struggling to accumulate values obtained from a json get request using jQuery. The problem is that the values keep getting replaced instead of being added up in the variable total. Can someone please guide me on how to properly sum up the values with ...

Employing the Confirm() function within the onbeforeunload() event

Is there a way to prompt the user with a confirmation box before exiting a page using JavaScript? If the user clicks "OK", a function should be executed before leaving the page, and if they click "Cancel", the page should remain open. window.onbeforeunloa ...

Executing RegisterStartupScript repeatedly in C#

Greetings to all, Here is a snippet from my code: start code protected void Button1_Click(object sender, EventArgs e) { ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", "<script>loadAdditionalInfoDialog(info1)< ...

Site performance stalls when a trailing slash is present in the URL while using Express routing

Whenever I visit a page on my development site with a URL like localhost:8000/news, everything loads perfectly fine. However, if I add a trailing slash to the URL, such as localhost:8000/news/, the entire site freezes. It seems to be trying to load partial ...