Struggling with ThreeJS bufferGeometry position attribute not refreshing after applying translation

Leveraging STLLoader, I successfully loaded an stl file onto a threeJS scene and obtained a BufferGeometry.

Following that, I utilized

myMesh.position.set(x, y, z) 
myMesh.rotation.setFromQuaternion(quaternion, 'XYZ');

to manipulate the geometry. This led to changes in

myMesh.position
myMesh.quaternion

The translation took place within the scene without any issues. I was anticipating that the

myMesh.geometry.attributes.position.array

would show a difference before and after the translation, but it remained unchanged. I am eager to extract the updated vertices from the buffergeometry following the translation. My attempts involved calling

myMesh.geometry.dynamic = true;
myMesh.geometry.attributes.position.needsUpdate = true;

in the render loop, however, this did not yield results as the vertices were not explicitly updated.

Answer №1

To determine the global position of a mesh's geometry while accounting for the mesh's transform matrix, use mesh.matrix. If your mesh geometry is of type THREE.BufferGeometry, follow this pattern:

Here are the steps to follow:

mesh = new THREE.Mesh( geometry, material );
mesh.position.set( 10, 10, 10 );
mesh.rotation.set( - Math.PI / 2, 0, 0 );
mesh.scale.set( 1, 1, 1 );
scene.add( mesh );

mesh.updateMatrix(); // Ensure that the mesh's matrix is up to date

var vec = new THREE.Vector3();
var attribute = mesh.geometry.attributes.position; // Retrieve the position data
var index = 1; // Index starts at zero, so this is the second vertex

vec.fromBufferAttribute( attribute, index ); // Extract the x,y,z coordinates

vec.applyMatrix4( mesh.matrix ); // Apply the mesh's matrix transformation

Based on three.js r157

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

Creating a customized URL using the famous "@" symbol

Is it possible to set up a dynamic route in Next.js that includes the @ symbol? For instance, localhost:3000/@some_username I'm familiar with creating dynamic routes using the pages folder, but I'm not sure how to achieve this specific format w ...

Retrieve the world coordinate using Curve.getPoint() in Three.js

I am currently attempting to determine the position of a point along an ellipse curve in world coordinates using Three.js. To do so, I am utilizing the following function: pt = ellipse.getPoint(t[i]); However, this function returns a Vector2 object with c ...

Is there a way to transform a string into a human-readable slug?

Is there a way to convert a slugified string into a more human-readable format? I am extracting parameters from a URL: bookmark/10/disco%20asdasd From this, I have the name "disco%20asdasd". However, this is not easily readable so I need to change it to ...

Using jQuery to initiate a page load into a new page within the WorkLight platform

I need help redirecting to a new page when the current page is loaded. My website is built using jQuery mobile in combination with WorkLight. Index.html: <body> <div data-role="importpages" id="pageport"> </div> </body> ...

Use JavaScript to change the text of a hyperlink to @sometext

<li class="some-class one-more"><label>twitter:</label> <a href="https://twitter.com/sometext?s=09" target="_blank" rel="noreferrer noopener">https://twitter.com/sometext?s=09</a> < ...

Embed message from a Discord Bot

Is it possible to include an image of a bot on the right side of the embedded message? if (message.includes('help')) { msg.channel.send({ embed: { title: "xxxxxx", color: 3447003, description:"Enter **agjgj** to know ...

Solving templateUrl resolution of directives in Angular with RequireJS

Currently, I am using AngularJS and RequireJS in my single-page application (SPA). To organize imports efficiently, I rely on RequireJS. With RequireJS, I can utilize features like baseUrl to resolve import paths seamlessly. Now, I wish to apply the same r ...

How do I create a new JSON Object with only two properties by selecting from an existing JSON Object with four properties?

Below is a JSON object example: [ {'car':'punto','brand':'fiat','color':'black','model':'2007'}, {'car':'XUV500','brand':&ap ...

JavaScript - the act of exiting functions

Is it necessary to explicitly return from a JavaScript function? Unlike in other languages where not returning can result in a stack overflow error, JavaScript seems to handle this differently. Furthermore, due to its asynchronous nature, determining when ...

Utilizing Node.js to retrieve streams in conjunction with OpenAI

I am currently working on setting up a node/react setup to stream results from openai. I came across an example project that accomplishes this using next.js. While I have successfully made the API call and received the results as expected, the challenge li ...

Tips on how to properly position the grandchild in world space by rotating and translating the grandparent

I am trying to figure out how to move a grandchild object to a specific position and orientation in world space. The task involves manipulating the grandparent's (root) position and rotation in order to move child 2. If I have the target's world ...

Slick carousel not functioning properly with Angular slickGoTo

I am currently working with the angular-slick-carousel library. Unfortunately, I am encountering an issue where the Slick methods, such as slickGoTo(), are not functioning as expected. Below is my Controller setup following the provided documentation: $s ...

Executing a nested function as an Onclick event in an HTML document

I am currently working on a project within the Phone Gap framework where I need to transfer values from one HTML page to another. My solution involved using the code snippet below. var searchString = window.location.search.substring(1),i, val, params = s ...

Creating interactive form fields using React

How can I update nested fields in react forms? First, create a new item using handleAddShareholder. Next, delete an existing item with handleRemoveShareholder. To change details of an item, use handleShareholderNameChange. You can then add a new array ...

How to Retrieve a Specific Line with jQuery

Could jQuery be used to retrieve the offset of the first letter in the 5th visual line of a block's content? I am referring to the visual line determined by the browser, not the line seen in the source code. ...

Navigating through Angular JS validation procedures step by step

I have a wizard in angular js consisting of multiple steps: <wizard on-before-step-change="log(event)" on-step-changing="log(event)" on-after-step-change="log(event)" user="user"> <step title="step 1"> </step> <step title="step 2"& ...

What is the best way to connect the imagemap shape with the checkbox?

I need assistance with synchronizing an imagemap collection of shapes and checkboxes. How can I ensure that clicking on a shape selects the corresponding checkbox, and vice versa? Imagemap: <div class="map_container"> <%= image_tag("maps/main ...

Accessing a variable from one function within another function in Vue

How can I access and utilize the ctx variable from the initGrid() function in the drawGrid() function? Despite trying to use "this," it seems that the variable cannot be accessed within the drawGrid() function. <script> export default { data: ( ...

Encountering a 'JSON parsing error' while transmitting data from Ajax to Django REST Framework

I've been encountering an issue while trying to send a JSON to my REST API built with Django Rest Framework. Every time I make this request, I receive an error message, regardless of the view I'm accessing. I suspect the problem lies in the AJAX ...

Is it possible to transfer data between two child components in Vue.js that share a common parent component?

In my project, I am working with 2 components that have a shared parent. My goal is to pass data from one child component to the other effectively. (I am specifically using vueJS 2 for this task) ...