Camera.createDefault constructor

Camera.createDefault({
  1. required double width,
  2. required double height,
  3. required double ndcYSign,
  4. double horizontalFovDegrees = 45.0,
  5. Vector3? position,
  6. Matrix3? rotation,
  7. int id = 0,
  8. double znear = 0.2,
  9. double zfar = 200.0,
})

Creates a reasonable default camera from a horizontal FOV and image size.

If position and rotation are omitted, an orbit-style pose is used.

Implementation

factory Camera.createDefault({
  required double width,
  required double height,
  required double ndcYSign,
  double horizontalFovDegrees = 45.0,
  Vector3? position,
  Matrix3? rotation,
  int id = 0,
  double znear = 0.2,
  double zfar = 200.0,
}) {
  final fx = _focalPixels(width, horizontalFovDegrees);
  final fy = fx; // square pixels

  // Pose: either provided or derived from simple orbit parameters.
  late final Vector3 pos;
  late final Matrix3 rot;

  if (position != null && rotation != null) {
    pos = position;
    rot = rotation;
  } else {
    const orbitDistance = 2.0;
    const double theta = 0;
    const phi = math.pi / 2.0;

    final x = orbitDistance * math.sin(phi) * math.sin(theta);
    final y = orbitDistance * math.cos(phi);
    final z = orbitDistance * math.sin(phi) * math.cos(theta);
    pos = Vector3(x, y, z);

    // Camera basis: right/up/forward in world space.
    final forward = (-pos).normalized();
    final up = Vector3(0, -1, 0);
    final right = up.cross(forward).normalized();
    final trueUp = forward.cross(right).normalized();
    rot = Matrix3.columns(right, trueUp, forward);
  }

  return Camera(
    id: id,
    width: width.toInt(),
    height: height.toInt(),
    position: pos,
    rotation: rot,
    fx: fx,
    fy: fy,
    znear: znear,
    zfar: zfar,
    ndcYSign: ndcYSign,
  );
}