@margoengine/shader 0.0.0-stage → 1.0.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1919 -2
- package/dist/attribute.d.ts +28 -0
- package/dist/black-hole.d.ts +246 -0
- package/dist/black-hole.js +445 -0
- package/dist/clock-BLGNexlt.js +52 -0
- package/dist/clock.d.ts +42 -0
- package/dist/compute-CXGG9qvI.js +360 -0
- package/dist/compute-texture.d.ts +100 -0
- package/dist/compute-texture.js +131 -0
- package/dist/compute-volume.d.ts +57 -0
- package/dist/compute-volume.js +130 -0
- package/dist/compute.d.ts +200 -0
- package/dist/control-CFjkGPBk.js +201 -0
- package/dist/control.d.ts +93 -0
- package/dist/deform.d.ts +111 -0
- package/dist/deform.js +89 -0
- package/dist/display-BrJM_Qkd.js +5 -0
- package/dist/editor-model.d.ts +192 -0
- package/dist/editor-pass.d.ts +71 -0
- package/dist/editor-pass.js +216 -0
- package/dist/editor.d.ts +150 -0
- package/dist/editor.js +1071 -0
- package/dist/effects.d.ts +150 -0
- package/dist/effects.js +87 -0
- package/dist/errors-CsQwshHA.js +7 -0
- package/dist/glsl/emit.d.ts +43 -0
- package/dist/glsl/host.d.ts +55 -0
- package/dist/glsl/material.d.ts +173 -0
- package/dist/glsl/parser.d.ts +76 -0
- package/dist/glsl/preprocess.d.ts +9 -0
- package/dist/glsl/presets.d.ts +33 -0
- package/dist/glsl/program.d.ts +210 -0
- package/dist/glsl/scene.d.ts +9 -0
- package/dist/glsl-material.d.ts +82 -0
- package/dist/glsl-material.js +288 -0
- package/dist/glsl-pass.d.ts +93 -0
- package/dist/glsl-pass.js +186 -0
- package/dist/glsl.d.ts +94 -0
- package/dist/glsl.js +170 -0
- package/dist/gpu-DQJk2xAv.js +55 -0
- package/dist/graph.d.ts +82 -0
- package/dist/graph.js +365 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +103 -0
- package/dist/lighting-JU9yQPfM.js +77 -0
- package/dist/lighting.d.ts +62 -0
- package/dist/lighting.js +1 -0
- package/dist/material-C6bpqlnE.js +1184 -0
- package/dist/material-CZ8V1FeA.js +299 -0
- package/dist/material.d.ts +79 -0
- package/dist/node-B6yoIC9V.js +590 -0
- package/dist/node.d.ts +433 -0
- package/dist/noise.d.ts +38 -0
- package/dist/noise.js +107 -0
- package/dist/own-stack-D1XnXrrz.js +11 -0
- package/dist/own-stack.d.ts +12 -0
- package/dist/pass.d.ts +109 -0
- package/dist/pass.js +239 -0
- package/dist/post-DLpZ110L.js +113 -0
- package/dist/post.d.ts +69 -0
- package/dist/preprocess-CWGWlBWQ.js +1195 -0
- package/dist/raw-0AYej3Ak.js +1500 -0
- package/dist/raw.d.ts +744 -0
- package/dist/raw.js +1 -0
- package/dist/readback.d.ts +42 -0
- package/dist/readback.js +85 -0
- package/dist/scope.d.ts +21 -0
- package/dist/serialization.d.ts +11 -0
- package/dist/serialization.js +192 -0
- package/dist/shadertoy/sound.d.ts +48 -0
- package/dist/shadertoy.d.ts +249 -0
- package/dist/shadertoy.js +909 -0
- package/dist/shared/caster.d.ts +159 -0
- package/dist/shared/fog.d.ts +33 -0
- package/dist/shared/fractal.d.ts +9 -0
- package/dist/shared/msaa.d.ts +30 -0
- package/dist/shared/stack.d.ts +22 -0
- package/dist/sound-CF_dzP41.js +78 -0
- package/dist/storage-format-DWaiSxmq.js +25 -0
- package/dist/storage-format.d.ts +7 -0
- package/dist/subgroups.d.ts +97 -0
- package/dist/subgroups.js +95 -0
- package/dist/texture-BawKO_h9.js +62 -0
- package/dist/texture-array.d.ts +7 -0
- package/dist/texture-array.js +11 -0
- package/dist/texture.d.ts +16 -0
- package/dist/utils.d.ts +106 -0
- package/dist/utils.js +90 -0
- package/dist/view-rect-Bm1ac-TK.js +16 -0
- package/package.json +149 -4
package/dist/deform.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { type StorageBuffer } from './compute.js';
|
|
2
|
+
import { type Node, type NodeInput } from './node.js';
|
|
3
|
+
/** What {@link skinningFromModel} reads of a loaded model: `PBRModel` from `@margoengine/gltf/pbr` is one. */
|
|
4
|
+
export interface SkinnedModel {
|
|
5
|
+
readonly nodes: readonly {
|
|
6
|
+
readonly matrix: ArrayLike<number>;
|
|
7
|
+
}[];
|
|
8
|
+
readonly asset: {
|
|
9
|
+
readonly doc: {
|
|
10
|
+
readonly nodes?: readonly {
|
|
11
|
+
readonly skin?: number;
|
|
12
|
+
}[];
|
|
13
|
+
readonly skins?: readonly {
|
|
14
|
+
readonly joints: readonly number[];
|
|
15
|
+
readonly inverseBindMatrices?: number;
|
|
16
|
+
}[];
|
|
17
|
+
};
|
|
18
|
+
accessor(index: number, type: string): ArrayLike<number>;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** What {@link skinningFromModel} reads of a primitive: an entry of `model.primitives`. */
|
|
22
|
+
export interface SkinnedPrimitive {
|
|
23
|
+
readonly nodeIndex: number;
|
|
24
|
+
/** Twenty floats a vertex, position first, then at 4 the normal and at 12 UV0, as gltf\/pbr packs them. */
|
|
25
|
+
readonly vertices: Float32Array;
|
|
26
|
+
readonly indices: Uint32Array;
|
|
27
|
+
readonly definition: {
|
|
28
|
+
readonly attributes?: Readonly<Record<string, number>>;
|
|
29
|
+
};
|
|
30
|
+
/** Set on a part of a primitive gltf\/pbr split into parts: the definition's vertex of each local vertex. */
|
|
31
|
+
readonly part?: {
|
|
32
|
+
readonly vertexMap: Uint32Array;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** A skin read from a loaded model, for `positionNode` and `normalNode` of a NodeMaterial. */
|
|
36
|
+
export interface ModelSkinning extends Skinning {
|
|
37
|
+
/** The primitive's bind pose, for `createMesh`. Draw it at the world origin: the skin works in world space. */
|
|
38
|
+
readonly geometry: {
|
|
39
|
+
readonly positions: Float32Array;
|
|
40
|
+
readonly normals: Float32Array;
|
|
41
|
+
readonly uvs: Float32Array;
|
|
42
|
+
readonly indices: Uint32Array;
|
|
43
|
+
};
|
|
44
|
+
/** The buffers `skinning()` reads, as `skinningFromModel` filled them. */
|
|
45
|
+
readonly bones: StorageBuffer<'vec4f'>;
|
|
46
|
+
readonly joints: StorageBuffer<'vec4u'>;
|
|
47
|
+
readonly weights: StorageBuffer<'vec4f'>;
|
|
48
|
+
/**
|
|
49
|
+
* Copies the joints' world matrices to `bones`. Call it after the joints moved and the engine updated its transforms,
|
|
50
|
+
* so after a render or `engine.store.tick(false)`.
|
|
51
|
+
*/
|
|
52
|
+
update(): void;
|
|
53
|
+
}
|
|
54
|
+
/** Options for `skinning()`. All three are `storage()` buffers. */
|
|
55
|
+
export interface SkinningOptions {
|
|
56
|
+
/** One 4 x 4 matrix per bone, four `vec4f` columns each: the joint's matrix times its inverse bind matrix, in mesh space. Update it to pose the mesh. */
|
|
57
|
+
bones: StorageBuffer<'vec4f'>;
|
|
58
|
+
/** Four bone numbers per vertex, in vertex order. */
|
|
59
|
+
joints: StorageBuffer<'vec4u'>;
|
|
60
|
+
/** Four weights per vertex that add up to 1, in vertex order. */
|
|
61
|
+
weights: StorageBuffer<'vec4f'>;
|
|
62
|
+
}
|
|
63
|
+
/** What `skinning()` returns. Each function takes the bind-pose value, by default the mesh's own. */
|
|
64
|
+
export interface Skinning {
|
|
65
|
+
/** The skinned position, in mesh space. Add effects before it to move the bind pose, or after it to move the pose. */
|
|
66
|
+
position(position?: NodeInput<'vec3f'>): Node<'vec3f'>;
|
|
67
|
+
/** The skinned unit normal, in mesh space. Bones with non-uniform scale skew it. */
|
|
68
|
+
normal(normal?: NodeInput<'vec3f'>): Node<'vec3f'>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Reads a loaded glTF skin into {@link skinning}: its joints and weights, the bind pose as `geometry`, and a joint palette
|
|
72
|
+
* that `update()` fills from the model's nodes. Whatever moves those nodes drives it, `Animation` included.
|
|
73
|
+
* @throws MargoError `'state'` when the primitive's node has no skin, and `'unsupported'` for more than four influences a
|
|
74
|
+
* vertex (`JOINTS_1`), which `skinning()` does not read.
|
|
75
|
+
*/
|
|
76
|
+
export declare function skinningFromModel(model: SkinnedModel, primitive: SkinnedPrimitive): ModelSkinning;
|
|
77
|
+
/**
|
|
78
|
+
* Linear blend skinning in the vertex stage. `positionNode: skin.position()` draws the posed mesh in every pass, and the
|
|
79
|
+
* normals follow it unless `positionNormals` says otherwise.
|
|
80
|
+
*/
|
|
81
|
+
export declare function skinning({ bones, joints, weights }: SkinningOptions): Skinning;
|
|
82
|
+
/** Options for `morphing()`. */
|
|
83
|
+
export interface MorphingOptions {
|
|
84
|
+
/** How many targets the buffers hold. */
|
|
85
|
+
count: number;
|
|
86
|
+
/** How many vertices the mesh has. */
|
|
87
|
+
vertices: number;
|
|
88
|
+
/** Position offsets, target after target: `count * vertices` entries. */
|
|
89
|
+
positions: StorageBuffer<'vec4f'>;
|
|
90
|
+
/** Normal offsets in the same order, for `normal()`. */
|
|
91
|
+
normals?: StorageBuffer<'vec4f'>;
|
|
92
|
+
/** One weight per target. Update it to animate the mesh. */
|
|
93
|
+
weights: StorageBuffer<'f32'>;
|
|
94
|
+
}
|
|
95
|
+
/** What `morphing()` returns. Each function takes the base value, by default the mesh's own. */
|
|
96
|
+
export interface Morphing {
|
|
97
|
+
/** The base position plus each target's offset times its weight, in mesh space. */
|
|
98
|
+
position(position?: NodeInput<'vec3f'>): Node<'vec3f'>;
|
|
99
|
+
/** The same for the normal, renormalised. Without `normals` it is the base normal. */
|
|
100
|
+
normal(normal?: NodeInput<'vec3f'>): Node<'vec3f'>;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Morph targets in the vertex stage. `positionNode: morph.position()` draws the blended mesh in every pass; chain it into
|
|
104
|
+
* `skinning()` with `skin.position(morph.position())`, the glTF order.
|
|
105
|
+
*/
|
|
106
|
+
export declare function morphing({ count, vertices, positions, normals, weights }: MorphingOptions): Morphing;
|
|
107
|
+
/**
|
|
108
|
+
* A mesh-space normal such as `skin.normal()` as `normalNode` wants it: world space, worked out per vertex and interpolated.
|
|
109
|
+
* It uses the cofactors of the model matrix, so a scaled or mirrored mesh keeps its normals perpendicular to the surface.
|
|
110
|
+
*/
|
|
111
|
+
export declare function worldNormal(normal: NodeInput<'vec3f'>): Node<'vec3f'>;
|
package/dist/deform.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { T as e, fn as t, ln as n, mt as r, on as i, st as a, vt as o, wt as s } from "./node-B6yoIC9V.js";
|
|
2
|
+
import { l as c } from "./compute-CXGG9qvI.js";
|
|
3
|
+
import { MargoError as l } from "@margoengine/math/errors";
|
|
4
|
+
//#region ../shared/vertex-map.js
|
|
5
|
+
var u = /* @__PURE__ */ new WeakMap();
|
|
6
|
+
function d(e, t, n) {
|
|
7
|
+
if (!n) return e;
|
|
8
|
+
let r = u.get(n);
|
|
9
|
+
r || u.set(n, r = /* @__PURE__ */ new WeakMap());
|
|
10
|
+
let i = r.get(e);
|
|
11
|
+
if (i) return i;
|
|
12
|
+
i = new e.constructor(n.length * t);
|
|
13
|
+
for (let r = 0; r < n.length; r++) {
|
|
14
|
+
let a = n[r] * t;
|
|
15
|
+
if (a + t > e.length) throw RangeError("Part vertex out of range; every per-vertex accessor needs one element per vertex");
|
|
16
|
+
for (let n = 0; n < t; n++) i[r * t + n] = e[a + n];
|
|
17
|
+
}
|
|
18
|
+
return r.set(e, i), i;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/deform.ts
|
|
22
|
+
function f(e, t) {
|
|
23
|
+
let { doc: n } = e.asset, { attributes: r = {} } = t.definition, i = n.skins?.[n.nodes?.[t.nodeIndex]?.skin ?? -1];
|
|
24
|
+
if (!i || r.JOINTS_0 === void 0 || r.WEIGHTS_0 === void 0) throw new l("state", "The primitive has no glTF skin; pick one whose node has a skin and JOINTS_0.");
|
|
25
|
+
if (r.JOINTS_1 !== void 0) throw new l("unsupported", "skinning() reads four influences a vertex; limit the file to JOINTS_0.");
|
|
26
|
+
let { vertices: a, indices: o } = t, s = a.length / 20, u = new Float32Array(s * 3), f = new Float32Array(s * 3), m = new Float32Array(s * 2);
|
|
27
|
+
for (let e = 0; e < s; e++) {
|
|
28
|
+
let t = e * 20;
|
|
29
|
+
u.set(a.subarray(t, t + 3), e * 3), f.set(a.subarray(t + 4, t + 7), e * 3), m.set(a.subarray(t + 12, t + 14), e * 2);
|
|
30
|
+
}
|
|
31
|
+
let { joints: h, inverseBindMatrices: g } = i, _ = g === void 0 ? void 0 : e.asset.accessor(g, "MAT4"), v = new Float32Array(h.length * 16), y = c(v, "vec4f"), b = t.part?.vertexMap, x = c(Uint32Array.from(d(e.asset.accessor(r.JOINTS_0, "VEC4"), 4, b)), "vec4u"), S = c(Float32Array.from(d(e.asset.accessor(r.WEIGHTS_0, "VEC4"), 4, b)), "vec4f"), C = () => {
|
|
32
|
+
h.forEach((t, n) => {
|
|
33
|
+
let r = e.nodes[t].matrix, i = n * 16;
|
|
34
|
+
if (!_) v.set(r, i);
|
|
35
|
+
else for (let e = 0; e < 4; e++) for (let t = 0; t < 4; t++) {
|
|
36
|
+
let n = 0;
|
|
37
|
+
for (let a = 0; a < 4; a++) n += r[a * 4 + t] * _[i + e * 4 + a];
|
|
38
|
+
v[i + e * 4 + t] = n;
|
|
39
|
+
}
|
|
40
|
+
}), y.update();
|
|
41
|
+
};
|
|
42
|
+
return C(), {
|
|
43
|
+
...p({
|
|
44
|
+
bones: y,
|
|
45
|
+
joints: x,
|
|
46
|
+
weights: S
|
|
47
|
+
}),
|
|
48
|
+
geometry: {
|
|
49
|
+
positions: u,
|
|
50
|
+
normals: f,
|
|
51
|
+
uvs: m,
|
|
52
|
+
indices: o
|
|
53
|
+
},
|
|
54
|
+
bones: y,
|
|
55
|
+
joints: x,
|
|
56
|
+
weights: S,
|
|
57
|
+
update: C
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function p({ bones: n, joints: r, weights: i }) {
|
|
61
|
+
let a = r.element(t), c = i.element(t), l = (e) => n.element(a.x.mul(4).add(e)).mul(c.x).add(n.element(a.y.mul(4).add(e)).mul(c.y)).add(n.element(a.z.mul(4).add(e)).mul(c.z)).add(n.element(a.w.mul(4).add(e)).mul(c.w)), u = l(0), d = l(1), f = l(2), p = l(3);
|
|
62
|
+
return {
|
|
63
|
+
position(t = s) {
|
|
64
|
+
let n = e(t, "vec3f", "skin position");
|
|
65
|
+
return u.mul(n.x).add(d.mul(n.y)).add(f.mul(n.z)).add(p).xyz;
|
|
66
|
+
},
|
|
67
|
+
normal(t = o) {
|
|
68
|
+
let n = e(t, "vec3f", "skin normal");
|
|
69
|
+
return u.mul(n.x).add(d.mul(n.y)).add(f.mul(n.z)).xyz.normalize();
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function m({ count: n, vertices: r, positions: i, normals: c, weights: l }) {
|
|
74
|
+
if (!Number.isInteger(n) || n < 1 || !Number.isInteger(r) || r < 1) throw RangeError("morphing() needs a whole count and vertices of at least 1");
|
|
75
|
+
let u = (e, i) => a(n, e, (e, n) => n.add(i.element(e.toUint().mul(r).add(t)).xyz.mul(l.element(e))));
|
|
76
|
+
return {
|
|
77
|
+
position: (t = s) => u(e(t, "vec3f", "morph position"), i),
|
|
78
|
+
normal(t = o) {
|
|
79
|
+
let n = e(t, "vec3f", "morph normal");
|
|
80
|
+
return c ? u(n, c).normalize() : n;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function h(t) {
|
|
85
|
+
let a = e(t, "vec3f", "worldNormal"), o = r.mul(n(1, 0, 0, 0)).xyz, s = r.mul(n(0, 1, 0, 0)).xyz, c = r.mul(n(0, 0, 1, 0)).xyz, l = s.cross(c);
|
|
86
|
+
return i(l.mul(a.x).add(c.cross(o).mul(a.y)).add(o.cross(s).mul(a.z)).mul(o.dot(l).sign())).normalize();
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
export { m as morphing, p as skinning, f as skinningFromModel, h as worldNormal };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
//#region ../shared/display.js
|
|
2
|
+
var e = "fn pbrNeutral(input:vec3f)->vec3f {let x=min(input.r,min(input.g,input.b));let offset=select(0.04,x-6.25*x*x,x<0.08);var v=input-offset;let peak=max(v.r,max(v.g,v.b));if(peak<0.76){return v;}let newPeak=1.0-0.24*0.24/(peak+0.24-0.76);v*=newPeak/peak;return mix(v,vec3f(newPeak),1.0-1.0/(0.15*(peak-newPeak)+1.0));}\n";
|
|
3
|
+
`${e}`;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { e as t };
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { type SurfaceFallback, type SurfaceGraph, type SurfaceOperation } from './graph.js';
|
|
2
|
+
/** A material output slot a graph can drive. */
|
|
3
|
+
export type OutputSlot = 'color' | 'roughness' | 'metalness' | 'emissive' | 'opacity' | 'normal';
|
|
4
|
+
/** Where each node sits, in editor pixels, by node id; the key `output` is the material output panel. */
|
|
5
|
+
export type GraphLayout = Record<string, readonly [number, number]>;
|
|
6
|
+
/** One node of the graph being edited. `inputs[i]` is the id of the node wired to input `i`, or null. */
|
|
7
|
+
export interface EditorNode {
|
|
8
|
+
id: string;
|
|
9
|
+
op: SurfaceOperation;
|
|
10
|
+
/** Source node id per input, null while unconnected. */
|
|
11
|
+
inputs: (string | null)[];
|
|
12
|
+
/** The constant of a `float` or `vec3` node, or the component index (0, 1, 2) of a `component` node. */
|
|
13
|
+
value?: number | [number, number, number];
|
|
14
|
+
/** Constants typed into unconnected inputs that have a default; undefined keeps the operation's default. */
|
|
15
|
+
inline: (number | undefined)[];
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
}
|
|
19
|
+
/** What {@link GraphModel.status} reports. */
|
|
20
|
+
export interface EditorStatus {
|
|
21
|
+
/** True when the graph validates and can be compiled. */
|
|
22
|
+
ok: boolean;
|
|
23
|
+
/** A short sentence: what is valid, or what to fix. */
|
|
24
|
+
message: string;
|
|
25
|
+
/** The node the message is about, when it names one. */
|
|
26
|
+
node: string | null;
|
|
27
|
+
/** Output type of every node while `ok` (f32 or vec3f), else empty. */
|
|
28
|
+
types: Record<string, 'f32' | 'vec3f'>;
|
|
29
|
+
}
|
|
30
|
+
/** Why a listener was called: `graph` for any change to nodes, wires, values or outputs (including undo), `layout` for moves only. */
|
|
31
|
+
export type ModelEvent = 'graph' | 'layout';
|
|
32
|
+
/** The kinds of node on the palette, in the order shown. */
|
|
33
|
+
export type OperationGroup = 'Input' | 'Constant' | 'Math' | 'Vector' | 'Shaping';
|
|
34
|
+
/** Palette entry of one operation. */
|
|
35
|
+
export interface EditorOperation {
|
|
36
|
+
op: SurfaceOperation;
|
|
37
|
+
label: string;
|
|
38
|
+
group: OperationGroup;
|
|
39
|
+
description: string;
|
|
40
|
+
}
|
|
41
|
+
/** Every operation the portable surface graph supports, with the name and one-line help the palette shows. */
|
|
42
|
+
export declare const EDITOR_OPERATIONS: readonly EditorOperation[];
|
|
43
|
+
/** Name of input `index` of an operation, as the editor labels its socket (`A`, `Factor`, `Vector`, ...). */
|
|
44
|
+
export declare const inputLabel: (op: SurfaceOperation, index: number) => string;
|
|
45
|
+
/**
|
|
46
|
+
* The constant an unconnected input takes, per operation and input; undefined means the input must be wired. The editor
|
|
47
|
+
* adds these as hidden `float` nodes when it exports, so an unconnected `Factor` mixes halfway and `B` of `Multiply` is 1.
|
|
48
|
+
*/
|
|
49
|
+
export declare const INPUT_DEFAULTS: Readonly<Partial<Record<SurfaceOperation, readonly (number | undefined)[]>>>;
|
|
50
|
+
/** The output slots, in the order the output panel lists them, with the type each accepts. */
|
|
51
|
+
export declare const OUTPUT_SLOTS: readonly {
|
|
52
|
+
slot: OutputSlot;
|
|
53
|
+
label: string;
|
|
54
|
+
type: 'f32' | 'vec3f';
|
|
55
|
+
}[];
|
|
56
|
+
/** Width of every node in editor pixels. */
|
|
57
|
+
export declare const NODE_WIDTH = 176;
|
|
58
|
+
/** Height of a node's title bar. */
|
|
59
|
+
export declare const HEAD_HEIGHT = 26;
|
|
60
|
+
/** Height of one row (an input, a value field or the output label). */
|
|
61
|
+
export declare const ROW_HEIGHT = 24;
|
|
62
|
+
/** Padding above and below a node's rows. */
|
|
63
|
+
export declare const ROW_PADDING = 6;
|
|
64
|
+
/** Rows below the six slots on the output panel: fallback color, emissive and the three scalars. */
|
|
65
|
+
export declare const OUTPUT_EXTRA_ROWS = 3;
|
|
66
|
+
/** Number of rows a node draws: its inputs, then its value field, with at least one for the output label. */
|
|
67
|
+
export declare const rowCount: (node: Pick<EditorNode, 'op' | 'inputs'>) => number;
|
|
68
|
+
/** Height of a node in editor pixels. */
|
|
69
|
+
export declare const nodeHeight: (node: Pick<EditorNode, 'op' | 'inputs'>) => number;
|
|
70
|
+
/** Height of the output panel in editor pixels. */
|
|
71
|
+
export declare const OUTPUT_HEIGHT: number;
|
|
72
|
+
/** Centre of input socket `index` of a node, or of slot `index` of the output panel, in editor pixels. */
|
|
73
|
+
export declare const inputSocket: (at: {
|
|
74
|
+
x: number;
|
|
75
|
+
y: number;
|
|
76
|
+
}, index: number) => [number, number];
|
|
77
|
+
/** Centre of a node's output socket in editor pixels. */
|
|
78
|
+
export declare const outputSocket: (at: {
|
|
79
|
+
x: number;
|
|
80
|
+
y: number;
|
|
81
|
+
}) => [number, number];
|
|
82
|
+
/**
|
|
83
|
+
* The editable state behind {@link NodeEditor}, with no DOM: nodes with positions, wires, constants, output wiring, undo
|
|
84
|
+
* and redo. Incomplete graphs are allowed while editing (an unconnected input is just empty); {@link GraphModel.status}
|
|
85
|
+
* says whether the graph validates and {@link GraphModel.toGraph} returns the portable `SurfaceGraph` once it does.
|
|
86
|
+
* Unconnected inputs that have a default (see {@link INPUT_DEFAULTS}) become hidden constants on export.
|
|
87
|
+
*/
|
|
88
|
+
export declare class GraphModel {
|
|
89
|
+
/** The nodes in creation order. Read them; change them through the methods so history and listeners stay right. */
|
|
90
|
+
readonly nodes: EditorNode[];
|
|
91
|
+
/** Source node id per output slot. */
|
|
92
|
+
outputs: Partial<Record<OutputSlot, string>>;
|
|
93
|
+
/** Constants for hosts that cannot run graphs; see `SurfaceFallback`. */
|
|
94
|
+
fallback: SurfaceFallback;
|
|
95
|
+
/** Position of the material output panel. */
|
|
96
|
+
output: {
|
|
97
|
+
x: number;
|
|
98
|
+
y: number;
|
|
99
|
+
};
|
|
100
|
+
private undoStack;
|
|
101
|
+
private redoStack;
|
|
102
|
+
private readonly listeners;
|
|
103
|
+
private depth;
|
|
104
|
+
private version;
|
|
105
|
+
private cached?;
|
|
106
|
+
/**
|
|
107
|
+
* @param graph A valid portable graph to edit; omit for an empty one.
|
|
108
|
+
* @param layout Node positions by id (and `output`); nodes without one are arranged automatically.
|
|
109
|
+
* @throws TypeError or RangeError from `validateSurfaceGraph` when `graph` is invalid (the message names the fix).
|
|
110
|
+
*/
|
|
111
|
+
constructor(graph?: SurfaceGraph, layout?: GraphLayout);
|
|
112
|
+
private fill;
|
|
113
|
+
/** Node by id, or undefined. */
|
|
114
|
+
node(id: string): EditorNode | undefined;
|
|
115
|
+
/** Positions of every node and the output panel, to save beside the graph and pass back to the constructor. */
|
|
116
|
+
get layout(): GraphLayout;
|
|
117
|
+
/** True when {@link GraphModel.undo} has a step to undo. */
|
|
118
|
+
get canUndo(): boolean;
|
|
119
|
+
/** True when {@link GraphModel.redo} has a step to redo. */
|
|
120
|
+
get canRedo(): boolean;
|
|
121
|
+
/** Calls `listener` after every change; returns a function that stops it. */
|
|
122
|
+
subscribe(listener: (event: ModelEvent) => void): () => void;
|
|
123
|
+
private snapshot;
|
|
124
|
+
private restore;
|
|
125
|
+
private emit;
|
|
126
|
+
/**
|
|
127
|
+
* Records an undo step before the next change. Every editing method does this itself; call it once yourself before a
|
|
128
|
+
* series of {@link GraphModel.move} calls (a drag) so the whole drag undoes in one step.
|
|
129
|
+
*/
|
|
130
|
+
checkpoint(): void;
|
|
131
|
+
/**
|
|
132
|
+
* Runs `edit` as one undo step with one notification. If it throws, the graph is restored and the error rethrown.
|
|
133
|
+
*/
|
|
134
|
+
batch(edit: () => void): void;
|
|
135
|
+
private change;
|
|
136
|
+
private freshId;
|
|
137
|
+
private need;
|
|
138
|
+
private dependsOn;
|
|
139
|
+
private checkValue;
|
|
140
|
+
/**
|
|
141
|
+
* Adds a node of `op` at editor position `at` (default: the next free spot) and returns it.
|
|
142
|
+
* @throws RangeError when the graph already has 128 nodes or `op` is unknown; the message names the fix.
|
|
143
|
+
*/
|
|
144
|
+
addNode(op: SurfaceOperation, at?: readonly [number, number], value?: number | [number, number, number]): EditorNode;
|
|
145
|
+
/** Deletes nodes; wires from them are cleared and output slots that used them are emptied. Unknown ids are ignored. */
|
|
146
|
+
removeNodes(ids: Iterable<string>): void;
|
|
147
|
+
/**
|
|
148
|
+
* Wires the output of `from` into input `input` (0-based) of `to`, replacing what was there.
|
|
149
|
+
* @throws RangeError for an unknown node or input, or when the wire would make a loop (`to` already feeds `from`).
|
|
150
|
+
*/
|
|
151
|
+
connect(from: string, to: string, input: number): void;
|
|
152
|
+
/** Wires the output of `from` to a material output slot. */
|
|
153
|
+
connectOutput(from: string, slot: OutputSlot): void;
|
|
154
|
+
/** Clears input `input` of `to`. */
|
|
155
|
+
disconnect(to: string, input: number): void;
|
|
156
|
+
/** Empties an output slot. */
|
|
157
|
+
disconnectOutput(slot: OutputSlot): void;
|
|
158
|
+
/** Sets the constant of a `float`, `vec3` or `component` node. */
|
|
159
|
+
setValue(id: string, value: number | readonly [number, number, number]): void;
|
|
160
|
+
/** Types a constant into an unconnected input that has a default; pass undefined to go back to the default. */
|
|
161
|
+
setInline(id: string, input: number, value: number | undefined): void;
|
|
162
|
+
/** Changes the fallback constants; each given field replaces the old one. */
|
|
163
|
+
setFallback(patch: Partial<SurfaceFallback>): void;
|
|
164
|
+
/**
|
|
165
|
+
* Moves nodes by (dx, dy). `output` moves the output panel. Does not record history: call {@link GraphModel.checkpoint}
|
|
166
|
+
* first when a gesture starts.
|
|
167
|
+
*/
|
|
168
|
+
move(ids: Iterable<string>, dx: number, dy: number): void;
|
|
169
|
+
/**
|
|
170
|
+
* Copies nodes (and the wires between them) 24 px down and right, keeping wires from outside the copy, and returns
|
|
171
|
+
* the new ids.
|
|
172
|
+
*/
|
|
173
|
+
duplicate(ids: Iterable<string>): string[];
|
|
174
|
+
/** Lays nodes out in columns by depth, sources on the left and the output panel on the right. */
|
|
175
|
+
arrange(silent?: boolean): void;
|
|
176
|
+
/** Steps back one change. Returns false when there is nothing to undo. */
|
|
177
|
+
undo(): boolean;
|
|
178
|
+
/** Steps forward one undone change. Returns false when there is nothing to redo. */
|
|
179
|
+
redo(): boolean;
|
|
180
|
+
/** Replaces the whole graph (one undo step). Same errors as the constructor. */
|
|
181
|
+
load(graph: SurfaceGraph, layout?: GraphLayout): void;
|
|
182
|
+
private raw;
|
|
183
|
+
/**
|
|
184
|
+
* The portable graph, validated. Hidden constants for unconnected inputs are included as `float` nodes.
|
|
185
|
+
* @throws TypeError or RangeError (the `validateSurfaceGraph` errors, or "incomplete" for an unconnected input without a default).
|
|
186
|
+
*/
|
|
187
|
+
toGraph(): SurfaceGraph;
|
|
188
|
+
/** Canonical JSON of {@link GraphModel.toGraph}. */
|
|
189
|
+
toJSON(): string;
|
|
190
|
+
/** Whether the graph validates, with the type of every node or the first thing to fix. Cached until the graph changes. */
|
|
191
|
+
status(): EditorStatus;
|
|
192
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import { NodeEditor, type GraphLayout } from './editor.js';
|
|
3
|
+
import { type SurfaceGraph } from './graph.js';
|
|
4
|
+
import { NodePass } from './post.js';
|
|
5
|
+
import { ComputePass, StorageBuffer } from './compute.js';
|
|
6
|
+
import { ComputeTexture } from './compute-texture.js';
|
|
7
|
+
import { ComputeVolume } from './compute-volume.js';
|
|
8
|
+
/** Resource names are saved; the application supplies their live objects again on load. */
|
|
9
|
+
export type PassGraphResources = Readonly<Record<string, StorageBuffer | ComputeTexture | ComputeVolume>>;
|
|
10
|
+
/** A saved post effect or procedural compute graph, including its authoring layout. */
|
|
11
|
+
export interface PassGraph {
|
|
12
|
+
version: 1;
|
|
13
|
+
kind: 'post' | 'compute';
|
|
14
|
+
graph: SurfaceGraph;
|
|
15
|
+
layout?: GraphLayout;
|
|
16
|
+
/** Post colour space, default ldr. */
|
|
17
|
+
colorSpace?: 'hdr' | 'ldr';
|
|
18
|
+
/** Compute dispatch shape and resource names. Required for compute. */
|
|
19
|
+
compute?: {
|
|
20
|
+
size: [number, number, number];
|
|
21
|
+
target: string;
|
|
22
|
+
source?: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/** Parse and validate a bounded, independent pass document without GPU handles or executable source. */
|
|
26
|
+
export declare function parsePassGraph(json: string): PassGraph;
|
|
27
|
+
/** Stable JSON for a graph and layout, ready for local files or application storage. */
|
|
28
|
+
export declare function serializePassGraph(doc: PassGraph): string;
|
|
29
|
+
/** Compile the saved graph to a native pass. Resources are borrowed; disposing the pass leaves them alive. */
|
|
30
|
+
export declare function compilePassGraph(value: PassGraph, resources?: PassGraphResources): NodePass | ComputePass;
|
|
31
|
+
/** Options for the shared NodeEditor canvas in post/compute mode. */
|
|
32
|
+
export interface PassNodeEditorOptions {
|
|
33
|
+
/** Initial document; default a post graph that copies screen colour. */
|
|
34
|
+
document?: PassGraph;
|
|
35
|
+
/** Named, borrowed sources and targets, default none. */
|
|
36
|
+
resources?: PassGraphResources;
|
|
37
|
+
/** Default false. */
|
|
38
|
+
readOnly?: boolean;
|
|
39
|
+
/** Dispose the editor when aborted. */
|
|
40
|
+
signal?: AbortSignal;
|
|
41
|
+
}
|
|
42
|
+
/** Post and procedural compute authoring using NodeEditor's wiring, palette, undo, layout and code preview. */
|
|
43
|
+
export declare class PassNodeEditor {
|
|
44
|
+
private readonly parent;
|
|
45
|
+
private readonly options;
|
|
46
|
+
private view;
|
|
47
|
+
private doc;
|
|
48
|
+
private readonly resources;
|
|
49
|
+
private readonly listeners;
|
|
50
|
+
private stopped;
|
|
51
|
+
private stop?;
|
|
52
|
+
private abort?;
|
|
53
|
+
constructor(parent: HTMLElement, options?: PassNodeEditorOptions);
|
|
54
|
+
private live;
|
|
55
|
+
private mount;
|
|
56
|
+
/** The existing canvas and model, including keyboard shortcuts and undo/redo. */
|
|
57
|
+
get editor(): NodeEditor;
|
|
58
|
+
/** Independent validated document with the current layout. */
|
|
59
|
+
get document(): PassGraph;
|
|
60
|
+
/** Save graph, pass settings and positions; contains names rather than GPU handles. */
|
|
61
|
+
save(): string;
|
|
62
|
+
/** Validate and replace the document; failed loads keep the existing canvas and pass. */
|
|
63
|
+
load(value: string | PassGraph): void;
|
|
64
|
+
/** Called immediately and on valid graph edits, for live pass replacement; moves do not recompile. */
|
|
65
|
+
onGraph(listener: (doc: PassGraph) => void): () => void;
|
|
66
|
+
/** Build a pass from the current valid document; the caller owns that pass. */
|
|
67
|
+
compile(): NodePass | ComputePass;
|
|
68
|
+
/** Remove the canvas and listeners; borrowed GPU resources stay alive. */
|
|
69
|
+
dispose(): void;
|
|
70
|
+
[Symbol.dispose]: () => void;
|
|
71
|
+
}
|