Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance runtime package with new math and systems modules

konstantin-paulus4dd3d32a8ad8
diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts
index 44c3960e..e0b18e7a 100644
--- a/packages/runtime/src/index.ts
+++ b/packages/runtime/src/index.ts
@@ -3,6 +3,8 @@ export * from './traits';
export * from './world/create-world';
export * from './world/serialize';
export * from './queries';
+export * from './systems';
+export * from './math';
export * from './capture';
export * from './fonts';
export * from './utils';
diff --git a/packages/runtime/src/math/aabb.ts b/packages/runtime/src/math/aabb.ts
new file mode 100644
index 00000000..e5856e5e
--- /dev/null
+++ b/packages/runtime/src/math/aabb.ts
@@ -0,0 +1,114 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * Axis-Aligned Bounding Box utilities.
+ */
+
+import type { Mat2D } from './matrix2d';
+
+export type AABB = { minX: number; minY: number; maxX: number; maxY: number };
+
+/**
+ * Compute the world-space AABB of a rectangle [0,0]→[w,h]
+ * transformed by a 2D affine matrix.
+ */
+export function aabbFromTransformedRect(mat: Mat2D, w: number, h: number): AABB {
+ const { a, b, c, d, e, f } = mat;
+
+ // Transform 4 corners: (0,0), (w,0), (w,h), (0,h)
+ const x0 = e, y0 = f;
+ const x1 = a * w + e, y1 = b * w + f;
+ const x2 = a * w + c * h + e, y2 = b * w + d * h + f;
+ const x3 = c * h + e, y3 = d * h + f;
+
+ return {
+ minX: Math.min(x0, x1, x2, x3),
+ minY: Math.min(y0, y1, y2, y3),
+ maxX: Math.max(x0, x1, x2, x3),
+ maxY: Math.max(y0, y1, y2, y3),
+ };
+}
+
+/** Test whether two AABBs overlap. */
+export function aabbsIntersect(a: AABB, b: AABB): boolean {
+ return !(
+ a.maxX < b.minX ||
+ a.minX > b.maxX ||
+ a.maxY < b.minY ||
+ a.minY > b.maxY
+ );
+}
+
+// ─── Polygon geometry ───────────────────────────────────────────
+
+export type Point = { x: number; y: number };
+
+/** Four corners of a transformed rect: [TL, TR, BR, BL]. */
+export type Quad = [Point, Point, Point, Point];
+
+/** Get the four world-space corners of a rect [0,0]→[w,h] through a Mat2D. */
+export function rectToQuad(mat: Mat2D, w: number, h: number): Quad {
+ const { a, b, c, d, e, f } = mat;
+ return [
+ { x: e, y: f },
+ { x: a * w + e, y: b * w + f },
+ { x: a * w + c * h + e, y: b * w + d * h + f },
+ { x: c * h + e, y: d * h + f },
+ ];
+}
+
+/** Transform a single point through a Mat2D. */
+export function transformPoint(mat: Mat2D, px: number, py: number): Point {
+ return {
+ x: mat.a * px + mat.c * py + mat.e,
+ y: mat.b * px + mat.d * py + mat.f,
+ };
+}
+
+/** Invert a Mat2D. */
+export function invert2D(m: Mat2D): Mat2D {
+ const { a, b, c, d, e, f } = m;
+ const det = a * d - b * c;
+
+ const inv = 1 / det;
+ return {
+ a: d * inv,
+ b: -b * inv,
+ c: -c * inv,
+ d: a * inv,
+ e: (c * f - d * e) * inv,
+ f: (b * e - a * f) * inv,
+ };
+}
+
+/** Point-in-polygon test (ray casting). */
+export function pointInQuad(px: number, py: number, q: Quad): boolean {
+ let inside = false;
+ for (let i = 0, j = 3; i < 4; j = i++) {
+ const xi = q[i].x, yi = q[i].y;
+ const xj = q[j].x, yj = q[j].y;
+ if (((yi > py) !== (yj > py)) && (px < (xj - xi) * (py - yi) / (yj - yi) + xi)) {
+ inside = !inside;
+ }
+ }
+ return inside;
+}
+
+/** Test whether two quads intersect (vertex containment check). */
+export function quadsIntersect(a: Quad, b: Quad): boolean {
+ for (let i = 0; i < 4; i++) {
+ if (pointInQuad(a[i].x, a[i].y, b)) return true;
+ if (pointInQuad(b[i].x, b[i].y, a)) return true;
+ }
+ return false;
+}
+
+/** Center point of a quad. */
+export function quadCenter(q: Quad): Point {
+ return {
+ x: (q[0].x + q[1].x + q[2].x + q[3].x) / 4,
+ y: (q[0].y + q[1].y + q[2].y + q[3].y) / 4,
+ };
+}
diff --git a/packages/runtime/src/math/common.ts b/packages/runtime/src/math/common.ts
new file mode 100644
index 00000000..3f13b64f
--- /dev/null
+++ b/packages/runtime/src/math/common.ts
@@ -0,0 +1,18 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * Shared math constants and helpers.
+ */
+
+export const DEG_TO_RAD = Math.PI / 180;
+export const RAD_TO_DEG = 180 / Math.PI;
+
+export function degToRad(deg: number): number {
+ return deg * DEG_TO_RAD;
+}
+
+export function radToDeg(rad: number): number {
+ return rad * RAD_TO_DEG;
+}
diff --git a/packages/runtime/src/math/index.ts b/packages/runtime/src/math/index.ts
index c19029b2..7366f8d0 100644
--- a/packages/runtime/src/math/index.ts
+++ b/packages/runtime/src/math/index.ts
@@ -1 +1,7 @@
-// Math primitives: aabb, matrix2d, common (moves wholesale from engine/math/).
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+export * from './common';
+export * from './matrix2d';
+export * from './aabb';
diff --git a/packages/runtime/src/math/matrix2d.ts b/packages/runtime/src/math/matrix2d.ts
new file mode 100644
index 00000000..4ebbfcb0
--- /dev/null
+++ b/packages/runtime/src/math/matrix2d.ts
@@ -0,0 +1,226 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * Pure 2D affine matrix utilities.
+ *
+ * Mat2D uses named properties { a, b, c, d, e, f } matching
+ * CanvasRenderingContext2D / DOMMatrix:
+ *
+ * | a c e |
+ * | b d f |
+ * | 0 0 1 |
+ */
+
+import { degToRad } from './common';
+
+export type Mat2D = {
+ a: number; b: number; c: number;
+ d: number; e: number; f: number;
+};
+
+/** Return a fresh identity matrix. */
+export function identity2D(): Mat2D {
+ return { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 };
+}
+
+/**
+ * Compose a local 2D affine matrix from transform properties.
+ *
+ * Equivalent to: T(pos+pivot) · R(rot) · SkewX · SkewY · S(scale) · T(-pivot)
+ *
+ * @param x Position X
+ * @param y Position Y
+ * @param rotation Rotation in **degrees**
+ * @param scaleX Scale X
+ * @param scaleY Scale Y
+ * @param pivotX Pivot X in pixels (anchorX × width)
+ * @param pivotY Pivot Y in pixels (anchorY × height)
+ * @param skewX Skew X in **degrees**
+ * @param skewY Skew Y in **degrees**
+ */
+export function compose2D(
+ x: number,
+ y: number,
+ rotation: number,
+ scaleX: number,
+ scaleY: number,
+ pivotX: number,
+ pivotY: number,
+ skewX: number,
+ skewY: number,
+): Mat2D {
+ const rot = degToRad(rotation);
+ const skx = degToRad(skewX);
+ const sky = degToRad(skewY);
+
+ const cos = Math.cos(rot);
+ const sin = Math.sin(rot);
+ const tanSkewX = Math.tan(skx);
+ const tanSkewY = Math.tan(sky);
+
+ // SkewX · SkewY · Scale (pre-rotation matrix)
+ const a1 = scaleX * (1 + tanSkewX * tanSkewY);
+ const b1 = tanSkewY * scaleX;
+ const c1 = tanSkewX * scaleY;
+ const d1 = scaleY;
+
+ // R(rot) · [a1 c1; b1 d1]
+ const a = cos * a1 - sin * b1;
+ const b = sin * a1 + cos * b1;
+ const c = cos * c1 - sin * d1;
+ const d = sin * c1 + cos * d1;
+
+ // T(pos+pivot) · M · T(-pivot)
+ const tx = x + pivotX;
+ const ty = y + pivotY;
+ const e = tx + a * (-pivotX) + c * (-pivotY);
+ const f = ty + b * (-pivotX) + d * (-pivotY);
+
+ return { a, b, c, d, e, f };
+}
+
+/**
+ * Multiply two 2D affine matrices: result = p × q.
+ */
+export function multiply2D(p: Mat2D, q: Mat2D): Mat2D {
+ return {
+ a: p.a * q.a + p.c * q.b,
+ b: p.b * q.a + p.d * q.b,
+ c: p.a * q.c + p.c * q.d,
+ d: p.b * q.c + p.d * q.d,
+ e: p.e + p.a * q.e + p.c * q.f,
+ f: p.f + p.b * q.e + p.d * q.f,
+ };
+}
+
+/** Return a translation matrix. */
+export function translate2D(tx: number, ty: number): Mat2D {
+ return { a: 1, b: 0, c: 0, d: 1, e: tx, f: ty };
+}
+
+/** Return a rotation matrix. Angle is in **degrees**. */
+export function rotate2D(degrees: number): Mat2D {
+ const r = degToRad(degrees);
+ const cos = Math.cos(r);
+ const sin = Math.sin(r);
+ return { a: cos, b: sin, c: -sin, d: cos, e: 0, f: 0 };
+}
+
+/** Return a scale matrix. */
+export function scale2D(sx: number, sy: number): Mat2D {
+ return { a: sx, b: 0, c: 0, d: sy, e: 0, f: 0 };
+}
+
+/** Return a skew matrix. Angles are in **degrees**. */
+export function skew2D(degreesX: number, degreesY: number): Mat2D {
+ const tx = Math.tan(degToRad(degreesX));
+ const ty = Math.tan(degToRad(degreesY));
+ return { a: 1, b: ty, c: tx, d: 1, e: 0, f: 0 };
+}
+
+export type Decomposed2D = {
+ x: number;
+ y: number;
+ rotation: number;
+ scaleX: number;
+ scaleY: number;
+ skewX: number; // degrees
+ skewY: number; // degrees
+};
+
+const EPS = 1e-12;
+
+/**
+ * Decompose a 2D matrix into translate / rotate / scale / skew.
+ *
+ * Uses column-based QR decomposition. In our matrix layout
+ * | a c e |
+ * | b d f |
+ * | 0 0 1 |
+ * column 0 is (a, b) and column 1 is (c, d).
+ */
+export function decompose2D(m: Mat2D): Decomposed2D {
+ const { a, b, c, d, e, f } = m;
+
+ // Translation is direct.
+ const x = e;
+ const y = f;
+
+ // Compute scaleX as length of column 0 (a, b).
+ let scaleX = Math.hypot(a, b);
+
+ // If scaleX is ~0, matrix is degenerate.
+ if (scaleX < EPS) {
+ // Fall back: treat column 0 as zero, get scaleY from column 1.
+ const scaleY = Math.hypot(c, d);
+ return {
+ x,
+ y,
+ rotation: 0,
+ scaleX: 0,
+ scaleY,
+ skewX: 0,
+ skewY: 0,
+ };
+ }
+
+ // Normalize column 0 to get rotation.
+ const r0x = a / scaleX;
+ const r0y = b / scaleX;
+
+ // "Shear" factor: dot of normalized column 0 with column 1.
+ let shear = r0x * c + r0y * d;
+
+ // Remove shear from column 1 to isolate scaleY.
+ let c2 = c - r0x * shear;
+ let d2 = d - r0y * shear;
+
+ let scaleY = Math.hypot(c2, d2);
+
+ // If scaleY is ~0, it's also degenerate.
+ if (scaleY < EPS) {
+ const rotationRad = Math.atan2(r0y, r0x);
+ return {
+ x,
+ y,
+ rotation: rotationRad * 180 / Math.PI,
+ scaleX,
+ scaleY: 0,
+ skewX: 0,
+ skewY: 0,
+ };
+ }
+
+ // Normalize shear to get the actual shear value in "tan(angle)" space.
+ shear /= scaleY;
+
+ // Detect reflection (negative determinant) and fold it into scaleY by convention.
+ const det = a * d - b * c;
+ if (det < 0) {
+ // Flip one axis. Common choice: flip scaleY.
+ scaleY = -scaleY;
+ shear = -shear;
+ c2 = -c2;
+ d2 = -d2;
+ }
+
+ // Rotation in radians from normalized column 0.
+ const rotationRad = Math.atan2(r0y, r0x);
+ const rotation = rotationRad * 180 / Math.PI;
+
+ // Convert shear factor to skew angle (X-shear).
+ const skewX = Math.atan(shear) * 180 / Math.PI;
+ const skewY = 0;
+
+ return {
+ x,
+ y,
+ rotation,
+ scaleX,
+ scaleY,
+ skewX,
+ skewY,
+ };
+}
diff --git a/packages/runtime/src/systems/index.ts b/packages/runtime/src/systems/index.ts
index d63a113d..420eaf42 100644
--- a/packages/runtime/src/systems/index.ts
+++ b/packages/runtime/src/systems/index.ts
@@ -1 +1,2 @@
-// Re-exports deterministic systems. Browser-only systems (input, keyboard, camera, hud, dom) stay in apps/web.
+// Deterministic systems. Browser-only systems (input, keyboard, camera, hud, dom) stay in apps/web.
+export * from './transform';
diff --git a/packages/runtime/src/systems/transform.ts b/packages/runtime/src/systems/transform.ts
index 44e0ccc1..fab35297 100644
--- a/packages/runtime/src/systems/transform.ts
+++ b/packages/runtime/src/systems/transform.ts
@@ -1 +1,330 @@
-// Transform system: local/world matrices and AABBs (was systems/transform.ts).
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+import { Not, Or, getStore } from 'koota';
+
+import {
+ ChildOf, Geometry, Group, Deleted, Hidden, Sequential, AdjustmentLayer,
+ Culled, Flip, Anchor, Computed, Cache, LocalTransform, WorldTransform,
+ WorldBounds, Camera, RenderSurface,
+} from '../traits';
+import { getParentEntity } from '../queries/hierarchy';
+
+import {
+ multiply2D,
+ aabbFromTransformedRect,
+ aabbsIntersect,
+ translate2D,
+ rotate2D,
+ skew2D,
+ scale2D,
+ type Mat2D,
+} from '../math';
+
+import type { Entity, World } from 'koota';
+
+// This system writes derived per-frame data (LocalTransform, WorldTransform,
+// WorldBounds, Computed group bounds) straight into trait stores via getStore:
+// no snapshots, no change events, matching the bitecs SoA hot path.
+
+/**
+ * Compute the local 2D affine matrix from Offset, Rotation, Scale,
+ * Anchor, Skew and Size. Stores the result in LocalTransform.
+ */
+export function computeLocalMatrix(world: World, entity: Entity): void {
+ const flip = getStore(world, Flip);
+ const anchor = getStore(world, Anchor);
+ const computed = getStore(world, Computed);
+ const local = getStore(world, LocalTransform);
+ const eid = entity.id();
+
+ const positionX = computed.positionX[eid] + computed.offsetX[eid];
+ const positionY = computed.positionY[eid] + computed.offsetY[eid];
+ const pivotX = (anchor.x[eid] ?? 0.5) * computed.width[eid];
+ const pivotY = (anchor.y[eid] ?? 0.5) * computed.height[eid];
+ const scaleX = (flip.x[eid] ?? 1) * computed.scaleX[eid];
+ const scaleY = (flip.y[eid] ?? 1) * computed.scaleY[eid];
+ const rotation = computed.rotation[eid];
+ const skewX = computed.skewX[eid];
+ const skewY = computed.skewY[eid];
+
+ let mat = translate2D(positionX, positionY);
+ mat = multiply2D(mat, translate2D(pivotX, pivotY));
+ mat = multiply2D(mat, rotate2D(rotation));
+ mat = multiply2D(mat, skew2D(skewX, skewY));
+ mat = multiply2D(mat, scale2D(scaleX, scaleY));
+ mat = multiply2D(mat, translate2D(-pivotX, -pivotY));
+
+ local.a[eid] = mat.a;
+ local.b[eid] = mat.b;
+ local.c[eid] = mat.c;
+ local.d[eid] = mat.d;
+ local.e[eid] = mat.e;
+ local.f[eid] = mat.f;
+}
+
+/**
+ * WorldTransform = Parent.WorldTransform * LocalMatrix
+ * For top-level entities (no entity parent), the camera × DPR matrix is used.
+ */
+export function computeWorldTransform(world: World, entity: Entity, parentEntity: Entity | null): void {
+ const worldStore = getStore(world, WorldTransform);
+ const localStore = getStore(world, LocalTransform);
+ const eid = entity.id();
+
+ if (localStore.a[eid] === undefined) {
+ localStore.a[eid] = 1;
+ localStore.b[eid] = 0;
+ localStore.c[eid] = 0;
+ localStore.d[eid] = 1;
+ localStore.e[eid] = 0;
+ localStore.f[eid] = 0;
+ }
+
+ let parent: Mat2D;
+ if (parentEntity !== null) {
+ const pid = parentEntity.id();
+ parent = {
+ a: worldStore.a[pid], b: worldStore.b[pid],
+ c: worldStore.c[pid], d: worldStore.d[pid],
+ e: worldStore.e[pid], f: worldStore.f[pid],
+ };
+ } else {
+ const camera = world.get(Camera)!;
+ const res = world.get(RenderSurface)?.resolution ?? 1;
+ parent = {
+ a: camera.a * res, b: camera.b * res,
+ c: camera.c * res, d: camera.d * res,
+ e: camera.e * res, f: camera.f * res,
+ };
+ }
+
+ const local: Mat2D = {
+ a: localStore.a[eid], b: localStore.b[eid],
+ c: localStore.c[eid], d: localStore.d[eid],
+ e: localStore.e[eid], f: localStore.f[eid],
+ };
+
+ const m = multiply2D(parent, local);
+
+ worldStore.a[eid] = m.a;
+ worldStore.b[eid] = m.b;
+ worldStore.c[eid] = m.c;
+ worldStore.d[eid] = m.d;
+ worldStore.e[eid] = m.e;
+ worldStore.f[eid] = m.f;
+}
+
+/**
+ * Compute world-space AABB from world transform and size.
+ */
+export function computeWorldBounds(world: World, entity: Entity): void {
+ const boundsStore = getStore(world, WorldBounds);
+ const worldStore = getStore(world, WorldTransform);
+ const computed = getStore(world, Computed);
+ const eid = entity.id();
+
+ const worldMat: Mat2D = {
+ a: worldStore.a[eid], b: worldStore.b[eid],
+ c: worldStore.c[eid], d: worldStore.d[eid],
+ e: worldStore.e[eid], f: worldStore.f[eid],
+ };
+ const mat = multiply2D(worldMat, translate2D(computed.originX[eid], computed.originY[eid]));
+ const bounds = aabbFromTransformedRect(mat, computed.width[eid], computed.height[eid]);
+
+ boundsStore.minX[eid] = bounds.minX;
+ boundsStore.minY[eid] = bounds.minY;
+ boundsStore.maxX[eid] = bounds.maxX;
+ boundsStore.maxY[eid] = bounds.maxY;
+}
+
+function cullEntity(world: World, entity: Entity, parentEntity: Entity | null): void {
+ // Inherit cull from the parent: the renderer already skips entire
+ // subtrees of culled parents, so descendants are effectively invisible
+ // even when their own AABB intersects the parent's. Propagating the tag
+ // down lets us free their decoders too. Pre-order DFS guarantees the
+ // parent's tag is already settled by the time we reach the child.
+
+ let intersect = true;
+
+ if (parentEntity === null) {
+ // Without a render surface (headless world) nothing is culled.
+ const canvas = world.get(RenderSurface)?.canvas;
+ if (canvas) {
+ const boundsStore = getStore(world, WorldBounds);
+ const eid = entity.id();
+ intersect = aabbsIntersect({
+ minX: boundsStore.minX[eid],
+ minY: boundsStore.minY[eid],
+ maxX: boundsStore.maxX[eid],
+ maxY: boundsStore.maxY[eid],
+ }, {
+ minX: 0,
+ minY: 0,
+ maxX: canvas.width,
+ maxY: canvas.height,
+ });
+ }
+ } else {
+ intersect = !parentEntity.has(Culled);
+ }
+
+ const wasCulled = entity.has(Culled);
+
+ if (!intersect) {
+ if (!wasCulled) {
+ entity.add(Culled);
+ }
+ } else if (wasCulled) {
+ entity.remove(Culled);
+ }
+}
+
+/**
+ * Recompute a Group entity's Computed bounds from the union of its children's
+ * transformed boxes. Sequences aren't spatial constructs: they mirror their
+ * parent's frame instead (the walk is parent-first, so the parent's Computed
+ * values are already settled). Leaf entities own an absolute Size and need no
+ * resolution here.
+ */
+export function computeGroupBounds(world: World, entity: Entity): void {
+ const computed = getStore(world, Computed);
+ const localStore = getStore(world, LocalTransform);
+ const eid = entity.id();
+
+ if (entity.has(Sequential)) {
+ const parent = getParentEntity(entity);
+ if (parent !== null) {
+ const pid = parent.id();
+ computed.width[eid] = computed.width[pid];
+ computed.height[eid] = computed.height[pid];
+ computed.originX[eid] = computed.originX[pid] ?? 0;
+ computed.originY[eid] = computed.originY[pid] ?? 0;
+ return;
+ }
+ }
+
+ if (entity.has(Group)) {
+ let minX = Infinity;
+ let minY = Infinity;
+ let maxX = -Infinity;
+ let maxY = -Infinity;
+ let any = false;
+
+ for (const child of world.query(Or(Geometry, Group), ChildOf(entity), Not(Deleted), Not(Hidden))) {
+ const cid = child.id();
+ const mat: Mat2D = {
+ a: localStore.a[cid] ?? 1, b: localStore.b[cid] ?? 0,
+ c: localStore.c[cid] ?? 0, d: localStore.d[cid] ?? 1,
+ e: localStore.e[cid] ?? 0, f: localStore.f[cid] ?? 0,
+ };
+ const bounds = aabbFromTransformedRect(mat, computed.width[cid], computed.height[cid]);
+ if (bounds.minX < minX) minX = bounds.minX;
+ if (bounds.minY < minY) minY = bounds.minY;
+ if (bounds.maxX > maxX) maxX = bounds.maxX;
+ if (bounds.maxY > maxY) maxY = bounds.maxY;
+ any = true;
+ }
+
+ if (!any) {
+ computed.width[eid] = 0;
+ computed.height[eid] = 0;
+ return;
+ }
+
+ computed.width[eid] = Math.max(0, maxX - minX);
+ computed.height[eid] = Math.max(0, maxY - minY);
+ computed.originX[eid] = minX;
+ computed.originY[eid] = minY;
+ }
+}
+
+function adjustLayers(world: World, adjust: Entity): void {
+ const computed = getStore(world, Computed);
+ const localStore = getStore(world, LocalTransform);
+ const aid = adjust.id();
+
+ if (computed.visibility[aid] === 0 || adjust.has(Hidden)) return;
+
+ // get next non sequential parent
+ let slot = adjust;
+ let parent = getParentEntity(slot);
+ while (parent !== null && parent.has(Sequential)) {
+ slot = parent;
+ parent = getParentEntity(slot);
+ }
+ if (parent === null) return;
+
+ const siblings = parent.get(Cache)?.children;
+ if (!siblings) return;
+ const idx = siblings.indexOf(slot);
+ if (idx <= 0) return;
+
+ const target = siblings[idx - 1];
+ if (target === undefined) return;
+
+ // adjust's own local matrix must be current; it is not visited by the walk
+ // (adjustment layers are neither Geometry nor Group) and reads only
+ // motion-resolved Computed values.
+ computeLocalMatrix(world, adjust);
+
+ const tid = target.id();
+ const adjustLocal: Mat2D = {
+ a: localStore.a[aid], b: localStore.b[aid], c: localStore.c[aid],
+ d: localStore.d[aid], e: localStore.e[aid], f: localStore.f[aid],
+ };
+ const clipLocal: Mat2D = {
+ a: localStore.a[tid], b: localStore.b[tid], c: localStore.c[tid],
+ d: localStore.d[tid], e: localStore.e[tid], f: localStore.f[tid],
+ };
+
+ const composed = multiply2D(adjustLocal, clipLocal);
+ localStore.a[tid] = composed.a;
+ localStore.b[tid] = composed.b;
+ localStore.c[tid] = composed.c;
+ localStore.d[tid] = composed.d;
+ localStore.e[tid] = composed.e;
+ localStore.f[tid] = composed.f;
+
+ const reworld = (entity: Entity, parentEntity: Entity | null) => {
+ computeWorldTransform(world, entity, parentEntity);
+ computeWorldBounds(world, entity);
+ cullEntity(world, entity, parentEntity);
+
+ for (const child of world.query(Or(Geometry, Group), ChildOf(entity), Not(Deleted))) {
+ reworld(child, entity);
+ }
+ };
+
+ reworld(target, parent);
+}
+
+/**
+ * Transform system entry point. Call once per frame before the render system.
+ */
+export function transformSystem(world: World): void {
+ const walk = (entity: Entity, parentEntity: Entity | null) => {
+ computeGroupBounds(world, entity);
+ computeLocalMatrix(world, entity);
+ computeWorldTransform(world, entity, parentEntity);
+ computeWorldBounds(world, entity);
+ cullEntity(world, entity, parentEntity);
+
+ for (const child of world.query(Or(Geometry, Group), ChildOf(entity), Not(Deleted))) {
+ walk(child, entity);
+ }
+ };
+
+ // koota cannot express "has no ChildOf target" as an indexed query
+ // (bitecs allowed Not(ChildOf('*'))), so roots are filtered per candidate.
+ for (const entity of world.query(Or(Geometry, Group), Not(Deleted))) {
+ if (entity.targetFor(ChildOf) === undefined) {
+ walk(entity, null);
+ }
+ }
+
+ for (const adjust of world.query(AdjustmentLayer, Not(Deleted))) {
+ adjustLayers(world, adjust);
+ }
+}
diff --git a/packages/runtime/src/world/create-world.ts b/packages/runtime/src/world/create-world.ts
index b2775db2..342bc516 100644
--- a/packages/runtime/src/world/create-world.ts
+++ b/packages/runtime/src/world/create-world.ts
@@ -18,6 +18,9 @@ import {
Mounts,
FramePromises,
} from '../traits/world';
+import {
+ Anchor, Cache, Computed, Flip, LocalTransform, WorldBounds, WorldTransform,
+} from '../traits';
/**
* World with the headless runtime singletons attached. The host injects its
@@ -26,7 +29,7 @@ import {
* top (input, history, persistence, asset stores).
*/
export function createRuntimeWorld(projectId: string) {
- return createWorld(
+ const world = createWorld(
Project({ id: projectId }),
Mode,
ActiveScene,
@@ -40,6 +43,14 @@ export function createRuntimeWorld(projectId: string) {
Mounts,
FramePromises,
);
+
+ // koota registers a trait's store on first add/query; getStore alone
+ // throws. Prime the stores the per-frame systems write through getStore
+ // (bitecs-style, without the trait necessarily being on the entity) so
+ // they run safely on a fresh world.
+ world.spawn(Anchor, Cache, Computed, Flip, LocalTransform, WorldBounds, WorldTransform).destroy();
+
+ return world;
}
export type RuntimeWorld = ReturnType<typeof createRuntimeWorld>;