@n3rdw1z4rd/core 0.19.1 → 0.21.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 (62) hide show
  1. package/README.md +27 -10
  2. package/dist/graph/doc.d.ts +141 -0
  3. package/dist/graph/doc.js +173 -0
  4. package/dist/graph/evaluate.d.ts +114 -0
  5. package/dist/graph/evaluate.js +319 -0
  6. package/dist/graph/index.d.ts +9 -0
  7. package/dist/graph/index.js +9 -0
  8. package/dist/graph/node.d.ts +153 -0
  9. package/dist/graph/node.js +94 -0
  10. package/dist/graph/nodes.d.ts +11 -0
  11. package/dist/graph/nodes.js +399 -0
  12. package/dist/graph/script.d.ts +56 -0
  13. package/dist/graph/script.js +98 -0
  14. package/dist/graph/sources.d.ts +11 -0
  15. package/dist/graph/sources.js +263 -0
  16. package/dist/graph/subgraph.d.ts +36 -0
  17. package/dist/graph/subgraph.js +280 -0
  18. package/dist/graph/types.d.ts +56 -0
  19. package/dist/graph/types.js +67 -0
  20. package/dist/graph.cjs +1709 -0
  21. package/dist/graph.d.cts +568 -0
  22. package/dist/index.cjs +495 -207
  23. package/dist/index.d.cts +231 -49
  24. package/dist/index.d.ts +5 -4
  25. package/dist/index.js +4 -3
  26. package/dist/input/_input.d.ts +25 -0
  27. package/dist/input/_input.js +97 -0
  28. package/dist/input/action-map.d.ts +42 -7
  29. package/dist/input/action-map.js +44 -18
  30. package/dist/input/bindings.d.ts +1 -1
  31. package/dist/input/bindings.js +1 -1
  32. package/dist/input/gestures.d.ts +20 -2
  33. package/dist/input/gestures.js +34 -8
  34. package/dist/input/index.d.ts +5 -4
  35. package/dist/input/index.js +3 -2
  36. package/dist/input/input-action-map.d.ts +327 -0
  37. package/dist/input/input-action-map.js +689 -0
  38. package/dist/input/input.d.ts +77 -272
  39. package/dist/input/input.js +201 -653
  40. package/dist/rng-Dhqq7owV.cjs +642 -0
  41. package/dist/{spatial-partition-3d-Ba-VBveM.cjs → spatial-partition-3d-CmSCL87Z.cjs} +19 -660
  42. package/dist/three/adaptive-grid.d.ts +43 -0
  43. package/dist/three/adaptive-grid.js +127 -0
  44. package/dist/three/ecs.d.ts +5 -3
  45. package/dist/three/ecs.js +5 -3
  46. package/dist/three/editor-camera.d.ts +93 -0
  47. package/dist/three/editor-camera.js +174 -0
  48. package/dist/three/index.d.ts +6 -1
  49. package/dist/three/index.js +6 -1
  50. package/dist/three/picking.d.ts +34 -0
  51. package/dist/three/picking.js +59 -0
  52. package/dist/three/schema.d.ts +139 -0
  53. package/dist/three/schema.js +214 -0
  54. package/dist/three/world.d.ts +192 -0
  55. package/dist/three/world.js +363 -0
  56. package/dist/three.cjs +970 -23
  57. package/dist/three.d.cts +497 -5
  58. package/dist/vite.cjs +40 -0
  59. package/dist/vite.d.cts +35 -0
  60. package/dist/vite.d.ts +35 -0
  61. package/dist/vite.js +39 -0
  62. 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,56 @@
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
+ * Compiles a script node's source into a node type named `type`.
24
+ *
25
+ * @category Graph
26
+ */
27
+ export declare function compileNodeScript(type: string, source: string): Promise<NodeDef>;
28
+ /**
29
+ * A behaviour: what a behaviour node evaluates to. The host runs `source`'s `step` (and
30
+ * `setup`) against whatever it's attached to, with `params`.
31
+ *
32
+ * @category Graph
33
+ */
34
+ export interface BehaviorValue {
35
+ kind: 'behavior';
36
+ /** the behaviour node's type */
37
+ type: string;
38
+ /** the node instance (for error reports) */
39
+ node: string;
40
+ /** its inputs and params at evaluation time */
41
+ params: Record<string, unknown>;
42
+ /** the module with step / setup */
43
+ source: string;
44
+ }
45
+ /**
46
+ * The `run` of a behaviour node: packs its inputs, params and source into a {@link BehaviorValue}
47
+ * on the `behavior` output. Behaviour modules (exporting `step`, no `run`) get it automatically.
48
+ *
49
+ * @category Graph
50
+ */
51
+ export declare function behaviorRun(inputs: Record<string, unknown>, params: Record<string, unknown>, ctx: RunContext): {
52
+ behavior: BehaviorValue;
53
+ };
54
+ /** A starting point for a new script node. @category Graph */
55
+ 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";
56
+ //#endregion
@@ -0,0 +1,98 @@
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
+ * Compiles a script node's source into a node type named `type`.
24
+ *
25
+ * @category Graph
26
+ */
27
+ async function compileNodeScript(type, source) {
28
+ const mod = await import(
29
+ /* @vite-ignore */
30
+ `data:text/javascript;charset=utf-8,${encodeURIComponent(source)}`
31
+ );
32
+ const m = { ...mod.default && typeof mod.default === "object" && !mod.run ? mod.default : mod };
33
+ if (typeof m.run !== "function" && (typeof m.step === "function" || typeof m.setup === "function")) {
34
+ m.run = behaviorRun;
35
+ m.outputs ??= { behavior: "behavior" };
36
+ m.category ??= "behavior";
37
+ }
38
+ if (typeof m.run !== "function") throw new Error(`script node "${type}": export a run(inputs, params, ctx) function (or step(self, world, dt, params) for a behaviour)`);
39
+ return defineNode({
40
+ type,
41
+ title: m.title,
42
+ category: m.category ?? "scripts",
43
+ description: m.description,
44
+ inputs: m.inputs,
45
+ params: m.params,
46
+ outputs: m.outputs,
47
+ output: m.output,
48
+ animate: m.animate,
49
+ run: m.run,
50
+ source
51
+ });
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 `step`, no `run`) get it automatically.
56
+ *
57
+ * @category Graph
58
+ */
59
+ function behaviorRun(inputs, params, ctx) {
60
+ if (!ctx.source) throw new Error(`behaviour node ${ctx.type} has no source module`);
61
+ return { behavior: {
62
+ kind: "behavior",
63
+ type: ctx.type,
64
+ node: ctx.nodeId,
65
+ params: {
66
+ ...inputs,
67
+ ...params
68
+ },
69
+ source: ctx.source
70
+ } };
71
+ }
72
+ /** A starting point for a new script node. @category Graph */
73
+ const SCRIPT_NODE_TEMPLATE = `// A node is a function with a typed interface.
74
+ // inputs: pins wired from other nodes (an unconnected one uses its widget value / default)
75
+ // params: widgets on the node
76
+ // outputs: what run() returns
77
+ // A "type[]" pin takes (or gives) a whole list; a list wired into a plain pin runs
78
+ // run() once per item and collects the results into lists.
79
+
80
+ export const inputs = {
81
+ a: { type: 'number', default: 1 },
82
+ b: { type: 'number', default: 2 },
83
+ };
84
+
85
+ export const params = {
86
+ scale: { type: 'number', default: 1, min: 0, max: 10, step: 0.1 },
87
+ };
88
+
89
+ export const outputs = {
90
+ result: 'number',
91
+ };
92
+
93
+ export function run({ a, b }, { scale }, ctx) {
94
+ return { result: (a + b) * scale };
95
+ }
96
+ `;
97
+ //#endregion
98
+ export { 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