setFromUnitVectors method

Quaternion setFromUnitVectors(
  1. Vector3 vFrom,
  2. Vector3 vTo
)

Implementation

Quaternion setFromUnitVectors(Vector3 vFrom, Vector3 vTo) {
  // assumes direction vectors vFrom and vTo are normalized

  var r = vFrom.dot(vTo) + 1;

  if (r < Math.epsilon) {
    r = 0;

    if (Math.abs(vFrom.x) > Math.abs(vFrom.z)) {
      _x = -vFrom.y;
      _y = vFrom.x;
      _z = 0;
      _w = r;
    } else {
      _x = 0;
      _y = -vFrom.z;
      _z = vFrom.y;
      _w = r;
    }
  } else {
    // crossVectors( vFrom, vTo ); // inlined to avoid cyclic dependency on Vector3

    _x = vFrom.y * vTo.z - vFrom.z * vTo.y;
    _y = vFrom.z * vTo.x - vFrom.x * vTo.z;
    _z = vFrom.x * vTo.y - vFrom.y * vTo.x;
    _w = r;
  }

  return normalize();
}