What is the method for obtaining the worldwide location of a vertex on a skinned mesh in Three.js?

In the realm of Three.js, we've recently acquired the ability to determine the global position of a vertex in a non-skinned mesh thanks to insights from this question. Now, the query arises - how can one ascertain the global position of a vertex in a skinned mesh adorned with bones and morph targets?

For instance, consider this scenario: how would one output (2.5, 1.5, 0.5) given the following conditions? mesh.geometry.vertices[0] initially resides at (0.5, 0.5, 0.5). Subsequently, bones[1] relocates the vertex to (2.5, 0.5, 0.5). Lastly, morphing transports the vertex to (2.5, 1.5, 0.5).

const scene = new THREE.Scene();

const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 200);
camera.position.z = 3;
camera.position.y = 2;
camera.lookAt(0, 0, 0);

const renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const geometry = new THREE.BoxGeometry(1, 1, 1);
geometry.morphTargets.push({name: "morph", vertices: []});
for (const vertex of geometry.vertices) {
  geometry.skinIndices.push(new THREE.Vector4(vertex.x < 0 ? 0 : 1, 0, 0, 0));
  geometry.skinWeights.push(new THREE.Vector4(1, 0, 0, 0));
  geometry.morphTargets[0].vertices.push(vertex.clone().add(new THREE.Vector3(0, 1, 0)));
}

const material = new THREE.MeshPhongMaterial({
  skinning: true,
  emissive: 0xffffff,
  wireframe: true,
  morphTargets: true
});

const mesh = new THREE.SkinnedMesh(geometry, material);

const bones = [new THREE.Bone(), new THREE.Bone()];
for (const bone of bones) {
  mesh.add(bone);
}

const skeleton = new THREE.Skeleton(bones);
mesh.bind(skeleton);

bones[0].position.x = -2;
bones[1].position.x = 2;
mesh.morphTargetInfluences[0] = 1;
scene.add(mesh);

// This code assigns (0.5, 0.5, 0.5) to pos,
// but I want to know the code which assigns (2.5, 1.5, 0.5) to pos. 
const pos = mesh.geometry.vertices[0].clone().applyMatrix4(mesh.matrixWorld);
console.log(`(${pos.x}, ${pos.y}, ${pos.z})`);

(function render() {
  requestAnimationFrame(render);
  renderer.render(scene, camera);
})();
body {
  margin: 0;
  overflow: hidden;
}
canvas {
  width: 100%;
  height: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>

Answer №1

After exploring various methods, I attempted to unravel this issue. While I did not thoroughly test it for all scenarios due to my laziness, here is what I discovered:

To begin with, I utilized the Shader Editor extension. Subsequently, I examined a skinned example by running this provided link, and assessed the generated shader using the Shader Editor tool.

https://i.sstatic.net/Nwxow.jpg

Upon reviewing the vertex shader, the crucial segment of code identified was as follows:

#ifdef USE_SKINNING
    // Relevant bone matrices
#endif
#ifdef USE_SKINNING
    // Skin matrix calculation
#endif

// Additional transformations and calculations...

Upon translating the above into JavaScript, the resulting script is outlined below:

Initially, ensure that the scene has updated its world matrices and the skeleton is refreshed.

// Update scene and skeleton
scene.updateMatrixWorld();
skeleton.update();

Then follow the subsequent snippet:

// Script block for processing data
const position = new THREE.Vector3();
const transformed = new THREE.Vector3();
// Various other declarations...

// Mesh transformation logic iterated over vertices and morphTargets
for (let vndx = 0; vndx < mesh.geometry.vertices.length; ++vndx) {
    // Logic for transformation and skinning operations...
}

For an illustrative instance of the operation, refer to the following:

// Sample demonstration utilizing Three.js library
// Initialized entities within the scene for rendering
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(...);
// Renderer setup and configuration...
const geometry = new THREE.BoxGeometry(...);
// Additional setup and configurations...
const material = new THREE.MeshPhongMaterial({
  // Material parameters and properties set here
});
// Creation and binding of mesh elements...
// Demonstration logic scenario...
putMeshesAtSkinnedVertices(mesh, scene, marker, markerMaterial, markers);

// Rendering engine function invocation
(function render() {
    renderer.render(scene, camera);
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>
<canvas></canvas>

The script was tested against this specific scenario and appeared to provide the desired outcome.

https://i.sstatic.net/QsDIX.png

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: Organize an array of objects into separate sections based on a specific field

Presented below is a set of data: const dataSet = [ { id: '1', name: 'River', address: 'Terminal A', type: 'OTHER', code: null, targetArrivalStep: 30, disabled: true, }, { id: &a ...

Using JSON parsing to dynamically create classes with preloaded background images

Today, I successfully deployed my browser game using MVC4 to my website for the first time. I am currently navigating through the differences between running the site off of localhost and running it from the actual website. My process involves loading all ...

jQuery UI Dialog is causing my DOM elements to shift positions

I encountered an issue where my jquery ui dialog would only open once, To address this, I attempted the following solution: /* Prepare */ $('.steps > div.step-1 .bicicleta .attributos').dialog({ autoOpen:false, width: ...

Utilizing Various Perspectives in Angular with the ng-view Directive

Can anyone assist me with a challenge I'm facing regarding views? While this is a hypothetical scenario and there's no code available to review... Let's imagine a simple site with three routes: /signin /users /users/:id Upon accessing t ...

What is the process of importing an IIFE-based JavaScript module into an Angular TypeScript application?

I have come across a third-party SDK that is structured as an oldschool IIFE based module. The code looks something like this: var ThirdPartySDK = (function() { var export = {}; // Adding some methods to export return export; })(); To use this SD ...

Is a Page Refresh Required to Reload Routes in ReactJS?

I have created menu links labeled Home, About, What I Do, and Experience for my portfolio site using BrowserRouter. However, when I click on these links, the components do not load properly on the page; they only render correctly after a page refresh. This ...

Exploring the Node.js view object within a function and delving into the reasons why a property of

As a beginner in programming, I am seeking tips on how to effectively learn node.js. Currently, I am utilizing the "Learning Behavior Driven Development with JavaScript" book for my learning journey. I would greatly appreciate any advice on how to view ob ...

Addressing the issue with this.setState and this.state on the Registration page

I am facing an issue with my Registration Page code. I am trying to send data to a database using this.setState and this.state, but every time I run the code, it shows an error in the onSubmit function related to this.state. Can someone please help me unde ...

Is there a way for me to control the permissions granted to jhipster's authorities?

In the process of developing a web application with JHipster code generator, I have extended the pre-existing roles to a total of 5: ROLE_USER, ROLE_ADMIN, ROLE_ANONYMOUS, ROLE_PRESIDENT, ROLE_VICE_PRESIDENT I am now seeking guidance on how to manage per ...

Implement safe instructions through communication between the client and server

I am currently using Fancy WebSockets in Javascript for communication with my php server to support my multiplayer game. At the moment, I am simply sending raw sockets (json) as Sending: {"command": "login", "data": {"id" : "1575","md5" : "6bd8937a8789a3 ...

Tips for choosing multiple checkboxes with the input type of "checkbox"

image of my columns and checkboxes <table class="table table-striped grid-table"> <tr> <th>Id</th> <th>Code</th> <th> <input type="checkbox" id="box&q ...

Having trouble submitting a date input form generated with vuejs on Safari browser

I am a huge fan of Vuejs and it's my go-to at work. The other day, I came across a rather perplexing scenario. If you have any insights into this, please do share. Here is the code in question: <script setup lang="ts"> import { ref ...

I'm experiencing issues with the control.reset() function in the trackball controls JS not functioning properly

Visit this website www.naamdesigns.com/arv Whenever a link is clicked, I want the sphere to rotate back to its original position. I have managed to reset the camera position successfully, but I'm struggling to tween the reset rotation. Any tips on ho ...

The transition effect of changing opacity from 0 to 1 is not functioning properly in Firefox

Having some trouble with this animation not working in Firefox. I'm triggering the animation using JavaScript after a delay like so: document.getElementById('my_id').style.webkitAnimationPlayState = "running"; I've also attempted: s ...

What is the top choice for creating a shallow copy of an array

While delving into the vue source code today, I stumbled upon a method of writing that left me puzzled. view source const deduped = [...new Set(pendingPostFlushCbs)] My initial thought is that it is a shallow copy of the array. But why is there a need t ...

What is the best way to pass a prop into the <router-link>?

If I replace this {{ name }}, the result is "campaigns" Now, I want to use that in my link <router-link :to="'/' + '123' + '/' + item.id"> {{ item.name }}</router-link> I attempted to substitute '1 ...

What is the best way to enforce constraints on three keys when validating an object using Joi?

Currently experimenting with Joi for object validation. Able to validate objects with constraints on two keys using any.when(). Now looking to implement validation with constraints on three keys, for example: var object = { dynamicPrize: false, ...

The io.on connection event is being activated for each emit

Each time an event handler is triggered on my socket.io, the io.on connection function is called first. For instance, in a chat application I created, every time I send a message (emit it to all clients), it triggers the io.on connection and then proceeds ...

Inject the code snippet from CodePen into a WordPress webpage

I have a WordPress page where I want to integrate the HTML, CSS and JS code from my Codepen project. The styling appears to be working correctly, but the JavaScript is not functioning as expected. You can view the page here: Could someone assist me in pr ...

A guide on triggering a function when the context changes in a React application

Can I automatically trigger a function in a component whenever the context changes? Specifically, I have a variable named isLoggedIn in the Navbar module. Whenever a user logs in, the value of isLoggedIn is updated to true. In my Blog module, how can I m ...