pickRay function

bool pickRay (Matrix4 cameraMatrix, num viewportX, num viewportWidth, num viewportY, num viewportHeight, num pickX, num pickY, Vector3 rayNear, Vector3 rayFar)

On success, rayNear and rayFar are the points where the screen space pickX, pickY intersect with the near and far planes respectively.

The viewport is specified by (viewportX, viewportWidth) and (viewportY, viewportHeight).

cameraMatrix includes both the projection and view transforms.

Returns false on error, for example, the mouse is not in the viewport.

Implementation

bool pickRay(
    Matrix4 cameraMatrix,
    num viewportX,
    num viewportWidth,
    num viewportY,
    num viewportHeight,
    num pickX,
    num pickY,
    Vector3 rayNear,
    Vector3 rayFar) {
  bool r;

  r = unproject(cameraMatrix, viewportX, viewportWidth, viewportY,
      viewportHeight, pickX, viewportHeight - pickY, 0.0, rayNear);
  if (!r) {
    return false;
  }

  r = unproject(cameraMatrix, viewportX, viewportWidth, viewportY,
      viewportHeight, pickX, viewportHeight - pickY, 1.0, rayFar);

  return r;
}