Skip to content

Camera ​

The guest chooses a view; the host draws it. Distances are metres, look angles are radians, and fovYDeg is vertical degrees. Import game APIs from gameable.

Pick a rig ​

Use player.camera: 'firstPerson' or 'thirdPerson' in defineGame for the built-in player rigs. The third-person rig retracts its boom against walls. Tune the follow camera in the adventure template. Use createTopDownCamera for pan/rotate/zoom controls; see Top-down camera.

For a custom system, preallocate its options and points, then write through ctx.camera.firstPerson, follow, or set; aim with ctx.camera.lookAt. An explicit camera write suppresses that step's built-in player rig. Reading ctx.camera.state also claims camera ownership, so use it deliberately.

Place a CameraActor ​

A CameraActor owns a component named cameraComponent. Spawn one during init:

ts
import {
  CameraActor,
  activateCamera,
  cameraActorComponent,
  spawnActor,
  type GameContext,
} from 'gameable';

function placeCamera(ctx: GameContext): void {
  const view = spawnActor(ctx, CameraActor, {
    position: { x: 0, y: 3, z: 8 },
    props: { fov: 55 },
  });
  cameraActorComponent.configure(view, {
    aspectRatio: 16 / 9,
    constrainAspectRatio: true,
  });
  activateCamera(ctx, view);
}

The default orientation looks down -Z. activateOnStart: true selects a placed camera immediately; autoActivateSeat: 0 selects it for seat zero (-1 disables automatic selection). A seated controller is required for manager transitions. deactivateCamera(ctx, seat) releases an activated camera; the player rig resumes on the next step. With no other writer, the last view remains.

Configure saved per-instance settings with cameraActorComponent.configure. For an orthographic view, set projection: 'orthographic' and orthoWidth: 20 (the horizontal world span). near/far control clipping; automatic ortho planes are optional. aspectRatio plus constrainAspectRatio: true letterboxes the render to that ratio on resize. postProcessSettings and postProcessBlendWeight supply the camera's look through the host VFX sink. Call notifyCameraCut(actor) when an actor camera teleports; for a directly written view, use ctx.camera.notifyCameraCut() to reset interpolation.

Attach a cameraArm ​

An actor camera can sit on a collision-aware boom. Declare components once, then use them on a pawn selected as the seat's view target:

ts
import { cameraArm, cameraComponent, defineActor, scene } from 'gameable';

const FollowView = defineActor('FollowView', {
  components: {
    root: scene(),
    arm: cameraArm(4, {
      attach: 'root',
      targetOffset: [0, 1.6, 0],
      socketOffset: [0.4, 0, 0],
      probeSize: 0.12,
      lagSpeed: 10,
      rotationLag: true,
      rotationLagSpeed: 10,
    }),
    cameraComponent: cameraComponent({ attach: 'arm', fov: 60 }),
  },
});

targetOffset moves the origin in world space; socketOffset shifts the boom endpoint locally. Position lag smooths the origin, rotation lag smooths the orientation, and collision retracts last. probeSize: 0 uses a ray; a positive radius uses a sphere. probeMask selects collision layers. Control rotation and pitch/yaw/roll inheritance are optional; lag substepping and maximum lag distance bound damping. Arm history survives snapshots.

Blend and add effects per seat ​

cameraManager.forSeat(0) returns the living seated controller's manager, or null. The player framework creates controllers. Use setViewTargetWithBlend to move between actors, and pass null to return to the pawn. Blend options are seconds, curve (linear, cubic, easeIn, easeOut, easeInOut), blendExp, and lockOutgoing. Zero time cuts; an interrupted blend starts from the visible pose.

Configure modifiers once after the controller is seated; trigger shakes and fades on game events:

ts
import { cameraManager, type GameContext } from 'gameable';

function configureCamera(): void {
  const manager = cameraManager.forSeat(0);
  if (!manager) return;
  manager.viewPitchMin = -1;
  manager.viewPitchMax = 0.5;
  manager.addNewCameraModifier({
    id: 'close-view',
    priority: 10,
    modify(view, _dt, alpha) {
      view.fovYDeg += (50 - view.fovYDeg) * alpha;
    },
  });
}

const impact = { amplitude: 0.08, frequency: 8, duration: 0.3 };
const black = { x: 0, y: 0, z: 0 };
function cameraImpact(ctx: GameContext): void {
  const manager = cameraManager.forSeat(0);
  manager?.startCameraShake(ctx, impact);
  manager?.startCameraFade(0, 1, 0.5, black, true);
}

stopCameraFade() clears a held fade. setFOV(60) locks FOV until unlockFOV(). Modifiers edit borrowed POV values in priority order; returning true stops later modifiers. Allocate nothing in callbacks. Register callbacks again in each fresh guest after seating; saved manager values survive snapshots. Actor hooks calcCamera, getEyesViewPoint, onBecomeViewTarget and onEndViewTarget customize view calculation and transitions.

Inspect and author a view ​

viewInfo.create allocates a reusable camera value during init; viewInfo.blendViewInfo blends into it. camera.projectionMatrix and camera.viewMatrix write into caller-owned arrays. Their matrices are column-major with WebGPU depth 0..1; camera.screenCoordinates and camera.unproject use the same viewport. On the page, window.__gameable.view.cameraMatrices() returns copies of the actual host matrices.

Page tools run in the browser console, outside the guest:

js
const view = window.__gameable.view;
const entity = view.entities().find((row) => row.name === 'monitor');
if (entity) {
  view.bounds(entity.id);
  view.drawStatus(entity.id);
  const photo = await view.capture({ target: entity.id, width: 512, height: 512 });
  console.log(photo.target); // onScreen, pixels, coverage
}

entities() includes undrawn entities; drawStatus distinguishes visibility, scene membership, placeholders and asset loading. capture returns a JPEG dataUrl from an independent perspective camera without moving the player. For a live texture inside the game, use scene capture.

window.__gameable.editor provides a temporary viewport. Enter before using it:

js
const editor = window.__gameable.editor;
await editor.enter();
editor.frameAll();
editor.orthoView('top', 30);
editor.bookmarks.set(0, 'Top');
editor.viewMode.set('wireframe');
editor.showFlags.set('grid', true);
editor.bookmarks.jump(0);
editor.exit();

Use editor.actors() to find actor ids, then focus(id), pilot(id) / eject(), hide(id), isolate([id]) and showAll(). Piloting moves the actor through editor transform edits. Hide masks, diagnostic materials and bookmarks belong to the page session; game visibility uses visibility.setHidden. commands lists command ids and default bindings; executeCommand(id) dispatches while editing. Query viewMode.supports and showFlags.supports before offering diagnostics: unsupported renderer features are rejected. Mesh diagnostics do not replace native splat shading. editor.screenshot() currently supports perspective only. Exiting restores the game's drawing state.