Skip to content

fdray.core.camera

source module fdray.core.camera

The camera implementation.

This camera implementation is based on the following Qiita article

We adopt the spherical coordinate system for camera positioning and the calculation methods for direction, right, and up vectors as proposed in the article. The sky parameter is also included as it's essential for proper orientation.

This camera model features:

  • Intuitive camera positioning using spherical coordinates (longitude, latitude).
  • Independent control of view range (view_scale) and perspective effect (distance).
  • Rotation control via camera tilt (tilt).
  • Proper handling of aspect ratio.

The following code is to reproduce the image in the article.

from PIL import Image

from fdray import Background, Camera, Color, Cylinder, LightSource, Renderer, Scene

scene = Scene(
    Background("white"),
    Camera(30, 30, view_scale=1),
    LightSource(0, "white"),  # at camera location
    Cylinder((0, 0, 0), (1, 0, 0), 0.1, Color("red")),
    Cylinder((0, 0, 0), (0, 1, 0), 0.1, Color("green")),
    Cylinder((0, 0, 0), (0, 0, 1), 0.1, Color("blue")),
)
renderer = Renderer(width=300, height=300)
array = renderer.render(scene)
Image.fromarray(array)

Classes

  • Camera A camera for viewing 3D scenes.

source dataclass Camera(longitude: InitVar[float] = 0, latitude: InitVar[float] = 0, view_scale: float = 1, distance: float = 0, tilt: float = 0, look_at: tuple[float, float, float] = (0, 0, 0), aspect_ratio: float = 4 / 3)

Bases : Descriptor

A camera for viewing 3D scenes.

Define the viewpoint and projection for a 3D scene. The camera position is specified using spherical coordinates, and various parameters allow adjusting the field of view and perspective effects.

Attributes

  • longitude : InitVar[float] The longitude of the camera in degrees.

  • latitude : InitVar[float] The latitude of the camera in degrees.

  • view_scale : float The scale of the view frustum, controlling how much of the scene is visible.

  • distance : float The distance of the camera from the look_at point.

  • tilt : float The tilt angle of the camera in degrees (-180 to 180).

  • look_at : tuple[float, float, float] The point the camera is looking at.

  • aspect_ratio : float The aspect ratio of the camera.

  • phi : float Internal storage for longitude in radians.

  • theta : float Internal storage for latitude in radians.

Methods

  • orbital_location Calculate a position in orbit around the camera's location.

source property Camera.z: Vector

source property Camera.x: Vector

source property Camera.y: Vector

source property Camera.direction: Vector

source property Camera.location: Vector

source property Camera.right: Vector

source property Camera.up: Vector

source property Camera.sky: Vector

source method Camera.orbital_location(forward: float = 0, angle: float = 0, rotation: float = 0)Vector

Calculate a position in orbit around the camera's location.

Imagine tilting your head up (angle) and then rotating counter-clockwise (rotation):

  • First, move forward along viewing direction (0=at camera.location, 1=at camera.look_at). Negative values move behind the camera.
  • Then, tilt up from viewing direction by 'angle' degrees
  • Finally, rotate counter-clockwise from up by 'rotation' degrees (0=up, 90=left, 180=down, 270=right)

Parameters

  • forward : float Relative position along viewing direction (0=camera, 1=look_at, negative=behind camera)

  • angle : float Tilt angle from viewing direction in degrees

  • rotation : float Rotation angle from up direction in degrees (counter-clockwise)

Returns

  • Vector Position vector in absolute coordinates