decompose method

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

Implementation

Matrix4 decompose(Vector3 position, Quaternion quaternion, Vector3 scale) {
  var te = elements;

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

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

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

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

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

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

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

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

  quaternion.setFromRotationMatrix(_matrix4m1);

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

  return this;
}