fromRotationTranslationScaleOrigin function

List<double> fromRotationTranslationScaleOrigin(
  1. List<double> out,
  2. List<double> q,
  3. List<double> v,
  4. List<double> s,
  5. List<double> o,
)

Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin This is equivalent to (but much faster than):

mat4.identity(dest);
mat4.translate(dest, vec);
mat4.translate(dest, origin);
final quatMat = mat4.create();
quat4.toMat4(quat, quatMat);
mat4.multiply(dest, quatMat);
mat4.scale(dest, scale)
mat4.translate(dest, negativeOrigin);

@param {mat4} out mat4 receiving operation result @param {quat4} q Rotation quaternion @param {ReadonlyVec3} v Translation vector @param {ReadonlyVec3} s Scaling vector @param {ReadonlyVec3} o The origin vector around which to scale and rotate @returns {mat4} out

Implementation

List<double> fromRotationTranslationScaleOrigin(
    List<double> out, List<double> q, List<double> v, List<double> s, List<double> o) {
  // Quaternion math
  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 xy = x * y2;
  final xz = x * z2;
  final yy = y * y2;
  final yz = y * z2;
  final zz = z * z2;
  final wx = w * x2;
  final wy = w * y2;
  final wz = w * z2;

  final sx = s[0];
  final sy = s[1];
  final sz = s[2];

  final ox = o[0];
  final oy = o[1];
  final oz = o[2];

  final out0 = (1 - (yy + zz)) * sx;
  final out1 = (xy + wz) * sx;
  final out2 = (xz - wy) * sx;
  final out4 = (xy - wz) * sy;
  final out5 = (1 - (xx + zz)) * sy;
  final out6 = (yz + wx) * sy;
  final out8 = (xz + wy) * sz;
  final out9 = (yz - wx) * sz;
  final out10 = (1 - (xx + yy)) * sz;

  out[0] = out0;
  out[1] = out1;
  out[2] = out2;
  out[3] = 0;
  out[4] = out4;
  out[5] = out5;
  out[6] = out6;
  out[7] = 0;
  out[8] = out8;
  out[9] = out9;
  out[10] = out10;
  out[11] = 0;
  out[12] = v[0] + ox - (out0 * ox + out4 * oy + out8 * oz);
  out[13] = v[1] + oy - (out1 * ox + out5 * oy + out9 * oz);
  out[14] = v[2] + oz - (out2 * ox + out6 * oy + out10 * oz);
  out[15] = 1;

  return out;
}