API Reference · Scene and camera

Camera

Namespace: Ava3D

public sealed class Camera

An orbit camera: a point it looks at, and a direction and distance it looks from.

Orbit rather than free-fly because this is a viewer control first — the overwhelmingly common thing to want is "show me this object, let me turn it round". A free-fly camera can be built on top by moving Camera.Target as well as the angles.

Properties

MemberDescription
float Distance { get; set; }

How far the camera sits from Camera.Target.

Nullable<float> FarPlane { get; set; }

Far clip distance, or null to derive one from the scene's size.

float FieldOfView { get; set; }

Vertical field of view in degrees.

Vector3 Forward { get; }

The unit vector the camera looks along. Comes from the angles rather than from Target − Position, so it is exact and stays meaningful at Camera.Distance zero.

Nullable<float> NearPlane { get; set; }

Near and far planes. Left null they are derived from Camera.Distance and the scene's size, which keeps the ratio tight — the single biggest cause of depth fighting is a near plane set far closer than the scene needs.

float Pitch { get; set; }

Elevation in radians, clamped just short of straight up and down. At exactly vertical the up vector and the view direction become parallel and the view matrix collapses.

Vector3 Position { get; set; }

Where the camera is in world space, derived from its target, distance, yaw and pitch.

Assigning it leaves Camera.Target alone and re-derives Camera.Distance, Camera.Yaw and Camera.Pitch, so it is the getter's inverse up to one thing: the ±89° clamp on Camera.Pitch. An eye placed exactly above the target comes back a degree short, because straight up is where the view matrix collapses.

Assigning the target's own position does nothing, since a single point gives no direction to look from and there is no answer that would be less arbitrary than the one already there.

Vector3 Right { get; }

Screen-right in world space, as a unit vector. Turns with Camera.Roll.

float Roll { get; set; }

Rotation about the view direction, in radians. Zero keeps world +Y upright, which is what an orbit camera does and what every scene got before this existed.

Positive turns the camera clockwise about Camera.Forward — the right-hand rule, seen from behind the camera — which tilts the image anticlockwise on screen. Sprites roll with it: all three backends take a billboard's axes from the view matrix, so they stay square to the viewport rather than to a horizon that is no longer level.

Not clamped, and not wrapped: a shot that rolls through several turns can just keep counting.

Vector3 Target { get; set; }

The point the camera looks at, in world space.

Vector3 Up { get; }

Screen-up in world space, as a unit vector. Turns with Camera.Roll, and is what Camera.View uses — at Camera.Roll zero it is world +Y made perpendicular to Camera.Forward, so the view matrix is the one this camera has always produced.

Matrix4x4 View { get; }

The view matrix: from Camera.Position, at Camera.Target, with Camera.Up up.

float Yaw { get; set; }

Rotation about the world Y axis, in radians.

Methods

MemberDescription
void Fit(BoundingBox bounds, float margin = 1.25f)

Frames bounds: looks at its centre from far enough away that it fits.

The distance comes from the bounding sphere and the field of view, which is right for an object you want to look at and wrong for a building you want to stand inside. There is no way to tell those apart from geometry alone, so callers who know better should set Camera.Distance themselves afterwards.

Matrix4x4 GetProjection(float aspectRatio)

The perspective projection for a viewport of the given width-to-height ratio.

aspectRatio

Viewport width divided by height. Values at or below zero are treated as 1.

void LookAt(Vector3 target)

Aims at target without moving the camera — a pan rather than an orbit. Camera.Position is where it was; Camera.Distance, Camera.Yaw and Camera.Pitch re-derive around the new target.

void LookFrom(Vector3 eye, Vector3 target)

Puts the camera at eye looking at target — the whole of what a scripted shot needs from an orbit camera.

It is exactly the two assignments in exactly this order, and the order is not optional: Camera.Position is measured from Camera.Target, so writing them the other way round leaves the eye somewhere neither argument asked for. This exists so that trap is not on offer.

void RollToward(Vector3 up)

Sets Camera.Roll so that up is as close to straight up on screen as it can be — the part of it along Camera.Forward cannot be shown and is ignored.

This is what a camera riding a banking aircraft wants: hand it the aircraft's own up axis and the horizon tips with the wings. Without it, using Camera.Roll at all means working out the sign convention from the source, which is not an API.

The result is kept continuous with the roll already there rather than being reduced to −π..π, so calling this every frame through a barrel roll counts up instead of snapping. A up pointing straight along Camera.Forward names no direction on screen and leaves the roll alone.

See also