fromQuat function

List<double> fromQuat(
  1. List<double> out,
  2. List<double> q
)

Calculates a 4x4 matrix from the given quaternion

@param {mat4} out mat4 receiving operation result @param {ReadonlyQuat} q Quaternion to create matrix from

@returns {mat4} out

Implementation

List<double> fromQuat(List<double> out, List<double> q) {
  final x = q[0], y = q[1], z = q[2], w = q[3];
  final x2 = x + x;
  final y2 = y + y;
  final z2 = z + z;

  final xx = x * x2;
  final yx = y * x2;
  final yy = y * y2;
  final zx = z * x2;
  final zy = z * y2;
  final zz = z * z2;
  final wx = w * x2;
  final wy = w * y2;
  final wz = w * z2;

  out[0] = 1 - yy - zz;
  out[1] = yx + wz;
  out[2] = zx - wy;
  out[3] = 0;

  out[4] = yx - wz;
  out[5] = 1 - xx - zz;
  out[6] = zy + wx;
  out[7] = 0;

  out[8] = zx + wy;
  out[9] = zy - wx;
  out[10] = 1 - xx - yy;
  out[11] = 0;

  out[12] = 0;
  out[13] = 0;
  out[14] = 0;
  out[15] = 1;

  return out;
}