Files
feuerwerk/src/viewport/cameraPicture.ts
T
adminandClaude Opus 5.5 2e55071fa5
game-maker / gate (push) Successful in 3m2s
game-maker / suite (push) Successful in 14m59s
Lay a shader over a camera's whole picture (#299)
A shader so far worked only on its own wearer. Effects over the whole
game picture, such as a vignette, grey, scanlines or a red flash on a
hit, had no place to live. A camera now wears one, drawn the way a
field shader of a particle source is (#170).

- The camera gets a Shader row and, while one is picked, a Shader
  resolution row. The shader's properties follow as camera rows. They
  load, animate and appear in a Properties node under Type Camera.
- Every view keeps masks and objects in one container. CameraShade
  renders it into the camera's picture, cleared to the background,
  and draws the shader over the frame. Game tab and play.html take
  the container off the stage while the picture stands in for it.
- Screen pixels gives the picture as many pixels as it covers on
  screen. Camera pixels uses View width x View height and scales up
  without smoothing.
- A pixel counts as a camera pixel in both (the user's choice). The
  uniform uPixelScale makes Texture size read View width x View
  height, so a Blur keeps its reach at any game scale.
- Each camera wears its own shader, overlay cameras included. A red
  flash on the game camera leaves the HUD of an overlay camera alone.
- The scene view's eye has a Shader switch on the Camera row, off by
  default. With it on, the main camera's shader lies over its frame,
  below symbols and gizmos, and the world around it stays drawn.
- New tool tools/perf-camera-shader.mjs measures the cost.

Measured on stein2 (HD 630, Game tab at Fit 1310x679): a camera
shader that hands the picture through stays inside the scene's own
spread of 3 to 5 fps between runs. A Blur of 4 cost 4 to 8 fps.

Tests:
- Gate on melektaus-atx: 227 files, 3497 tests (3487 + 10).
- e2e 63 alone: 11 checks (6 + 5), e2e sum 1725 counted.
- 63e-63i: pass-through is byte-equal and upright at 100%, flat red
  reaches the corners while the overlay keeps its green, Texture
  size reads 640 x 360 in both resolutions at Fit, no shader draws
  as before, and the scene view shows it only with the switch on.
- Counter-checks: a camera left out of the shader wearers fails 1
  unit test. Counting screen pixels fails 63g with #ab6000.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 16:03:09 +02:00

55 lines
2.3 KiB
TypeScript

/**
* The picture a camera's shader is laid over (#299), as numbers: how many pixels it gets, which
* transform takes the world into it, and what a pixel of it counts as. Pure, so the arithmetic is
* tested without a GPU.
*/
import type { Mat } from '../core/model/matrix';
import type { PropertyBag } from '../core/model/Properties';
import { shaderResolutionOf } from '../core/properties/cameraShader';
import { cameraFrame, cameraSize } from '../core/properties/camera';
/** The longest side the picture may have, a game tab on a large screen included. */
export const CAMERA_MAX_PIXELS = 4096;
export interface CameraPicture {
/** The picture's size in pixels: the camera's raster, or the frame as large as it stands on screen. */
pixels: { w: number; h: number };
/** World units to pixels of the picture: the frame's top left corner is 0,0. */
toPicture: Mat;
/** The frame in world units, centred on the camera: where the shaded picture is drawn. */
frame: { w: number; h: number };
/**
* Camera pixels one pixel of the picture covers, per axis: what the shader counts a pixel as in
* either resolution (the user's choice), so a Blur of 4 is 4 camera pixels at any game scale.
*/
pixelScale: { x: number; y: number };
/** Whether the picture is scaled up without smoothing: the camera's raster, for pixel art. */
blocky: boolean;
}
/** `devicePixelsPerUnit` is what one world unit covers on screen, in device pixels. */
export function cameraPicture(
camera: { x: number; y: number; props: PropertyBag },
devicePixelsPerUnit: number,
): CameraPicture {
const frame = cameraFrame(camera.props);
const size = cameraSize(camera.props);
const blocky = shaderResolutionOf(camera.props) === 'camera';
const pixels = blocky
? { w: pixelsAlong(size.w), h: pixelsAlong(size.h) }
: { w: pixelsAlong(frame.w * devicePixelsPerUnit), h: pixelsAlong(frame.h * devicePixelsPerUnit) };
const a = pixels.w / frame.w;
const d = pixels.h / frame.h;
return {
pixels,
toPicture: { a, b: 0, c: 0, d, tx: pixels.w / 2 - camera.x * a, ty: pixels.h / 2 - camera.y * d },
frame,
pixelScale: { x: size.w / pixels.w, y: size.h / pixels.h },
blocky,
};
}
function pixelsAlong(length: number): number {
return Math.min(CAMERA_MAX_PIXELS, Math.max(1, Math.round(Number.isFinite(length) ? length : 1)));
}