As I understand it from here, we should use it when we want to assign global position to the mesh but using local coordinates of other mesh. But why? We can just parent one mesh to another and always use its local coordinates. In case if we still want to track global coordinates of child mesh, we can do getAbsolutePosition()
. In this case we can just forget about computeWorldMatrix()
and any coordinate transformations.
What are other use cases of computeWorldMatrix()
?
I worry that I am missing very important concept of 3D world. So, I would like to fix it once and for all.
When a mesh is created the vertexData for the mesh is maintained unless updated by the user. The position, rotation, scale of the mesh is stored in its worldMatrix which is then used to render the mesh in the correct placement and orientation. The worldMatrix is calculated, normally, just before rendering takes place.
When you write code that alters the position, rotation or scale of a mesh there will be times, as per your example, when there will be need for the worldMatrix to be known to change the state of the mesh. Since no rendering takes place when you write the code to implement the change of state in these cases the worldMatrix needs to be computed first.
Also if you search the forum for computeWorldMatrix
you will find the examples where this is offered as a solution to give you more understanding.
@JohnK , let me explain what you said using my own words, so I’ll be sure that I got it right.
So, when we modify position, rotation or scale properties, we don’t actually change the mesh yet. We save those transformations somewhere else. Later, the new frame generation triggers computeWorldMatrix(true)
on each mesh, and this is when all transformations are actually flushed to meshes. Is that correct?
So, that means, that if we want, for example, check meshes intersections right after we changed position, rotation or scale properties, we need to call computeWorldMatrix(true)
on meshes of interest to flush those transformation manually, because new frame wasn’t generated yet. Is that correct?
Depends if you are using scene.onAfterRenderObservable
and doing the intersection check within this loop then computing the world matrix will have already been done. Otherwise yes.
I am revisiting some old bugs in my project, so I would like to clarify what exactly is stored in the .worldMatrix
? I understand that this is 4x4 mathematical Matrix, but what does each cell represent?
It would help me to debug.
It stores a transformation matrix as TRS translation rotation and scale Transformation matrix - Wikipedia