@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.
- package/README.md +27 -10
- package/dist/graph/doc.d.ts +141 -0
- package/dist/graph/doc.js +173 -0
- package/dist/graph/evaluate.d.ts +114 -0
- package/dist/graph/evaluate.js +319 -0
- package/dist/graph/index.d.ts +9 -0
- package/dist/graph/index.js +9 -0
- package/dist/graph/node.d.ts +153 -0
- package/dist/graph/node.js +94 -0
- package/dist/graph/nodes.d.ts +11 -0
- package/dist/graph/nodes.js +399 -0
- package/dist/graph/script.d.ts +56 -0
- package/dist/graph/script.js +98 -0
- package/dist/graph/sources.d.ts +11 -0
- package/dist/graph/sources.js +263 -0
- package/dist/graph/subgraph.d.ts +36 -0
- package/dist/graph/subgraph.js +280 -0
- package/dist/graph/types.d.ts +56 -0
- package/dist/graph/types.js +67 -0
- package/dist/graph.cjs +1709 -0
- package/dist/graph.d.cts +568 -0
- package/dist/index.cjs +495 -207
- package/dist/index.d.cts +231 -49
- package/dist/index.d.ts +5 -4
- package/dist/index.js +4 -3
- package/dist/input/_input.d.ts +25 -0
- package/dist/input/_input.js +97 -0
- package/dist/input/action-map.d.ts +42 -7
- package/dist/input/action-map.js +44 -18
- package/dist/input/bindings.d.ts +1 -1
- package/dist/input/bindings.js +1 -1
- package/dist/input/gestures.d.ts +20 -2
- package/dist/input/gestures.js +34 -8
- package/dist/input/index.d.ts +5 -4
- package/dist/input/index.js +3 -2
- package/dist/input/input-action-map.d.ts +327 -0
- package/dist/input/input-action-map.js +689 -0
- package/dist/input/input.d.ts +77 -272
- package/dist/input/input.js +201 -653
- package/dist/rng-Dhqq7owV.cjs +642 -0
- package/dist/{spatial-partition-3d-Ba-VBveM.cjs → spatial-partition-3d-CmSCL87Z.cjs} +19 -660
- package/dist/three/adaptive-grid.d.ts +43 -0
- package/dist/three/adaptive-grid.js +127 -0
- package/dist/three/ecs.d.ts +5 -3
- package/dist/three/ecs.js +5 -3
- package/dist/three/editor-camera.d.ts +93 -0
- package/dist/three/editor-camera.js +174 -0
- package/dist/three/index.d.ts +6 -1
- package/dist/three/index.js +6 -1
- package/dist/three/picking.d.ts +34 -0
- package/dist/three/picking.js +59 -0
- package/dist/three/schema.d.ts +139 -0
- package/dist/three/schema.js +214 -0
- package/dist/three/world.d.ts +192 -0
- package/dist/three/world.js +363 -0
- package/dist/three.cjs +970 -23
- package/dist/three.d.cts +497 -5
- package/dist/vite.cjs +40 -0
- package/dist/vite.d.cts +35 -0
- package/dist/vite.d.ts +35 -0
- package/dist/vite.js +39 -0
- package/package.json +21 -1
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ This package ships ESM and CJS builds plus `.d.ts` types (built with `tsdown`).
|
|
|
34
34
|
|
|
35
35
|
The data and logic modules load and run anywhere, including Node (the tests run there): math, `Random`/`Rng`, `Vector`, `Rectangle`, `Map2D`/`Map3D`/`Tilemap`, `findPath` (A*), the dungeon generator, `ECS`, the event helpers (`Emitter`, `Observable`, `Signal`, `EventQueue`, `Disposable`), `Color` and color palettes, the spatial partitions and the particle simulations.
|
|
36
36
|
|
|
37
|
-
These need a browser when used (importing them is still safe anywhere): `CanvasRenderer`, `ParticleRenderer`, `WebGlRenderer` and the WebGL helpers, `Input`, `WorkerInterface`, `Clock.start()`, `Color.fromName()` and `GetUrlParams()`, plus anything in `@n3rdw1z4rd/core/three` that renders or loads textures.
|
|
37
|
+
These need a browser when used (importing them is still safe anywhere): `CanvasRenderer`, `ParticleRenderer`, `WebGlRenderer` and the WebGL helpers, `Input`, `InputActionMap`, `WorkerInterface`, `Clock.start()`, `Color.fromName()` and `GetUrlParams()`, plus anything in `@n3rdw1z4rd/core/three` that renders or loads textures.
|
|
38
38
|
|
|
39
39
|
## Quick start
|
|
40
40
|
|
|
@@ -61,25 +61,42 @@ renderer.setAnimationLoop((time) => {
|
|
|
61
61
|
});
|
|
62
62
|
```
|
|
63
63
|
|
|
64
|
-
Input
|
|
64
|
+
`Input` is event-based and needs no frame loop, so it suits apps, editors and turn-based games alike:
|
|
65
65
|
|
|
66
66
|
```ts
|
|
67
|
-
import { Input
|
|
67
|
+
import { Input } from "@n3rdw1z4rd/core";
|
|
68
68
|
|
|
69
|
-
const input =
|
|
69
|
+
const input = Input.instance;
|
|
70
|
+
input.target = canvas; // pointer positions are measured on it (keys are always read from window)
|
|
70
71
|
|
|
71
|
-
input.
|
|
72
|
-
input.
|
|
72
|
+
input.on("keyw_down", "arrowup_down", () => move(0, -1));
|
|
73
|
+
input.on("space_pressed", wait); // released within inputThreshold ms
|
|
74
|
+
input.on("button0_clicked", () => select(input.pointerPosition)); // not after a drag
|
|
75
|
+
input.on("pointer_move", () => {
|
|
76
|
+
if (input.isButtonDown(0)) pan(input.pointerDelta.x, input.pointerDelta.y);
|
|
77
|
+
});
|
|
78
|
+
input.on("mouse_wheel", (ev: WheelEvent) => zoom(ev.deltaY));
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
`InputActionMap` adds a Godot-style action map on top, for games that read input once per frame: game code asks about actions rather than keys or buttons.
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { InputActionMap, key, pad, padAxis, gesture, xr } from "@n3rdw1z4rd/core";
|
|
85
|
+
|
|
86
|
+
const controls = new InputActionMap(); // reads Input.instance; includes ui_accept, ui_cancel, ui_left, ... defaults
|
|
87
|
+
|
|
88
|
+
controls.actions.add("jump", [key("Space"), pad("A"), gesture("tap"), xr("right", "primary")]);
|
|
89
|
+
controls.actions.add("move_left", [key("KeyA"), padAxis("leftX", -1)]);
|
|
73
90
|
// ... move_right, move_forward, move_back
|
|
74
91
|
|
|
75
92
|
// once per frame, before game logic
|
|
76
|
-
|
|
93
|
+
controls.update();
|
|
77
94
|
|
|
78
|
-
if (
|
|
79
|
-
const move =
|
|
95
|
+
if (controls.isActionJustPressed("jump")) player.jump();
|
|
96
|
+
const move = controls.getVector("move_left", "move_right", "move_forward", "move_back");
|
|
80
97
|
```
|
|
81
98
|
|
|
82
|
-
Keyboard, mouse and pen, touch gestures, gamepads (each its own device for local multiplayer) and WebXR controllers (`
|
|
99
|
+
Keyboard, mouse and pen, touch gestures, gamepads (each its own device for local multiplayer) and WebXR controllers (`controls.attachXR(session)`) all feed the same actions. `repeat` keeps a held action pressing again like a held key in a text field (`{ repeat: true }`; the `ui_*` directions repeat by default), `controls.gestures.events` says where the last frame's taps and swipes happened, `controls.actions.toJSON()` / `load()` save rebinds, and `controls.captureBinding()` waits for the next input for rebinding menus.
|
|
83
100
|
|
|
84
101
|
Three.js helpers come from the `three` subpath:
|
|
85
102
|
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { TypeRegistry } from "./types.js";
|
|
2
|
+
import { NodeDef, NodeRegistry } from "./node.js";
|
|
3
|
+
//#region src/graph/doc.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The graph document: plain JSON, saved and shared as-is.
|
|
6
|
+
*
|
|
7
|
+
* ```json
|
|
8
|
+
* {
|
|
9
|
+
* "format": "n3rdw1z4rd.graph", "version": 1,
|
|
10
|
+
* "nodes": [{ "id": "a1", "type": "list/grid2d", "pos": [0, 0], "values": { "cols": 14 } }],
|
|
11
|
+
* "links": [{ "id": "l1", "from": { "node": "a1", "port": "points" }, "to": { "node": "b2", "port": "at" } }],
|
|
12
|
+
* "scripts": { "my/ball": "export const inputs = …" }
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* `values` holds the node's params and the values of its unconnected inputs; `scripts` holds
|
|
17
|
+
* the source of the script node types the graph uses, so a graph file is self-contained.
|
|
18
|
+
*
|
|
19
|
+
* @module
|
|
20
|
+
*/
|
|
21
|
+
export declare const GRAPH_FORMAT = "n3rdw1z4rd.graph";
|
|
22
|
+
export declare const GRAPH_VERSION = 1;
|
|
23
|
+
/** @category Graph */
|
|
24
|
+
export interface GraphNode {
|
|
25
|
+
id: string;
|
|
26
|
+
type: string;
|
|
27
|
+
/** top-left, in graph units */
|
|
28
|
+
pos: [number, number];
|
|
29
|
+
/** set when resized (e.g. an output viewport) */
|
|
30
|
+
size?: [number, number];
|
|
31
|
+
/** params and unconnected-input values, by name */
|
|
32
|
+
values: Record<string, unknown>;
|
|
33
|
+
/** shown instead of the type's title */
|
|
34
|
+
title?: string;
|
|
35
|
+
collapsed?: boolean;
|
|
36
|
+
/** re-run every frame while animating (overrides the type's default) */
|
|
37
|
+
animate?: boolean;
|
|
38
|
+
/** switched off: doesn't run; downstream treats it like a failed node (skipped) */
|
|
39
|
+
disabled?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/** @category Graph */
|
|
42
|
+
export interface PortRef {
|
|
43
|
+
node: string;
|
|
44
|
+
port: string;
|
|
45
|
+
}
|
|
46
|
+
/** @category Graph */
|
|
47
|
+
export interface GraphLink {
|
|
48
|
+
id: string;
|
|
49
|
+
from: PortRef;
|
|
50
|
+
to: PortRef;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A group: a titled, colored frame around nodes. Purely visual — the evaluator ignores it;
|
|
54
|
+
* the nodes "in" a group are the ones inside its rectangle.
|
|
55
|
+
*
|
|
56
|
+
* @category Graph
|
|
57
|
+
*/
|
|
58
|
+
export interface GraphGroup {
|
|
59
|
+
id: string;
|
|
60
|
+
title: string;
|
|
61
|
+
color: string;
|
|
62
|
+
/** x, y, width, height in graph units */
|
|
63
|
+
rect: [number, number, number, number];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A subgraph node type: an inner graph plus its interface. Each input feeds one or more inner
|
|
67
|
+
* input ports; each output reads one inner output port.
|
|
68
|
+
*
|
|
69
|
+
* @category Graph
|
|
70
|
+
*/
|
|
71
|
+
export interface SubgraphDef {
|
|
72
|
+
title: string;
|
|
73
|
+
nodes: GraphNode[];
|
|
74
|
+
links: GraphLink[];
|
|
75
|
+
groups?: GraphGroup[];
|
|
76
|
+
inputs: {
|
|
77
|
+
name: string;
|
|
78
|
+
type: string;
|
|
79
|
+
multi?: boolean;
|
|
80
|
+
to: PortRef[];
|
|
81
|
+
}[];
|
|
82
|
+
outputs: {
|
|
83
|
+
name: string;
|
|
84
|
+
type: string;
|
|
85
|
+
from: PortRef;
|
|
86
|
+
}[];
|
|
87
|
+
}
|
|
88
|
+
/** @category Graph */
|
|
89
|
+
export interface GraphDoc {
|
|
90
|
+
format: typeof GRAPH_FORMAT;
|
|
91
|
+
version: number;
|
|
92
|
+
nodes: GraphNode[];
|
|
93
|
+
links: GraphLink[];
|
|
94
|
+
/** script node type → ES module source */
|
|
95
|
+
scripts?: Record<string, string>;
|
|
96
|
+
/** frames around nodes (visual only) */
|
|
97
|
+
groups?: GraphGroup[];
|
|
98
|
+
/** subgraph node types: type → definition (see subgraph.ts) */
|
|
99
|
+
subgraphs?: Record<string, SubgraphDef>;
|
|
100
|
+
meta?: Record<string, unknown>;
|
|
101
|
+
}
|
|
102
|
+
/** @category Graph */
|
|
103
|
+
export declare function createGraph(): GraphDoc;
|
|
104
|
+
/** A short id, unique within a graph (random part + counter). @category Graph */
|
|
105
|
+
export declare function graphId(prefix?: string): string;
|
|
106
|
+
/** Checks a parsed JSON value is a graph document and fills in missing optional parts. @category Graph */
|
|
107
|
+
export declare function normalizeGraph(raw: unknown): GraphDoc;
|
|
108
|
+
/** @category Graph */
|
|
109
|
+
export declare function getNode(doc: GraphDoc, id: string): GraphNode | undefined;
|
|
110
|
+
/** The link into an input, if any (the first, for a multi input). @category Graph */
|
|
111
|
+
export declare function linkInto(doc: GraphDoc, node: string, port: string): GraphLink | undefined;
|
|
112
|
+
/** Every link into an input, in the order they were made. @category Graph */
|
|
113
|
+
export declare function linksInto(doc: GraphDoc, node: string, port: string): GraphLink[];
|
|
114
|
+
/** Adds a node of `def`'s type with its default values. @category Graph */
|
|
115
|
+
export declare function addNode(doc: GraphDoc, def: NodeDef, pos?: [number, number], values?: Record<string, unknown>): GraphNode;
|
|
116
|
+
/** Removes a node and every link to or from it. @category Graph */
|
|
117
|
+
export declare function removeNode(doc: GraphDoc, id: string): void;
|
|
118
|
+
/** @category Graph */
|
|
119
|
+
export type ConnectCheck = {
|
|
120
|
+
ok: true;
|
|
121
|
+
} | {
|
|
122
|
+
ok: false;
|
|
123
|
+
reason: string;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* Whether `from` (an output) may connect to `to` (an input): both exist, the types fit,
|
|
127
|
+
* and it wouldn't make a cycle. An input's existing link doesn't block (it gets replaced).
|
|
128
|
+
*
|
|
129
|
+
* @category Graph
|
|
130
|
+
*/
|
|
131
|
+
export declare function canConnect(doc: GraphDoc, registry: NodeRegistry, from: PortRef, to: PortRef, types?: TypeRegistry): ConnectCheck;
|
|
132
|
+
/**
|
|
133
|
+
* Connects an output to an input; throws if it can't. A plain input's existing link is
|
|
134
|
+
* replaced; a multi input keeps its links and adds this one (an identical link isn't doubled).
|
|
135
|
+
*
|
|
136
|
+
* @category Graph
|
|
137
|
+
*/
|
|
138
|
+
export declare function connect(doc: GraphDoc, registry: NodeRegistry, from: PortRef, to: PortRef, types?: TypeRegistry): GraphLink;
|
|
139
|
+
/** @category Graph */
|
|
140
|
+
export declare function disconnect(doc: GraphDoc, linkId: string): void;
|
|
141
|
+
//#endregion
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { portTypes } from "./types.js";
|
|
2
|
+
//#region src/graph/doc.ts
|
|
3
|
+
/**
|
|
4
|
+
* The graph document: plain JSON, saved and shared as-is.
|
|
5
|
+
*
|
|
6
|
+
* ```json
|
|
7
|
+
* {
|
|
8
|
+
* "format": "n3rdw1z4rd.graph", "version": 1,
|
|
9
|
+
* "nodes": [{ "id": "a1", "type": "list/grid2d", "pos": [0, 0], "values": { "cols": 14 } }],
|
|
10
|
+
* "links": [{ "id": "l1", "from": { "node": "a1", "port": "points" }, "to": { "node": "b2", "port": "at" } }],
|
|
11
|
+
* "scripts": { "my/ball": "export const inputs = …" }
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* `values` holds the node's params and the values of its unconnected inputs; `scripts` holds
|
|
16
|
+
* the source of the script node types the graph uses, so a graph file is self-contained.
|
|
17
|
+
*
|
|
18
|
+
* @module
|
|
19
|
+
*/
|
|
20
|
+
const GRAPH_FORMAT = "n3rdw1z4rd.graph";
|
|
21
|
+
const GRAPH_VERSION = 1;
|
|
22
|
+
/** @category Graph */
|
|
23
|
+
function createGraph() {
|
|
24
|
+
return {
|
|
25
|
+
format: GRAPH_FORMAT,
|
|
26
|
+
version: 1,
|
|
27
|
+
nodes: [],
|
|
28
|
+
links: []
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
let idCounter = 0;
|
|
32
|
+
/** A short id, unique within a graph (random part + counter). @category Graph */
|
|
33
|
+
function graphId(prefix = "n") {
|
|
34
|
+
return `${prefix}${Math.random().toString(36).slice(2, 7)}${(idCounter++).toString(36)}`;
|
|
35
|
+
}
|
|
36
|
+
/** Checks a parsed JSON value is a graph document and fills in missing optional parts. @category Graph */
|
|
37
|
+
function normalizeGraph(raw) {
|
|
38
|
+
const g = raw;
|
|
39
|
+
if (!g || g.format !== "n3rdw1z4rd.graph") throw new Error("not a graph document");
|
|
40
|
+
if (typeof g.version !== "number" || g.version > 1) throw new Error(`unsupported graph version ${g.version}`);
|
|
41
|
+
const nodes = (Array.isArray(g.nodes) ? g.nodes : []).filter((n) => !!n && typeof n.id === "string" && typeof n.type === "string").map((n) => ({
|
|
42
|
+
...n,
|
|
43
|
+
pos: Array.isArray(n.pos) ? [Number(n.pos[0]) || 0, Number(n.pos[1]) || 0] : [0, 0],
|
|
44
|
+
values: n.values && typeof n.values === "object" ? n.values : {}
|
|
45
|
+
}));
|
|
46
|
+
const ids = new Set(nodes.map((n) => n.id));
|
|
47
|
+
const links = (Array.isArray(g.links) ? g.links : []).filter((l) => !!l && typeof l.id === "string" && ids.has(l.from?.node) && ids.has(l.to?.node) && typeof l.from.port === "string" && typeof l.to.port === "string");
|
|
48
|
+
const groups = (Array.isArray(g.groups) ? g.groups : []).filter((gr) => !!gr && typeof gr.id === "string" && Array.isArray(gr.rect) && gr.rect.length === 4 && gr.rect.every((v) => Number.isFinite(v))).map((gr) => ({
|
|
49
|
+
id: gr.id,
|
|
50
|
+
title: String(gr.title ?? "group"),
|
|
51
|
+
color: String(gr.color ?? "#3a3a3a"),
|
|
52
|
+
rect: [...gr.rect]
|
|
53
|
+
}));
|
|
54
|
+
return {
|
|
55
|
+
...g,
|
|
56
|
+
format: GRAPH_FORMAT,
|
|
57
|
+
version: 1,
|
|
58
|
+
nodes,
|
|
59
|
+
links,
|
|
60
|
+
...groups.length ? { groups } : { groups: void 0 }
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** @category Graph */
|
|
64
|
+
function getNode(doc, id) {
|
|
65
|
+
return doc.nodes.find((n) => n.id === id);
|
|
66
|
+
}
|
|
67
|
+
/** The link into an input, if any (the first, for a multi input). @category Graph */
|
|
68
|
+
function linkInto(doc, node, port) {
|
|
69
|
+
return doc.links.find((l) => l.to.node === node && l.to.port === port);
|
|
70
|
+
}
|
|
71
|
+
/** Every link into an input, in the order they were made. @category Graph */
|
|
72
|
+
function linksInto(doc, node, port) {
|
|
73
|
+
return doc.links.filter((l) => l.to.node === node && l.to.port === port);
|
|
74
|
+
}
|
|
75
|
+
/** Adds a node of `def`'s type with its default values. @category Graph */
|
|
76
|
+
function addNode(doc, def, pos = [0, 0], values = {}) {
|
|
77
|
+
const node = {
|
|
78
|
+
id: graphId(),
|
|
79
|
+
type: def.type,
|
|
80
|
+
pos: [pos[0], pos[1]],
|
|
81
|
+
values: {}
|
|
82
|
+
};
|
|
83
|
+
for (const p of def.params) if (p.default !== void 0) node.values[p.name] = clone(p.default);
|
|
84
|
+
for (const p of def.inputs) if (p.default !== void 0) node.values[p.name] = clone(p.default);
|
|
85
|
+
Object.assign(node.values, clone(values));
|
|
86
|
+
doc.nodes.push(node);
|
|
87
|
+
return node;
|
|
88
|
+
}
|
|
89
|
+
/** Removes a node and every link to or from it. @category Graph */
|
|
90
|
+
function removeNode(doc, id) {
|
|
91
|
+
doc.nodes = doc.nodes.filter((n) => n.id !== id);
|
|
92
|
+
doc.links = doc.links.filter((l) => l.from.node !== id && l.to.node !== id);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Whether `from` (an output) may connect to `to` (an input): both exist, the types fit,
|
|
96
|
+
* and it wouldn't make a cycle. An input's existing link doesn't block (it gets replaced).
|
|
97
|
+
*
|
|
98
|
+
* @category Graph
|
|
99
|
+
*/
|
|
100
|
+
function canConnect(doc, registry, from, to, types = portTypes) {
|
|
101
|
+
const a = getNode(doc, from.node);
|
|
102
|
+
const b = getNode(doc, to.node);
|
|
103
|
+
if (!a || !b) return {
|
|
104
|
+
ok: false,
|
|
105
|
+
reason: "no such node"
|
|
106
|
+
};
|
|
107
|
+
if (a.id === b.id) return {
|
|
108
|
+
ok: false,
|
|
109
|
+
reason: "a node can’t connect to itself"
|
|
110
|
+
};
|
|
111
|
+
const out = registry.get(a.type)?.outputs.find((p) => p.name === from.port);
|
|
112
|
+
const inp = registry.get(b.type)?.inputs.find((p) => p.name === to.port);
|
|
113
|
+
if (!out) return {
|
|
114
|
+
ok: false,
|
|
115
|
+
reason: `no output "${from.port}"`
|
|
116
|
+
};
|
|
117
|
+
if (!inp) return {
|
|
118
|
+
ok: false,
|
|
119
|
+
reason: `no input "${to.port}"`
|
|
120
|
+
};
|
|
121
|
+
if (!types.canConnect(out.type, inp.type)) return {
|
|
122
|
+
ok: false,
|
|
123
|
+
reason: `${out.type} doesn’t fit ${inp.type}`
|
|
124
|
+
};
|
|
125
|
+
if (reaches(doc, b.id, a.id)) return {
|
|
126
|
+
ok: false,
|
|
127
|
+
reason: "that would make a loop"
|
|
128
|
+
};
|
|
129
|
+
return { ok: true };
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Connects an output to an input; throws if it can't. A plain input's existing link is
|
|
133
|
+
* replaced; a multi input keeps its links and adds this one (an identical link isn't doubled).
|
|
134
|
+
*
|
|
135
|
+
* @category Graph
|
|
136
|
+
*/
|
|
137
|
+
function connect(doc, registry, from, to, types = portTypes) {
|
|
138
|
+
const check = canConnect(doc, registry, from, to, types);
|
|
139
|
+
if (!check.ok) throw new Error(check.reason);
|
|
140
|
+
const multi = registry.get(getNode(doc, to.node).type)?.inputs.find((p) => p.name === to.port)?.multi;
|
|
141
|
+
const same = doc.links.find((l) => l.to.node === to.node && l.to.port === to.port && l.from.node === from.node && l.from.port === from.port);
|
|
142
|
+
if (multi && same) return same;
|
|
143
|
+
if (!multi) doc.links = doc.links.filter((l) => !(l.to.node === to.node && l.to.port === to.port));
|
|
144
|
+
const link = {
|
|
145
|
+
id: graphId("l"),
|
|
146
|
+
from: { ...from },
|
|
147
|
+
to: { ...to }
|
|
148
|
+
};
|
|
149
|
+
doc.links.push(link);
|
|
150
|
+
return link;
|
|
151
|
+
}
|
|
152
|
+
/** @category Graph */
|
|
153
|
+
function disconnect(doc, linkId) {
|
|
154
|
+
doc.links = doc.links.filter((l) => l.id !== linkId);
|
|
155
|
+
}
|
|
156
|
+
/** Whether following links downstream from `start` reaches `target`. */
|
|
157
|
+
function reaches(doc, start, target) {
|
|
158
|
+
const seen = /* @__PURE__ */ new Set();
|
|
159
|
+
const stack = [start];
|
|
160
|
+
while (stack.length) {
|
|
161
|
+
const id = stack.pop();
|
|
162
|
+
if (id === target) return true;
|
|
163
|
+
if (seen.has(id)) continue;
|
|
164
|
+
seen.add(id);
|
|
165
|
+
for (const l of doc.links) if (l.from.node === id) stack.push(l.to.node);
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
function clone(v) {
|
|
170
|
+
return v !== null && typeof v === "object" ? structuredClone(v) : v;
|
|
171
|
+
}
|
|
172
|
+
//#endregion
|
|
173
|
+
export { GRAPH_FORMAT, GRAPH_VERSION, addNode, canConnect, connect, createGraph, disconnect, getNode, graphId, linkInto, linksInto, normalizeGraph, removeNode };
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { NodeRegistry } from "./node.js";
|
|
2
|
+
import { GraphDoc } from "./doc.js";
|
|
3
|
+
//#region src/graph/evaluate.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Evaluates a graph: runs the nodes the outputs depend on, in dependency order, passing
|
|
6
|
+
* values along the links.
|
|
7
|
+
*
|
|
8
|
+
* - **Lazy:** only output nodes (`output: true`) and what they depend on run, unless you
|
|
9
|
+
* name other targets.
|
|
10
|
+
* - **Memoized:** a node re-runs only when its params / input values, an upstream result or
|
|
11
|
+
* its type changed since the last evaluation; otherwise its previous outputs are reused.
|
|
12
|
+
* - **List broadcasting:** a list wired into a single-value input runs the node once per
|
|
13
|
+
* item (several lists: longest wins, shorter ones repeat their last item) and every
|
|
14
|
+
* output becomes a list. A list port (`vec2[]`) takes the whole list instead; one value
|
|
15
|
+
* wired into it arrives as a one-item list. A list output of a broadcast node is
|
|
16
|
+
* flattened into the result list.
|
|
17
|
+
* - **Animation (optional):** a node with `animate` on (per node, or its type's default)
|
|
18
|
+
* re-runs on every evaluation that passes a new `frame`, and so does everything downstream
|
|
19
|
+
* of it; the rest stays cached. {@link GraphEvaluator.animated} tells the host whether any
|
|
20
|
+
* output depends on an animated node — if not, there's nothing to re-evaluate per frame.
|
|
21
|
+
* - **Disabled nodes** (`GraphNode.disabled`) don't run; downstream they count as failed
|
|
22
|
+
* (blocked, or left out of a multi input).
|
|
23
|
+
* - **Errors stay local:** a throwing node gets an `error`; nodes downstream of it are
|
|
24
|
+
* `blocked` (skipped) and don't run; the rest of the graph still evaluates. A **multi**
|
|
25
|
+
* input leaves failed sources out instead (listed in `skipped`), so one broken branch
|
|
26
|
+
* doesn't take down everything it feeds into.
|
|
27
|
+
*
|
|
28
|
+
* Nodes must treat their inputs as read-only: values are shared with upstream caches.
|
|
29
|
+
*
|
|
30
|
+
* @category Graph
|
|
31
|
+
*/
|
|
32
|
+
export declare class GraphEvaluator {
|
|
33
|
+
readonly registry: NodeRegistry;
|
|
34
|
+
private readonly cache;
|
|
35
|
+
private stamp;
|
|
36
|
+
constructor(registry: NodeRegistry);
|
|
37
|
+
/** The last result of a node. */
|
|
38
|
+
result(nodeId: string): NodeResult | undefined;
|
|
39
|
+
/** Forgets cached results (all, or one node's), so they re-run next time. */
|
|
40
|
+
invalidate(nodeId?: string): void;
|
|
41
|
+
/** The animated nodes the outputs (or `targets`) depend on; empty: no need to evaluate per frame. */
|
|
42
|
+
animated(doc: GraphDoc, targets?: string[]): string[];
|
|
43
|
+
evaluate(doc: GraphDoc, options?: EvaluateOptions): Promise<EvaluateResult>;
|
|
44
|
+
private defaultTargets;
|
|
45
|
+
/** Dependency order (upstream first) of the targets and everything they depend on. */
|
|
46
|
+
private order;
|
|
47
|
+
private run;
|
|
48
|
+
private store;
|
|
49
|
+
}
|
|
50
|
+
/** @category Graph */
|
|
51
|
+
export interface EvaluateOptions {
|
|
52
|
+
/** nodes to evaluate (with their dependencies); default: the output nodes, or every node if there are none */
|
|
53
|
+
targets?: string[];
|
|
54
|
+
/** passed to every node as `ctx.env` (not part of the cache key: `invalidate()` when it changes) */
|
|
55
|
+
env?: Record<string, unknown>;
|
|
56
|
+
/** the current frame while animating: animated nodes re-run when it changes */
|
|
57
|
+
frame?: FrameInfo;
|
|
58
|
+
/**
|
|
59
|
+
* Values for inputs that have no link, by `"<node>:<port>"` — how a subgraph's inputs
|
|
60
|
+
* reach its inner nodes. Bump `externalsStamp` when they change.
|
|
61
|
+
*/
|
|
62
|
+
externals?: Map<string, {
|
|
63
|
+
value: unknown;
|
|
64
|
+
list: boolean;
|
|
65
|
+
}[]>;
|
|
66
|
+
externalsStamp?: number;
|
|
67
|
+
/** prefixes `ctx.nodeId` (`<scope>/<id>`), so instances of a subgraph make distinct ids */
|
|
68
|
+
scope?: string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* A key a node's `run` may add to its result: `{ [OUTPUT_LISTS]: { out: true } }` marks outputs
|
|
72
|
+
* as lists regardless of their declared type.
|
|
73
|
+
*
|
|
74
|
+
* @category Graph
|
|
75
|
+
*/
|
|
76
|
+
export declare const OUTPUT_LISTS = "__lists";
|
|
77
|
+
/** @category Graph */
|
|
78
|
+
export interface FrameInfo {
|
|
79
|
+
/** seconds since animation started */
|
|
80
|
+
time: number;
|
|
81
|
+
/** seconds since the last frame */
|
|
82
|
+
dt: number;
|
|
83
|
+
/** frame number */
|
|
84
|
+
frame: number;
|
|
85
|
+
}
|
|
86
|
+
/** @category Graph */
|
|
87
|
+
export interface NodeResult {
|
|
88
|
+
/** output values by port name */
|
|
89
|
+
outputs: Record<string, unknown>;
|
|
90
|
+
/** which outputs are lists */
|
|
91
|
+
lists: Record<string, boolean>;
|
|
92
|
+
/** the node threw (or couldn't run) */
|
|
93
|
+
error?: string;
|
|
94
|
+
/** switched off by the user: didn't run */
|
|
95
|
+
disabled?: boolean;
|
|
96
|
+
/** an upstream node failed, was blocked or is disabled, so this one didn't run: its id */
|
|
97
|
+
blocked?: string;
|
|
98
|
+
/** multi inputs only: sources that failed and were left out (the node still ran) */
|
|
99
|
+
skipped?: string[];
|
|
100
|
+
/** time spent in run(), ms */
|
|
101
|
+
ms: number;
|
|
102
|
+
/** changes whenever the node actually re-ran */
|
|
103
|
+
stamp: number;
|
|
104
|
+
}
|
|
105
|
+
/** @category Graph */
|
|
106
|
+
export interface EvaluateResult {
|
|
107
|
+
/** every evaluated node, upstream first */
|
|
108
|
+
order: string[];
|
|
109
|
+
/** the nodes that actually ran (the rest reused cached results) */
|
|
110
|
+
ran: string[];
|
|
111
|
+
results: Map<string, NodeResult>;
|
|
112
|
+
}
|
|
113
|
+
//#endregion
|
|
114
|
+
export type { GraphDoc };
|