Transform3D.fromMatrix4 constructor

Transform3D.fromMatrix4(
  1. Matrix4 matrix
)

This class describes a generic 3D transform, which is a combination of translations, rotations and scaling. These transforms are combined into a single matrix, that can be either applied to a graphical device like the canvas, composed with another transform, or used directly to convert coordinates.

The transform can be visualized as 2 reference frames: a "global" and a "local". At first, these two reference frames coincide. Then, the following sequence of transforms is applied:

  • translation to point position;
  • rotate using the rotation;
  • scaling in X, Y and Z directions by scale factors.

The class is optimized for repeated use: the transform matrix is cached and then recalculated only when the underlying properties change. Moreover, recalculation of the transform is postponed until the matrix is actually requested by the user. Thus, modifying multiple properties at once does not incur the penalty of unnecessary recalculations.

This class implements the ChangeNotifier API, allowing you to subscribe for notifications whenever the transform matrix changes. In addition, you can subscribe to get notified when individual components of the transform change: position, scale, and rotation.

Create an instance of Transform3D and apply the matrix on it.

Implementation

factory Transform3D.fromMatrix4(Matrix4 matrix) {
  final transform = Transform3D();
  matrix.decompose(transform.position, transform.rotation, transform.scale);
  return transform;
}