Incorporating a rigged and animated asset into a preexisting scene using Three.js

Currently, I'm developing a browser app similar to Tamagotchi using three.js. However, I've hit a roadblock while trying to implement a hand that can pet the avatar when clicked.

The Hand is a rigged Blender model with two animations: idle and poking. When viewed in the gltf Viewer, the model works perfectly with both animations. But, when added in JavaScript, the hand either appears distorted or rendered correctly but fails to recognize positions for movement with the cursor.

Most of the examples I've come across only show how to add a general scene, without focusing on just one animated model. In both versions of those animations, I encounter an animation error.

Here's the code for the distorted version:

loader.load('resources/models/gltf/Hand.gltf', function(gltf) {
  gltf.scene.traverse(function(node) {
    if (node.isMesh) hand = node;
  });
  //hand.material.morphTargets = true;

  scene.add(hand);
  mixer = new THREE.AnimationMixer(hand);
  clips = hand.animations;
  hand = gltf;
  scene.add(hand.scene);
});

In the second version, where the Hand renders correctly but doesn't recognize positions for event handling:

loader.load('resources/models/gltf/Hand.gltf', function(gltf) {
  var hand = gltf.scene;
  var animations = gltf.animations;
  mixer = new THREE.AnimationMixer(hand);
  for (var i = 0; i < animations.length; i++) {
    mixer.clipAction(animations[i]).play();
  }
  scene.add(hand);
});

Function for the idle animation:

function idleAnim() {
  var idleClip = THREE.AnimationClip.findByName(clips, "Idle");
  var action = mixer.clipAction(idleClip);
  action.play();
  console.log("idling");
}

Link: https://github.com/JoeJoe49/AnimTest

Thank you in advance for any help and best regards!

Answer №1

Your approach in the initial example involves extracting the "hand" object from the import scene, integrating it into the render scene, and then including the remaining components of the import scene in the render scene as well. To ensure the correct hierarchy for the animation is maintained, you may need to retrieve the "hand" object from a higher level within the structure. It is possible that there are parent objects associated with it that also need to be included.

Performing a scene traversal using scene.traverse((o)=>{console.log(o)}) can provide valuable insights into the export setup of your scene. For instance, when using the blender gltf exporter, I have observed separate parent nodes dedicated to positioning and scaling+rotation. These nuances are important to consider as they may not align with your initial expectations.

Upon examining your repository and loading the gltfs into my model previewer, I did not detect any animations on them. My previewer typically plays all animations it identifies sequentially, so this discrepancy is puzzling. Are these objects skinnedmeshes rather than morphtargets by any chance?

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

JavaScript - exploring techniques to alter the relationship between parents and children

I'm facing an issue with transforming the parent-child relationship in my data structure. Currently, it looks like this: { "id": 7, "name": "Folder 1", "parent_folder": null, "folders": ...

Drop-down Navigation in HTML and CSS is a popular way to create

My navigation menu is functioning well and has an appealing design. The HTML structure for the menu is as follows: <div id="menubar"> <div id="welcome"> <h1><a href="#">Cedars Hair <span>Academy</span></ ...

The occurrence of an unanticipated character '#' was found

I'm currently facing an issue while using webpack to load dependencies. Whenever I execute the npm run dev command, I encounter the following error: Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: ...

Issue: 040A1079 - Failure in RSA Padding Check with PKCS1 OAEP MGF1 Decoding Error Detected on Amazon Web Services

Utilizing the crypto package, I execute the following tasks: Using crypto.generateKeyPairSync() to create publicKey and privateKey The keys are generated only once and stored in the .env file Applying crypto.publicEncrypt() to encrypt data before savin ...

href not functioning properly on subsequent clicks, whereas onclick remains active

I'm experiencing an issue with a button that opens a modal. When clicking the button, it's supposed to open a new page in the modal and also make an API call for processing before loading the new page. <a class="btn btn-primary" type='b ...

Is it possible to use a full-width material-ui Button inside a Badge component?

Within a grid, I had initially used fullWidth on a Button to make it expand and fill the container. Everything was functioning correctly until I enclosed the Button in a Badge element. Now, the fullWidth property is not being applied, and the button rever ...

Unable to locate the 'react-native' command, attempted various fixes but none were successful

Working on an older react native project that was functioning perfectly until I tried to pick it back up and encountered a problem. https://i.stack.imgur.com/1JUdh.png This issue revolves around the package.json file. https://i.stack.imgur.com/v6ZEf.png ...

What could be causing the JSON.parse() function to fail in my program?

Currently utilizing Django and attempting to fetch data directly using javascript. Below are the code snippets. Within the idx_map.html, the JS section appears as follows: var act = '{{ activities_json }}'; document.getElementById("json") ...

What is the best way to deliver data from the main Vue instance to components that are being utilized by vue-router?

Currently, I am utilizing vue-router within a single HTML file instead of using separate file components. I am encountering an issue where I am unsure how to pass the data stored in the Vue data object to the component when loading it for a route. Oddly e ...

Commencing a New Ember.js Venture

I've recently started using Ember.js and I'm used to simply typing rails project33 to create a new Rails project. But with Ember, it seems like there are multiple steps involved: mkdir project43 && cd project43 npm install -g genera ...

Some variables are not being properly tracked by Google Analytics

Within the document.ready() function of the primary GSP layout in my application, I have included the following code: var pageTitle = document.getElementsByTagName('title')[0].innerHTML; if('index' == 'list') { pageTitle ...

What is the process of using an if statement in jQuery to verify the existence of a property in a JSON file?

I am working on a task to incorporate an if statement that checks for the existence of a specific property in a JSON file. If the property exists, I need to display its value within HTML tags <div class='titleHolder'> and "<div class=&ap ...

The property 'matMenuTrigger' cannot be attached to 'a' because it is not recognized

Trying to implement matMenuTrigger but encountering an error saying "Can't bind to 'matMenuTrigger' since it isn't a known property of 'a'". Any assistance would be greatly appreciated. ...

Organize array elements based on their values - using javascript

I am currently exploring the most effective approach to divide an array into multiple arrays based on specific operations performed on the values within the array. Imagine I have an array like this: var arr = [100, 200, 300, 500, 600, 700, 1000, 1100, 12 ...

After props have been passed, the ReactJS ComponentWillMount() function is triggered

Can anyone explain why the child component is only rendered once, even though I pass props to it every time I click a button? Whenever I click a button that passes props to the child, the ComponentWillMount() method of the child component doesn't tri ...

Methods for reloading the requirejs module

There are two modules: settingmap.js var settingMap = { scWidth : [4000, 6000, 8000], scHeight : [5000, 7000, 9000], bxWidth : [100, 90, 80], bxHeight : [100, 90, 80], totalTime : [50, 40, 30], level : [1, 2, 3], boxColor : [&a ...

Manipulating the value of an array obtained from an API alters its data when trying to log it in the render function of a React TSX component

My program can fetch data from an API I created using a component that interacts with the backend: import React, { Fragment, useEffect, useState } from 'react' import { Button, Stack } from '@mui/material'; import TabsComponent from &ap ...

I am experiencing issues with the customsort function when trying to sort a column of

Seeking assistance with customizing the sorting function for a Date column in a primeng table. Currently, the column is displaying data formatted as 'hh:mm a' and not sorting correctly (e.g. sorting as 1am, 1pm, 10am, 10pm instead of in chronolog ...

Save to a JSON file

Hey there, I'm having some trouble with pushing a temporary value to a JSON file using the command "MyJSON.name.push". It keeps giving me an error saying "Undefined is not an object". I've tried different approaches and using JavaScript arrays wo ...

Using Vue to showcase the result of a form submission on a separate page

Working with a <form> in the context of vue has been successful as I am able to send the form data to the server, receive a JSON response, and print it on the console. However, my current challenge involves displaying this JSON response on a differe ...