probeFaceViewProjection function

Matrix4 probeFaceViewProjection(
  1. int face,
  2. Vector3 position, {
  3. required double near,
  4. required double far,
  5. required FramebufferOrigin origin,
  6. required DepthRange depthRange,
})

The view-projection that draws face of a probe at position into a cube face a sampler reads correctly, in the clip space of a backend with the given origin and depthRange.

Ninety degrees, square, from near to far. The mirror in x is applied to every face — see the library note — and clip y is negated as well on a bottom-left backend. The depth remap is the same one every camera in the engine goes through at the boundary.

Implementation

Matrix4 probeFaceViewProjection(
  int face,
  Vector3 position, {
  required double near,
  required double far,
  required FramebufferOrigin origin,
  required DepthRange depthRange,
}) {
  final camera = ProbeFace.all[face];
  final view = lookAtRightHanded(position, position + camera.aim, camera.up);
  final projection = PerspectiveProjection(
    fovYRadians: math.pi / 2,
    near: near,
    far: far,
  ).toMatrix(1.0);
  // Mirror x in clip space: the cube-map table is left-handed, and a
  // right-handed camera puts every face's left on the right. And y where row
  // zero is at the bottom, so the picture's top lands on the last row. Typed,
  // because `Matrix4.operator*` returns `dynamic`.
  final Matrix4 mirrored = Matrix4.identity()
    ..setEntry(0, 0, -1.0)
    ..setEntry(1, 1, origin == FramebufferOrigin.bottomLeft ? -1.0 : 1.0)
    ..multiply(projection)
    ..multiply(view);
  return toDepthRange(mirrored, depthRange);
}