pixelRay function

({Vector3 along, Vector3 origin}) pixelRay(
  1. Matrix4 inverse,
  2. double u,
  3. double v
)

PixelRay: where the ray through u, v starts and which way it goes.

Two unprojections rather than one and the camera position, which is the whole of what makes an orthographic camera work here — see worldAtDepth. The direction is also the direction the pixel looks, which is what a reflection reflects about.

Implementation

({Vector3 origin, Vector3 along}) pixelRay(
  Matrix4 inverse,
  double u,
  double v,
) {
  final x = u * 2.0 - 1.0;
  final y = 1.0 - v * 2.0;
  final Vector4 nearH = inverse * Vector4(x, y, 0.0, 1.0);
  final Vector4 farH = inverse * Vector4(x, y, 1.0, 1.0);
  final origin = Vector3(nearH.x, nearH.y, nearH.z)..scale(1.0 / nearH.w);
  return (
    origin: origin,
    along: Vector3(farH.x, farH.y, farH.z)
      ..scale(1.0 / farH.w)
      ..sub(origin)
      ..normalize(),
  );
}