Foundations for animating with three.js

I am aiming to create a skeletal structure with three bones, each joint represented by a sphere. For instance, the arm would have spheres on the shoulder, elbow, and wrist.

Most resources I came across utilize JSON loaders for skeleton construction. Referring to the Three.js documentation, I devised this code that currently displays three spheres at coordinates (0, 0, 0).

//code snippet within my init() function
var bones = [];
var shoulder = new THREE.Bone();
var elbow = new THREE.Bone();
var wrist = new THREE.Bone();

//connecting the bones
shoulder.add(elbow);
elbow.add(wrist);

//defining default positions of the bones
shoulder.position.set(1, 1, 1);
elbow.position.set(1, 1, 2);
wrist.position.set(1, 1, 3);

//collating all bones into an array for the skeleton
bones.push(shoulder);
bones.push(elbow);
bones.push(wrist);

//global variable in my script
skel = new THREE.Skeleton(bones);

var m = new THREE.MeshPhongMaterial( { color: 0xffff00, emissive: 0x072534, shading: THREE.SmoothShading} );

//creating geometry for the spheres
var g1 = new THREE.SphereGeometry(0.2, 100, 100);
var g2 = new THREE.SphereGeometry(0.2, 100, 100);
var g3 = new THREE.SphereGeometry(0.2, 100, 100);

for (var i = 0; i < g1.vertices.length; i++){
    // assigning bone indices and weights to each vertex of the geometry
    g1.skinIndices[i] = new THREE.Vector4(0, 0, 0, 0);
    g1.skinWeights[i] = new THREE.Vector4(1, 0, 0, 0);
    g2.skinIndices[i] = new THREE.Vector4(1, 0, 0, 0);
    g2.skinWeights[i] = new THREE.Vector4(1, 0, 0, 0);
    g3.skinIndices[i] = new THREE.Vector4(2, 0, 0, 0);
    g3.skinWeights[i] = new THREE.Vector4(1, 0, 0, 0);
}

//creating skinned meshes
var s1 = new THREE.SkinnedMesh(g1, m);
var s2 = new THREE.SkinnedMesh(g2, m);
var s3 = new THREE.SkinnedMesh(g3, m);

//associating meshes with bones.
//This seems to be causing issues
s1.add(skel.bones[0]);
s1.bind(skel);
s2.add(skel.bones[1]);
s2.bind(skel);
s3.add(skel.bones[2]);
s3.bind(skel);

scene.add(s1);
scene.add(s2);
scene.add(s3);

I have implemented render() and animate() functions capable of showing basic geometry. These are excluded here to eliminate unnecessary details.

Frankly, I am uncertain about the purpose of s1.add(skel.bones[0]) and s1.bind(skel): based on an example I encountered, I attempted to mimic the syntax but it does not produce the desired outcome. My goal is simply to position each sphere differently while utilizing the skeleton for animation purposes.

Answer №1

Just to clarify, English is not my first language!

I concur with the sentiment that finding a truly basic tutorial can be challenging! This is why I had to resort to experimentation. The process itself is quite intricate and involves several necessary steps, one of which is understanding the functions of "s1.add(skel.bones[0]) and s1.bind(skel)".

Initially, you need to establish a hierarchy of bones that depend on each other. Following this, you must determine how vertices relate to these bones based on specific geometry. Each vertex can have up to four bone dependencies, but in my example, I only focused on two.

The next phase involves creating a skeleton structure using the defined bones. Subsequently, you need to connect the bones, skeleton, and mesh as follows:

skeleton = new THREE.Skeleton( bones );<br>
mesh.add( bones[ 0 ] );   // attach the first bone to the mesh<br>
mesh.bind( skeleton );    // establish the connection with the skeleton

To visualize the skeleton, you can use:

skeletonHelper = new THREE.SkeletonHelper( mesh );

In the provided example, the bones are generated using the function

createBones(positionY, height, boneCount)

Make sure to explore the functionalities of skinIndexWeigtLatheBody()and

skinIndexWeightCylinder(geometry)
, as cylinders and lathe bodies are easier to work with compared to spheres due to their more systematic vertex arrangement.

Finally, when it comes to animating, remember to adjust the rotations of the bones using:

mesh.skeleton.bones.rotation.x = ...

The script mentioned above details the necessary setup for this skeletal animation project.

The example is available at under Skeleton Basic.

For an enhanced demonstration, "Hummel Mara" (bumblebee Mara) showcases a bumblebee model with bones! This build expands on previous skeleton examples. While the comments within the code may be in German, most variables are named in English.

Knochen - bone, Skelet - skeleton, Knoten - vertex , (- node ...), Ziffer - number, Tafel - table, bewegen - move ... Utilize a translator such as dict.cc or Google Translate for any translation assistance between German and English.


A pro tip: Avoid starting with 100 sphere segments initially.

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

Top tips for data manipulation

I am facing an issue with my JavaScript code that makes an ajax request to the server and receives JSON data, which is not correctly formatted for array-based manipulation. A colleague suggested a client-side solution to convert object-based JSON into arra ...

What is the best way to add clickable links to 3D objects and meshes with ThREE?

My goal is to create a simple box that can act as a link when clicked. This seemingly straightforward task has proven difficult for me, so I would greatly appreciate any assistance. Despite my efforts to troubleshoot and research online, I have not been ...

Tinymce editor does not display icons as expected

I am attempting to create a custom styled list that is editable with tinymce. The list-items are using Material-Check-Icons as bullet-points, which are added as css-pseudo-elements ::before. This setup works well, but when I integrate tinymce (v5) into the ...

steps for including preset text in an input field within a form on WordPress

I am currently utilizing a WordPress plugin that automatically generates a form in the admin area based on user input. The form includes five fields that require URL validation. Users have the option to enter optional link text which will be displayed as t ...

Exploring idTech 4 vertex blending method within the realm of three.js

Looking to incorporate the vertex blend technique used in the idTech 4 engine, I came across this informative article. However, I'm struggling to grasp how to implement it in three.js. It seems like I might need THREE.ShaderMaterial with a specific sh ...

Access the OpenWeatherMap API to retrieve the weather forecast for a single time slot within a 5-day period

Utilizing the openweathermap api, I am retrieving a 5-day/3-hour forecast here. The API call provides multiple results for each day with data available every 3 hours. For the full JSON response, you can access it here. Please note that the API key is only ...

Discovering the worth of objects in a MongoDB array - a guide

I need assistance to access the value of a nested object within an array. I have the _id of the parent object and the _id of the child object in the array, but I am struggling to get the value of "solvedOn" in order to toggle a checkbox behavior. Here is ...

Switch out 2 Bootstrap columns for 2 concealed columns with just a click. Utilizing Rails 4 and Bootstrap

Using Twitter Bootstrap 3 for a column system showcasing four similar advertisements at the bottom of the page. Code Snippet: <div class="row similar"> <% @recomended_ads.each do |advertisement| %> <div class="col- ...

What steps do I need to take to activate CSS Modules in React version 16.13.1?

Hello everyone! I'm currently delving into tutorials and articles on how to activate CSS Modules in React 16.13.1. It's clear that the first step is to run "npm run eject", which I've already completed. The next step involves modifying the " ...

Guide on utilizing direction.set within threejs for Vector3 types

In the code below, I have defined a plane, wall, and a character. Now, I am trying to set the direction using vector3(). However, I seem to be encountering an issue. Whenever I press the left or right arrow key on the keyboard, I keep receiving the follow ...

Setting up a dropdown list with a Django variable following an AJAX request

I am facing an issue with implementing a dropdown that triggers an AJAX call when a new value is selected. In my setup, the AJAX script calls a Django view which returns a list of values. The problem arises when I try to populate a second dropdown with the ...

Sending a Nan alert regarding a JSON attribute

I recently completed a project using Angular4 that involves connecting to a nodeExpressJS server to retrieve JSON data and update the object with new information. Below is the onSubmit function from the addemployeeform. onSubmit(formValue: any) { cons ...

What is the best approach for implementing AJAX in this situation?

Here is the javascript code snippet: function addNewPerson() { var data = { 'entry_new_person_': $('#entry_new_person_1').val(), 'pop_new_person_identity_no': $(' ...

Unable to locate the JavaScript files within the NextJs and ReactJs project

I've encountered an issue when trying to import js files (which are libraries) in my project. I am currently using NextJS version 14.1.3 and ReactJS version 18.2.0. You can find the path to these files here Here is a glimpse of the project structure ...

Employing AJAX to send form data to a pair of destinations

Can anyone help me out? I am having trouble using AJAX to submit my form to one location and then to another. Once it's posted to the second location, I want it to send an email with PHP to a specified recipient, but for some reason, it's not wor ...

Interactive image rotator featuring navigation buttons for next and previous slides

I recently followed a tutorial from W3Schools Now, I am looking to enhance it by adding previous / next buttons for the indicators, rather than for the slider itself Here is what I aim to accomplish: https://i.sstatic.net/qH1PQ.png Below is the code sn ...

Leveraging jQuery functions with dynamically generated DOM elements in JavaScript

On my website, I have a button that dynamically generates a dropdown menu, a textfield, and two buttons when clicked. The purpose of the textfield is to track the quantity selected, while the two buttons allow users to increase or decrease the value by 1. ...

Is there a way in Javascript or JQuery to determine if a function is currently executing, or is there a way to tap into a return event?

Let me paint a picture of a common scenario: there's a form with numerous inputs (around 60-70). Each input must undergo validation, and if it is invalid, the form returns false. To prevent multiple AJAX requests with visual feedback, I need to safegu ...

Are there any tools available that can convert ThreeJS code into WebGL?

Are there any existing tools that can convert ThreeJS to WebGL? Or, could you provide guidance on creating a converter for ThreeJS to WebGL? ...

How do callback functions in Javascript interact with variables outside of their scope?

Greetings to all. I am facing an issue regarding the 'callback function - scope of variable', My intention is to utilize the 'i in for loop' with the 'callback function User_UidSearch', however, I am unable to implement it. ...