targetTo function

List<double> targetTo(
  1. List<double> out,
  2. List<double> eye,
  3. List<double> target,
  4. List<double> up
)

Generates a matrix that makes something look at something else.

@param {mat4} out mat4 frustum matrix will be written into @param {ReadonlyVec3} eye Position of the viewer @param {ReadonlyVec3} center Point the viewer is looking at @param {ReadonlyVec3} up vec3 pointing up @returns {mat4} out

Implementation

List<double> targetTo(List<double> out, List<double> eye, List<double> target, List<double> up) {
  final eyex = eye[0], eyey = eye[1], eyez = eye[2], upx = up[0], upy = up[1], upz = up[2];

  double z0 = eyex - target[0], z1 = eyey - target[1], z2 = eyez - target[2];

  double len = z0 * z0 + z1 * z1 + z2 * z2;
  if (len > 0) {
    len = 1 / math.sqrt(len);
    z0 *= len;
    z1 *= len;
    z2 *= len;
  }

  double x0 = upy * z2 - upz * z1, x1 = upz * z0 - upx * z2, x2 = upx * z1 - upy * z0;

  len = x0 * x0 + x1 * x1 + x2 * x2;
  if (len > 0) {
    len = 1 / math.sqrt(len);
    x0 *= len;
    x1 *= len;
    x2 *= len;
  }

  out[0] = x0;
  out[1] = x1;
  out[2] = x2;
  out[3] = 0;
  out[4] = z1 * x2 - z2 * x1;
  out[5] = z2 * x0 - z0 * x2;
  out[6] = z0 * x1 - z1 * x0;
  out[7] = 0;
  out[8] = z0;
  out[9] = z1;
  out[10] = z2;
  out[11] = 0;
  out[12] = eyex;
  out[13] = eyey;
  out[14] = eyez;
  out[15] = 1;
  return out;
}