@n3rdw1z4rd/core 0.20.0 → 0.22.0

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 (44) hide show
  1. package/dist/graph/doc.d.ts +141 -0
  2. package/dist/graph/doc.js +173 -0
  3. package/dist/graph/evaluate.d.ts +114 -0
  4. package/dist/graph/evaluate.js +319 -0
  5. package/dist/graph/index.d.ts +9 -0
  6. package/dist/graph/index.js +9 -0
  7. package/dist/graph/node.d.ts +153 -0
  8. package/dist/graph/node.js +94 -0
  9. package/dist/graph/nodes.d.ts +11 -0
  10. package/dist/graph/nodes.js +399 -0
  11. package/dist/graph/script.d.ts +64 -0
  12. package/dist/graph/script.js +111 -0
  13. package/dist/graph/sources.d.ts +11 -0
  14. package/dist/graph/sources.js +263 -0
  15. package/dist/graph/subgraph.d.ts +36 -0
  16. package/dist/graph/subgraph.js +280 -0
  17. package/dist/graph/types.d.ts +56 -0
  18. package/dist/graph/types.js +67 -0
  19. package/dist/graph.cjs +1723 -0
  20. package/dist/graph.d.cts +576 -0
  21. package/dist/index.cjs +53 -52
  22. package/dist/rng-Dhqq7owV.cjs +642 -0
  23. package/dist/{spatial-partition-3d-Ba-VBveM.cjs → spatial-partition-3d-CmSCL87Z.cjs} +19 -660
  24. package/dist/three/adaptive-grid.d.ts +43 -0
  25. package/dist/three/adaptive-grid.js +127 -0
  26. package/dist/three/ecs.d.ts +5 -3
  27. package/dist/three/ecs.js +5 -3
  28. package/dist/three/editor-camera.d.ts +93 -0
  29. package/dist/three/editor-camera.js +174 -0
  30. package/dist/three/index.d.ts +6 -1
  31. package/dist/three/index.js +6 -1
  32. package/dist/three/picking.d.ts +34 -0
  33. package/dist/three/picking.js +59 -0
  34. package/dist/three/schema.d.ts +139 -0
  35. package/dist/three/schema.js +214 -0
  36. package/dist/three/world.d.ts +192 -0
  37. package/dist/three/world.js +363 -0
  38. package/dist/three.cjs +970 -23
  39. package/dist/three.d.cts +497 -5
  40. package/dist/vite.cjs +40 -0
  41. package/dist/vite.d.cts +35 -0
  42. package/dist/vite.d.ts +35 -0
  43. package/dist/vite.js +39 -0
  44. package/package.json +21 -1
@@ -0,0 +1,399 @@
1
+ import { Rng } from "../rng.js";
2
+ import { NodeRegistry } from "./node.js";
3
+ import { BUILTIN_SOURCES } from "./sources.js";
4
+ //#region src/graph/nodes.ts
5
+ const v2 = (x = 0, y = 0) => ({
6
+ x,
7
+ y
8
+ });
9
+ const num = (v, d = 0) => typeof v === "number" && Number.isFinite(v) ? v : d;
10
+ /** @category Graph */
11
+ const BUILTIN_NODES = [
12
+ {
13
+ type: "value/number",
14
+ title: "Number",
15
+ params: { value: {
16
+ type: "number",
17
+ default: 0
18
+ } },
19
+ outputs: { value: "number" },
20
+ run: (_, { value }) => ({ value: num(value) })
21
+ },
22
+ {
23
+ type: "value/vec2",
24
+ title: "Vec2",
25
+ inputs: {
26
+ x: {
27
+ type: "number",
28
+ default: 0
29
+ },
30
+ y: {
31
+ type: "number",
32
+ default: 0
33
+ }
34
+ },
35
+ outputs: { vec: "vec2" },
36
+ run: ({ x, y }) => ({ vec: v2(num(x), num(y)) })
37
+ },
38
+ {
39
+ type: "value/color",
40
+ title: "Color",
41
+ params: { color: {
42
+ type: "color",
43
+ default: "#3fd1a4"
44
+ } },
45
+ outputs: { color: "color" },
46
+ run: (_, { color }) => ({ color })
47
+ },
48
+ {
49
+ type: "value/text",
50
+ title: "Text",
51
+ params: { text: {
52
+ type: "string",
53
+ default: ""
54
+ } },
55
+ outputs: { text: "string" },
56
+ run: (_, { text }) => ({ text: String(text ?? "") })
57
+ },
58
+ {
59
+ type: "value/time",
60
+ title: "Time",
61
+ description: "the animation clock (animates by default)",
62
+ animate: true,
63
+ params: { speed: {
64
+ type: "number",
65
+ default: 1,
66
+ step: .1
67
+ } },
68
+ outputs: {
69
+ time: "number",
70
+ dt: "number",
71
+ frame: "number"
72
+ },
73
+ run: (_, { speed }, ctx) => ({
74
+ time: ctx.time * num(speed, 1),
75
+ dt: ctx.dt * num(speed, 1),
76
+ frame: ctx.frame
77
+ })
78
+ },
79
+ {
80
+ type: "math/wave",
81
+ title: "Wave",
82
+ description: "sin / triangle / square / saw of t",
83
+ inputs: {
84
+ t: {
85
+ type: "number",
86
+ default: 0
87
+ },
88
+ period: {
89
+ type: "number",
90
+ default: 1
91
+ },
92
+ amplitude: {
93
+ type: "number",
94
+ default: 1
95
+ }
96
+ },
97
+ params: { shape: {
98
+ type: "enum",
99
+ options: [
100
+ "sine",
101
+ "triangle",
102
+ "square",
103
+ "saw"
104
+ ],
105
+ default: "sine"
106
+ } },
107
+ outputs: { value: "number" },
108
+ run: ({ t, period, amplitude }, { shape }) => {
109
+ const p = num(t) / (num(period, 1) || 1);
110
+ const f = p - Math.floor(p);
111
+ const w = {
112
+ sine: Math.sin(p * Math.PI * 2),
113
+ triangle: 1 - 4 * Math.abs(f - .5),
114
+ square: f < .5 ? 1 : -1,
115
+ saw: f * 2 - 1
116
+ };
117
+ return { value: (w[shape] ?? w.sine) * num(amplitude, 1) };
118
+ }
119
+ },
120
+ {
121
+ type: "math/math",
122
+ title: "Math",
123
+ description: "a ⊕ b for the chosen operation",
124
+ inputs: {
125
+ a: {
126
+ type: "number",
127
+ default: 0
128
+ },
129
+ b: {
130
+ type: "number",
131
+ default: 0
132
+ }
133
+ },
134
+ params: { op: {
135
+ type: "enum",
136
+ options: [
137
+ "+",
138
+ "−",
139
+ "×",
140
+ "÷",
141
+ "min",
142
+ "max",
143
+ "pow",
144
+ "mod"
145
+ ],
146
+ default: "+"
147
+ } },
148
+ outputs: { result: "number" },
149
+ run: ({ a, b }, { op }) => {
150
+ const x = num(a);
151
+ const y = num(b);
152
+ return { result: {
153
+ "+": x + y,
154
+ "−": x - y,
155
+ "×": x * y,
156
+ "÷": y === 0 ? 0 : x / y,
157
+ min: Math.min(x, y),
158
+ max: Math.max(x, y),
159
+ pow: x ** y,
160
+ mod: y === 0 ? 0 : (x % y + y) % y
161
+ }[op] ?? x + y };
162
+ }
163
+ },
164
+ {
165
+ type: "math/remap",
166
+ title: "Remap",
167
+ inputs: {
168
+ value: {
169
+ type: "number",
170
+ default: .5
171
+ },
172
+ inMin: {
173
+ type: "number",
174
+ default: 0
175
+ },
176
+ inMax: {
177
+ type: "number",
178
+ default: 1
179
+ },
180
+ outMin: {
181
+ type: "number",
182
+ default: 0
183
+ },
184
+ outMax: {
185
+ type: "number",
186
+ default: 100
187
+ }
188
+ },
189
+ params: { clamp: {
190
+ type: "boolean",
191
+ default: false
192
+ } },
193
+ outputs: { result: "number" },
194
+ run: ({ value, inMin, inMax, outMin, outMax }, { clamp }) => {
195
+ let t = inMax === inMin ? 0 : (value - inMin) / (inMax - inMin);
196
+ if (clamp) t = Math.min(1, Math.max(0, t));
197
+ return { result: outMin + (outMax - outMin) * t };
198
+ }
199
+ },
200
+ {
201
+ type: "vec2/split",
202
+ title: "Split Vec2",
203
+ inputs: { vec: "vec2" },
204
+ outputs: {
205
+ x: "number",
206
+ y: "number"
207
+ },
208
+ run: ({ vec }) => ({
209
+ x: num(vec?.x),
210
+ y: num(vec?.y)
211
+ })
212
+ },
213
+ {
214
+ type: "vec2/offset",
215
+ title: "Offset",
216
+ description: "vec + by",
217
+ inputs: {
218
+ vec: "vec2",
219
+ by: {
220
+ type: "vec2",
221
+ default: {
222
+ x: 0,
223
+ y: 0
224
+ }
225
+ }
226
+ },
227
+ outputs: { vec: "vec2" },
228
+ run: ({ vec, by }) => ({ vec: v2(num(vec?.x) + num(by?.x), num(vec?.y) + num(by?.y)) })
229
+ },
230
+ {
231
+ type: "list/range",
232
+ title: "Range",
233
+ description: "numbers from start (inclusive) to end (exclusive)",
234
+ inputs: {
235
+ start: {
236
+ type: "number",
237
+ default: 0
238
+ },
239
+ end: {
240
+ type: "number",
241
+ default: 10
242
+ },
243
+ step: {
244
+ type: "number",
245
+ default: 1
246
+ }
247
+ },
248
+ outputs: { numbers: "number[]" },
249
+ run: ({ start, end, step }) => {
250
+ const s = num(step, 1) || 1;
251
+ const out = [];
252
+ for (let v = num(start); s > 0 ? v < end : v > end; v += s) {
253
+ out.push(v);
254
+ if (out.length >= 1e5) break;
255
+ }
256
+ return { numbers: out };
257
+ }
258
+ },
259
+ {
260
+ type: "list/grid2d",
261
+ title: "Grid",
262
+ description: "points on a grid, centered on `center`; stagger shifts every other row by half a cell",
263
+ inputs: {
264
+ cols: {
265
+ type: "number",
266
+ default: 5,
267
+ min: 1,
268
+ step: 1
269
+ },
270
+ rows: {
271
+ type: "number",
272
+ default: 5,
273
+ min: 1,
274
+ step: 1
275
+ },
276
+ spacing: {
277
+ type: "vec2",
278
+ default: {
279
+ x: 32,
280
+ y: 32
281
+ }
282
+ },
283
+ center: {
284
+ type: "vec2",
285
+ default: {
286
+ x: 0,
287
+ y: 0
288
+ }
289
+ }
290
+ },
291
+ params: { stagger: {
292
+ type: "boolean",
293
+ default: false
294
+ } },
295
+ outputs: { points: "vec2[]" },
296
+ run: ({ cols, rows, spacing, center }, { stagger }) => {
297
+ const c = Math.max(0, Math.min(1e3, Math.floor(num(cols))));
298
+ const r = Math.max(0, Math.min(1e3, Math.floor(num(rows))));
299
+ const sx = num(spacing?.x, 32);
300
+ const sy = num(spacing?.y, 32);
301
+ const points = [];
302
+ for (let j = 0; j < r; j++) {
303
+ const shift = stagger && j % 2 === 1 ? sx / 2 : 0;
304
+ for (let i = 0; i < c; i++) points.push(v2(num(center?.x) + (i - (c - 1) / 2) * sx + shift, num(center?.y) + ((r - 1) / 2 - j) * sy));
305
+ }
306
+ return { points };
307
+ }
308
+ },
309
+ {
310
+ type: "list/random",
311
+ title: "Random",
312
+ description: "seeded random numbers in [min, max)",
313
+ inputs: {
314
+ count: {
315
+ type: "number",
316
+ default: 10,
317
+ min: 0,
318
+ step: 1
319
+ },
320
+ min: {
321
+ type: "number",
322
+ default: 0
323
+ },
324
+ max: {
325
+ type: "number",
326
+ default: 1
327
+ },
328
+ seed: {
329
+ type: "number",
330
+ default: 1,
331
+ step: 1
332
+ }
333
+ },
334
+ outputs: { numbers: "number[]" },
335
+ run: ({ count, min, max, seed }) => {
336
+ const rng = new Rng(num(seed, 1));
337
+ const n = Math.max(0, Math.min(1e5, Math.floor(num(count))));
338
+ return { numbers: Array.from({ length: n }, () => num(min) + rng.nextFloat() * (num(max, 1) - num(min))) };
339
+ }
340
+ },
341
+ {
342
+ type: "list/count",
343
+ title: "Count",
344
+ inputs: { list: "any[]" },
345
+ outputs: { count: "number" },
346
+ run: ({ list }) => ({ count: list.length })
347
+ },
348
+ {
349
+ type: "list/merge",
350
+ title: "Merge",
351
+ description: "joins every list wired in, in order",
352
+ inputs: { lists: {
353
+ type: "any[]",
354
+ multi: true,
355
+ optional: true
356
+ } },
357
+ outputs: { list: "any[]" },
358
+ run: ({ lists }) => ({ list: [...lists ?? []] })
359
+ },
360
+ {
361
+ type: "list/pick",
362
+ title: "Pick",
363
+ description: "every nth item from a list",
364
+ inputs: {
365
+ list: "any[]",
366
+ every: {
367
+ type: "number",
368
+ default: 2,
369
+ min: 1,
370
+ step: 1
371
+ },
372
+ offset: {
373
+ type: "number",
374
+ default: 0,
375
+ step: 1
376
+ }
377
+ },
378
+ outputs: { list: "any[]" },
379
+ run: ({ list, every, offset }) => {
380
+ const n = Math.max(1, Math.floor(num(every, 1)));
381
+ return { list: list.filter((_, i) => (i - Math.floor(num(offset))) % n === 0 && i >= num(offset)) };
382
+ }
383
+ }
384
+ ];
385
+ /**
386
+ * A registry with the built-in nodes.
387
+ *
388
+ * @category Graph
389
+ */
390
+ function createNodeRegistry() {
391
+ const registry = new NodeRegistry();
392
+ for (const spec of BUILTIN_NODES) registry.register({
393
+ ...spec,
394
+ source: spec.source ?? BUILTIN_SOURCES[spec.type]
395
+ });
396
+ return registry;
397
+ }
398
+ //#endregion
399
+ export { BUILTIN_NODES, createNodeRegistry };
@@ -0,0 +1,64 @@
1
+ import { NodeDef, RunContext } from "./node.js";
2
+ //#region src/graph/script.d.ts
3
+ /**
4
+ * Script nodes: a node type written as a plain ES module, compiled at runtime.
5
+ *
6
+ * ```js
7
+ * export const title = 'Ball'; // optional: title, category, description, output, animate
8
+ * export const inputs = { at: 'vec2', radius: { type: 'number', default: 8 } };
9
+ * export const params = { color: { type: 'color', default: '#e8554e' } };
10
+ * export const outputs = { entity: 'entity' };
11
+ * export function run({ at, radius }, { color }, ctx) {
12
+ * return { entity: { shape: 'ball', x: at.x, y: at.y, r: radius, color } };
13
+ * }
14
+ * ```
15
+ *
16
+ * The module is imported from a `data:` URL, which works in browsers, workers and Node.
17
+ * It runs with the page's (or worker's) privileges — treat shared scripts as untrusted and
18
+ * evaluate them in a worker.
19
+ *
20
+ * @module
21
+ */
22
+ /**
23
+ * The functions a behaviour module can export (any of them, and no `run`): what they mean is up to
24
+ * the host. apparatus runs `setup` once, `step` every physics step, and `enter` / `exit` when a body
25
+ * starts / stops touching (or overlapping) the one the behaviour is on.
26
+ *
27
+ * @category Graph
28
+ */
29
+ export declare const BEHAVIOR_HOOKS: readonly ["setup", "step", "enter", "exit"];
30
+ /**
31
+ * Compiles a script node's source into a node type named `type`.
32
+ *
33
+ * @category Graph
34
+ */
35
+ export declare function compileNodeScript(type: string, source: string): Promise<NodeDef>;
36
+ /**
37
+ * A behaviour: what a behaviour node evaluates to. The host runs `source`'s hooks
38
+ * ({@link BEHAVIOR_HOOKS}) against whatever it's attached to, with `params`.
39
+ *
40
+ * @category Graph
41
+ */
42
+ export interface BehaviorValue {
43
+ kind: 'behavior';
44
+ /** the behaviour node's type */
45
+ type: string;
46
+ /** the node instance (for error reports) */
47
+ node: string;
48
+ /** its inputs and params at evaluation time */
49
+ params: Record<string, unknown>;
50
+ /** the module with its hooks (setup / step / enter / exit) */
51
+ source: string;
52
+ }
53
+ /**
54
+ * The `run` of a behaviour node: packs its inputs, params and source into a {@link BehaviorValue}
55
+ * on the `behavior` output. Behaviour modules (exporting a hook, no `run`) get it automatically.
56
+ *
57
+ * @category Graph
58
+ */
59
+ export declare function behaviorRun(inputs: Record<string, unknown>, params: Record<string, unknown>, ctx: RunContext): {
60
+ behavior: BehaviorValue;
61
+ };
62
+ /** A starting point for a new script node. @category Graph */
63
+ export declare const SCRIPT_NODE_TEMPLATE = "// A node is a function with a typed interface.\n// inputs: pins wired from other nodes (an unconnected one uses its widget value / default)\n// params: widgets on the node\n// outputs: what run() returns\n// A \"type[]\" pin takes (or gives) a whole list; a list wired into a plain pin runs\n// run() once per item and collects the results into lists.\n\nexport const inputs = {\n a: { type: 'number', default: 1 },\n b: { type: 'number', default: 2 },\n};\n\nexport const params = {\n scale: { type: 'number', default: 1, min: 0, max: 10, step: 0.1 },\n};\n\nexport const outputs = {\n result: 'number',\n};\n\nexport function run({ a, b }, { scale }, ctx) {\n return { result: (a + b) * scale };\n}\n";
64
+ //#endregion
@@ -0,0 +1,111 @@
1
+ import { defineNode } from "./node.js";
2
+ //#region src/graph/script.ts
3
+ /**
4
+ * Script nodes: a node type written as a plain ES module, compiled at runtime.
5
+ *
6
+ * ```js
7
+ * export const title = 'Ball'; // optional: title, category, description, output, animate
8
+ * export const inputs = { at: 'vec2', radius: { type: 'number', default: 8 } };
9
+ * export const params = { color: { type: 'color', default: '#e8554e' } };
10
+ * export const outputs = { entity: 'entity' };
11
+ * export function run({ at, radius }, { color }, ctx) {
12
+ * return { entity: { shape: 'ball', x: at.x, y: at.y, r: radius, color } };
13
+ * }
14
+ * ```
15
+ *
16
+ * The module is imported from a `data:` URL, which works in browsers, workers and Node.
17
+ * It runs with the page's (or worker's) privileges — treat shared scripts as untrusted and
18
+ * evaluate them in a worker.
19
+ *
20
+ * @module
21
+ */
22
+ /**
23
+ * The functions a behaviour module can export (any of them, and no `run`): what they mean is up to
24
+ * the host. apparatus runs `setup` once, `step` every physics step, and `enter` / `exit` when a body
25
+ * starts / stops touching (or overlapping) the one the behaviour is on.
26
+ *
27
+ * @category Graph
28
+ */
29
+ const BEHAVIOR_HOOKS = [
30
+ "setup",
31
+ "step",
32
+ "enter",
33
+ "exit"
34
+ ];
35
+ /**
36
+ * Compiles a script node's source into a node type named `type`.
37
+ *
38
+ * @category Graph
39
+ */
40
+ async function compileNodeScript(type, source) {
41
+ const mod = await import(
42
+ /* @vite-ignore */
43
+ `data:text/javascript;charset=utf-8,${encodeURIComponent(source)}`
44
+ );
45
+ const m = { ...mod.default && typeof mod.default === "object" && !mod.run ? mod.default : mod };
46
+ if (typeof m.run !== "function" && BEHAVIOR_HOOKS.some((h) => typeof m[h] === "function")) {
47
+ m.run = behaviorRun;
48
+ m.outputs ??= { behavior: "behavior" };
49
+ m.category ??= "behavior";
50
+ }
51
+ if (typeof m.run !== "function") throw new Error(`script node "${type}": export a run(inputs, params, ctx) function (or setup / step / enter / exit for a behaviour)`);
52
+ return defineNode({
53
+ type,
54
+ title: m.title,
55
+ category: m.category ?? "scripts",
56
+ description: m.description,
57
+ inputs: m.inputs,
58
+ params: m.params,
59
+ outputs: m.outputs,
60
+ output: m.output,
61
+ animate: m.animate,
62
+ run: m.run,
63
+ source
64
+ });
65
+ }
66
+ /**
67
+ * The `run` of a behaviour node: packs its inputs, params and source into a {@link BehaviorValue}
68
+ * on the `behavior` output. Behaviour modules (exporting a hook, no `run`) get it automatically.
69
+ *
70
+ * @category Graph
71
+ */
72
+ function behaviorRun(inputs, params, ctx) {
73
+ if (!ctx.source) throw new Error(`behaviour node ${ctx.type} has no source module`);
74
+ return { behavior: {
75
+ kind: "behavior",
76
+ type: ctx.type,
77
+ node: ctx.nodeId,
78
+ params: {
79
+ ...inputs,
80
+ ...params
81
+ },
82
+ source: ctx.source
83
+ } };
84
+ }
85
+ /** A starting point for a new script node. @category Graph */
86
+ const SCRIPT_NODE_TEMPLATE = `// A node is a function with a typed interface.
87
+ // inputs: pins wired from other nodes (an unconnected one uses its widget value / default)
88
+ // params: widgets on the node
89
+ // outputs: what run() returns
90
+ // A "type[]" pin takes (or gives) a whole list; a list wired into a plain pin runs
91
+ // run() once per item and collects the results into lists.
92
+
93
+ export const inputs = {
94
+ a: { type: 'number', default: 1 },
95
+ b: { type: 'number', default: 2 },
96
+ };
97
+
98
+ export const params = {
99
+ scale: { type: 'number', default: 1, min: 0, max: 10, step: 0.1 },
100
+ };
101
+
102
+ export const outputs = {
103
+ result: 'number',
104
+ };
105
+
106
+ export function run({ a, b }, { scale }, ctx) {
107
+ return { result: (a + b) * scale };
108
+ }
109
+ `;
110
+ //#endregion
111
+ export { BEHAVIOR_HOOKS, SCRIPT_NODE_TEMPLATE, behaviorRun, compileNodeScript };
@@ -0,0 +1,11 @@
1
+ //#region src/graph/sources.d.ts
2
+ /**
3
+ * The built-in nodes as script modules: the same interface and behaviour as the TypeScript
4
+ * definitions in `nodes.ts`, written self-contained so the editor can show a built-in's code
5
+ * and fork it into a custom node. A test compiles each one and checks it matches.
6
+ *
7
+ * @module
8
+ */
9
+ /** @category Graph */
10
+ export declare const BUILTIN_SOURCES: Record<string, string>;
11
+ //#endregion