Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get world scale #7116

Closed
wants to merge 1 commit into from
Closed

Get world scale #7116

wants to merge 1 commit into from

Conversation

simonThiele
Copy link
Contributor

Maybe I'm getting it wrong, but why is the hole matrix being decomposed if only the scale part is needed?
If it's not necessary, this PR just uses setFromMatrixScale to extract the scale from worldMatrix. Please check unit test if I missed any situation where decomposing and setFromMatrixScale may differ and please ignore first two commits (the second one reverts the first one) I just forgot to create a branch. Thx.

@simonThiele
Copy link
Contributor Author

commented code is removed

@arose
Copy link
Contributor

arose commented Sep 6, 2015

Looks good.

Another thing, looking at the Matrix4.decompose function I wonder if scale is correct when the determinant is below zero or if sx needs to be inverted again before assigning to scale.x?!

// if determine is negative, we need to invert one scale
var det = this.determinant();
if ( det < 0 ) {
    sx = - sx;
}
// ...
scale.x = sx;
scale.y = sy;
scale.z = sz;

function checkIfDecomposeAndGetWorldScaleAreEqual(obj) {
var result = new THREE.Vector3();

// this is the old way to get scale: decompose the complete matrix and return scale part
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could re-phrase these comments, removing the reference to old and new ways, which are quite specific to this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

@simonThiele
Copy link
Contributor Author

Yeah I was wondering about the same but it should also be implemented in setFromMatrixScale but it's not. Don't know why.


checkIfDecomposeAndGetWorldScaleAreEqual(obj);

obj.scale.set(0, 0, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why zero scale?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To test all ways of how an object can be scaled. It does not really matter in production code but should be checked what happens if a matrix is empty (other than the 1 on 3,3)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

three.js does not support zero-determinant transforms. Most of your tests have a zero scale in at least one dimension. We only need to test what is supported.

However, if you would like to improve the library so zero-determinant transforms work, that might be useful. I am not sure if there is a compelling reason to do so, however...

@WestLangley
Copy link
Collaborator

why is the [whole] matrix being decomposed if only the scale part is needed?

Your approach can get a different answer in cases like the following:

mesh.scale.set( - 1, 1, 1 );
mesh.updateMatrix();
var vec1 = mesh.getWorldScale();
var vec2 = new THREE.Vector3().setFromMatrixScale( mesh.matrix );

three.js does not officially support reflections in the object matrix, but we have done our best to do something reasonable in the math library.

If anyone wants to try to break the library, that would be helpful, and we can figure out what to do about it.

this.matrixWorld.decompose( position, quaternion, result );

return result;
return result.setFromMatrixScale( this.matrixWorld );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not make this change at this point. It can produce a different answer from the current method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because the decompose generates a -sx at negative determinant. But why isn't that necessary on setFromMatrixScale? @mrdoob

@simonThiele
Copy link
Contributor Author

@mrdoob Do you know why vector.setFromMatrixScale doesn't contain the xScale switching ?

var det = this.determinant();
if ( det < 0 ) {
   sx = - sx;
}

@WestLangley
Copy link
Collaborator

@simonThiele See #4272 for a discussion of the issues involved.

Also, experiment with this example.

mesh.position.set( 0, 0, 0 );
mesh.rotation.set( 0, 0, 0 );
mesh.scale.set( 1, 1, - 1 );
mesh.updateMatrix();

var p = new THREE.Vector3();
var q = new THREE.Quaternion();
var s = new THREE.Vector3();
mesh.matrix.decompose( p, q, s );
console.log( p );
console.log( q );
console.log( s );

@mrdoob
Copy link
Owner

mrdoob commented Sep 16, 2015

I'll wait for @WestLangley approval on this one 😉

@threejsworker
Copy link

The examples of this pullrequest are now built and visible in threejsworker. To view them, go to the following link:

http://threejsworker.com/viewpullrequest.html#7116

@mrdoob mrdoob closed this Oct 15, 2015
@simonThiele simonThiele deleted the getWorldScale branch November 16, 2015 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants