Three.js - The initial child leaps into action and inherits the parent's transformations

I am currently working on establishing a parent/child hierarchy involving three objects using the ".add()" method in Three.js. When parenting two objects, everything works smoothly. However, I encountered an issue when trying to create a hierarchy with three levels - the middle child object suddenly jumps to an offset equivalent to its parent's offset from the origin.

Here is the hierarchy structure:

Red Box (parent of Green Box) -> Green Box (parent of Blue Box) -> Blue Box

Below is an image showing how the boxes should be positioned both before and after the parenting process: https://i.sstatic.net/Jv0ZK.png

Here is an image displaying the repositioned boxes after parenting: https://i.sstatic.net/3wMc9.png

The relevant code snippet looks like this:

        var testObject_G1 = new THREE.BoxGeometry(50, 100, 100);
        var testObject_G2 = new THREE.BoxGeometry(100, 100, 50);
        var testObject_G3 = new THREE.BoxGeometry(50, 100, 100);

        var testObject_MR = new THREE.MeshBasicMaterial({ color: 0xff0000 });
        var testObject_MG = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
        var testObject_MB = new THREE.MeshBasicMaterial({ color: 0x0000ff });

        var testObject_Mesh = new THREE.Mesh(testObject_G1, testObject_MR);
        testObject_Mesh.position.x = -100;

        var testObject_Mesh2 = new THREE.Mesh(testObject_G2, testObject_MG);
        testObject_Mesh2.position.x = 0;

        var testObject_Mesh3 = new THREE.Mesh(testObject_G3, testObject_MB);
        testObject_Mesh3.position.x = 100;

        testObject_Mesh2.add( testObject_Mesh3 );
        testObject_Mesh.add( testObject_Mesh2 );

        scene.add( testObject_Mesh );

You can view the JS Fiddle for this issue here: jumping second child

Thank you in advance for your help.

Answer №1

It is indeed accurate. When dealing with a hierarchy, the child objects will adopt the transformations of their parent objects. This means that testMesh (red) will be positioned at x=-100, testMesh2 (green) will also be at x=-100 (as it inherits its parent's x transform), and testMesh3 (blue) will be located at x=0 (resulting from -100+0+100=0).

To visualize the relative transformations, include the following code snippet in your fiddle:

scene.add( new THREE.AxisHelper( 100 ));

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

I encountered a "TypeError: Unable to access property 'name' of undefined" error when attempting to export a redux reducer

UPDATE: I encountered an issue where the namePlaceholder constant was returning undefined even though I was dispatching actions correctly. When attempting to export my selector function, I received an error: https://i.sstatic.net/MwfUP.png Here is the c ...

Configuring Webpack for a React application with Express and Babel

I am in the process of developing a React application, utilizing React version ^15.6.1 along with Webpack, Express, Babel, and Yarn as the package manager. After successfully running the application, I am now preparing to transition it to production by co ...

Is there a way to modify the text within a "span" element that directly follows an "i" element using jQuery?

I am attempting to modify the text displayed on a button within a plugin by using jQuery. Here is the outer HTML: <button value="[[3,1,1488182400]]" data-group="2017-02-27" class="ab-hour"> <span class="ladda-label"> <i class="ab-hour ...

Requesting information from a NodeJs endpoint

I have a NodeJs application that requires calling an end-point (http:\localhost:9900\get\employee - asp.net web-api) to retrieve data. What are some options available for achieving this task? Is it possible to utilize promises in this scenar ...

`Express.js Controllers: The Key to Context Binding`

I'm currently working on a project in Express.js that involves a UserController class with methods like getAllUsers and findUserById. When using these methods in my User router, I have to bind each method when creating an instance of the UserControlle ...

Bringing in PeerJs to the NextJs framework

Currently delving into NextJs and working on creating an audio chat application, I've hit a roadblock while attempting to import PeerJs. An error message keeps popping up saying 'window is not defined'. import Peer from 'peerjs'; ...

What is the method for duplicating the HTML code of multiple elements in the Chrome console?

Is there a way to easily copy all the HTML code from elements that have the "abc" class? I attempted using $('.abc') in the Chrome DevTools console, but it only returns an array that I can't expand and copy all at once... Any suggestions on ...

Each time Firebase retrieves an object, it comes back with a unique name

One query has been bothering me: const questionsRef = firebase .database() .ref('Works') .orderByChild('firebaseKey') .equalTo(this.props.match.params.id); It's functional, but the issue lies in the output: "-LDvDwsIrf_SCwSinpMa ...

populate vueJS table with data

I encountered an issue while trying to load data from the database into my table created in VueJS. I have set up my component table and my script in app.js, but I am seeing the following error in the view: [Vue warn]: Property or method "datosUsuario" ...

Using $.getJSON is not functioning properly, but including the JSON object directly within the script is effective

I'm currently working on dynamically creating a simple select element where an object's property serves as the option, based on specific constraints. Everything is functioning properly when my JSON data is part of the script. FIDDLE The follow ...

Validate if a string in JQuery contains a specific substring

How can I determine if one string contains another string? var str1 = "ABCDEFGHIJKLMNOP"; var str2 = "DEFG"; What function should I utilize to check if the string str1 includes the string str2? ...

Tips on resolving JavaScript's failure to adjust to the latest HTML inputs

I'm working on a homepage where users can choose their preferred search engine. The issue I'm facing is that even if they switch between search engines, the result remains the same. I've experimented with if-then statements, but the selecti ...

How to upload an image using Java and store it on a server without the use of Html5

Looking to upload an image file on the server using a servlet without utilizing HTML5. While browsing through stackoverflow, most answers I found were based on PHP. I attempted to read the image file at the client-side in JavaScript with FileReader.readAsD ...

Converting UTC Date Time to Full Date Using ES6

Is there a way to transform the date 2021-01-10 12:47:29 UTC into January 10, 2021? I've been attempting to achieve this using moment.js code below, but it seems to be functioning in all browsers except Safari. {moment(video?.createdAt).format(' ...

Implementing React hooks to efficiently remove multiple items from an array and update the state

Looking for help on deleting multiple items from an array and updating the state. I have selected multiple items using checkboxes, which are [5, 4, 3]. My goal is to remove all these items from the array based on their ids and then update the state accordi ...

Sorry, but React does not accept objects as valid children. Make sure the content you are passing is a valid React child element

I encountered an issue with rendering on a screen that involves receiving an object. The error message I received is as follows: Error: Objects are not valid as a React child (found: object with keys {_U, _V, _W, _X}). If you meant to render a collection o ...

Resolving parent routes in Angular 2

I am encountering an issue with my code. The 'new' route is a child route for the 'users' route. The 'users' route has a resolver, and everything works fine up to this point. However, after successfully creating a new user, ...

Can I install more than one instance of Framework7 on the same device?

Currently, I am working on a project using cordova 6.2.0 and framework7 1.6.5. However, now I need to initiate a new project that will be based on cordova 7.1.0 and framework7 2.0.7. I am aware that there is version-manager-cordova-software [1] available ...

How to access selection range styles using JavaScript

It is common knowledge that we can retrieve the selection of text in JavaScript using the following method: var range = window.getSelection (); However, how can we obtain the style of this selection? For example, when I select bolded text or italicized ...

What is the best way to show an image within a div using HTML and fetching data from an API response?

I am attempting to showcase an image within a div using HTML. I utilized fetch to retrieve the JSON data from the following API: . The response contains JSON data with the image URL labeled "primaryImage". While I can display it as text, I am encounterin ...