Source: lib/teal_book/cuon-matrix.js

/**
 * @file
 *
 * Summary.
 * <p>This is a class for handling 4x4 matrices.</p>
 *
 * It contains functions to create geometric transformations equivalent to OpenGL's:
 * <ul>
 *  <li>{@link https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glLoadIdentity.xml glLoadIdentity}</li>
 *  <li>{@link https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml glRotate}</li>
 *  <li>{@link https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glScale.xml glScale}</li>
 *  <li>{@link https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml glTranslate}</li>
 *  <li>{@link https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/gluLookAt.xml gluLookAt}</li>
 *  <li>{@link https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml gluPerspective}</li>
 *  <li>{@link https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml glOrtho}</li>
 *  <li>{@link https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml glFrustum}</li>
 * </ul>
 *
 * This matrix is then right multiplied by one of those transformations.<br>
 *
 * @author Kanda and Matsuda
 * @copyright © 2012 Kanda and Matsuda
 * @since 28/09/2015
 * @see <a href="/cwdc/13-webgl/lib/teal_book/cuon-matrix.js">source</a>
 */

/**
 * <p>Constructor of Matrix4.</p>
 * If opt_src is specified, new matrix is initialized by opt_src.<br>
 * Otherwise, new matrix is initialized by identity matrix.
 * @constructs Matrix4
 * @param {Array<Number>} opt_src source matrix (optional).
 */
var Matrix4 = function (opt_src) {
  var i, s, d;
  if (
    opt_src &&
    typeof opt_src === "object" &&
    opt_src.hasOwnProperty("elements")
  ) {
    s = opt_src.elements;
    d = new Float32Array(16);
    for (i = 0; i < 16; ++i) {
      d[i] = s[i];
    }
    /**
     * Matrix container.
     * @type {Float32Array}
     */
    this.elements = d;
  } else {
    this.elements = new Float32Array([
      1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
    ]);
  }
};

/**
 * Set the identity matrix.
 * @return {Matrix4} this matrix.
 */
// prettier-ignore
Matrix4.prototype.setIdentity = function () {
  var e = this.elements;
  e[0] = 1; e[4] = 0; e[8]  = 0; e[12] = 0;
  e[1] = 0; e[5] = 1; e[9]  = 0; e[13] = 0;
  e[2] = 0; e[6] = 0; e[10] = 1; e[14] = 0;
  e[3] = 0; e[7] = 0; e[11] = 0; e[15] = 1;
  return this;
};

/**
 * Copy src to this matrix.
 * @param {Matrix4} src source matrix
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.set = function (src) {
  var i, s, d;

  s = src.elements;
  d = this.elements;

  if (s === d) {
    return;
  }

  for (i = 0; i < 16; ++i) {
    d[i] = s[i];
  }

  return this;
};

/**
 * Right multiply this matrix by other.
 * @param {Matrix4} other a matrix.
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.concat = function (other) {
  var i, e, a, b, ai0, ai1, ai2, ai3;

  // Calculate e = a * b
  e = this.elements;
  a = this.elements;
  b = other.elements;

  // If e equals b, copy b to temporary matrix.
  if (e === b) {
    b = new Float32Array(16);
    for (i = 0; i < 16; ++i) {
      b[i] = e[i];
    }
  }

  for (i = 0; i < 4; i++) {
    ai0 = a[i];
    ai1 = a[i + 4];
    ai2 = a[i + 8];
    ai3 = a[i + 12];
    e[i] = ai0 * b[0] + ai1 * b[1] + ai2 * b[2] + ai3 * b[3];
    e[i + 4] = ai0 * b[4] + ai1 * b[5] + ai2 * b[6] + ai3 * b[7];
    e[i + 8] = ai0 * b[8] + ai1 * b[9] + ai2 * b[10] + ai3 * b[11];
    e[i + 12] = ai0 * b[12] + ai1 * b[13] + ai2 * b[14] + ai3 * b[15];
  }

  return this;
};

/**
 * Right multiply this matrix by other.
 * @param {Matrix4} other a matrix.
 * @return {Matrix4} this matrix.
 * @function
 */
Matrix4.prototype.multiply = Matrix4.prototype.concat;

/**
 * Multiply this 3D vector by pos.
 * @param {Vector3} pos a 3D vector.
 * @return {Float32Array} result of multiplication.
 */
Matrix4.prototype.multiplyVector3 = function (pos) {
  var e = this.elements;
  var p = pos.elements;
  var v = new Vector3();
  var result = v.elements;

  result[0] = p[0] * e[0] + p[1] * e[4] + p[2] * e[8] + e[11];
  result[1] = p[0] * e[1] + p[1] * e[5] + p[2] * e[9] + e[12];
  result[2] = p[0] * e[2] + p[1] * e[6] + p[2] * e[10] + e[13];

  return v;
};

/**
 * Multiply this 4D vector by pos.
 * @param {Vector4} pos a 4D vector.
 * @return {Float32Array} result of multiplication.
 */
Matrix4.prototype.multiplyVector4 = function (pos) {
  var e = this.elements;
  var p = pos.elements;
  var v = new Vector4();
  var result = v.elements;

  result[0] = p[0] * e[0] + p[1] * e[4] + p[2] * e[8] + p[3] * e[12];
  result[1] = p[0] * e[1] + p[1] * e[5] + p[2] * e[9] + p[3] * e[13];
  result[2] = p[0] * e[2] + p[1] * e[6] + p[2] * e[10] + p[3] * e[14];
  result[3] = p[0] * e[3] + p[1] * e[7] + p[2] * e[11] + p[3] * e[15];

  return v;
};

/**
 * Transpose this matrix.
 * @return {Matrix4} this matrix.
 */
// prettier-ignore
Matrix4.prototype.transpose = function () {
  var e, t;

  e = this.elements;

  t = e[1];  e[1]  = e[4];  e[4]  = t;
  t = e[2];  e[2]  = e[8];  e[8]  = t;
  t = e[3];  e[3]  = e[12]; e[12] = t;
  t = e[6];  e[6]  = e[9];  e[9]  = t;
  t = e[7];  e[7]  = e[13]; e[13] = t;
  t = e[11]; e[11] = e[14]; e[14] = t;

  return this;
};

/**
 * Set this matrix to the inverse of other.
 * @param {Matrix4} other source matrix.
 * @return {Matrix4} this matrix.
 */
// prettier-ignore
Matrix4.prototype.setInverseOf = function (other) {
  var i, s, d, inv, det;

  s = other.elements;
  d = this.elements;
  inv = new Float32Array(16);

  inv[0] =    s[5] * s[10] * s[15] - s[5]  * s[11] * s[14] - s[9]  * s[6] * s[15]
            + s[9] * s[7]  * s[14] + s[13] * s[6]  * s[11] - s[13] * s[7] * s[10];
  inv[4] =  - s[4] * s[10] * s[15] + s[4]  * s[11] * s[14] + s[8]  * s[6] * s[15]
            - s[8] * s[7]  * s[14] - s[12] * s[6]  * s[11] + s[12] * s[7] * s[10];
  inv[8] =    s[4] * s[9]  * s[15] - s[4]  * s[11] * s[13] - s[8]  * s[5] * s[15]
            + s[8] * s[7]  * s[13] + s[12] * s[5]  * s[11] - s[12] * s[7] * s[9];
  inv[12] = - s[4] * s[9]  * s[14] + s[4]  * s[10] * s[13] + s[8]  * s[5] * s[14]
            - s[8] * s[6]  * s[13] - s[12] * s[5]  * s[10] + s[12] * s[6] * s[9];

  inv[1] =  - s[1] * s[10] * s[15] + s[1]  * s[11] * s[14] + s[9]  * s[2] * s[15]
            - s[9] * s[3]  * s[14] - s[13] * s[2]  * s[11] + s[13] * s[3] * s[10];
  inv[5] =    s[0] * s[10] * s[15] - s[0]  * s[11] * s[14] - s[8]  * s[2] * s[15]
            + s[8] * s[3]  * s[14] + s[12] * s[2]  * s[11] - s[12] * s[3] * s[10];
  inv[9] =  - s[0] * s[9]  * s[15] + s[0]  * s[11] * s[13] + s[8]  * s[1] * s[15]
            - s[8] * s[3]  * s[13] - s[12] * s[1]  * s[11] + s[12] * s[3] * s[9];
  inv[13] =   s[0] * s[9]  * s[14] - s[0]  * s[10] * s[13] - s[8]  * s[1] * s[14]
            + s[8] * s[2]  * s[13] + s[12] * s[1]  * s[10] - s[12] * s[2] * s[9];

  inv[2] =    s[1] * s[6] * s[15] - s[1]  * s[7] * s[14] - s[5]  * s[2] * s[15]
            + s[5] * s[3] * s[14] + s[13] * s[2] * s[7]  - s[13] * s[3] * s[6];
  inv[6] =  - s[0] * s[6] * s[15] + s[0]  * s[7] * s[14] + s[4]  * s[2] * s[15]
            - s[4] * s[3] * s[14] - s[12] * s[2] * s[7]  + s[12] * s[3] * s[6];
  inv[10] =   s[0] * s[5] * s[15] - s[0]  * s[7] * s[13] - s[4]  * s[1] * s[15]
            + s[4] * s[3] * s[13] + s[12] * s[1] * s[7]  - s[12] * s[3] * s[5];
  inv[14] = - s[0] * s[5] * s[14] + s[0]  * s[6] * s[13] + s[4]  * s[1] * s[14]
            - s[4] * s[2] * s[13] - s[12] * s[1] * s[6]  + s[12] * s[2] * s[5];

  inv[3] =  - s[1] * s[6] * s[11] + s[1] * s[7] * s[10] + s[5] * s[2] * s[11]
            - s[5] * s[3] * s[10] - s[9] * s[2] * s[7]  + s[9] * s[3] * s[6];
  inv[7] =    s[0] * s[6] * s[11] - s[0] * s[7] * s[10] - s[4] * s[2] * s[11]
            + s[4] * s[3] * s[10] + s[8] * s[2] * s[7]  - s[8] * s[3] * s[6];
  inv[11] = - s[0] * s[5] * s[11] + s[0] * s[7] * s[9]  + s[4] * s[1] * s[11]
            - s[4] * s[3] * s[9]  - s[8] * s[1] * s[7]  + s[8] * s[3] * s[5];
  inv[15] =   s[0] * s[5] * s[10] - s[0] * s[6] * s[9]  - s[4] * s[1] * s[10]
            + s[4] * s[2] * s[9]  + s[8] * s[1] * s[6]  - s[8] * s[2] * s[5];

  det = s[0] * inv[0] + s[1] * inv[4] + s[2] * inv[8] + s[3] * inv[12];
  if (det === 0) {
    return this;
  }

  det = 1 / det;
  for (i = 0; i < 16; i++) {
    d[i] = inv[i] * det;
  }

  return this;
};

/**
 * Invert this matrix.
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.invert = function () {
  return this.setInverseOf(this);
};

/**
 * Set the orthographic projection matrix.
 * @param {Number} left coordinate of the left clipping plane.
 * @param {Number} right coordinate of the right clipping plane.
 * @param {Number} bottom coordinate of the bottom clipping plane.
 * @param {Number} top coordinate of the top clipping plane.
 * @param {Number} near distance to the near clipping plane.<br>
 *                      This value is negative if the plane is behind the viewer.
 * @param {Number}far distance to the far clipping plane.<br>
 *                    This value is negtive if the plane is behind the viewer.
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.setOrtho = function (left, right, bottom, top, near, far) {
  var e, rw, rh, rd;

  if (left === right || bottom === top || near === far) {
    throw "null frustum";
  }

  rw = 1 / (right - left);
  rh = 1 / (top - bottom);
  rd = 1 / (far - near);

  e = this.elements;

  e[0] = 2 * rw;
  e[1] = 0;
  e[2] = 0;
  e[3] = 0;

  e[4] = 0;
  e[5] = 2 * rh;
  e[6] = 0;
  e[7] = 0;

  e[8] = 0;
  e[9] = 0;
  e[10] = -2 * rd;
  e[11] = 0;

  e[12] = -(right + left) * rw;
  e[13] = -(top + bottom) * rh;
  e[14] = -(far + near) * rd;
  e[15] = 1;

  return this;
};

/**
 * Right multiply this matrix by an orthographic projection matrix.
 * @param {Number} left coordinate of the left clipping plane.
 * @param {Number} right coordinate of the right clipping plane.
 * @param {Number} bottom coordinate of the bottom clipping plane.
 * @param {Number} top coordinate of the top clipping plane.
 * @param {Number} near distance to the near clipping plane.<br>
 *                      This value is negative if the plane is behind the viewer.
 * @param {Number} far distance to the far clipping plane.<br>
 *                     This value is negative if the plane is behind the viewer.
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.ortho = function (left, right, bottom, top, near, far) {
  return this.concat(
    new Matrix4().setOrtho(left, right, bottom, top, near, far),
  );
};

/**
 * Set the perspective projection matrix.
 * @param {Number} left coordinate of the left clipping plane.
 * @param {Number} right coordinate of the right clipping plane.
 * @param {Number} bottom coordinate of the bottom clipping plane.
 * @param {Number} top coordinate of the top clipping plane.
 * @param {Number} near distance to the near clipping plane. <br>
 *                      This value must be positive.
 * @param {Number} far distance to the far clipping plane. <br>
 *                     This value must be positive.
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.setFrustum = function (left, right, bottom, top, near, far) {
  var e, rw, rh, rd;

  if (left === right || top === bottom || near === far) {
    throw "null frustum";
  }
  if (near <= 0) {
    throw "near <= 0";
  }
  if (far <= 0) {
    throw "far <= 0";
  }

  rw = 1 / (right - left);
  rh = 1 / (top - bottom);
  rd = 1 / (far - near);

  e = this.elements;

  e[0] = 2 * near * rw;
  e[1] = 0;
  e[2] = 0;
  e[3] = 0;

  e[4] = 0;
  e[5] = 2 * near * rh;
  e[6] = 0;
  e[7] = 0;

  e[8] = (right + left) * rw;
  e[9] = (top + bottom) * rh;
  e[10] = -(far + near) * rd;
  e[11] = -1;

  e[12] = 0;
  e[13] = 0;
  e[14] = -2 * near * far * rd;
  e[15] = 0;

  return this;
};

/**
 * Right multiply this matrix by a perspective projection matrix.
 * @param {Number} left coordinate of the left clipping plane.
 * @param {Number} right coordinate of the right clipping plane.
 * @param {Number} bottom coordinate of the bottom clipping plane.
 * @param {Number} top coordinate of the top clipping plane.
 * @param {Number} near distance to the near clipping plane. <br>
 *                      This value must be positive.
 * @param {Number} far distance to the far clipping plane. <br>
 *                     This value must be positive.
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.frustum = function (left, right, bottom, top, near, far) {
  return this.concat(
    new Matrix4().setFrustum(left, right, bottom, top, near, far),
  );
};

/**
 * Set the perspective projection matrix.
 * @param {Number} fovy field of view angle between the upper and lower sides of the view frustum.
 * @param {Number} aspect aspect ratio of the view frustum (width/height).
 * @param {Number} near distance to the near clipping plane. <br>
 *                      This value must be positive.
 * @param {Number} far distance to the far clipping plane. <br>
 *                     This value must be positive.
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.setPerspective = function (fovy, aspect, near, far) {
  var e, rd, s, ct;

  if (near === far || aspect === 0) {
    throw "null frustum";
  }
  if (near <= 0) {
    throw "near <= 0";
  }
  if (far <= 0) {
    throw "far <= 0";
  }

  fovy = (Math.PI * fovy) / 180 / 2;
  s = Math.sin(fovy);
  if (s === 0) {
    throw "null frustum";
  }

  rd = 1 / (far - near);
  ct = Math.cos(fovy) / s;

  e = this.elements;

  e[0] = ct / aspect;
  e[1] = 0;
  e[2] = 0;
  e[3] = 0;

  e[4] = 0;
  e[5] = ct;
  e[6] = 0;
  e[7] = 0;

  e[8] = 0;
  e[9] = 0;
  e[10] = -(far + near) * rd;
  e[11] = -1;

  e[12] = 0;
  e[13] = 0;
  e[14] = -2 * near * far * rd;
  e[15] = 0;

  return this;
};

/**
 * Right multiply this matrix by a perspective projection matrix.
 * @param {Number} fovy field of view angle between the upper and lower sides of the view frustum.
 * @param {Number} aspect aspect ratio of the view frustum (width/height).
 * @param {Number} near distance to the near clipping plane. <br>
 *                      This value must be positive.
 * @param {Number} far distance to the far clipping plane. <br>
 *                     This value must be positive.
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.perspective = function (fovy, aspect, near, far) {
  return this.concat(new Matrix4().setPerspective(fovy, aspect, near, far));
};

/**
 * Set the matrix for scaling.
 * @param {Number} x scale factor along the X axis.
 * @param {Number} y scale factor along the Y axis.
 * @param {Number} z scale factor along the Z axis.
 * @return {Matrix4} this matrix.
 */
// prettier-ignore
Matrix4.prototype.setScale = function (x, y, z) {
  var e = this.elements;
  e[0] = x; e[4] = 0; e[8]  = 0; e[12] = 0;
  e[1] = 0; e[5] = y; e[9]  = 0; e[13] = 0;
  e[2] = 0; e[6] = 0; e[10] = z; e[14] = 0;
  e[3] = 0; e[7] = 0; e[11] = 0; e[15] = 1;
  return this;
};

/**
 * Right multiply this matrix by a scale matrix.
 * @param {Number} x scale factor along the X axis.
 * @param {Number} y scale factor along the Y axis.
 * @param {Number} z scale factor along the Z axis.
 * @return {Matrix4} this matrix.
 */
// prettier-ignore
Matrix4.prototype.scale = function (x, y, z) {
  var e = this.elements;
  e[0] *= x; e[4] *= y; e[8]  *= z;
  e[1] *= x; e[5] *= y; e[9]  *= z;
  e[2] *= x; e[6] *= y; e[10] *= z;
  e[3] *= x; e[7] *= y; e[11] *= z;
  return this;
};

/**
 * Set the matrix for translation.
 * @param {Number} x translation in the X direction.
 * @param {Number} y translation in the Y direction.
 * @param {Number} z translation in the Z direction.
 * @return {Matrix4} this matrix.
 */
// prettier-ignore
Matrix4.prototype.setTranslate = function (x, y, z) {
  var e = this.elements;
  e[0] = 1; e[4] = 0; e[8]  = 0; e[12] = x;
  e[1] = 0; e[5] = 1; e[9]  = 0; e[13] = y;
  e[2] = 0; e[6] = 0; e[10] = 1; e[14] = z;
  e[3] = 0; e[7] = 0; e[11] = 0; e[15] = 1;
  return this;
};

/**
 * Right multiply this matrix by a translation matrix.
 * @param {Number} x translation in the X direction.
 * @param {Number} y translation in the Y direction.
 * @param {Number} z translation in the Z direction.
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.translate = function (x, y, z) {
  var e = this.elements;
  e[12] += e[0] * x + e[4] * y + e[8] * z;
  e[13] += e[1] * x + e[5] * y + e[9] * z;
  e[14] += e[2] * x + e[6] * y + e[10] * z;
  e[15] += e[3] * x + e[7] * y + e[11] * z;
  return this;
};

/**
 * <p>Set the matrix for rotation.</p>
 * The rotation axis vector may not be normalized.
 * @param {Number} angle angle of rotation (degrees).
 * @param {Number} x X coordinate of rotation axis vector.
 * @param {Number} y Y coordinate of rotation axis vector.
 * @param {Number} z Z coordinate of rotation axis vector.
 * @return {Matrix4} this matrix.
 */
// prettier-ignore
Matrix4.prototype.setRotate = function (angle, x, y, z) {
  var e, s, c, len, rlen, nc, xy, yz, zx, xs, ys, zs;

  angle = (Math.PI * angle) / 180;
  e = this.elements;

  s = Math.sin(angle);
  c = Math.cos(angle);

  if (0 !== x && 0 === y && 0 === z) {
    // Rotation around X axis
    if (x < 0) {
      s = -s;
    }
    e[0] = 1; e[4] = 0; e[8]  = 0;  e[12] = 0;
    e[1] = 0; e[5] = c; e[9]  = -s; e[13] = 0;
    e[2] = 0; e[6] = s; e[10] = c;  e[14] = 0;
    e[3] = 0; e[7] = 0; e[11] = 0;  e[15] = 1;
  } else if (0 === x && 0 !== y && 0 === z) {
    // Rotation around Y axis
    if (y < 0) {
      s = -s;
    }
    e[0] = c;  e[4] = 0; e[8]  = s; e[12] = 0;
    e[1] = 0;  e[5] = 1; e[9]  = 0; e[13] = 0;
    e[2] = -s; e[6] = 0; e[10] = c; e[14] = 0;
    e[3] = 0;  e[7] = 0; e[11] = 0; e[15] = 1;
  } else if (0 === x && 0 === y && 0 !== z) {
    // Rotation around Z axis
    if (z < 0) {
      s = -s;
    }
    e[0] = c; e[4] = -s; e[8]  = 0; e[12] = 0;
    e[1] = s; e[5] = c;  e[9]  = 0; e[13] = 0;
    e[2] = 0; e[6] = 0;  e[10] = 1; e[14] = 0;
    e[3] = 0; e[7] = 0;  e[11] = 0; e[15] = 1;
  } else {
    // Rotation around another axis
    len = Math.sqrt(x * x + y * y + z * z);
    if (len !== 1) {
      rlen = 1 / len;
      x *= rlen;
      y *= rlen;
      z *= rlen;
    }
    nc = 1 - c;
    xy = x * y;
    yz = y * z;
    zx = z * x;
    xs = x * s;
    ys = y * s;
    zs = z * s;

    e[0] = x * x * nc + c;
    e[1] = xy * nc + zs;
    e[2] = zx * nc - ys;
    e[3] = 0;

    e[4] = xy * nc - zs;
    e[5] = y * y * nc + c;
    e[6] = yz * nc + xs;
    e[7] = 0;

    e[8] = zx * nc + ys;
    e[9] = yz * nc - xs;
    e[10] = z * z * nc + c;
    e[11] = 0;

    e[12] = 0;
    e[13] = 0;
    e[14] = 0;
    e[15] = 1;
  }

  return this;
};

/**
 * <p>Right multiply this matrix by a rotation matrix.</p>
 * The rotation axis vector may not be normalized.
 * @param {Number} angle angle of rotation (degrees)
 * @param {Number} x X coordinate of rotation axis vector.
 * @param {Number} y Y coordinate of rotation axis vector.
 * @param {Number} z Z coordinate of rotation axis vector.
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.rotate = function (angle, x, y, z) {
  return this.concat(new Matrix4().setRotate(angle, x, y, z));
};

/**
 * Set the viewing matrix.
 * @param {Number} eyeX X coordinate of the eye position.
 * @param {Number} eyeY Y coordinate of the eye position.
 * @param {Number} eyeZ Z coordinate of the eye position.
 * @param {Number} centerX X coordinate of the reference point.
 * @param {Number} centerY Y coordinate of the reference point.
 * @param {Number} centerZ Z coordinate of the reference point.
 * @param {Number} upX X coordinate of the view up vector.
 * @param {Number} upY Y coordinate of the view up vector.
 * @param {Number} upZ Z coordinate of the view up vector.
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.setLookAt = function (
  eyeX,
  eyeY,
  eyeZ,
  centerX,
  centerY,
  centerZ,
  upX,
  upY,
  upZ,
) {
  var e, fx, fy, fz, rlf, sx, sy, sz, rls, ux, uy, uz;

  fx = centerX - eyeX;
  fy = centerY - eyeY;
  fz = centerZ - eyeZ;

  // Normalize f.
  rlf = 1 / Math.sqrt(fx * fx + fy * fy + fz * fz);
  fx *= rlf;
  fy *= rlf;
  fz *= rlf;

  // Calculate cross product of f and up.
  sx = fy * upZ - fz * upY;
  sy = fz * upX - fx * upZ;
  sz = fx * upY - fy * upX;

  // Normalize s.
  rls = 1 / Math.sqrt(sx * sx + sy * sy + sz * sz);
  sx *= rls;
  sy *= rls;
  sz *= rls;

  // Calculate cross product of s and f.
  ux = sy * fz - sz * fy;
  uy = sz * fx - sx * fz;
  uz = sx * fy - sy * fx;

  // Set to this.
  e = this.elements;
  e[0] = sx;
  e[1] = ux;
  e[2] = -fx;
  e[3] = 0;

  e[4] = sy;
  e[5] = uy;
  e[6] = -fy;
  e[7] = 0;

  e[8] = sz;
  e[9] = uz;
  e[10] = -fz;
  e[11] = 0;

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

  // Translate.
  return this.translate(-eyeX, -eyeY, -eyeZ);
};

/**
 * Right multiply this matrix by the viewing matrix.
 * @param {Number} eyeX X coordinate of the eye position.
 * @param {Number} eyeY Y coordinate of the eye position.
 * @param {Number} eyeZ Z coordinate of the eye position.
 * @param {Number} centerX X coordinate of the reference point.
 * @param {Number} centerY Y coordinate of the reference point.
 * @param {Number} centerZ Z coordinate of the reference point.
 * @param {Number} upX X coordinate of the view up vector.
 * @param {Number} upY Y coordinate of the view up vector.
 * @param {Number} upZ Z coordinate of the view up vector.
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.lookAt = function (
  eyeX,
  eyeY,
  eyeZ,
  centerX,
  centerY,
  centerZ,
  upX,
  upY,
  upZ,
) {
  return this.concat(
    new Matrix4().setLookAt(
      eyeX,
      eyeY,
      eyeZ,
      centerX,
      centerY,
      centerZ,
      upX,
      upY,
      upZ,
    ),
  );
};

/**
 * Right multiply this matrix by a matrix for projecting a vertex onto a plane.
 * @param {Array<Number>} plane coefficients [A, B, C, D] of the plane equation: "Ax + By + Cz + D = 0".
 * @param {Array<Number>} light array holding the coordinates of the light source. <br>
 *                              If light[3] == 0, consider light source at infinity (parallel rays).
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.dropShadow = function (plane, light) {
  var mat = new Matrix4();
  var e = mat.elements;

  var dot =
    plane[0] * light[0] +
    plane[1] * light[1] +
    plane[2] * light[2] +
    plane[3] * light[3];

  e[0] = dot - light[0] * plane[0];
  e[1] = -light[1] * plane[0];
  e[2] = -light[2] * plane[0];
  e[3] = -light[3] * plane[0];

  e[4] = -light[0] * plane[1];
  e[5] = dot - light[1] * plane[1];
  e[6] = -light[2] * plane[1];
  e[7] = -light[3] * plane[1];

  e[8] = -light[0] * plane[2];
  e[9] = -light[1] * plane[2];
  e[10] = dot - light[2] * plane[2];
  e[11] = -light[3] * plane[2];

  e[12] = -light[0] * plane[3];
  e[13] = -light[1] * plane[3];
  e[14] = -light[2] * plane[3];
  e[15] = dot - light[3] * plane[3];

  return this.concat(mat);
};

/**
 * <p>Right multiply this matrix by the matrix for projecting a vertex onto a plane.</p>
 * (Projected by parallel light.)
 * @param {Number} normX X coordinate of the normal vector of the plane.
 * @param {Number} normY Y coordinate of the normal vector of the plane.
 * @param {Number} normZ Z coordinate of the normal vector of the plane. <br>
 *                       (Not necessary to be normalized.)
 * @param {Number} planeX X coordinate of an arbitrary point on the plane.
 * @param {Number} planeY Y coordinate of an arbitrary point on the plane.
 * @param {Number} planeZ Z coordinate of an arbitrary point on the plane.
 * @param {Number} lightX X coordinate of the light direction.
 * @param {Number} lightY Y coordinate of the light direction.
 * @param {Number} lightZ Z coordinate of the light direction. <br>
 *                        (Not necessary to be normalized.)
 * @return {Matrix4} this matrix.
 */
Matrix4.prototype.dropShadowDirectionally = function (
  normX,
  normY,
  normZ,
  planeX,
  planeY,
  planeZ,
  lightX,
  lightY,
  lightZ,
) {
  var a = planeX * normX + planeY * normY + planeZ * normZ;
  return this.dropShadow(
    [normX, normY, normZ, -a],
    [lightX, lightY, lightZ, 0],
  );
};

/**
 * <p>Constructor of Vector3.</p>
 * If opt_src is specified, the new vector is initialized by opt_src.
 * @constructs Vector3
 * @param {Array<Number>} opt_src source vector (optional).
 */
var Vector3 = function (opt_src) {
  var v = new Float32Array(3);
  if (opt_src && typeof opt_src === "object") {
    v[0] = opt_src[0];
    v[1] = opt_src[1];
    v[2] = opt_src[2];
  }
  /**
   * Vector container.
   * @type {Float32Array}
   */
  this.elements = v;
};

/**
 * Normalize this vector.
 * @return {Vector3} this
 */
Vector3.prototype.normalize = function () {
  var v = this.elements;
  var c = v[0],
    d = v[1],
    e = v[2],
    g = Math.sqrt(c * c + d * d + e * e);
  if (g) {
    if (g == 1) return this;
  } else {
    v[0] = 0;
    v[1] = 0;
    v[2] = 0;
    return this;
  }
  g = 1 / g;
  v[0] = c * g;
  v[1] = d * g;
  v[2] = e * g;
  return this;
};

/**
 * <p>Constructor of Vector4.</p>
 * If opt_src is specified, the new vector is initialized by opt_src.
 * @constructs Vector4
 * @param {Array<Number>} opt_src source vector (optional).
 */
var Vector4 = function (opt_src) {
  var v = new Float32Array(4);
  if (opt_src && typeof opt_src === "object") {
    v[0] = opt_src[0];
    v[1] = opt_src[1];
    v[2] = opt_src[2];
    v[3] = opt_src[3];
  }
  /**
   * Vector container.
   * @type {Float32Array}
   */
  this.elements = v;
};