pyglet.window.camera
Camera helpers for 2D and 3D rendering.
- class Camera2D
Bases:
BaseCamera[Camera2DView]Manage an orthographic projection and movable 2D view.
A
Camera2Dkeeps position, zoom, viewport, coordinate conversion, and shader matrix storage together. Select it for a batch or group to draw a world, UI layer, minimap, or other 2D rendering scope.Added in version 3.0.
- __init__(
- window: Window,
- *,
- scroll_speed: float = 1.0,
- min_zoom: float = 1.0,
- max_zoom: float = 4.0,
- zoom_speed: float = 1.0,
- viewport: tuple[int, int, int, int] | None = None,
- register_handlers: bool = True,
- window_block: UniformBlock | None = None,
- copies_per_resource: int = 3,
- projection_uniform: str = 'u_projection',
- view_uniform: str = 'u_view',
Create a 2D camera for a window.
The root view initially uses a lower-left origin and a zoom of
1.0. Unless an explicit viewport is supplied, it follows the full framebuffer as the window is resized or its scale changes.- Parameters:
window (
Window) – Window whose graphics context and framebuffer dimensions are used by this camera.scroll_speed (
float) – Distance applied to each axis bymove(). Scale input by elapsed time when using it for continuous movement.min_zoom (
float) – Minimum value accepted byzoom.max_zoom (
float) – Maximum value accepted byzoom.zoom_speed (
float) – Suggested zoom-rate scalar for application input handling.viewport (
tuple[int,int,int,int] |None) – Optional fixed root viewport as(x, y, width, height)in framebuffer coordinates.Nonetracks the full framebuffer.register_handlers (
bool) – IfTrue, register for window resize and scale events. Disable this for fixed-size offscreen cameras.window_block (
UniformBlock|None) – Optional shaderWindowBlockused to create the camera’s uniform-buffer storage on modern graphics backends. By default, the block is obtained from pyglet’s default shader.copies_per_resource (
int) – Number of ring-buffered matrix copies reserved for this camera on uniform-buffer backends.projection_uniform (
str) – Projection-matrix uniform name used by OpenGL 2 and OpenGL ES 2.view_uniform (
str) – View-matrix uniform name used by OpenGL 2 and OpenGL ES 2.
- set_centered_origin(enabled: bool = True) None
Set whether the root camera origin is viewport-centered.
When enabled, world-space
(0, 0)is drawn at the viewport center. When disabled, world-space(0, 0)is drawn at the viewport lower-left.- Return type:
- set_scissor_area_relative( ) CameraScissor
Set view-relative scissor clipping on the root view.
This clipping follows camera/view movement and zoom, and is applied automatically for group camera scopes.
- Return type:
- zoom_in_to_point(x: float, y: float, amount: float) None
Increase zoom while keeping a screen-space point fixed in world space.
- Return type:
- class Camera2DView
Bases:
_CameraViewBaseA transform and viewport scope belonging to a
Camera2D.Views can add position, zoom, viewport, and scissor state while inheriting transforms from a parent view. Create them with
Camera2D.create_view().Added in version 3.0.
- __init__(
- camera: Camera2D,
- storage: CameraViewStorage | None,
- *,
- parent: Camera2DView | None = None,
- set_scissor_area_relative( ) CameraScissor
Set a view-relative scissor area.
Unlike
set_scissor_area(window-space), this scissor follows the view’s transform. This is useful for moving UI panels, nested clipped views, and other cases where clipping should travel with the view.- Return type:
- zoom_in_to_point(x: float, y: float, amount: float) None
Increase zoom while keeping a screen-space point fixed in world space.
- Return type:
- zoom_out_to_point(x: float, y: float, amount: float) None
Decrease zoom while keeping a screen-space point fixed in world space.
- Return type:
- zoom_to_point(x: float, y: float, zoom: float) None
Set zoom while keeping a screen-space point fixed in world space.
This is useful for mouse-wheel zooming: pass the cursor coordinates and the desired new zoom, and the view position will shift so the world point under the cursor stays when zooming.
- Return type:
- class Camera3D
Bases:
BaseCamera[Camera3DView]Manage a perspective projection and an oriented 3D view.
Camera3Dcombines position, pitch/yaw orientation, clipping distances, field of view, coordinate conversion, and shader matrix storage. It can be selected per batch or group for 3D scenes and rendering passes.Added in version 3.0.
- __init__(
- window: Window,
- *,
- position: Vec3 | None = None,
- target: Vec3 | None = None,
- near: float = 0.1,
- far: float = 1000.0,
- field_of_view: float = 60.0,
- walk_speed: float = 10.0,
- look_speed: float = 10.0,
- viewport: tuple[int, int, int, int] | None = None,
- register_handlers: bool = True,
- window_block: UniformBlock | None = None,
- copies_per_resource: int = 3,
- projection_uniform: str = 'u_projection',
- view_uniform: str = 'u_view',
Create a perspective 3D camera for a window.
The initial orientation looks from
positiontowardtarget. If no target is provided, the camera looks along the negative Z axis. Unless an explicit viewport is supplied, the root view follows the full framebuffer and updates its aspect ratio automatically.- Parameters:
window (
Window) – Window whose graphics context and framebuffer dimensions are used by this camera.position (
Vec3|None) – Initial world-space camera position. Defaults to the origin.target (
Vec3|None) – Optional world-space point used to initialize pitch and yaw.near (
float) – Positive distance to the near clipping plane.far (
float) – Distance to the far clipping plane. It must be greater thannear.field_of_view (
float) – Vertical field of view in degrees.walk_speed (
float) – World-space movement rate used byapply_movement_input().look_speed (
float) – Suggested rotation-rate scalar for mouse or controller input.viewport (
tuple[int,int,int,int] |None) – Optional fixed root viewport as(x, y, width, height)in framebuffer coordinates.Nonetracks the full framebuffer.register_handlers (
bool) – IfTrue, register for window resize and scale events. Disable this for fixed-size offscreen cameras.window_block (
UniformBlock|None) – Optional shaderWindowBlockused to create the camera’s uniform-buffer storage on modern graphics backends. By default, the block is obtained from pyglet’s default shader.copies_per_resource (
int) – Number of ring-buffered matrix copies reserved for this camera on uniform-buffer backends.projection_uniform (
str) – Projection-matrix uniform name used by OpenGL 2 and OpenGL ES 2.view_uniform (
str) – View-matrix uniform name used by OpenGL 2 and OpenGL ES 2.
- class Camera3DView
Bases:
_CameraViewBaseA positional view scope belonging to a
Camera3D.A view adds a local 3D offset to its camera and can define an independent viewport or scissor. Child views can inherit the offsets of their parents.
Added in version 3.0.
- __init__(
- camera: Camera3D,
- storage: CameraViewStorage,
- *,
- parent: Camera3DView | None = None,
- class CameraViewStorage
Bases:
ProtocolTarget for camera matrix writes.
- __init__(*args, **kwargs)
- bind_camera(
- draw_context: DrawContext,
Bind or apply committed camera data for drawing.
- Return type:
- commit(draw_context: DrawContext) None
Commit staged camera data to GPU-visible state when drawing.
- Return type:
- class FPSCamera
Bases:
Camera3DFirst-person camera preset built on
Camera3D.- __init__(window: Window, **kwargs: Any) None
Create a perspective 3D camera for a window.
The initial orientation looks from
positiontowardtarget. If no target is provided, the camera looks along the negative Z axis. Unless an explicit viewport is supplied, the root view follows the full framebuffer and updates its aspect ratio automatically.- Parameters:
window (
Window) – Window whose graphics context and framebuffer dimensions are used by this camera.position – Initial world-space camera position. Defaults to the origin.
target – Optional world-space point used to initialize pitch and yaw.
near – Positive distance to the near clipping plane.
far – Distance to the far clipping plane. It must be greater than
near.field_of_view – Vertical field of view in degrees.
walk_speed – World-space movement rate used by
apply_movement_input().look_speed – Suggested rotation-rate scalar for mouse or controller input.
viewport – Optional fixed root viewport as
(x, y, width, height)in framebuffer coordinates.Nonetracks the full framebuffer.register_handlers – If
True, register for window resize and scale events. Disable this for fixed-size offscreen cameras.window_block – Optional shader
WindowBlockused to create the camera’s uniform-buffer storage on modern graphics backends. By default, the block is obtained from pyglet’s default shader.copies_per_resource – Number of ring-buffered matrix copies reserved for this camera on uniform-buffer backends.
projection_uniform – Projection-matrix uniform name used by OpenGL 2 and OpenGL ES 2.
view_uniform – View-matrix uniform name used by OpenGL 2 and OpenGL ES 2.
- class ThirdPersonCamera
Bases:
Camera3DThird-person orbit camera that follows a target point.
- __init__(
- window: Window,
- *,
- target: Vec3 | None = None,
- distance: float = 8.0,
- target_height: float = 1.5,
- **kwargs: Any,
Create a perspective 3D camera for a window.
The initial orientation looks from
positiontowardtarget. If no target is provided, the camera looks along the negative Z axis. Unless an explicit viewport is supplied, the root view follows the full framebuffer and updates its aspect ratio automatically.- Parameters:
window (
Window) – Window whose graphics context and framebuffer dimensions are used by this camera.position – Initial world-space camera position. Defaults to the origin.
target (
Vec3|None) – Optional world-space point used to initialize pitch and yaw.near – Positive distance to the near clipping plane.
far – Distance to the far clipping plane. It must be greater than
near.field_of_view – Vertical field of view in degrees.
walk_speed – World-space movement rate used by
apply_movement_input().look_speed – Suggested rotation-rate scalar for mouse or controller input.
viewport – Optional fixed root viewport as
(x, y, width, height)in framebuffer coordinates.Nonetracks the full framebuffer.register_handlers – If
True, register for window resize and scale events. Disable this for fixed-size offscreen cameras.window_block – Optional shader
WindowBlockused to create the camera’s uniform-buffer storage on modern graphics backends. By default, the block is obtained from pyglet’s default shader.copies_per_resource – Number of ring-buffered matrix copies reserved for this camera on uniform-buffer backends.
projection_uniform – Projection-matrix uniform name used by OpenGL 2 and OpenGL ES 2.
view_uniform – View-matrix uniform name used by OpenGL 2 and OpenGL ES 2.
- class UniformBufferCameraRegion
Bases:
UniformBufferRegionCamera storage adapter for UBO-backed data uploads.
- __init__(
- ubo: UniformBufferObject,
- *,
- copies_per_resource: int | None = None,
- commit(
- _draw_context: DrawContext | None = None,
Upload dirty CPU-side data to the next writable GPU range.
- Return type: