The mesh's position has become invalid due to the dynamic change in vertices' positions

When attempting to create a mesh using PlaneGeometry, I encountered an issue where after dynamically changing the geometry's vertices, the mesh's position and rotation were no longer correct. Is there a way to update these properties properly to match the new vertices?

My attempted solution looked like this:

var geometry = new THREE.PlaneGeometry(500, 500);
geometry.dynamic = true;
var material = new THREE.MeshBasicMaterial({
            side: THREE.DoubleSide
});
var plane = new THREE.Mesh(geometry, material);
scene.add(plane);

geometry.vertices[0].x += 15;
geometry.vertices[1].x += 15;
geometry.vertices[2].x += 10;
geometry.vertices[3].x += 10;
geometry.verticesNeedUpdate = true;

However, even after these vertex updates, the plane's position remained at Vector3(0, 0, 0). How can I correctly update the position to reflect the changes made to the vertices?

Answer №1

The correlation between position and vertices is noteworthy. Imagine a scenario in which a mesh is situated at (0, 0, 0), yet appears in an entirely different location due to the large numerical values of its vertices. The vertex position essentially denotes a location that is relative to the object's position. Consider your plane defined by

new THREE.PlaneGeometry(500, 500)
:

(-250, 250, 0)            (250, 250, 0)
*--------------------------*
|                          |
|                          |
|                          |
|        (0, 0, 0)         |
|            O             |
|                          |
|                          |
|                          |
*--------------------------*
(-250, 250, 0)             (250, -250, 0) 

The symbol O represents the origin. Upon applying a transformation:

(-235, 250, 0)            (265, 250, 0)
*--------------------------*
|                          |
|                          |
|                          |
|        (0, 0, 0)         |
|            O             |
|                          |
|                          |
|                          |
*--------------------------*
(-240, 250, 0)             (260, -250, 0) 

Although the parallelogram shifts slightly, the origin remains unchanged as its position was not altered intentionally. An example of a geometric shape following a similar concept can be depicted as:

                                 (500, 250, 0)            (750, 250, 0)
                                 *--------------------------*
                                 |                          |
                                 |                          |
                                 |                          |
(0, 0, 0)                        |                          |
    O                            |                          |
                                 |                          |
                                 |                          |
                                 |                          |
                                 *--------------------------*
                                 (500, 250, 0)             (750, -250, 0) 

Remarkably, this configuration remains valid. The primary concern lies in your objective. It is advisable to refrain from manipulating a mesh by adjusting vertex positions solely. Moreover, if your intention involves deforming the mesh along with shifting its vertices independently while keeping the mesh centered:

var boundingBox = new THREE.Box3().setFromObject(mesh);
var centerDisplacement = boundingBox.getCenter().sub(mesh.position); // Difference between current position and center
geometry.vertices[0].add(centerDisplacement);
geometry.vertices[1].add(centerDisplacement);
geometry.vertices[2].add(centerDisplacement);
geometry.vertices[3].add(centerDisplacement);
geometry.verticesNeedUpdate = true;

Answer №2

The positioning of the plane does not rely on the vertices' positions; instead, it is the vertices' positions that are determined relative to the plane's geometry. In other words, the world position of vertex n is determined by

mesh.position + geometry.vertices[n]
(assuming no rotation or scale).

To properly adjust the plane, you should first move its center:

plane.translateX(12.5);
//Make sure to update the matrix for the changes to take effect:
plane.updateMatrix();

Then, proceed to update the positions of the vertices:

//Since the plane has been shifted by 12.5 units, vertices that should move 15 units only need to move 2.5 units:
geometry.vertices[0].x += 2.5;
geometry.vertices[1].x += 2.5;
//On the other hand, vertices that should move 10 units have overshot by 2.5 units, so we need to deduct that from their positions:
geometry.vertices[2].x -= 2.5;
geometry.vertices[3].x -= 2.5;
geometry.verticesNeedUpdate = true;

The 12.5 unit shift serves the purpose of maintaining symmetry. If symmetry is not a concern, moving the plane by 10 units and adjusting vertices 0 and 1 by 5 units each would suffice.

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

Exploring the Wonders of React Memo

I recently started delving into the world of React. One interesting observation I've made is that when interacting with componentized buttons, clicking on one button triggers a re-render of all button components, as well as the parent component. impo ...

Storing the Token from the AJAX Login API Call in JavaScript: Best Practices for Keeping Credentials Secure

After sending my credentials to a login API, I received a successful response containing user data and a token. I would like to know how I can store this information so that the user doesn't have to log in every time. Is it possible for me to save the ...

Issue: The keyword in React/React-Native is returning a boolean value instead of the expected element object

I've recently delved into learning and coding with React, and I'm encountering a bug that I need help fixing. The issue lies within my application screen where I have two checkboxes that should function like radio buttons. This means that when on ...

Notification of Leaf Name in d3.js

I am trying to display the leaf name when it is clicked, but I am unsure how to do it. I am new to D3 and would appreciate any guidance on how to achieve this. Source: http://bl.ocks.org/mbostock/7607535 var circle = svg.selectAll("circle") .data(nod ...

What is the reason behind the momentary functionality of v-for?

I'm facing a strange issue while working with Vue.js. I'm not sure if the problem lies with my code or if it's a bug in the framework. Here's the scenario: When I use v-for with a comma before it (v-bind), there are no errors but nothi ...

Is there a way for me to achieve a vertical page turning effect on a single page?

I am seeking to develop a unique calendar display consisting of 12 images that give the illusion of flipping up when clicked. While I am aware of turn.js, my knowledge of javascript is limited and I need guidance on how to proceed. Despite having a program ...

Capturing click events on a stacked area chart with nvd3

Could someone assist me in capturing the click event on a nvd3 stacked area chart? I have successfully managed to capture tooltip show and hide events but am struggling with the click event. How can I obtain information about the clicked point? Please help ...

"Enhancing User Experience with jQuery: Implementing a Smooth Scroll Feature with Current

I could really use some guidance on this issue that's been causing me trouble. Take a look at this fiddle for reference: http://jsfiddle.net/NtUpw/ Currently, the code is functioning as expected. However, I'm facing an issue where when the curre ...

Storing HTML values in a Meteor database is a common practice for web

Within my meteor project, I have a paragraph in HTML containing a JSON value as follows: {"Active Template Id":"6467","Shirt Brand":"levis","ProductId":"EB301","Brand":"on","Material":"cotton","Price":"1800","Combo Id":"S90"} I am looking to store this v ...

Is there a way to prevent the DOM from loading images until Angular has successfully injected the correct variables?

Having some trouble with this block of code I have: <div class="image" ng-repeat="image in images"> <img src="{{image.url}}"></img> </div> It seems that the image sources are being set correctly, but I keep getting an error wh ...

Creating dynamic HTML elements by utilizing both jQuery and native JavaScript within the DOM

I have an old application that I'm revamping, and instead of using the node's id, I want to apply the DOM structure to its class. Here is a snippet of my code where I am attempting to combine jQuery (selecting the node by its class) with the exi ...

Creating a Page with Python Selenium for JavaScript Rendering

When using Python Splinter Selenium (Chromedriver) to scrape a webpage, I encountered an issue with parsing a table that was created with JavaScript. Despite attempting to parse it with Beautiful Soup, the table does not appear in the parsed data. I am str ...

Creating a fetcher that seamlessly functions on both the server and client within Nextjs 13 - the ultimate guide!

My Nextjs 13 frontend (app router) interacts with a Laravel-powered backend through an api. To handle authentication in the api, I am utilizing Laravel Sanctum as suggested by Laravel for SPAs. This involves setting two cookies (a session and a CSRF token) ...

Guide to locating a particular node within an array of nested objects by utilizing the object

Dealing with an array of nested objects, the goal is to compare values with a flat array and update the property matchFound. If the parent's matchFound is true, then all its children should inherit this value. treeData = [{ field: 'make&a ...

Guide on inserting a new user to the db.json

My database file, db.json, has the following structure: { "users": [ { "id": 0, "isActive": true, "balance": "$2,309.20", "picture": "http://placehold.it/128x128", "age": 26, "accessLevel": "guest", "firstNa ...

Using VueLoaderPlugin() results in an 'undefined error for 'findIndex' function

Currently, I am in the process of integrating a Vue CLI app into another web project that we are actively developing. The Vue app functions without any issues when utilizing the development server bundled with Vue CLI. Due to the presence of .vue files wi ...

The ng-view DIV in Angular JS 1.x mysteriously vanishes

Currently, I am working on a project that involves angularJS and ASP.NET in Visual Studio 2013. However, I have encountered a frustrating issue where my DIV node for ng-view is being replaced with an ng-view comment without any errors appearing while testi ...

The content within the iframe is not displayed

I've set up a dropdown menu with separate iframes for each option. Here's the code I used: $(function(){ $('#klanten-lijst').on('change',function(){ $('#klanten div').hide(); $('.klant-'+t ...

I'm struggling to figure out why my code is throwing an Unexpected token error. What am I missing here?

I keep encountering an Unexpected token error in my code, specifically with a closing parenthesis ). What exactly does this error signify? Experimented by adding and removing parentheses as well as curly brackets. const getUserChoice = userInput => {u ...

"Changing a Java script base addon file to a customized addon in Odoo 16: A step-by-step guide

Here is the JavaScript file located in the "documents" enterprise base addon: I want to include the field "document_type_id" in the export const inspectorFields = [] array through a custom addon. /** @odoo-module **/ import { device } from "web.confi ...