camera

package
v0.22.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 4 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package camera is the view onto a world: screen conversion, culling, the visible window and its control. It is a root package, not a plugin; every renderer draws through a camera, and a game gets one from the cameras plugin, built from a Config through the maker it was given.

Camera

A Camera converts between world and screen, culls, and is moved, panned and zoomed within the world: wrapping on a wrapping axis, held inside the world on any other. One keeping the whole world in view (Config.Whole, a minimap) fits it at every size instead and is not panned or zoomed; a Fitting camera says so. Its pixels are whoever shows it's: a render.Feed sizes it every frame.

Projection

A Projection is the arithmetic a Camera draws through: TopDown here, the cameras plugin's own in relief. Sorts says whether what is drawn through it must go back to front, Wraps whether a wrapping world can be drawn through it, Toward the way towards the eye.

State

State is the camera's window and zoom. Persisted hands saves the window, the zoom and the fastening, which Restore takes back.

Fastening and what a camera says of itself

A Fastenable camera is fastened to an entity: its Fastening says to which and How, and HowOf is what the bindings hold in. The rest are small interfaces a camera may implement: a Picker finds what is under a screen point and its height; a Scaler draws a world unit larger near the eye than far off, and ScaleAt is the scale at a point through any camera; a Rayer says which way a screen point looks, Rays every line of sight at once as a RayField, an Eyed camera where its eye stands, a Fronted one which points lie InFront of it and a Vanisher where a direction vanishes, a Focused camera the world point it observes, a Headed one the way along the ground that runs up its screen. A Transform is a RayField as the GPU draws it.

Sight and resolution

A Sighted camera sees a multiple of the world's sight, measured from its Viewpoint, the eye or else the point it observes; SightOf is any camera's, 1 for one without. ScaleShown is the scale at a point in the pixels shown, which the world's levels of detail go by. A Resolved camera is drawn at a resolution of the pixels it is shown at, its Viewport, Zoom and screen conversions in those camera pixels and its window on the world the same; ResolutionOf is any camera's, 1 for one without. A Shifted camera draws its sectors levels coarser than the world sees them, as the frame budget shifts them, one seen at lod.Standard or finer full; LevelShiftOf is any camera's, 0 for one without. A Layered camera has its picture drawn in layers (Layer), the widest first, each a crop of the frame at a resolution of its own laid out as its Layout says (Inset in the middle, Footed on the bottom edge; Arrange gives each layer its Window), by a camera that is Cropping: Crop has its Viewport be a window of its frame, anywhere in it, until Uncrop, and InFrame is where a pixel of the frame lies in the window; LayersOf, LayoutOf and IsCropped are any camera's.

FromScreenRect

FromScreenRect converts a screen rectangle to world space, for click and drag logic; on a torus the result may need wrapping.

Index

Constants

View Source
const Free = Loose | Centred

Free is where the hand scrolls the world: a camera fastened to nothing or Centred over an entity; one kept on an arm behind an entity, or inside it, is not scrolled.

View Source
const Outside = Loose | Centred | Behind

Outside is where the bindings of a free hand hold: every How but Inside.

Variables

This section is empty.

Functions

func FromScreenRect

func FromScreenRect(cam Camera, sx0, sy0, sx1, sy1 float32) (x0, y0, x1, y1 float32)

FromScreenRect converts a screen rectangle to world space; on a torus it may need WrapAABB.

func InFrame added in v0.3.0

func InFrame(cam Camera, sx, sy float32) (x, y float32)

InFrame is where the frame's shown pixel (sx, sy) lies in cam's pixels as it is drawn now: outside them when a cropped camera's part of the frame leaves it out.

func InFront added in v0.18.0

func InFront(cam Camera, x, y, z float32) bool

InFront reports whether the world point lies before cam's eye: every point does through a camera that is not Fronted.

func IsCropped added in v0.3.0

func IsCropped(cam Camera) bool

IsCropped reports whether cam is drawn a part of its frame at the moment.

func LevelShiftOf added in v0.18.0

func LevelShiftOf(cam Camera) lod.Level

LevelShiftOf is how many levels coarser than the world sees them cam draws its sectors seen past lod.Standard, 0 for a camera without a shift.

func ResolutionOf added in v0.3.0

func ResolutionOf(cam Camera) float32

ResolutionOf is cam's resolution, camera pixels a shown one: 1 for a camera without one.

func ScaleAt added in v0.3.0

func ScaleAt(cam Camera, x, y, z float32) float32

ScaleAt is how many screen units a world unit spans at the point through cam: a Scaler's own, else its Zoom.

func ScaleShown added in v0.18.0

func ScaleShown(cam Camera, x, y, z float32) float32

ScaleShown is how many shown pixels a world unit spans at the point through cam, so the resolution the frame budget sets moves no level of detail.

func SharpFiguresOf added in v0.3.0

func SharpFiguresOf(cam Camera) bool

SharpFiguresOf reports whether cam's figures are drawn at every pixel; false for a camera without the choice.

func SightOf added in v0.18.0

func SightOf(cam Camera) float32

SightOf is the multiple of the world's sight cam sees, 1 for a camera without one.

func Viewpoint added in v0.3.0

func Viewpoint(cam Camera) (x, y float32, ok bool)

Viewpoint is where cam looks from, the point its sight is measured on the ground from: its eye, else the point it observes; false for a camera knowing neither.

Types

type AABB

type AABB = geom.AABB

AABB is geom.AABB, the world-coordinate rectangle camera and render speak in.

type Camera

type Camera interface {
	// Projection is the mapping this camera draws through.
	Projection() Projection
	// Project maps the world point (x, y) at height z to the screen.
	Project(x, y, z float32) (sx, sy float32)
	// Unproject inverts Project at height z.
	Unproject(sx, sy, z float32) (x, y float32)
	// Depth orders drawing through the projection: further back is smaller.
	Depth(x, y, z float32) float32
	// Viewport is the screen the camera draws to, in pixels.
	Viewport() (w, h float32)
	// SetViewport resizes the screen the camera draws to, keeping the point in the middle of it.
	// A screen larger than the world at the current zoom raises the zoom until the world covers it.
	SetViewport(w, h float32)
	// ToScreen is Project at height 0.
	ToScreen(x, y float32) (float32, float32)
	// FromScreen is Unproject at height 0.
	FromScreen(sx, sy float32) (float32, float32)
	// ToScreenQuads appends to dst the rectangle projected to screen space, split at a wrap seam.
	ToScreenQuads(x0, y0, x1, y1 float32, dst []Quad) []Quad
	// Visible reports whether box lies in view.
	Visible(box AABB) bool
	// Bounds is the visible window in world units.
	Bounds() AABB
	// MoveTo puts the visible window's top-left corner at (x, y), keeping its size.
	MoveTo(x, y float32)
	// CenterOn puts the world point (x, y) at height z in the middle of the screen, as far as the
	// window may go. A top-down camera ignores z.
	CenterOn(x, y, z float32)
	// Translate shifts the visible window by a delta in world units.
	Translate(dx, dy float32)
	// Pan shifts the visible window by a delta in screen pixels, the same at any zoom.
	Pan(dx, dy float32)
	// Zoom is the current zoom factor, screen pixels a world unit.
	Zoom() float32
	// ZoomIn multiplies the zoom by factor, keeping the ground point (anchorX, anchorY) fixed on
	// screen as far as the camera's limits allow.
	ZoomIn(factor float32, anchorX, anchorY float32)
	// ZoomOut is ZoomIn(1/factor, anchorX, anchorY).
	ZoomOut(factor float32, anchorX, anchorY float32)
	// State is the camera's current window and zoom.
	State() State
	// Persisted is pointers to the live window, zoom and fastening, for saves and loads.
	Persisted() []any
	// Restore rebuilds derived state after a load has written through Persisted's pointers.
	Restore()
	// SetMinZoom raises ZoomOut's floor above the world-fit one; 0 keeps only that.
	SetMinZoom(minZoom float32)
	// SetMaxZoom caps ZoomIn; 0 leaves it unrestricted.
	SetMaxZoom(maxZoom float32)
}

Camera is the view onto a world: screen conversion, culling, the visible window and its control.

func DrawnOf added in v0.5.0

func DrawnOf(cam Camera) Camera

DrawnOf is cam's copy for the frame drawn, cam itself for a camera that is not Snapped.

func OriginOf added in v0.5.0

func OriginOf(cam Camera) Camera

OriginOf is the camera cam is a copy of, cam itself for one that is no copy.

type Config

type Config struct {
	// Zoom is the scale it starts at, screen pixels a world unit; 0 is where its maker begins,
	// 1 from above.
	Zoom float32
	// Whole keeps the whole world in view at whatever size the camera is shown, centred, the
	// background in bars: a minimap. Such a camera is not panned or zoomed.
	Whole bool
	// Follow is the name of the entity it starts fastened Centred over; empty, it starts fastened
	// to nothing.
	Follow string
	// MinZoom raises ZoomOut's floor above the world-fit one; 0 keeps only that.
	MinZoom float32
	// MaxZoom caps ZoomIn; 0 leaves it unrestricted.
	MaxZoom float32
}

Config is how a camera starts: its window on the world and what it follows. It is never persisted: a loaded camera is as it was saved.

type Cropping added in v0.3.0

type Cropping interface {
	Crop(win Window, s float32)
	Uncrop()
	Cropped() bool
	Frame() (w, h float32)
	Window() Window
}

Cropping is a camera drawn a part at a time: Crop has its Viewport be the part win of its frame at s of its shown pixels, the camera and its zoom staying where they are, until Uncrop puts the frame back exactly. Cropped is true for a part of the frame, Frame is the whole frame's shown size and Window the part drawn, the whole frame uncropped.

type Eyed added in v0.3.0

type Eyed interface {
	Eye() (x, y, z float32, ok bool)
}

Eyed is a camera with an eye at a point of the world, a perspective's. Eye is where it stands, false while the camera has none.

type Fastenable added in v0.3.0

type Fastenable interface {
	Fasten(Fastening)
	Fastening() Fastening
}

Fastenable is a camera that can be fastened to an entity.

type Fastening added in v0.3.0

type Fastening struct {
	Entity uid.UID64
	How    How
}

Fastening is what a camera is fastened to and how; the zero value, to nothing.

type Figured added in v0.3.0

type Figured interface {
	SetSharpFigures(on bool)
	SharpFigures() bool
}

Figured is a camera whose figures, all that lies over the ground, whoever shows it draws at every shown pixel while its ground is drawn at its Resolution.

type Fitting added in v0.3.0

type Fitting interface {
	Fits() (worldWidth, worldHeight float32, ok bool)
}

Fitting is a camera that may keep the whole world in view (Config.Whole). Fits says whether it does, and the world's size, whose proportions a picture of it keeps.

type Focused added in v0.3.0

type Focused interface {
	Focus() (x, y, z float32)
}

Focused is a camera that knows the world point it observes: the middle of what it shows, or the eye of what it rides in.

type Fronted added in v0.18.0

type Fronted interface {
	InFront(x, y, z float32) bool
}

Fronted is a camera some points lie behind the eye of, a perspective's. InFront reports whether the world point (x, y, z) lies before the eye, near enough to be drawn.

type Headed added in v0.3.0

type Headed interface {
	Ahead() (x, y float32)
}

Headed is a camera turned about the vertical: Ahead is the way along the ground that runs up its screen, a unit vector.

type How added in v0.3.0

type How uint8

How is how a camera is fastened to an entity: Loose to none, Centred over it, Behind it on an arm turned the way it walks, Inside it in first person. Hows are bits, so a binding may hold in several.

const (
	Loose How = 1 << iota
	Centred
	Behind
	Inside
)

func HowOf added in v0.3.0

func HowOf(cam Camera) How

HowOf is how cam is fastened: Loose for one fastened to nothing, or that cannot be.

type Layer added in v0.3.0

type Layer struct{ Share, Resolution float32 }

Layer is a layer of a picture drawn in layers: Share of the frame wide and high (1 the whole) drawn at Resolution of its shown pixels, laid in the frame as the picture's Layout says.

func LayersOf added in v0.3.0

func LayersOf(cam Camera) []Layer

LayersOf is the layers cam's picture is drawn in, a copy; nil for a camera without.

type Layered added in v0.3.0

type Layered interface {
	Layers() []Layer
	Layout() Layout
	SetLayers(layers []Layer, layout Layout)
}

Layered is a camera whose picture whoever shows it draws in layers, the widest first, each a crop of the frame over the last, laid out as its Layout says; none or one is drawn as a Resolved camera's. The innermost layer's Resolution is the camera's own, so its screen conversions stay the centre's.

type Layout added in v0.3.0

type Layout uint8

Layout is how a picture's layers lie in its frame: Inset, each the central part of the frame, inset on every side; Footed, each centred across the frame and standing on its bottom edge.

const (
	Inset Layout = iota
	Footed
)

func LayoutOf added in v0.3.0

func LayoutOf(cam Camera) Layout

LayoutOf is how cam's layers lie in its frame; Inset for a camera without.

type Picker added in v0.3.0

type Picker interface {
	Pick(sx, sy float32) (x, y, z float32, ok bool)
}

Picker is a camera that finds what is under a screen point itself. Pick is the first point the screen point (sx, sy) sees and its height; where it sees none, a point far along its line and false.

type Projection added in v0.3.0

type Projection interface {
	// Project maps the world point (x, y) at height z to screen units.
	Project(x, y, z float32) (sx, sy float32)
	// Unproject inverts Project at height z.
	Unproject(sx, sy, z float32) (x, y float32)
	// Depth orders drawing: what is further back is smaller.
	Depth(x, y, z float32) float32
	// Wraps reports whether a world wrapping at its edges can be drawn through this projection.
	Wraps() bool
	// Sorts reports whether what is drawn through it must go back to front by Depth.
	Sorts() bool
	// Toward is the way from the world towards whoever looks through it, a unit vector with z up.
	Toward() [3]float32
}

Projection is how a world point at a height lands on the screen at zoom 1, with the world's origin at the screen's: the mapping a Camera draws through.

type Quad

type Quad struct {
	X0, Y0, X1, Y1     float32
	T0X, T1X, T0Y, T1Y float32
}

Quad is one piece of a rectangle projected to screen space, with its UV sub-range in T0/T1.

type RayField added in v0.3.0

type RayField struct {
	Origin, DX, DY [3]float32
	Dir, DDX, DDY  [3]float32
	Bend           float32
}

RayField is every line of sight of a camera at once: the ray through the screen point (sx, sy) starts at Origin + sx·DX + sy·DY and runs along Dir + sx·DDX + sy·DDY, not a unit vector. Bend is how far below the eye's level the ground d off along it is drawn, per d²; 0 flat.

func (RayField) At added in v0.3.0

func (f RayField) At(sx, sy float32) (origin, dir [3]float32)

At is the ray through the screen point (sx, sy): where it starts and the way it runs, not a unit vector.

type Rayer added in v0.3.0

type Rayer interface {
	Ray(sx, sy float32) (dx, dy, dz float32, ok bool)
}

Rayer is a camera that can say which way a screen point looks. Ray is the way from the eye through the screen point (sx, sy), a unit vector, false for a camera without an eye at the moment.

type Rays added in v0.3.0

type Rays interface {
	Rays() (RayField, bool)
}

Rays is a camera that can say every one of its lines of sight at once, as a RayField; false for a camera that cannot at the moment.

type Resolved added in v0.3.0

type Resolved interface {
	SetResolution(s float32)
	Resolution() float32
}

Resolved is a camera drawn at a resolution of the pixels it is shown at: SetResolution has its Viewport, Zoom and every screen conversion speak those camera pixels, the shown ones times s, its window on the world unchanged.

type Scaler added in v0.3.0

type Scaler interface {
	ScaleAt(x, y, z float32) float32
}

Scaler is a camera whose scale changes over the screen. ScaleAt is how many screen units a world unit spans at the world point (x, y, z); a point behind the eye takes the scale of one as far before it.

type Shifted added in v0.18.0

type Shifted interface {
	SetLevelShift(n lod.Level)
	LevelShift() lod.Level
}

Shifted is a camera whose picture is drawn LevelShift levels coarser than the world sees its sectors at, the frame budget's shift; a sector seen at lod.Standard or finer stays full.

type Sighted added in v0.18.0

type Sighted interface {
	Sight() float32
}

Sighted is a camera seeing a multiple of the world's sight, 1 as made.

type Snapped added in v0.5.0

type Snapped interface {
	Snap()
	Drawn() Camera
	Origin() Camera
}

Snapped is a camera drawn from a copy of itself (Snap), taken as the frame is laid out on its barrier, so the frame draws it as it stood while the ticks move it on. Drawn is the copy, the camera itself before the first; Origin is the camera a copy was taken of, the camera itself for one that is no copy. A copy is for drawing alone: it picks no ground and takes no command.

type State

type State struct {
	Viewport AABB
	Zoom     float32
}

State is a Camera's visible window and zoom.

type TopDown added in v0.3.0

type TopDown struct{}

TopDown is the plain map view: screen x and y are world x and y, height is not drawn.

func (TopDown) Depth added in v0.3.0

func (TopDown) Depth(_, y, _ float32) float32

func (TopDown) Project added in v0.3.0

func (TopDown) Project(x, y, _ float32) (float32, float32)

func (TopDown) Sorts added in v0.3.0

func (TopDown) Sorts() bool

func (TopDown) Toward added in v0.3.0

func (TopDown) Toward() [3]float32

func (TopDown) Unproject added in v0.3.0

func (TopDown) Unproject(sx, sy, _ float32) (float32, float32)

func (TopDown) Wraps added in v0.3.0

func (TopDown) Wraps() bool

type Transform added in v0.3.0

type Transform struct {
	M    [16]float32
	Eye  [3]float32
	Bend float32
}

Transform is how a camera's picture lies on the GPU: a world point p, sunk under the eye's level by Bend·d², goes to clip space as M·(p, 1), M column by column as WGSL holds it. The depth is reversed: 1 nearest, 0 furthest.

func SceneTransform added in v0.3.0

func SceneTransform(f RayField, w, h float32) (Transform, bool)

SceneTransform is the transform every mesh of a scene is drawn through, the rays f onto a viewport w by h, so their depths meet.

func TransformOf added in v0.3.0

func TransformOf(f RayField, w, h, near, far float32) (Transform, bool)

TransformOf is the transform drawing the rays f onto a viewport w by h pixels, its depth from near to far along the rays. It is false for rays that do not span the space.

func (Transform) Apply added in v0.3.0

func (t Transform) Apply(x, y, z, w, h float32) (sx, sy, depth float32, ok bool)

Apply is where t draws the world point (x, y, z) on a viewport w by h pixels, and its depth; false behind the eye.

func (Transform) Shows added in v0.22.0

func (tr Transform) Shows(box geom.AABB, margin, low, top, w, h float32, hole Window,
	holed bool) bool

Shows reports whether anything in the box, grown by margin and standing from low to top, may show through tr on a viewport w by h: not where it lies off one side of it, nor wholly within hole, the window a later layer covers (holed).

func (Transform) Unproject added in v0.3.0

func (t Transform) Unproject() ([16]float32, bool)

Unproject is the inverse of t's matrix, column by column: from clip space back to the world point drawn there, sunk as t sinks it. It is false where there is none.

type Vanisher added in v0.3.0

type Vanisher interface {
	Vanish(dx, dy, dz float32) (sx, sy float32, ok bool)
}

Vanisher is a camera with vanishing points, a perspective's. Vanish is where the direction (dx, dy, dz) vanishes on the screen, false for one not ahead of the eye or a view without vanishing points at the moment.

type Window added in v0.3.0

type Window struct{ X, Y, W, H float32 }

Window is a part of a camera's frame in its shown pixels: its top-left corner and its size.

func Arrange added in v0.3.0

func Arrange(layout Layout, layers []Layer, fw, fh int) []Window

Arrange is the window of each of layers in a frame fw by fh shown pixels laid out as layout says, in whole pixels, an inset part's margins even so it lies a whole pixel in.

func Inner added in v0.22.0

func Inner(cam Camera) (Window, bool)

Inner is the window of the layer drawn after the one cam is cropped to, in cam's own pixels: what that layer covers whole. False for the last layer and for a picture of one.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL