Error in Three.js: Incorrect values retrieved for object's position, rotation, and scale

Working with a three.js object in my scene, I utilize the THREE.TransformControls to rotate, scale, and move it. After positioning the object, I need to save the values of rotation, position, and scale. The code snippet below shows how I am currently retrieving these values:

// Position
$scope.scene.updateMatrixWorld(true);
var position = new THREE.Vector3();
position.getPositionFromMatrix( $scope.object.matrixWorld );

// Rotation
var rotation = new THREE.Euler();
$scope.object.getWorldRotation(rotation);

// Size
var size = new THREE.Box3().setFromObject($scope.object);
var x = (size.max.x - size.min.x);
var y = (size.max.y - size.min.y);
var z = (size.max.z - size.min.z);

However, when I save and reload the scene with these values, the object appears significantly different than expected. It seems that the functions are returning incorrect values for rotation, scale, and position. For instance, even though size.z should always be 0, it is showing a different value upon saving and printing it in the console.

Does anyone have any insights into why this might be happening?

Thank you for your assistance.

Answer №1

If you're using Three.js, there is a handy matrix decomposition function that you can take advantage of:

Check out this example on how to utilize it:

var translation = new THREE.Vector3();
var rotationQ = new THREE.Quaternion();
var scale = new THREE.Vector3();

myObject.matrixWorld.decompose(translation, rotationQ, scale);

And if you need to convert the rotation into Euler angles:

var rotationE = new THREE.Euler().setFromQuaternion(rotationQ.normalize());

This code snippet applies to three.js version r88.

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

show image with the help of jquery and ajax

To showcase company information along with an image, I have a controller set up as follows: public JsonResult DisplayCompanyDetails() { CompanyModel cm = new CompanyModel(); string query = "select CompanyName,Address,Conta ...

Is it possible to modify CSS properties by clicking on buttons?

Hello, I really need help with this task. I want to change the style of a button (which is actually a link) by clicking on it. These buttons are housed within a dynamic child div that changes each time, but the name of the dynamic child div remains con ...

Are there varying levels of efficiency when retrieving elements by ID versus by class using JavaScript/jQuery?

When it comes to JavaScript/jQuery, is there a performance disparity between locating elements by id versus by class? Is one method superior over the other? Could it be attributed to ID or Class indexes being generated in the DOM? Ultimately, does the di ...

Toggle the visibility of a div by clicking on a label

I am working with three unique labels, each with different ids and three distinct divs also with different ids. <asp:Label ID="CA" runat="server" Font-Bold="False" Font-Names="Arial" Font-Size="10pt" style="padding-top:6px;" ForeColor="#CCCCCC" Height ...

What is the best way to create an express route for a delayed string response that is loaded only after an API call promise is fulfilled?

app.get(`/${sumName}`, async (req, res) => { const link = `https://na1.api.riotgames.com/lol/league/v4/challengerleagues/by-queue/RANKED_SOLO_5x5?api_key=${RiotKey}` const response = await fetch(link); let data = await response.j ...

Removing a parameter from a variable in jQuery and JavaScript

I have coded something and assigned it to a variable. I now want to replace that value with another one. Just so you know, I do most of my coding in perl. Specifically, I am looking to remove the menu_mode value. Any advice on this would be greatly appre ...

Is it possible to adjust the pivot point for rotation on TrackballControls without changing the camera's lookAt position?

Despite numerous attempts, I have been unable to change the rotation pivot on THREE.TrackballControls without affecting the camera's lookAt. Can you provide guidance on how to solve this issue? My goal is to achieve mouse controls similar to Revit. ...

Having trouble displaying Vue Toast messages?

I have been trying to incorporate a toast message into my code, but unfortunately, the message does not appear and there are no errors in the console. The code functions properly and the invitation is sent successfully. I have used similar code in other fi ...

The value of the $scope variable remains constant within the function

I am facing an issue with a scope variable that I passed to a function. Even though the variable is accessible inside the function, its value does not change when I try to modify it. Below is my HTML code: <div class="console_item" ng-class="dropdwns.a ...

What could be causing the entire app to malfunction when the Redux Provider tag is used

I am facing an issue while trying to integrate a React Toolkit app into my project. The error message I encountered is as follows: Error: Invalid hook call. Hooks can only be called inside the body of a function component. This error may occur due to one ...

Switching to a jQuery IF statement instead of a FOR loop allows for more streamlined

I recently made a request to sqlite and am fetching data using the code snippet below: var rows = results.rows; alert(rows.length); for (var index = 0; index < rows.length; index++) { var x = rows.item(index); $( ...

What is the best way to specify the correct content type for different parts of form data when using AngularJS, especially when including file uploads and other form fields?

As demonstrated in a helpful solution here, I have successfully managed to send multipart formdata including both a file and other form fields. However, I noticed that for the other form fields, the content type is missing in the request. The multipart for ...

Output various strings to the standard output and stream them individually

Is there a way to redirect each string output to standard out to another command? // Example file: example.js #!/usr/bin/env node process.stdout.write('foo') process.stdout.write('bar') After running ./example.js | wc -m, the total ch ...

JavaScript and Node.js

I recently created a function in vanilla JS called fetch to retrieve data from an API. Now, I am looking to send this data to my MongoDB database using Node.js. It's a bit chaotic right now as my friend is handling the backend while I focus on the fro ...

Is there a way to retrieve a singular value from various select options sharing the same name within a single form?

I am facing an issue with my code where only the value assigned to the last select option panel is being saved in the database table. I need assistance as it was working well initially but for some reason, it's not functioning correctly now. <?php ...

Display a loading animation before the page loads upon clicking

$("#button").click(function(){ $(document).ready(function() { $('#wrapper').load('page.php'); }); }); <div id="wrapper"></div> <div id="button">click me</div> I would like to display a loading ic ...

Working on rectifying the Chat Engine API code that was causing a 403 Status Code to be generated

Encountering a status code 403 while attempting to create a chat engine IO page, even though all authentication headers are believed to be accurate. Double-checked for typos but still unable to identify the issue. Despite console logging the user correctly ...

What could be the reason for my jQuery script not functioning properly?

<script> var currentLevel = 0; $(document).ready(function(){ $("#tolevel_" + (currentLevel+1) ).click(function(){ $("#level_" + currentLevel).hide(500,'swing', function(){ $("#level ...

Tips for integrating my HTML5/javascript game into my existing website

Embarking on my first game development journey, I'm feeling a bit lost in the sea of coding. Creating a browser game is unfamiliar territory for me, and I'm struggling to grasp the usual process. My game relies on the Phaser framework of HTML an ...

How to stop double-clicking on a button in angularjs?

How can I display a list multiple times when a user clicks on a button in AngularJS? ...