DEV Community

Reene
Reene

Posted on

[WebGL2.0][Basic]scene graph

Transition Matrix: localMatrix and worldMatrix

In 3D graphics rendering, both localMatrix and worldMatrix are transformation matrices used to describe the position, rotation, and scaling of an object in different spaces. Multiplying these matrices is necessary to progressively apply the transformations of each object in the hierarchy to the overall scene’s world coordinate system. Here’s a deeper explanation of why these matrices are multiplied and their specific roles.

1. localMatrix and worldMatrix

  • localMatrix: Represents the transformation of the current node (object) in its local coordinate space. This space is relative to its parent node, meaning the object’s position, rotation, and scaling are all described relative to its parent. Any child nodes will inherit this transformation.

  • localMatrix describe itself information rather than a single coordinate system, same as worldMatrix.

  • worldMatrix: Represents the transformation of the current node in the world (or scene) coordinate space. This matrix transforms the node from its local coordinate space all the way up to the root node of the scene graph, effectively placing it in world space.

2. Why Multiply the Matrices?

Matrix multiplication is used to apply transformations from one space to another. For instance:

  • The parent node’s worldMatrix represents its position and orientation in the world space.
  • The current node’s localMatrix represents its position and orientation relative to the parent node.

To compute the position and orientation of the current node in world space, we multiply the parent node’s worldMatrix by the current node’s localMatrix. This multiplication effectively applies the parent’s transformation, followed by the current node’s transformation, resulting in the current node’s worldMatrix.

3. Example: Earth and Moon

Consider a scenario where the Earth orbits the Sun, and the Moon orbits the Earth. Using matrices, we can represent these transformations:

  • Earth’s worldMatrix describes its position and rotation around the Sun in the world (solar system) space.
  • Moon’s localMatrix describes its position and rotation around the Earth, in Earth’s local space.

To compute the Moon’s position in the world space, we multiply Earth’s worldMatrix by Moon’s localMatrix, resulting in Moon’s worldMatrix. This way, the Moon orbits Earth, which in turn orbits the Sun, capturing the hierarchical transformation relationship.

4. Conclusion

By multiplying matrices, we can apply each level’s transformation within a hierarchy (like a scene graph), obtaining the final position and orientation of each node in world coordinates. This method is fundamental in 3D scenes, especially for complex parent-child transformation relationships.

Top comments (0)