Three.js: Matrix4: Don't require all inputs for decompose() or add other way to extract position

Created on 21 Feb 2020  路  5Comments  路  Source: mrdoob/three.js

I often find myself getting the world position of an object via object.matrixWorld.decompose(position, quaternion, scale), without any need to get the quaternion or scale. However, not passing in a valid quaternion/euler or scale results in an error.

https://github.com/mrdoob/three.js/blob/c23b245814ac71999d8303c07434fcfd244288dd/src/math/Matrix4.js#L785-L789

Would it be possible ignore undefined parameters with something like the following pseudo code?

if (position) {
    calculate and set position;
}

if (quaternion) {
    calculate and set quaternion;
}

if (scale) {
    calculate and set scale;
}

Alternatively, could a extractPosition() function and maybe a extractScale() function be added to go along with extractRotation()?

Question

Most helpful comment

You can use these Vector3 methods:

vector.setFromMatrixPosition( matrix );

vector.setFromMatrixScale( matrix );

All 5 comments

You can use these Vector3 methods:

vector.setFromMatrixPosition( matrix );

vector.setFromMatrixScale( matrix );

You can use these Vector3 methods:

vector.setFromMatrixPosition( matrix );

vector.setFromMatrixScale( matrix );

Ah, that's how it's done. Thank you very much!

I actually ran into this before because I wasn't aware of the Vector3 functions for getting translation / scale from a Matrix4.

Maybe it's worth adding a link in the docs, for functions like Matrix4.setPosition, to their corresponding getter functions like Vector3.setFromMatrixPosition?

Sounds good to me.
Feel free to send a PR with the enhancement 馃檹

Added to the docs in #18767

Was this page helpful?
0 / 5 - 0 ratings