decompose method

Matrix4 decompose(
  1. Vector3 position,
  2. Quaternion quaternion,
  3. Vector3 scale
)

Implementation

Matrix4 decompose(Vector3 position, Quaternion quaternion, Vector3 scale) {
  final te = storage;

  double sx = (_matrix4v1.setValues(te[0], te[1], te[2])).length;
  final sy = (_matrix4v1.setValues(te[4], te[5], te[6])).length;
  final sz = (_matrix4v1.setValues(te[8], te[9], te[10])).length;

  // if determine is negative, we need to invert one scale
  final det = determinant();
  if (det < 0) sx = -sx;

  position.x = te[12];
  position.y = te[13];
  position.z = te[14];

  // scale the rotation part
  _matrix4m1.setFrom(this);

  final invSX = 1 / sx;
  final invSY = 1 / sy;
  final invSZ = 1 / sz;

  _matrix4m1.storage[0] *= invSX;
  _matrix4m1.storage[1] *= invSX;
  _matrix4m1.storage[2] *= invSX;

  _matrix4m1.storage[4] *= invSY;
  _matrix4m1.storage[5] *= invSY;
  _matrix4m1.storage[6] *= invSY;

  _matrix4m1.storage[8] *= invSZ;
  _matrix4m1.storage[9] *= invSZ;
  _matrix4m1.storage[10] *= invSZ;

  quaternion.setFromRotationMatrix(_matrix4m1);

  scale.x = sx;
  scale.y = sy;
  scale.z = sz;

  return this;
}