project method

Offset? project(
  1. Vector3 world
)

Projects a world position to a pixel coordinate within viewport.

Returns null when the point sits behind the camera or outside the projection, so callers can skip drawing it.

Implementation

Offset? project(Vector3 world) {
  final clip = projectionMatrix *
      viewMatrix *
      Vector4(world.x, world.y, world.z, 1.0);
  final w = clip.w;
  if (w <= 0) return null; // behind the camera

  // Normalised device coords (-1..1) → pixels. Y is flipped because NDC
  // is bottom-up while screen coordinates are top-down.
  final ndcX = clip.x / w;
  final ndcY = clip.y / w;
  return Offset(
    (ndcX + 1) / 2 * viewport.width,
    (1 - ndcY) / 2 * viewport.height,
  );
}