Three.js: [BUG] ObjectLoader does not update object matrix

Created on 7 Jan 2018  路  6Comments  路  Source: mrdoob/three.js

Description of the problem

When loading an object with ObjectLoader, the matrix of the object is not read. This results in all read objects to have an identity matrix, breaking all groups and all positioning. An updateMatrixWorld must be executed on the imported object in order for it to be correctly positioned.

Explanation
const objectLoader = new ObjectLoader();
const object = objectLoader.parse(myModel);
// Now we have:
// object.children[0].matrix == [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
// But it should not be this value!

In order to have the correct matrices, I had to call updateMatrixWorld on the resulting object:

const objectLoader = new ObjectLoader();
const object = objectLoader.parse(myModel);
object.updateMatrixWorld();
// Now we have the correct value:
// object.children[0].matrix ==correct value;

Related lines of code in three js codebase:

https://github.com/mrdoob/three.js/blob/dev/src/loaders/ObjectLoader.js#L800

Here we see that object.matrix is not set. We should do it here.

Three.js version
  • [ ] Dev
  • [x] r89
  • [ ] ...
Browser
  • [x] All of them
  • [ ] Chrome
  • [ ] Firefox
  • [ ] Internet Explorer
OS
  • [x] All of them
  • [ ] Windows
  • [ ] macOS
  • [ ] Linux
  • [ ] Android
  • [ ] iOS

All 6 comments

When loading an object with ObjectLoader, the matrix of the object is not read.

It is read; it is just not set. The position, quaternion, and scale are set, instead.

We could set the matrix, too.

object.matrix.fromArray( data.matrix );
object.matrix.decompose( object.position, object.quaternion, object.scale );

We could set the matrix, too.

I vote for this change. Honestly, i also would expect that the matrix is set correctly.

It is read; it is just not set. The position, quaternion, and scale are set, instead.

You are right, and I saw that in the source code that I linked initially.

Here I just tried to explain simply the situation. From an user "black box" perspective of my use case, the matrix could not have been read at all and the result would be the same.

Honestly, i also would expect that the matrix is set correctly.

Yes, me too, I spent quite some time understanding why my rayCaster did not work before understanding that the bug was actually during the import of my model. I did not expect my imported model to have all its children at the origin, with an identity matrix, when I designed them to be in various positions and orientations.

@crubier

I spent quite some time understanding why my rayCaster did not work

The change I proposed will not solve your problem. Raycaster requires updated world matrices; ObjectLoader does not update matrixWorld. The renderer calls updateMatrixWorld() for you.

Are you raycasting without adding the object to the scene?

the matrix could not have been read at all and the result would be the same.

If the matrix were not read, then your use of updateMatrixWorld() would not have worked.

the bug was actually during the import of my model

This is not a bug. The object's transform is set via position, quaternion, and scale, instead. I do not object to making the code change, however.

Ok things are getting more clear to me now.

Are you raycasting without adding the object to the scene?

No BUT I do not use a renderer at all, since I use Three to perform spatial computation only, with no rendering, on node JS.
Adding the imported object to the scene does not solve the problem, but I suspect this is normal, because the scene is never rendered since I do not render anything.

If the matrix were not read, then your use of updateMatrixWorld() would not have worked.

True.

The object's transform is set via position, quaternion, and scale, instead. I do not object to making the code change, however.

Would this code change solve my problem, or would I always need to call updateMatrixWorld() manually anyway in my situation ?

You would always need to call updateMatrixWorld() manually in your situation.

Fixed. Thank you for the report. :)

Was this page helpful?
0 / 5 - 0 ratings