Skip to content

Visualize Your Ideas With Code

feat: world space cache (#498)

Jacob633e9e140dfb
diff --git a/packages/2d/src/components/Node.ts b/packages/2d/src/components/Node.ts
index dee3891f..4b0395b6 100644
--- a/packages/2d/src/components/Node.ts
+++ b/packages/2d/src/components/Node.ts
@@ -490,27 +490,6 @@ export class Node implements Promisable<Node> {
return matrix;
}
- @computed()
- protected cacheMatrix(): DOMMatrix {
- const parent = this.parent()?.cacheMatrix() ?? new DOMMatrix();
- const requiresCache = this.requiresCache();
-
- return requiresCache ? parent.multiply(this.worldToLocal()) : parent;
- }
-
- @computed()
- protected compositeMatrix(): DOMMatrix {
- const requiresCache = this.requiresCache();
-
- if (this.composite()) {
- const parent = this.parent()?.cacheMatrix() ?? new DOMMatrix();
- return requiresCache ? parent : parent.multiply(this.localToWorld());
- }
-
- const parent = this.parent()?.compositeMatrix() ?? new DOMMatrix();
- return requiresCache ? parent.multiply(this.worldToLocal()) : parent;
- }
-
/**
* A matrix mapping composite space to world space.
*
@@ -520,13 +499,8 @@ export class Node implements Promisable<Node> {
* appears relative to the closes composite root.
*/
@computed()
- public compositeToWorld() {
- if (this.composite()) {
- const parent = this.parent()?.cacheMatrix() ?? new DOMMatrix();
- return parent.multiply(this.localToWorld());
- }
-
- return this.parent()?.compositeMatrix() ?? new DOMMatrix();
+ public compositeToWorld(): DOMMatrix {
+ return this.compositeRoot()?.localToWorld() ?? new DOMMatrix();
}
@computed()
@@ -544,7 +518,7 @@ export class Node implements Promisable<Node> {
if (root) {
const worldToLocal = this.worldToLocal();
worldToLocal.m44 = 1;
- return root.localToWorld().multiply();
+ return root.localToWorld().multiply(worldToLocal);
}
return new DOMMatrix();
}
@@ -1009,11 +983,20 @@ export class Node implements Promisable<Node> {
@computed()
protected cachedCanvas() {
const context = this.cacheCanvas();
- const cache = this.cacheBBox();
+ const cache = this.worldSpaceCacheBBox();
+ const matrix = this.localToWorld();
+
context.canvas.width = cache.width;
context.canvas.height = cache.height;
- context.resetTransform();
- context.translate(-cache.x, -cache.y);
+
+ context.setTransform(
+ matrix.a,
+ matrix.b,
+ matrix.c,
+ matrix.d,
+ matrix.e - cache.x,
+ matrix.f - cache.y,
+ );
this.draw(context);
return context;
@@ -1038,7 +1021,7 @@ export class Node implements Promisable<Node> {
const cache = this.getCacheBBox();
const children = this.children();
if (children.length === 0) {
- return cache.pixelPerfect;
+ return cache;
}
const points: Vector2[] = cache.corners;
@@ -1050,7 +1033,7 @@ export class Node implements Promisable<Node> {
);
}
- return BBox.fromPoints(...points).pixelPerfect;
+ return BBox.fromPoints(...points);
}
/**
@@ -1087,6 +1070,37 @@ export class Node implements Promisable<Node> {
return result;
}
+ /**
+ * Get a bounding box in world space for the contents rendered by this node as
+ * well as its children.
+ *
+ * @remarks
+ * This is the same the bounding box returned by {@link cacheBBox} only
+ * transformed to world space.
+ */
+ @computed()
+ protected worldSpaceCacheBBox(): BBox {
+ return BBox.fromPoints(
+ ...this.cacheBBox().transformCorners(this.localToWorld()),
+ ).pixelPerfect;
+ }
+
+ /**
+ * Get the offset for this node's cache canvas.
+ *
+ * @remarks
+ * To put the cached contents in the correct position, cache canvases need to
+ * be offset relatively to the canvas they are being drawn on.
+ */
+ @computed()
+ protected cacheOffset(): Vector2 {
+ if (!this.requiresCache()) {
+ return this.parent()?.cacheOffset() ?? Vector2.zero;
+ }
+
+ return this.worldSpaceCacheBBox().position;
+ }
+
/**
* Prepare the given context for drawing a cached node onto it.
*
@@ -1132,17 +1146,21 @@ export class Node implements Promisable<Node> {
this.transformContext(context);
if (this.requiresCache()) {
- const cacheRect = this.cacheBBox();
+ const cacheRect = this.worldSpaceCacheBBox();
if (cacheRect.width !== 0 && cacheRect.height !== 0) {
+ const offset = cacheRect.position.sub(
+ this.parent()?.cacheOffset() ?? Vector2.zero,
+ );
this.setupDrawFromCache(context);
const cacheContext = this.cachedCanvas();
const compositeOverride = this.compositeOverride();
- context.drawImage(cacheContext.canvas, cacheRect.x, cacheRect.y);
+ context.resetTransform();
+ context.drawImage(cacheContext.canvas, offset.x, offset.y);
if (compositeOverride > 0) {
context.save();
context.globalAlpha *= compositeOverride;
context.globalCompositeOperation = 'source-over';
- context.drawImage(cacheContext.canvas, cacheRect.x, cacheRect.y);
+ context.drawImage(cacheContext.canvas, offset.x, offset.y);
context.restore();
}
}
diff --git a/packages/2d/src/decorators/filtersSignal.ts b/packages/2d/src/decorators/filtersSignal.ts
index 633dc8d4..013dc66b 100644
--- a/packages/2d/src/decorators/filtersSignal.ts
+++ b/packages/2d/src/decorators/filtersSignal.ts
@@ -43,7 +43,9 @@ export class FiltersSignalContext<TOwner> extends SignalContext<
) => {
if (newValue === undefined) {
return (
- this.get()?.find(filter => filter.name === props.name) ??
+ this.get()
+ ?.find(filter => filter.name === props.name)
+ ?.value() ??
props.default ??
0
);
diff --git a/packages/2d/src/scenes/Scene2D.ts b/packages/2d/src/scenes/Scene2D.ts
index 86407ac2..1ab4596a 100644
--- a/packages/2d/src/scenes/Scene2D.ts
+++ b/packages/2d/src/scenes/Scene2D.ts
@@ -58,7 +58,9 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {
public inspectPosition(x: number, y: number): InspectedElement | null {
return this.execute(
- () => this.getView().hit(new Vector2(x, y))?.key ?? null,
+ () =>
+ this.getView().hit(new Vector2(x, y).scale(this.resolutionScale))
+ ?.key ?? null,
);
}
@@ -93,7 +95,12 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {
): void {
const node = this.getNode(element);
if (node) {
- node.drawOverlay(context, matrix.multiply(node.localToWorld()));
+ node.drawOverlay(
+ context,
+ matrix
+ .scale(1 / this.resolutionScale, 1 / this.resolutionScale)
+ .multiplySelf(node.localToWorld()),
+ );
}
}
@@ -116,7 +123,8 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {
this.execute(() => {
const size = this.getSize();
this.view = new View2D({
- position: size.scale(0.5),
+ position: size.scale(this.resolutionScale / 2),
+ scale: this.resolutionScale,
size,
});
});
diff --git a/packages/core/src/app/Player.ts b/packages/core/src/app/Player.ts
index 99f8cfa0..3327788f 100644
--- a/packages/core/src/app/Player.ts
+++ b/packages/core/src/app/Player.ts
@@ -26,6 +26,7 @@ export interface PlayerSettings {
fps: number;
size: Vector2;
audioOffset: number;
+ resolutionScale: number;
}
const MAX_AUDIO_DESYNC = 1 / 50;
@@ -96,6 +97,7 @@ export class Player {
private requestedSeek = -1;
private requestedRecalculation = true;
private size: Vector2;
+ private resolutionScale: number;
private active = false;
private get startFrame() {
@@ -136,6 +138,7 @@ export class Player {
this.status = new PlaybackStatus(this.playback);
this.audio = new AudioManager(this.logger);
this.size = settings.size ?? new Vector2(1920, 1080);
+ this.resolutionScale = settings.resolutionScale ?? 1;
this.startTime = settings.range?.[0] ?? 0;
this.endTime = settings.range?.[1] ?? Infinity;
this.playback.fps = settings.fps ?? 60;
@@ -152,6 +155,7 @@ export class Player {
playback: this.status,
logger: this.project.logger,
size: this.size,
+ resolutionScale: this.resolutionScale,
timeEventsClass: EditableTimeEvents,
});
description.onReplaced?.subscribe(description => {
@@ -177,9 +181,16 @@ export class Player {
frame = Math.floor(frame * ratio);
recalculate = true;
}
- if (!settings.size.exactlyEquals(this.size)) {
+ if (
+ !settings.size.exactlyEquals(this.size) ||
+ settings.resolutionScale !== this.resolutionScale
+ ) {
this.size = settings.size;
- this.playback.reload({size: this.size});
+ this.resolutionScale = settings.resolutionScale;
+ this.playback.reload({
+ size: this.size,
+ resolutionScale: this.resolutionScale,
+ });
}
if (settings.audioOffset !== undefined) {
this.audio.setOffset(settings.audioOffset);
diff --git a/packages/core/src/app/Presenter.ts b/packages/core/src/app/Presenter.ts
index 477a4972..291e2062 100644
--- a/packages/core/src/app/Presenter.ts
+++ b/packages/core/src/app/Presenter.ts
@@ -86,6 +86,7 @@ export class Presenter {
logger: this.logger,
playback: this.status,
size: new Vector2(1920, 1080),
+ resolutionScale: 1,
timeEventsClass: ReadOnlyTimeEvents,
});
scenes.push(scene);
@@ -159,7 +160,7 @@ export class Presenter {
this.stage.configure(settings);
this.playback.fps = settings.fps;
- await this.reloadScenes(settings.size);
+ await this.reloadScenes(settings);
if (signal.aborted) return;
this.playback.state = PlaybackState.Playing;
await this.playback.recalculate();
@@ -180,13 +181,14 @@ export class Presenter {
});
}
- private async reloadScenes(size: Vector2) {
+ private async reloadScenes(settings: PresenterSettings) {
for (let i = 0; i < this.project.scenes.length; i++) {
const description = this.project.scenes[i];
const scene = this.playback.onScenesRecalculated.current[i];
scene.reload({
config: description.onReplaced.current.config,
- size: size,
+ size: settings.size,
+ resolutionScale: settings.resolutionScale,
});
scene.meta.set(description.meta.get());
}
diff --git a/packages/core/src/app/Renderer.ts b/packages/core/src/app/Renderer.ts
index af791075..fca1f2d0 100644
--- a/packages/core/src/app/Renderer.ts
+++ b/packages/core/src/app/Renderer.ts
@@ -76,6 +76,7 @@ export class Renderer {
logger: this.project.logger,
playback: this.status,
size: new Vector2(1920, 1080),
+ resolutionScale: 1,
timeEventsClass: ReadOnlyTimeEvents,
});
scenes.push(scene);
@@ -132,7 +133,7 @@ export class Renderer {
this.playback.fps = settings.fps;
this.playback.state = PlaybackState.Rendering;
- await this.reloadScenes(settings.size);
+ await this.reloadScenes(settings);
await this.playback.reset();
await this.playback.seek(this.status.secondsToFrames(settings.range[0]));
await this.stage.render(
@@ -170,7 +171,7 @@ export class Renderer {
const from = this.status.secondsToFrames(settings.range[0]);
const to = this.status.secondsToFrames(settings.range[1]);
- await this.reloadScenes(settings.size);
+ await this.reloadScenes(settings);
if (signal.aborted) return RendererResult.Aborted;
await this.playback.reset();
if (signal.aborted) return RendererResult.Aborted;
@@ -212,13 +213,14 @@ export class Renderer {
return result;
}
- private async reloadScenes(size: Vector2) {
+ private async reloadScenes(settings: RendererSettings) {
for (let i = 0; i < this.project.scenes.length; i++) {
const description = this.project.scenes[i];
const scene = this.playback.onScenesRecalculated.current[i];
scene.reload({
config: description.onReplaced.current.config,
- size: size,
+ size: settings.size,
+ resolutionScale: settings.resolutionScale,
});
scene.meta.set(description.meta.get());
}
diff --git a/packages/core/src/scenes/GeneratorScene.ts b/packages/core/src/scenes/GeneratorScene.ts
index 8aa9c409..f40cffd1 100644
--- a/packages/core/src/scenes/GeneratorScene.ts
+++ b/packages/core/src/scenes/GeneratorScene.ts
@@ -107,6 +107,7 @@ export abstract class GeneratorScene<T>
return this.previousScene;
}
+ protected resolutionScale: number;
private runnerFactory: ThreadGeneratorFactory<T>;
private previousScene: Scene | null = null;
private runner: ThreadGenerator | null = null;
@@ -120,6 +121,7 @@ export abstract class GeneratorScene<T>
) {
this.name = description.name;
this.size = description.size;
+ this.resolutionScale = description.resolutionScale;
this.logger = description.logger;
this.playback = description.playback;
this.meta = description.meta;
@@ -174,6 +176,7 @@ export abstract class GeneratorScene<T>
config,
size,
stack,
+ resolutionScale,
}: SceneDescriptionReload<ThreadGeneratorFactory<T>> = {}) {
if (config) {
this.runnerFactory = config;
@@ -181,6 +184,9 @@ export abstract class GeneratorScene<T>
if (size) {
this.size = size;
}
+ if (resolutionScale) {
+ this.resolutionScale = resolutionScale;
+ }
if (stack) {
this.creationStack = stack;
}
diff --git a/packages/core/src/scenes/Scene.ts b/packages/core/src/scenes/Scene.ts
index 019eb2a2..24f716e2 100644
--- a/packages/core/src/scenes/Scene.ts
+++ b/packages/core/src/scenes/Scene.ts
@@ -56,6 +56,7 @@ export interface SceneDescription<T = unknown> {
export interface FullSceneDescription<T = unknown> extends SceneDescription<T> {
name: string;
size: Vector2;
+ resolutionScale: number;
variables: Variables;
playback: PlaybackStatus;
logger: Logger;
@@ -70,6 +71,7 @@ export interface FullSceneDescription<T = unknown> extends SceneDescription<T> {
*/
export interface SceneDescriptionReload<T = unknown> {
size?: Vector2;
+ resolutionScale?: number;
config?: T;
stack?: string;
}