lookAt method

Matrix4 lookAt(
  1. Vector3 eye,
  2. Vector3 target,
  3. Vector3 up
)

Implementation

Matrix4 lookAt(Vector3 eye, Vector3 target, Vector3 up) {
  var te = elements;

  _matrix4z.subVectors(eye, target);

  if (_matrix4z.lengthSq() == 0) {
    // eye and target are in the same position

    _matrix4z.z = 1;
  }

  _matrix4z.normalize();
  _matrix4x.crossVectors(up, _matrix4z);

  if (_matrix4x.lengthSq() == 0) {
    // up and z are parallel

    if (Math.abs(up.z) == 1) {
      _matrix4z.x += 0.0001;
    } else {
      _matrix4z.z += 0.0001;
    }

    _matrix4z.normalize();
    _matrix4x.crossVectors(up, _matrix4z);
  }

  _matrix4x.normalize();
  _matrix4y.crossVectors(_matrix4z, _matrix4x);

  te[0] = _matrix4x.x.toDouble();
  te[4] = _matrix4y.x.toDouble();
  te[8] = _matrix4z.x.toDouble();
  te[1] = _matrix4x.y.toDouble();
  te[5] = _matrix4y.y.toDouble();
  te[9] = _matrix4z.y.toDouble();
  te[2] = _matrix4x.z.toDouble();
  te[6] = _matrix4y.z.toDouble();
  te[10] = _matrix4z.z.toDouble();

  return this;
}