@bendyline/molen-tooling 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/README.md +22 -4
  2. package/dist/capture/harness.js +216 -216
  3. package/dist/capture/worldgen-preview.js +288 -288
  4. package/dist/cli.mjs +134 -12
  5. package/dist/docs-src/guide/3d-art-guidelines.md +2 -2
  6. package/dist/docs-src/guide/3d-model-assets.md +49 -13
  7. package/dist/docs-src/guide/adaptive-performance.md +4 -0
  8. package/dist/docs-src/guide/agent-loop.md +6 -6
  9. package/dist/docs-src/guide/aircraft.md +25 -17
  10. package/dist/docs-src/guide/audio.md +247 -0
  11. package/dist/docs-src/guide/browser-mount.md +17 -1
  12. package/dist/docs-src/guide/building-interiors.md +7 -5
  13. package/dist/docs-src/guide/determinism.md +27 -22
  14. package/dist/docs-src/guide/earth-view.md +444 -0
  15. package/dist/docs-src/guide/entities.md +17 -6
  16. package/dist/docs-src/guide/examples.md +39 -23
  17. package/dist/docs-src/guide/experience-playback.md +2 -3
  18. package/dist/docs-src/guide/game-samples.md +50 -21
  19. package/dist/docs-src/guide/graphics-performance.md +40 -8
  20. package/dist/docs-src/guide/input.md +9 -0
  21. package/dist/docs-src/guide/markers.md +52 -0
  22. package/dist/docs-src/guide/materials.md +10 -3
  23. package/dist/docs-src/guide/navigation.md +144 -0
  24. package/dist/docs-src/guide/project.md +23 -3
  25. package/dist/docs-src/guide/quickstart.md +39 -30
  26. package/dist/docs-src/guide/recognizable-places.md +24 -17
  27. package/dist/docs-src/guide/rendering-backends.md +5 -4
  28. package/dist/docs-src/guide/scripting.md +8 -4
  29. package/dist/docs-src/guide/sky.md +26 -9
  30. package/dist/docs-src/guide/source-bundles.md +0 -1
  31. package/dist/docs-src/guide/structure-library.md +20 -10
  32. package/dist/docs-src/guide/terrain.md +51 -6
  33. package/dist/docs-src/guide/vehicles.md +27 -14
  34. package/dist/docs-src/guide/worldgen.md +34 -24
  35. package/dist/docs-src/llms.txt +35 -18
  36. package/dist/docs-src/schemas/README.md +4 -1
  37. package/dist/docs-src/schemas/archive-set.md +175 -0
  38. package/dist/docs-src/schemas/components.md +1104 -1
  39. package/dist/docs-src/schemas/matgraph.md +6 -0
  40. package/dist/docs-src/schemas/soundbank.md +251 -0
  41. package/dist/docs-src/schemas/structure-placements.md +496 -0
  42. package/dist/docs-src/schemas/terrain-package.md +120 -0
  43. package/dist/index.d.mts +31 -3
  44. package/dist/index.mjs +2 -2
  45. package/dist/{mcp-BFnCWrg0.mjs → mcp-BTVwVKFb.mjs} +78 -4
  46. package/dist/{ops-BGxhSP6w.mjs → ops-BPdHodFR.mjs} +1181 -61
  47. package/dist/templates/city-courier/README.md +66 -0
  48. package/dist/templates/city-courier/checks.json +20 -0
  49. package/dist/templates/city-courier/commands.json +32 -0
  50. package/dist/templates/city-courier/delivery-run.replay.json +214 -0
  51. package/dist/templates/city-courier/gen/molen-types.ts +1016 -0
  52. package/dist/templates/city-courier/index.html +2 -0
  53. package/dist/templates/city-courier/package.json +26 -0
  54. package/dist/templates/city-courier/project.json +8 -0
  55. package/dist/templates/city-courier/scene.json +3474 -0
  56. package/dist/templates/city-courier/scripts/game.ts +131 -0
  57. package/dist/templates/city-courier/scripts/molen-scripts.d.ts +1288 -0
  58. package/dist/templates/city-courier/scripts/tsconfig.json +16 -0
  59. package/dist/templates/city-courier/src/audio.ts +96 -0
  60. package/dist/templates/city-courier/src/game-shell.css +317 -0
  61. package/dist/templates/city-courier/src/main.ts +84 -0
  62. package/dist/templates/city-courier/src/scene.ts +17 -0
  63. package/dist/templates/city-courier/src/vite-env.d.ts +1 -0
  64. package/dist/templates/city-courier/src/worker.ts +5 -0
  65. package/dist/templates/city-courier/test/headless.test.ts +95 -0
  66. package/dist/templates/city-courier/test/playthrough.test.js +52 -0
  67. package/dist/templates/city-courier/test/replay.test.ts +22 -0
  68. package/dist/templates/city-courier/tsconfig.json +17 -0
  69. package/dist/templates/city-courier/vite.config.ts +19 -0
  70. package/dist/templates/cubes/README.md +51 -0
  71. package/dist/templates/cubes/index.html +16 -0
  72. package/dist/templates/cubes/package.json +24 -0
  73. package/dist/templates/cubes/scene.json +15 -0
  74. package/dist/templates/cubes/src/cubes.ts +100 -0
  75. package/dist/templates/cubes/src/main.ts +21 -0
  76. package/dist/templates/cubes/src/worker.ts +15 -0
  77. package/dist/templates/cubes/test/headless.test.ts +98 -0
  78. package/dist/templates/cubes/tsconfig.json +17 -0
  79. package/dist/templates/cubes/vite.config.ts +17 -0
  80. package/dist/templates/data-viz/README.md +47 -0
  81. package/dist/templates/data-viz/camera-track.json +12 -0
  82. package/dist/templates/data-viz/data.json +9 -0
  83. package/dist/templates/data-viz/index.html +16 -0
  84. package/dist/templates/data-viz/package.json +25 -0
  85. package/dist/templates/data-viz/scene.json +7 -0
  86. package/dist/templates/data-viz/src/main.ts +39 -0
  87. package/dist/templates/data-viz/src/viz.ts +91 -0
  88. package/dist/templates/data-viz/src/worker.ts +12 -0
  89. package/dist/templates/data-viz/test/headless.test.ts +75 -0
  90. package/dist/templates/data-viz/tsconfig.json +17 -0
  91. package/dist/templates/data-viz/vite.config.ts +16 -0
  92. package/dist/templates/figures-gallery/README.md +46 -0
  93. package/dist/templates/figures-gallery/index.html +16 -0
  94. package/dist/templates/figures-gallery/package.json +25 -0
  95. package/dist/templates/figures-gallery/scene.json +276 -0
  96. package/dist/templates/figures-gallery/src/main.ts +44 -0
  97. package/dist/templates/figures-gallery/src/worker.ts +17 -0
  98. package/dist/templates/figures-gallery/test/headless.test.ts +90 -0
  99. package/dist/templates/figures-gallery/tsconfig.json +17 -0
  100. package/dist/templates/figures-gallery/vite.config.ts +17 -0
  101. package/dist/templates/index.json +191 -0
  102. package/dist/templates/skybound/README.md +66 -0
  103. package/dist/templates/skybound/checks.json +24 -0
  104. package/dist/templates/skybound/commands.json +32 -0
  105. package/dist/templates/skybound/first-leap.replay.json +224 -0
  106. package/dist/templates/skybound/gen/molen-types.ts +1016 -0
  107. package/dist/templates/skybound/index.html +2 -0
  108. package/dist/templates/skybound/package.json +26 -0
  109. package/dist/templates/skybound/project.json +8 -0
  110. package/dist/templates/skybound/scene.json +1777 -0
  111. package/dist/templates/skybound/scripts/game.ts +122 -0
  112. package/dist/templates/skybound/scripts/molen-scripts.d.ts +1288 -0
  113. package/dist/templates/skybound/scripts/tsconfig.json +16 -0
  114. package/dist/templates/skybound/src/audio.ts +96 -0
  115. package/dist/templates/skybound/src/game-shell.css +317 -0
  116. package/dist/templates/skybound/src/main.ts +63 -0
  117. package/dist/templates/skybound/src/scene.ts +17 -0
  118. package/dist/templates/skybound/src/vite-env.d.ts +1 -0
  119. package/dist/templates/skybound/src/worker.ts +5 -0
  120. package/dist/templates/skybound/test/headless.test.ts +124 -0
  121. package/dist/templates/skybound/test/playthrough.test.js +38 -0
  122. package/dist/templates/skybound/test/replay.test.ts +22 -0
  123. package/dist/templates/skybound/tsconfig.json +17 -0
  124. package/dist/templates/skybound/vite.config.ts +19 -0
  125. package/dist/templates/terrain-flyover/README.md +46 -0
  126. package/dist/templates/terrain-flyover/index.html +16 -0
  127. package/dist/templates/terrain-flyover/package.json +26 -0
  128. package/dist/templates/terrain-flyover/src/flyover.ts +23 -0
  129. package/dist/templates/terrain-flyover/src/main.ts +93 -0
  130. package/dist/templates/terrain-flyover/terrain.json +15 -0
  131. package/dist/templates/terrain-flyover/test/headless.test.ts +72 -0
  132. package/dist/templates/terrain-flyover/tsconfig.json +17 -0
  133. package/dist/templates/terrain-flyover/vite.config.ts +15 -0
  134. package/dist/templates/top-down-arena/README.md +65 -0
  135. package/dist/templates/top-down-arena/arena-skirmish.replay.json +204 -0
  136. package/dist/templates/top-down-arena/index.html +16 -0
  137. package/dist/templates/top-down-arena/package.json +26 -0
  138. package/dist/templates/top-down-arena/project.json +10 -0
  139. package/dist/templates/top-down-arena/scene.json +182 -0
  140. package/dist/templates/top-down-arena/scripts/chase.ts +17 -0
  141. package/dist/templates/top-down-arena/scripts/combat.ts +13 -0
  142. package/dist/templates/top-down-arena/scripts/control.ts +8 -0
  143. package/dist/templates/top-down-arena/scripts/molen-scripts.d.ts +1258 -0
  144. package/dist/templates/top-down-arena/scripts/spawner.ts +11 -0
  145. package/dist/templates/top-down-arena/scripts/tsconfig.json +16 -0
  146. package/dist/templates/top-down-arena/src/arena.ts +8 -0
  147. package/dist/templates/top-down-arena/src/audio.ts +96 -0
  148. package/dist/templates/top-down-arena/src/gen/molen-types.ts +1020 -0
  149. package/dist/templates/top-down-arena/src/main.ts +34 -0
  150. package/dist/templates/top-down-arena/src/scene.ts +24 -0
  151. package/dist/templates/top-down-arena/src/vite-env.d.ts +1 -0
  152. package/dist/templates/top-down-arena/src/worker.ts +7 -0
  153. package/dist/templates/top-down-arena/test/headless.test.ts +87 -0
  154. package/dist/templates/top-down-arena/test/replay.test.ts +22 -0
  155. package/dist/templates/top-down-arena/tsconfig.json +17 -0
  156. package/dist/templates/top-down-arena/types/arena.types.json +54 -0
  157. package/dist/templates/top-down-arena/vite.config.ts +21 -0
  158. package/package.json +14 -14
@@ -0,0 +1,22 @@
1
+ /// <reference types="node" />
2
+ import { join } from 'node:path';
3
+ import { runReplayFile } from '@bendyline/molen-tooling';
4
+ import { describe, expect, it } from 'vitest';
5
+
6
+ // A committed replay fixture is the long-lived half of the determinism guarantee. The pinned hash
7
+ // in headless.test.ts freezes one run of the current build; this freezes a *recorded command log*
8
+ // plus per-tick reference hashes, so when behaviour drifts the harness reports the first tick that
9
+ // diverged instead of "the hash moved". It also distinguishes a non-deterministic build from an
10
+ // intentional behaviour change — refresh with `molen replay <fixture> --record`.
11
+
12
+ describe('city-courier: recorded replay', () => {
13
+ it('reproduces the recorded run tick for tick', async () => {
14
+ const result = await runReplayFile({
15
+ path: join(process.cwd(), 'delivery-run.replay.json'),
16
+ });
17
+ expect(result.error).toBeUndefined();
18
+ expect(result.ok, result.report).toBe(true);
19
+ expect(result.actualHash).toBe(result.expectedHash);
20
+ expect(result.firstDivergentTick).toBeUndefined();
21
+ }, 60_000);
22
+ });
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "lib": ["ES2023", "DOM", "DOM.Iterable"],
7
+ "types": [],
8
+ "strict": true,
9
+ "noUncheckedIndexedAccess": true,
10
+ "verbatimModuleSyntax": true,
11
+ "skipLibCheck": true,
12
+ "forceConsistentCasingInFileNames": true,
13
+ "noEmit": true,
14
+ "resolveJsonModule": true
15
+ },
16
+ "include": ["src", "test"]
17
+ }
@@ -0,0 +1,19 @@
1
+ import { molenScripts } from '@bendyline/molen-client/vite';
2
+ import { defineConfig } from 'vite';
3
+
4
+ export default defineConfig({
5
+ // Scene scripts are authored in TypeScript; strip types when they are read as text.
6
+ plugins: [molenScripts()],
7
+ base: './',
8
+ // Vite gives worker bundles their own plugin pipeline, and the kernel runs in the Worker —
9
+ // without this the Worker would receive unstripped TypeScript.
10
+ worker: { format: 'es', plugins: () => [molenScripts()] },
11
+ build: {
12
+ target: 'es2022',
13
+ // main.ts mounts with a top-level `await`, and the client lazy-loads its WebGPU driver. By
14
+ // default Rollup puts three.js and the client in the entry chunk, so the lazy chunk imports
15
+ // the entry while the entry is still suspended at that `await`: the import never settles and
16
+ // the page stays blank in every browser with a WebGPU adapter. Their own chunk breaks the cycle.
17
+ rollupOptions: { output: { manualChunks: { vendor: ['three', '@bendyline/molen-client'] } } },
18
+ },
19
+ });
@@ -0,0 +1,51 @@
1
+ # Cubes
2
+
3
+ Fifty seeded cubes spinning and drifting inside a bounce box, one `spawn_cube` command, one pinned determinism test. This is the smallest complete Molen experience and the reference for the architecture at minimum size: a scene manifest, the kernel in a Worker, the three.js client mounted on the page. Copy it when you start something new.
4
+
5
+ ![Cubes](https://raw.githubusercontent.com/bendyline/molen/main/examples/cubes/preview.png)
6
+
7
+ ## Run
8
+
9
+ Play it in the browser at [molen.dev/play/cubes](https://molen.dev/play/cubes/). To run and change
10
+ your own copy, make one from the npm packages (no clone of the engine repository needed):
11
+
12
+ ```sh
13
+ npx @bendyline/molen-tooling new my-cubes --template cubes
14
+ cd my-cubes
15
+ npm install
16
+ npm run dev
17
+ ```
18
+
19
+ Space: spawn one more cube. That is the scene's own `input` block — `Space` binds to a `spawn` action, and a `press` rule emits the `spawn_cube` command.
20
+
21
+ ## Authoring map
22
+
23
+ - `scene.json`: fifteen lines of data — seed, 30 Hz tick rate, the fixed camera, the `Space` input rule, and the command with its doc string. No entities: the cubes are seeded in code.
24
+ - `src/cubes.ts`: the whole demo. The `spin` and `drift` components, the two systems that move them, the `spawn_cube` handler, and the fifty initial cubes seeded from the manifest seed via `createRng`. Rotation goes through `dmath`, never transcendental `Math`.
25
+ - `src/worker.ts`: `buildWorld(scene, setup)` under a `KernelHost` — the same build order the CLI uses.
26
+ - `src/main.ts`: `await mountExperience({ link: worker, scene, canvas })` plus a one-line hint. Framing and input come from `scene.json`, so this file re-declares nothing.
27
+
28
+ Cubes is a **setup-module** sample: the systems are TypeScript because ECS systems in other phases are exactly what a setup module is for. For the same shape expressed as pure scene data, read [top-down-arena](https://github.com/bendyline/molen/tree/main/examples/top-down-arena) (`--template top-down-arena`).
29
+
30
+ ## Verify
31
+
32
+ From this directory:
33
+
34
+ ```sh
35
+ npx molen validate scene.json
36
+ npx molen sim run scene.json --ticks 300 --setup src/cubes.ts --hash
37
+ npx molen shot scene.json --ticks 200 --setup src/cubes.ts --camera 9,6,17 --look 0,0,0 --out cubes.png
38
+ npm test
39
+ ```
40
+
41
+ `--setup src/cubes.ts` hands Node a TypeScript module, which it loads with its built-in type
42
+ stripping: that is on by default from Node 22.18 (on 22.13–22.17, run with
43
+ `NODE_OPTIONS=--experimental-strip-types`).
44
+
45
+ `test/headless.test.ts` is the only test file, and it holds a **pinned state hash** at 300 ticks — the same hash `sim run --hash` prints above, so a drift is visible from the command line. Around it: run-to-run equality, component counts through a `molen/assert@1` doc, three `spawn_cube` commands adjudicated at their tick (50 + 3 cubes), and a check that the world changes at all between tick 1 and tick 100.
46
+
47
+ There is no golden image and no replay fixture here. Cubes is a determinism fixture, not a render one; `shot` is for looking at it, not for regressions.
48
+
49
+ In the engine repository this sample lives in `examples/cubes/`. The same commands run from
50
+ that directory, and `pnpm --filter @bendyline/molen-examples-cubes test:unit` runs its tests from the
51
+ root.
@@ -0,0 +1,16 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>molen — cubes</title>
7
+ <style>
8
+ html, body { margin: 0; height: 100%; overflow: hidden; background: #11131a; }
9
+ #view { display: block; width: 100vw; height: 100vh; }
10
+ </style>
11
+ </head>
12
+ <body>
13
+ <canvas id="view"></canvas>
14
+ <script type="module" src="/src/main.ts"></script>
15
+ </body>
16
+ </html>
@@ -0,0 +1,24 @@
1
+ {
2
+ "private": true,
3
+ "type": "module",
4
+ "scripts": {
5
+ "dev": "vite --port 5225",
6
+ "build": "vite build",
7
+ "preview": "vite preview",
8
+ "typecheck": "tsc -p tsconfig.json",
9
+ "test": "vitest run"
10
+ },
11
+ "dependencies": {
12
+ "@bendyline/molen-client": "workspace:*",
13
+ "@bendyline/molen-kernel": "workspace:*",
14
+ "@bendyline/molen-schema": "workspace:*",
15
+ "three": "0.184.0"
16
+ },
17
+ "devDependencies": {
18
+ "@bendyline/molen-tooling": "workspace:*",
19
+ "@types/three": "0.184.0",
20
+ "typescript": "^6.0.3",
21
+ "vite": "^6.4.0",
22
+ "vitest": "^5.0.1"
23
+ }
24
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "format": "molen/scene@3",
3
+ "name": "cubes",
4
+ "seed": "cubes-phase0",
5
+ "tickRate": 30,
6
+ "keyframeInterval": 60,
7
+ "camera": { "mode": "fixed", "position": [0, 6, 26], "lookAt": [0, 0, 0] },
8
+ "input": {
9
+ "bindings": { "Space": "spawn" },
10
+ "emit": [{ "kind": "press", "action": "spawn", "command": "spawn_cube" }]
11
+ },
12
+ "commands": {
13
+ "spawn_cube": { "doc": "Spawn one more cube at a seeded random pose." }
14
+ }
15
+ }
@@ -0,0 +1,100 @@
1
+ import {
2
+ createRng,
3
+ defineComponent,
4
+ dmath,
5
+ type JsonObject,
6
+ type Rng,
7
+ type System,
8
+ Transform,
9
+ type World,
10
+ type WorldSetup,
11
+ } from '@bendyline/molen-kernel';
12
+
13
+ // Components specific to the cubes demo. All pure JSON.
14
+ interface Spin extends JsonObject {
15
+ axis: [number, number, number];
16
+ radPerTick: number;
17
+ angle: number;
18
+ }
19
+ interface Drift extends JsonObject {
20
+ vel: [number, number, number];
21
+ range: number;
22
+ }
23
+
24
+ const SpinC = defineComponent<Spin>('spin');
25
+ const DriftC = defineComponent<Drift>('drift');
26
+
27
+ const PALETTE = ['#e6194b', '#3cb44b', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6'];
28
+
29
+ /** Quaternion from axis (assumed normalized) + angle, via the engine's deterministic math. */
30
+ function quatFromAxisAngle(
31
+ axis: [number, number, number],
32
+ angle: number,
33
+ ): [number, number, number, number] {
34
+ const half = angle / 2;
35
+ const s = dmath.sin(half);
36
+ return [axis[0] * s, axis[1] * s, axis[2] * s, dmath.cos(half)];
37
+ }
38
+
39
+ function normalize3(v: [number, number, number]): [number, number, number] {
40
+ const len = dmath.hypot(v[0], v[1], v[2]) || 1;
41
+ return [v[0] / len, v[1] / len, v[2] / len];
42
+ }
43
+
44
+ function makeCube(rng: Rng): Record<string, JsonObject> {
45
+ const axis = normalize3([rng.range(-1, 1), rng.range(-1, 1), rng.range(-1, 1)]);
46
+ const color = PALETTE[rng.int(PALETTE.length)] as string;
47
+ return {
48
+ transform: {
49
+ pos: [rng.range(-8, 8), rng.range(-5, 5), rng.range(-8, 8)],
50
+ rot: [0, 0, 0, 1],
51
+ },
52
+ spin: { axis, radPerTick: rng.range(0.01, 0.08), angle: 0 },
53
+ drift: { vel: [rng.range(-1, 1), rng.range(-1, 1), rng.range(-1, 1)], range: 10 },
54
+ renderable: { kind: 'primitive', ref: 'box', materialRef: `palette:${color}` },
55
+ };
56
+ }
57
+
58
+ /** Registers the cubes systems + spawn command and seeds ~50 initial cubes. */
59
+ export const setup: WorldSetup = (world: World, manifest): void => {
60
+ const initRng = createRng(`${manifest.seed}:init`);
61
+ for (let i = 0; i < 50; i++) world.spawnRaw(makeCube(initRng));
62
+
63
+ const spinSystem: System = (w) => {
64
+ for (const [id, spin] of w.query(SpinC)) {
65
+ const angle = spin.angle + spin.radPerTick;
66
+ w.patch(id, SpinC, { angle });
67
+ w.patch(id, Transform, { rot: quatFromAxisAngle(spin.axis, angle) });
68
+ }
69
+ };
70
+
71
+ const driftSystem: System = (w, ctx) => {
72
+ for (const [id, t, drift] of w.query(Transform, DriftC)) {
73
+ const pos: [number, number, number] = [
74
+ t.pos[0] + drift.vel[0] * ctx.dt,
75
+ t.pos[1] + drift.vel[1] * ctx.dt,
76
+ t.pos[2] + drift.vel[2] * ctx.dt,
77
+ ];
78
+ const vel: [number, number, number] = [...drift.vel];
79
+ // Bounce at the bounds so cubes stay on-screen.
80
+ for (let a = 0; a < 3; a++) {
81
+ if (dmath.abs(pos[a] as number) > drift.range) {
82
+ vel[a] = -(vel[a] as number);
83
+ pos[a] = dmath.sign(pos[a] as number) * drift.range;
84
+ }
85
+ }
86
+ w.patch(id, Transform, { pos });
87
+ w.patch(id, DriftC, { vel });
88
+ }
89
+ };
90
+
91
+ world.addSystem(spinSystem, { name: 'spin' });
92
+ world.addSystem(driftSystem, { name: 'drift' });
93
+
94
+ // Space in the browser sends this; the kernel adjudicates and spawns deterministically.
95
+ world.registerCommand('spawn_cube', (w, _cmd, ctx) => {
96
+ w.spawnRaw(makeCube(ctx.rng));
97
+ });
98
+ };
99
+
100
+ export default setup;
@@ -0,0 +1,21 @@
1
+ import { mountExperience } from '@bendyline/molen-client';
2
+ import { validate } from '@bendyline/molen-schema';
3
+ import sceneDoc from '../scene.json';
4
+
5
+ // Browser entry: the kernel runs in a Worker (see worker.ts); this side mounts the client on
6
+ // it. The scene's own `camera` and `input` blocks drive framing and the Space -> spawn_cube
7
+ // command, so nothing here re-implements what scene.json already declares.
8
+ const scene = validate('scene', sceneDoc);
9
+ if (!scene.ok) throw new Error(scene.formatted);
10
+
11
+ const canvas = document.getElementById('view') as HTMLCanvasElement;
12
+ const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' });
13
+
14
+ await mountExperience({ link: worker, scene: scene.value, canvas, clearColor: '#11131a' });
15
+
16
+ // Tiny on-screen hint.
17
+ const hint = document.createElement('div');
18
+ hint.textContent = 'press SPACE to spawn a cube';
19
+ hint.style.cssText =
20
+ 'position:fixed;left:12px;bottom:10px;font:13px system-ui;color:#9aa;pointer-events:none';
21
+ document.body.appendChild(hint);
@@ -0,0 +1,15 @@
1
+ /// <reference lib="webworker" />
2
+ import { buildWorld, KernelHost } from '@bendyline/molen-kernel';
3
+ import { type MessageLink, validate } from '@bendyline/molen-schema';
4
+ import sceneDoc from '../scene.json';
5
+ import { setup } from './cubes';
6
+
7
+ // The Worker entry: build the world (scene entities + cubes systems/commands) and host it,
8
+ // wiring the kernel protocol to the worker's own message port.
9
+ const scene = validate('scene', sceneDoc);
10
+ if (!scene.ok) throw new Error(scene.formatted);
11
+
12
+ const world = buildWorld(scene.value, setup);
13
+ new KernelHost(world, self as unknown as MessageLink, {
14
+ keyframeInterval: scene.value.keyframeInterval,
15
+ });
@@ -0,0 +1,98 @@
1
+ import type { Command, SceneManifest } from '@bendyline/molen-schema';
2
+ import { validate } from '@bendyline/molen-schema';
3
+ import { runSimulation } from '@bendyline/molen-tooling';
4
+ import { describe, expect, it } from 'vitest';
5
+ import sceneDoc from '../scene.json';
6
+ import { setup } from '../src/cubes';
7
+
8
+ function scene(): SceneManifest {
9
+ const r = validate('scene', sceneDoc);
10
+ if (!r.ok) throw new Error(r.formatted);
11
+ return r.value;
12
+ }
13
+
14
+ describe('cubes headless', () => {
15
+ // A pinned hash, not just run-to-run equality: comparing a run with itself proves the engine
16
+ // is reproducible, never that its behaviour is unchanged. Only a literal moves when a refactor
17
+ // alters system order, float sequencing or the broad phase. Refresh it deliberately.
18
+ it('matches the pinned state hash across engine versions', async () => {
19
+ const r = await runSimulation({ scene: scene(), ticks: 300, setup });
20
+ expect(r.stateHash).toBe(
21
+ 'sha256:e707c63c138d69cb2d044ca67f0157b092887c41a4c631f729350381db5d46c5',
22
+ );
23
+ });
24
+
25
+ it('runs 300 ticks deterministically (identical hash across runs)', async () => {
26
+ const a = await runSimulation({ scene: scene(), ticks: 300, setup });
27
+ const b = await runSimulation({ scene: scene(), ticks: 300, setup });
28
+ expect(a.tick).toBe(300);
29
+ expect(a.stateHash).toBe(b.stateHash);
30
+ });
31
+
32
+ it('seeds ~50 cubes and they all have transform + renderable', async () => {
33
+ const r = await runSimulation({
34
+ scene: scene(),
35
+ ticks: 1,
36
+ setup,
37
+ assertDoc: {
38
+ format: 'molen/assert@1',
39
+ assertions: [
40
+ { select: 'has:spin', op: 'count', value: 50 },
41
+ { select: 'has:renderable', op: 'count', value: 50 },
42
+ { select: 'has:transform', op: 'count', value: 50 },
43
+ ],
44
+ },
45
+ });
46
+ expect(r.ok).toBe(true);
47
+ });
48
+
49
+ it('spawn_cube commands add cubes at their adjudicated tick', async () => {
50
+ const commands: Command[] = [
51
+ {
52
+ kind: 'command',
53
+ seq: 0,
54
+ source: 'local',
55
+ tick: 5,
56
+ tickExecuted: 5,
57
+ type: 'spawn_cube',
58
+ payload: {},
59
+ },
60
+ {
61
+ kind: 'command',
62
+ seq: 1,
63
+ source: 'local',
64
+ tick: 5,
65
+ tickExecuted: 5,
66
+ type: 'spawn_cube',
67
+ payload: {},
68
+ },
69
+ {
70
+ kind: 'command',
71
+ seq: 2,
72
+ source: 'local',
73
+ tick: 8,
74
+ tickExecuted: 8,
75
+ type: 'spawn_cube',
76
+ payload: {},
77
+ },
78
+ ];
79
+ const r = await runSimulation({
80
+ scene: scene(),
81
+ ticks: 20,
82
+ setup,
83
+ commands,
84
+ assertDoc: {
85
+ format: 'molen/assert@1',
86
+ assertions: [{ select: 'has:spin', op: 'count', value: 53 }], // 50 + 3 spawned
87
+ },
88
+ });
89
+ expect(r.ok).toBe(true);
90
+ });
91
+
92
+ it('cubes move over time (drift changes positions)', async () => {
93
+ const t0 = await runSimulation({ scene: scene(), ticks: 1, setup });
94
+ const t100 = await runSimulation({ scene: scene(), ticks: 100, setup });
95
+ // different ticks -> different state hash (cubes spinning/drifting)
96
+ expect(t0.stateHash).not.toBe(t100.stateHash);
97
+ });
98
+ });
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "lib": ["ES2023", "DOM", "DOM.Iterable", "WebWorker"],
7
+ "types": [],
8
+ "strict": true,
9
+ "noUncheckedIndexedAccess": true,
10
+ "verbatimModuleSyntax": true,
11
+ "skipLibCheck": true,
12
+ "forceConsistentCasingInFileNames": true,
13
+ "noEmit": true,
14
+ "resolveJsonModule": true
15
+ },
16
+ "include": ["src", "test"]
17
+ }
@@ -0,0 +1,17 @@
1
+ import { defineConfig } from 'vite';
2
+
3
+ export default defineConfig({
4
+ // Relative asset URLs, so one production build plays from any directory (molen.dev/play hosts
5
+ // the samples beneath subdirectories).
6
+ base: './',
7
+ // The kernel Worker is an ES module, bundled like the page.
8
+ worker: { format: 'es' },
9
+ build: {
10
+ target: 'es2022',
11
+ // main.ts mounts with a top-level `await`, and the client lazy-loads its WebGPU driver. By
12
+ // default Rollup puts three.js and the client in the entry chunk, so the lazy chunk imports
13
+ // the entry while the entry is still suspended at that `await`: the import never settles and
14
+ // the page stays blank in every browser with a WebGPU adapter. Their own chunk breaks the cycle.
15
+ rollupOptions: { output: { manualChunks: { vendor: ['three', '@bendyline/molen-client'] } } },
16
+ },
17
+ });
@@ -0,0 +1,47 @@
1
+ # Data viz
2
+
3
+ Seven rows of JSON become seven entities, a kernel system grows them into a bar chart, and a `molen/cameratrack@1` document orbits the camera around it. The engine used for something that is not a game: no player, no input, no win condition. This is the reference for `spawnFromData` and for camera tracks — the machinima path, where the camera is authored rather than driven.
4
+
5
+ ![Data viz](https://raw.githubusercontent.com/bendyline/molen/main/examples/data-viz/preview.png)
6
+
7
+ ## Run
8
+
9
+ Play it in the browser at [molen.dev/play/data-viz](https://molen.dev/play/data-viz/). To run and change
10
+ your own copy, make one from the npm packages (no clone of the engine repository needed):
11
+
12
+ ```sh
13
+ npx @bendyline/molen-tooling new my-data-viz --template data-viz
14
+ cd my-data-viz
15
+ npm install
16
+ npm run dev
17
+ ```
18
+
19
+ No controls. The scene declares no `input` block; `src/main.ts` mounts with `frameLoop: 'manual'` and drives the camera itself from wall-clock time, so the track loops forever.
20
+
21
+ ## Authoring map
22
+
23
+ - `data.json`: the dataset — seven `{ label, value }` rows. Change the chart by changing this file and nothing else.
24
+ - `scene.json`: a seven-line manifest. No entities, no prefabs, no camera: everything is bound at build time.
25
+ - `src/viz.ts`: `spawnFromData(world, DATA, ...)` mints one `bar-<i>` entity per row, with height and palette colour derived from the value, plus a `grow` system that eases each bar up from the ground over 36 ticks so successive frames differ.
26
+ - `camera-track.json`: five keyframes, `easing: "smooth"`, `loop: true` — a full orbit at height 10-16 in 240 ticks.
27
+ - `src/worker.ts` / `src/main.ts`: the kernel runs in a Worker as usual; the page calls `evaluateCameraTrack(track, tick)` and `client.renderFrame(now)` per frame.
28
+
29
+ ## Verify
30
+
31
+ From this directory:
32
+
33
+ ```sh
34
+ npx molen validate scene.json
35
+ npx molen validate camera-track.json
36
+ npm test
37
+ ```
38
+
39
+ `test/headless.test.ts` checks one bar per datum, growth over time toward `value * heightScale`, height ordering (taller means bigger), the camera track validating and actually moving, and a **pinned state hash** at 60 ticks alongside run-to-run equality.
40
+
41
+ In the engine repository, `test/golden/viz.golden.test.ts` is the render half (golden tests stay there; a template copy does not include them): `screenshotScene` at tick 40 from the track's own pose at tick 40, 512x320, asserting seven rendered entities and comparing against the committed `test/golden/__goldens__/viz.png`. It then runs `exportFrames` from tick 0 to 60 in steps of 30 along the same track and asserts three PNGs — the machinima sequence in miniature. There is no replay fixture: nothing here takes commands.
42
+
43
+ One wrinkle worth knowing: `src/viz.ts` imports `data.json` without an import attribute, which Vite and Vitest supply and plain Node does not. Passing `--setup src/viz.ts` to `molen sim run` or `molen shot` therefore fails to load the module, which is why the headless render and the frame export run inside Vitest rather than from the CLI.
44
+
45
+ In the engine repository this sample lives in `examples/data-viz/`. The same commands run from
46
+ that directory, and `pnpm --filter @bendyline/molen-examples-data-viz test:unit` (or `test:golden`) runs its tests from the
47
+ root.
@@ -0,0 +1,12 @@
1
+ {
2
+ "format": "molen/cameratrack@1",
3
+ "easing": "smooth",
4
+ "loop": true,
5
+ "keyframes": [
6
+ { "tick": 0, "position": [0, 10, 22], "lookAt": [0, 4, 0] },
7
+ { "tick": 60, "position": [22, 12, 0], "lookAt": [0, 4, 0] },
8
+ { "tick": 120, "position": [0, 16, -22], "lookAt": [0, 4, 0] },
9
+ { "tick": 180, "position": [-22, 12, 0], "lookAt": [0, 4, 0] },
10
+ { "tick": 240, "position": [0, 10, 22], "lookAt": [0, 4, 0] }
11
+ ]
12
+ }
@@ -0,0 +1,9 @@
1
+ [
2
+ { "label": "jan", "value": 4 },
3
+ { "label": "feb", "value": 7 },
4
+ { "label": "mar", "value": 5 },
5
+ { "label": "apr", "value": 9 },
6
+ { "label": "may", "value": 6 },
7
+ { "label": "jun", "value": 11 },
8
+ { "label": "jul", "value": 8 }
9
+ ]
@@ -0,0 +1,16 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>molen — data viz</title>
7
+ <style>
8
+ html, body { margin: 0; height: 100%; overflow: hidden; background: #10131a; }
9
+ #view { display: block; width: 100vw; height: 100vh; }
10
+ </style>
11
+ </head>
12
+ <body>
13
+ <canvas id="view"></canvas>
14
+ <script type="module" src="/src/main.ts"></script>
15
+ </body>
16
+ </html>
@@ -0,0 +1,25 @@
1
+ {
2
+ "private": true,
3
+ "type": "module",
4
+ "scripts": {
5
+ "dev": "vite --port 5225",
6
+ "build": "vite build",
7
+ "preview": "vite preview",
8
+ "typecheck": "tsc -p tsconfig.json",
9
+ "test": "vitest run"
10
+ },
11
+ "dependencies": {
12
+ "@bendyline/molen-client": "workspace:*",
13
+ "@bendyline/molen-kernel": "workspace:*",
14
+ "@bendyline/molen-schema": "workspace:*",
15
+ "three": "0.184.0"
16
+ },
17
+ "devDependencies": {
18
+ "@bendyline/molen-tooling": "workspace:*",
19
+ "@types/node": "^24",
20
+ "@types/three": "0.184.0",
21
+ "typescript": "^6.0.3",
22
+ "vite": "^6.4.0",
23
+ "vitest": "^5.0.1"
24
+ }
25
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "format": "molen/scene@3",
3
+ "name": "data-viz",
4
+ "seed": "viz-1",
5
+ "tickRate": 30,
6
+ "keyframeInterval": 60
7
+ }
@@ -0,0 +1,39 @@
1
+ import { mountExperience } from '@bendyline/molen-client';
2
+ import { type CameraTrackDoc, evaluateCameraTrack } from '@bendyline/molen-client/camera-track';
3
+ import { validate } from '@bendyline/molen-schema';
4
+ import trackDoc from '../camera-track.json';
5
+ import sceneDoc from '../scene.json';
6
+
7
+ // Browser entry: kernel (bar-chart animation) in a Worker, client renders, and the camera
8
+ // follows the looping track over wall-clock time — machinima playback of a data visualization.
9
+ // `frameLoop: 'manual'` keeps the per-frame camera control here.
10
+ const scene = validate('scene', sceneDoc);
11
+ if (!scene.ok) throw new Error(scene.formatted);
12
+
13
+ const canvas = document.getElementById('view') as HTMLCanvasElement;
14
+ const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' });
15
+ const track = trackDoc as CameraTrackDoc;
16
+
17
+ const { client } = await mountExperience({
18
+ link: worker,
19
+ scene: scene.value,
20
+ canvas,
21
+ frameLoop: 'manual',
22
+ clearColor: '#10131a',
23
+ });
24
+
25
+ const TICK_RATE = 30;
26
+ const start = performance.now();
27
+ function loop(now: number): void {
28
+ const tick = ((now - start) / 1000) * TICK_RATE;
29
+ client.setCamera(evaluateCameraTrack(track, tick));
30
+ client.renderFrame(now);
31
+ requestAnimationFrame(loop);
32
+ }
33
+ requestAnimationFrame(loop);
34
+
35
+ const hint = document.createElement('div');
36
+ hint.textContent = 'data-driven bar chart · camera on a looping track';
37
+ hint.style.cssText =
38
+ 'position:fixed;left:12px;bottom:10px;font:13px system-ui;color:#9aa;pointer-events:none';
39
+ document.body.appendChild(hint);