@margoengine/shader 0.1.0 → 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 -201
- package/NOTICE +4 -4
- package/README.md +1920 -1771
- package/dist/attribute.d.ts +28 -0
- package/dist/black-hole.js +3 -3
- package/dist/clock-BLGNexlt.js +3 -3
- package/dist/{compute-D-BghbXj.js → compute-CXGG9qvI.js} +76 -70
- package/dist/compute-texture.js +3 -3
- package/dist/compute-volume.js +3 -3
- package/dist/compute.d.ts +8 -2
- package/dist/{control-DvjfV7R0.js → control-CFjkGPBk.js} +1 -1
- package/dist/deform.js +2 -2
- package/dist/editor-pass.js +17 -17
- package/dist/effects.js +2 -2
- package/dist/glsl-material.js +2 -2
- package/dist/glsl-pass.js +5 -5
- package/dist/glsl.js +18 -18
- package/dist/gpu-DQJk2xAv.js +55 -0
- package/dist/graph.js +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +103 -8
- package/dist/{lighting-Dx8_RsBz.js → lighting-JU9yQPfM.js} +2 -2
- package/dist/lighting.js +1 -1
- package/dist/{material-CU7p-Q8u.js → material-C6bpqlnE.js} +1 -1
- package/dist/{material-REzUrXtu.js → material-CZ8V1FeA.js} +95 -71
- package/dist/material.d.ts +9 -4
- package/dist/{node-DzKv9CF2.js → node-B6yoIC9V.js} +126 -126
- package/dist/node.d.ts +7 -1
- package/dist/noise.js +1 -1
- package/dist/pass.d.ts +1 -0
- package/dist/pass.js +24 -21
- package/dist/{post-DC62ZleD.js → post-DLpZ110L.js} +17 -17
- package/dist/{raw-D0W8U3Qa.js → raw-0AYej3Ak.js} +422 -273
- package/dist/raw.d.ts +125 -15
- package/dist/raw.js +1 -1
- package/dist/readback.js +13 -12
- package/dist/serialization.js +12 -12
- package/dist/shadertoy.js +6 -6
- package/dist/shared/caster.d.ts +159 -152
- package/dist/shared/fog.d.ts +33 -33
- package/dist/shared/fractal.d.ts +9 -9
- package/dist/shared/msaa.d.ts +30 -25
- package/dist/shared/stack.d.ts +22 -22
- package/dist/subgroups.js +4 -4
- package/dist/{texture-D6vy0bam.js → texture-BawKO_h9.js} +1 -1
- package/dist/texture-array.js +2 -2
- package/dist/utils.js +32 -32
- package/package.json +151 -151
- package/dist/gpu-DQ5Jmggz.js +0 -31
|
@@ -1,25 +1,35 @@
|
|
|
1
|
-
import { It as e, T as t, b as n, l as r, vt as i, wt as a } from "./node-
|
|
1
|
+
import { It as e, T as t, b as n, l as r, vt as i, wt as a } from "./node-B6yoIC9V.js";
|
|
2
2
|
import { t as o } from "./clock-BLGNexlt.js";
|
|
3
3
|
import { MargoError as s } from "@margoengine/math/errors";
|
|
4
4
|
import { Material as c } from "@margoengine/materials";
|
|
5
|
+
//#region ../shared/displacement.js
|
|
6
|
+
function l(e, t) {
|
|
7
|
+
let n = e.flatMap(({ resource: e }) => [
|
|
8
|
+
e.buffer ?? e,
|
|
9
|
+
e.offset,
|
|
10
|
+
e.size
|
|
11
|
+
]), r = n.length !== t.length || n.some((e, n) => e !== t[n]);
|
|
12
|
+
return t.splice(0, t.length, ...n), r;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
5
15
|
//#region src/material.ts
|
|
6
|
-
var
|
|
16
|
+
var u = /*@__PURE__*/ n("materialColor", "vec4f"), d = /*@__PURE__*/ n("materialNormal", "vec3f"), f = /*@__PURE__*/ n("materialRoughness", "f32"), p = /*@__PURE__*/ n("materialMetalness", "f32"), m = /*@__PURE__*/ n("materialEmissive", "vec3f"), h = /*@__PURE__*/ n("frontFacing", "bool"), g = 8, _ = (e, t, n) => (e.declare("margoTangent", "fn margoTangent(n:vec3f,t:vec3f)->vec3f {\n let a=select(vec3f(1,0,0),vec3f(0,0,1),abs(n.x)>0.5);\n let s=t-n*dot(n,t);\n return normalize(select(a-n*dot(n,a),s,dot(s,s)>0.000000000001));\n}"), `margoTangent(${t}, ${n})`), v = (e, t, n, r) => `(cross(${t}, ${_(e, t, n)}) * select(1.0, ${r}, abs(${r}) > 0.5))`, y = (e) => (e.declare("margoProjection", "fn margoProjection()->mat4x4f {\n let r=transpose(mat3x3f(f.view[0].xyz,f.view[1].xyz,f.view[2].xyz));\n return f.camera*mat4x4f(vec4f(r[0],0),vec4f(r[1],0),vec4f(r[2],0),vec4f(-(r*f.view[3].xyz),1));\n}"), "margoProjection()"), b = {
|
|
7
17
|
modelMatrix: "f.model",
|
|
8
18
|
viewMatrix: "f.view",
|
|
9
|
-
projectionMatrix:
|
|
19
|
+
projectionMatrix: y,
|
|
10
20
|
cameraNear: (e) => {
|
|
11
|
-
let t =
|
|
21
|
+
let t = y(e);
|
|
12
22
|
return `select(${t}[3].z/(${t}[2].z+1.0),(${t}[3].z-1.0)/${t}[2].z,${t}[3].w>0.5)`;
|
|
13
23
|
},
|
|
14
24
|
cameraFar: (e) => {
|
|
15
|
-
let t =
|
|
25
|
+
let t = y(e);
|
|
16
26
|
return `(${t}[3].z/${t}[2].z)`;
|
|
17
27
|
}
|
|
18
|
-
},
|
|
19
|
-
instanceMatrix:
|
|
20
|
-
instanceColor:
|
|
21
|
-
instanceUV:
|
|
22
|
-
},
|
|
28
|
+
}, x = (e, t, n) => (r) => (r.declare(e, `const ${e}:${t}=${n};`), e), S = {
|
|
29
|
+
instanceMatrix: x("margoInstanceMatrix", "mat4x4f", "mat4x4f(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)"),
|
|
30
|
+
instanceColor: x("margoInstanceColor", "vec4f", "vec4f(1)"),
|
|
31
|
+
instanceUV: x("margoInstanceUV", "vec4f", "vec4f(1,1,0,0)")
|
|
32
|
+
}, C = (e) => {
|
|
23
33
|
let t = `(f.model * vec4f(${e}, 1.0)).xyz`, n = "normalize((f.normalMatrix * vec4f(i.normal.xyz, 0.0)).xyz)", r = "(f.model * vec4f(i.tangent.xyz, 0.0)).xyz";
|
|
24
34
|
return {
|
|
25
35
|
kind: "vertex",
|
|
@@ -34,17 +44,17 @@ var l = /*@__PURE__*/ n("materialColor", "vec4f"), u = /*@__PURE__*/ n("material
|
|
|
34
44
|
viewDirection: `normalize(f.eye.xyz - ${t})`,
|
|
35
45
|
positionView: `(f.view * vec4f(${t}, 1.0)).xyz`,
|
|
36
46
|
normalView: "normalize((f.view * vec4f((f.normalMatrix * vec4f(i.normal.xyz, 0.0)).xyz, 0.0)).xyz)",
|
|
37
|
-
tangentLocal: (e) =>
|
|
38
|
-
tangentWorld: (e) =>
|
|
39
|
-
bitangentWorld: (e) =>
|
|
47
|
+
tangentLocal: (e) => _(e, "normalize(i.normal.xyz)", "i.tangent.xyz"),
|
|
48
|
+
tangentWorld: (e) => _(e, n, r),
|
|
49
|
+
bitangentWorld: (e) => v(e, n, r, "(i.tangent.w * f.coordsB.y)"),
|
|
40
50
|
linearDepth: `(-(f.view * vec4f(${t}, 1.0)).z)`,
|
|
41
51
|
vertexIndex: "i.ndVertex",
|
|
42
52
|
instanceIndex: "i.ndInstance",
|
|
43
|
-
...
|
|
44
|
-
...
|
|
53
|
+
...b,
|
|
54
|
+
...S
|
|
45
55
|
}
|
|
46
56
|
};
|
|
47
|
-
},
|
|
57
|
+
}, w = /*@__PURE__*/ C("i.position.xyz"), T = {
|
|
48
58
|
kind: "fragment",
|
|
49
59
|
builtins: {
|
|
50
60
|
uv: "i.uv.xy",
|
|
@@ -60,14 +70,14 @@ var l = /*@__PURE__*/ n("materialColor", "vec4f"), u = /*@__PURE__*/ n("material
|
|
|
60
70
|
positionView: "(f.view * vec4f(i.position, 1.0)).xyz",
|
|
61
71
|
normalView: "normalize((f.view * vec4f(normalize(i.normal), 0.0)).xyz)",
|
|
62
72
|
tangentLocal: (e) => e.stage.varying(e, n("tangentLocal", "vec3f")),
|
|
63
|
-
tangentWorld: (e) =>
|
|
64
|
-
bitangentWorld: (e) =>
|
|
73
|
+
tangentWorld: (e) => _(e, "normalize(i.normal)", "i.tangent.xyz"),
|
|
74
|
+
bitangentWorld: (e) => v(e, "normalize(i.normal)", "i.tangent.xyz", "i.tangent.w"),
|
|
65
75
|
linearDepth: "(-(f.view * vec4f(i.position, 1.0)).z)",
|
|
66
76
|
vertexIndex: (e) => e.stage.varying(e, n("vertexIndex", "u32")),
|
|
67
77
|
instanceIndex: (e) => e.stage.varying(e, n("instanceIndex", "u32")),
|
|
68
78
|
instanceColor: (e) => e.stage.varying(e, n("instanceColor", "vec4f")),
|
|
69
79
|
instanceUV: (e) => e.stage.varying(e, n("instanceUV", "vec4f")),
|
|
70
|
-
...
|
|
80
|
+
...b,
|
|
71
81
|
hdrTarget: "f.settings.w",
|
|
72
82
|
materialColor: "s.base",
|
|
73
83
|
materialNormal: "s.n",
|
|
@@ -79,23 +89,23 @@ var l = /*@__PURE__*/ n("materialColor", "vec4f"), u = /*@__PURE__*/ n("material
|
|
|
79
89
|
varying(e, t) {
|
|
80
90
|
let n = e.program.varyings, r = e.program.canon(t), i = n.findIndex((t) => e.program.canon(t) === r);
|
|
81
91
|
if (i < 0) {
|
|
82
|
-
if (n.length >=
|
|
92
|
+
if (n.length >= g) throw RangeError(`NodeMaterial supports at most ${g} varyings; use fewer varying nodes.`);
|
|
83
93
|
i = n.push(t) - 1;
|
|
84
94
|
}
|
|
85
95
|
return e.local("i.ndv" + i);
|
|
86
96
|
}
|
|
87
|
-
},
|
|
88
|
-
function
|
|
97
|
+
}, E = /if\((?:margoFaceTest&&)?!facing&&f\.settings\.z<0\.5\)\{discard;\}/, D = "let p=f.model*vec4f(i.position.xyz,1);", O = "o.normal=(f.normalMatrix*vec4f(i.normal.xyz,0)).xyz;", k = "let margoView=f.eye.xyz-i.position;let margoMesh=normalize(i.normal);let margoEdges=cross(dpdx(i.position),dpdy(i.position));\ns.n=normalize(s.n-select(-margoMesh,margoMesh,dot(margoMesh,margoView)>=0.0)+normalize(select(-margoEdges,margoEdges,dot(margoEdges,margoView)>=0.0)));", A = "let margoAcross=normalize(cross(i.normal.xyz,select(vec3f(1,0,0),vec3f(0,1,0),abs(i.normal.x)>0.9)));\nlet margoAlong=cross(i.normal.xyz,margoAcross);\nlet margoA=i.position.xyz+margoAcross*0.001;\nlet margoB=i.position.xyz+margoAlong*0.001;", j = (e, t, n) => `var ${e}=vec3f(0);{\n${t.join("\n")}\n${e}=${n};\n}`, M = (e) => e.frame("cameraEye", "vec3f"), N = (e) => e.frame("cameraView", "mat4x4f");
|
|
98
|
+
function P(e) {
|
|
89
99
|
let t = e?.canvas, n = o.find(e);
|
|
90
100
|
return {
|
|
91
101
|
width: t?.width ?? 1,
|
|
92
102
|
height: t?.height ?? 1,
|
|
93
103
|
time: n?.time ?? 0,
|
|
94
104
|
delta: n?.delta ?? 0,
|
|
95
|
-
frames:
|
|
105
|
+
frames: F(e)
|
|
96
106
|
};
|
|
97
107
|
}
|
|
98
|
-
function
|
|
108
|
+
function F(e) {
|
|
99
109
|
let t = e?.camera;
|
|
100
110
|
return t && {
|
|
101
111
|
cameraEye: t.position,
|
|
@@ -105,22 +115,22 @@ function P(e) {
|
|
|
105
115
|
cameraFar: [t.far]
|
|
106
116
|
};
|
|
107
117
|
}
|
|
108
|
-
function
|
|
118
|
+
function I(e, t) {
|
|
109
119
|
let r = (e) => () => (t[e] = !0, "vec3f(0)"), o = (e, t) => Object.fromEntries(Object.keys(e.builtins).map((e) => [e, r(t)])), s = (e) => (t) => t.stage.varying(t, e), c = (e, t) => ({
|
|
110
|
-
cameraPosition:
|
|
111
|
-
viewMatrix:
|
|
120
|
+
cameraPosition: M,
|
|
121
|
+
viewMatrix: N,
|
|
112
122
|
projectionMatrix: (e) => e.frame("cameraProjection", "mat4x4f"),
|
|
113
123
|
cameraNear: (e) => e.frame("cameraNear", "f32"),
|
|
114
124
|
cameraFar: (e) => e.frame("cameraFar", "f32"),
|
|
115
|
-
viewDirection: (t) => `normalize(${
|
|
116
|
-
positionView: (t) => `(${
|
|
117
|
-
normalView: (e) => `normalize((${
|
|
118
|
-
linearDepth: (t) => `(-(${
|
|
125
|
+
viewDirection: (t) => `normalize(${M(t)} - ${e(t)})`,
|
|
126
|
+
positionView: (t) => `(${N(t)} * vec4f(${e(t)}, 1.0)).xyz`,
|
|
127
|
+
normalView: (e) => `normalize((${N(e)} * vec4f(${t(e)}, 0.0)).xyz)`,
|
|
128
|
+
linearDepth: (t) => `(-(${N(t)} * vec4f(${e(t)}, 1.0)).z)`
|
|
119
129
|
});
|
|
120
130
|
return [{
|
|
121
131
|
kind: "vertex",
|
|
122
132
|
builtins: {
|
|
123
|
-
...o(
|
|
133
|
+
...o(w, "vertex"),
|
|
124
134
|
uv: "i.uv.xy",
|
|
125
135
|
vertexColor: "i.color",
|
|
126
136
|
positionLocal: "i.position.xyz",
|
|
@@ -134,7 +144,7 @@ function F(e, t) {
|
|
|
134
144
|
}, {
|
|
135
145
|
kind: "fragment",
|
|
136
146
|
builtins: {
|
|
137
|
-
...o(
|
|
147
|
+
...o(T, "fragment"),
|
|
138
148
|
uv: s(n("uv", "vec2f")),
|
|
139
149
|
vertexColor: s(n("vertexColor", "vec4f")),
|
|
140
150
|
positionWorld: s(n("positionWorld", "vec3f")),
|
|
@@ -143,23 +153,23 @@ function F(e, t) {
|
|
|
143
153
|
normalLocal: s(i),
|
|
144
154
|
vertexIndex: s(n("vertexIndex", "u32")),
|
|
145
155
|
instanceIndex: s(n("instanceIndex", "u32")),
|
|
146
|
-
screenCoordinate:
|
|
147
|
-
screenUV:
|
|
156
|
+
screenCoordinate: T.builtins.screenCoordinate,
|
|
157
|
+
screenUV: T.builtins.screenUV,
|
|
148
158
|
frontFacing: "front",
|
|
149
159
|
...c((e) => e.stage.varying(e, n("positionWorld", "vec3f")), (e) => `normalize(${e.stage.varying(e, n("normalWorld", "vec3f"))})`)
|
|
150
160
|
},
|
|
151
161
|
varying(t, n) {
|
|
152
162
|
let r = t.program.canon(n), i = e.findIndex((e) => t.program.canon(e) === r);
|
|
153
163
|
if (i < 0) {
|
|
154
|
-
if (e.length >=
|
|
164
|
+
if (e.length >= g) throw RangeError(`NodeMaterial supports at most ${g} varyings; use fewer varying nodes.`);
|
|
155
165
|
i = e.push(n) - 1;
|
|
156
166
|
}
|
|
157
167
|
return t.local("i.ndv" + i);
|
|
158
168
|
}
|
|
159
169
|
}];
|
|
160
170
|
}
|
|
161
|
-
var
|
|
162
|
-
function
|
|
171
|
+
var L = (e) => (e.includes("i.ndVertex") ? ",\n @builtin(vertex_index) ndVertex:u32" : "") + (e.includes("i.ndInstance") ? ",\n @builtin(instance_index) ndInstance:u32" : ""), R = (t) => t.map((t, n) => `,\n @location(${8 + n})${e(t.type) === "f32" ? "" : " @interpolate(flat)"} ndv${n}:${t.type}`).join(""), z = 0, B = 2;
|
|
172
|
+
function V(e, t, n) {
|
|
163
173
|
let r = /^texture_(depth_)?(multisampled_)?(2d_array|cube_array|2d|3d|cube)(?:<(\w+)>)?/.exec(n), i = /^texture_storage_(2d_array|2d|3d)<(\w+), ?(\w+)>/.exec(n);
|
|
164
174
|
return t ? {
|
|
165
175
|
binding: e,
|
|
@@ -167,7 +177,7 @@ function B(e, t, n) {
|
|
|
167
177
|
buffer: { type: t.includes("uniform") ? "uniform" : "read-only-storage" }
|
|
168
178
|
} : i ? {
|
|
169
179
|
binding: e,
|
|
170
|
-
visibility:
|
|
180
|
+
visibility: B,
|
|
171
181
|
storageTexture: {
|
|
172
182
|
format: i[2],
|
|
173
183
|
access: i[3] === "read" ? "read-only" : i[3] === "write" ? "write-only" : "read-write",
|
|
@@ -187,15 +197,15 @@ function B(e, t, n) {
|
|
|
187
197
|
sampler: { type: n.includes("comparison") ? "comparison" : "filtering" }
|
|
188
198
|
};
|
|
189
199
|
}
|
|
190
|
-
function
|
|
200
|
+
function H(e, t) {
|
|
191
201
|
let n = /* @__PURE__ */ new Map();
|
|
192
|
-
for (let [, r, i, a, o] of e.matchAll(/@binding\((\d+)\) var(<[^>]*>)? (\w+) ?: ?([^;\n]+);/g)) (t + e).match(RegExp(`\\b${a}\\b`, "g")).length > 1 && n.set(+r,
|
|
202
|
+
for (let [, r, i, a, o] of e.matchAll(/@binding\((\d+)\) var(<[^>]*>)? (\w+) ?: ?([^;\n]+);/g)) (t + e).match(RegExp(`\\b${a}\\b`, "g")).length > 1 && n.set(+r, V(+r, i, o));
|
|
193
203
|
return n;
|
|
194
204
|
}
|
|
195
|
-
var
|
|
205
|
+
var U = class extends c {
|
|
196
206
|
constructor(e = {}, n) {
|
|
197
207
|
let { colorNode: i, opacityNode: a, roughnessNode: o, metalnessNode: s, emissiveNode: c, normalNode: l, positionNode: u, positionNormals: d, ...f } = e;
|
|
198
|
-
if (super(f), this.program = new r(48), this.patched = /* @__PURE__ */ new Map(), i !== void 0 && i?.type !== "vec3f" && i?.type !== "vec4f") throw TypeError("colorNode must be a vec3f or vec4f node");
|
|
208
|
+
if (super(f), this.program = new r(48), this.patched = /* @__PURE__ */ new Map(), this.depthRecords = /* @__PURE__ */ new WeakMap(), i !== void 0 && i?.type !== "vec3f" && i?.type !== "vec4f") throw TypeError("colorNode must be a vec3f or vec4f node");
|
|
199
209
|
let p = [
|
|
200
210
|
[i, (e) => i.type === "vec4f" ? `s.base = ${e};` : `s.base = vec4f(${e}, s.base.a);`],
|
|
201
211
|
[a === void 0 ? void 0 : t(a, "f32", "opacityNode"), (e) => `s.base.a = ${e};`],
|
|
@@ -203,15 +213,15 @@ var H = class extends c {
|
|
|
203
213
|
[s === void 0 ? void 0 : t(s, "f32", "metalnessNode"), (e) => `s.metal = clamp(${e}, 0.0, 1.0);`],
|
|
204
214
|
[c && t(c, "vec3f", "emissiveNode"), (e) => `s.emissive = ${e};`],
|
|
205
215
|
[l && t(l, "vec3f", "normalNode"), (e) => `s.n = normalize(${e});`]
|
|
206
|
-
].filter((e) => e[0]), m = this.program.stage(
|
|
216
|
+
].filter((e) => e[0]), m = this.program.stage(T, p.map((e) => e[0]));
|
|
207
217
|
n?.(this.program);
|
|
208
|
-
let h = u && t(u, "vec3f", "positionNode"), g = this.program.stage(
|
|
218
|
+
let h = u && t(u, "vec3f", "positionNode"), g = this.program.stage(w, [...h ? [h] : [], ...this.program.varyings]), _ = [...g.values], v = h ? _.shift() : "i.position.xyz", y = h && this.program.stage(C("margoA"), [h]), b = y ? d ?? (/i\.uv/.test(y.lines.join("\n") + y.values[0]) ? "faces" : "neighbours") : "keep", x = [];
|
|
209
219
|
if (y && b === "neighbours") {
|
|
210
|
-
let e = this.program.stage(
|
|
211
|
-
x.push(
|
|
220
|
+
let e = this.program.stage(C("margoB"), [h]);
|
|
221
|
+
x.push(A, j("margoAt", y.lines, y.values[0]), j("margoUp", e.lines, e.values[0]), `let margoCross=cross(margoAt-(${v}),margoUp-(${v}));`, "let margoNormal=select(i.normal.xyz,normalize(margoCross),dot(margoCross,margoCross)>1e-20);");
|
|
212
222
|
}
|
|
213
223
|
this.rederived = x.length > 0, this.fragmentCode = [
|
|
214
|
-
...b === "faces" && !l ? [
|
|
224
|
+
...b === "faces" && !l ? [k] : [],
|
|
215
225
|
...m.lines,
|
|
216
226
|
...p.map((e, t) => e[1](m.values[t]))
|
|
217
227
|
].map((e) => " " + e + "\n").join(""), this.vertexCode = [
|
|
@@ -219,57 +229,71 @@ var H = class extends c {
|
|
|
219
229
|
...x,
|
|
220
230
|
`let p=f.model*vec4f(${v},1);`,
|
|
221
231
|
..._.map((e, t) => `o.ndv${t} = ${e};`)
|
|
222
|
-
].map((e) => " " + e + "\n").join(""), this.varyingFields =
|
|
223
|
-
let
|
|
232
|
+
].map((e) => " " + e + "\n").join(""), this.varyingFields = R(this.program.varyings), this.colourBindings = this.program.next;
|
|
233
|
+
let S = [], E = {
|
|
224
234
|
vertex: !1,
|
|
225
235
|
fragment: !1
|
|
226
|
-
}, [D,
|
|
227
|
-
this.depthFragment = N ? M : "", N || (
|
|
228
|
-
let P = h && this.program.stage(D, [h]),
|
|
236
|
+
}, [D, O] = I(S, E), M = this.program.stage(O, p.map((e) => e[0])).lines.join("\n"), N = !E.fragment && M.includes("discard;");
|
|
237
|
+
this.depthFragment = N ? M : "", N || (S.length = 0);
|
|
238
|
+
let P = h && this.program.stage(D, [h]), F = P && !E.vertex ? [...P.lines, `margoPosition=${P.values[0]};`] : [];
|
|
229
239
|
E.vertex = !1;
|
|
230
|
-
let
|
|
231
|
-
E.vertex && (this.depthFragment = "",
|
|
240
|
+
let L = this.program.stage(D, S);
|
|
241
|
+
E.vertex && (this.depthFragment = "", S.length = 0), this.depthFields = R(S), this.depthMove = F, this.depthVertex = F.length || S.length ? `{\n${F.join("\n")}\n}\n{\n${[...S.length ? L.lines : [], ...S.length ? L.values.map((e, t) => `o.ndv${t}=${e};`) : []].join("\n")}\n}` : "";
|
|
232
242
|
}
|
|
233
243
|
shader(e, t) {
|
|
234
244
|
let n = super.shader(e, t), r = t + n, i = this.patched.get(r);
|
|
235
245
|
if (i) return i;
|
|
236
246
|
let a = 0;
|
|
237
|
-
i = n.replace(
|
|
238
|
-
let o =
|
|
247
|
+
i = n.replace(E, (e) => (a++, this.fragmentCode.trimStart() + " " + e)).replace(D, () => (a++, this.vertexCode.trim())), this.rederived && (i = i.replace(O, () => (a++, "o.normal=(f.normalMatrix*vec4f(margoNormal,0)).xyz;")));
|
|
248
|
+
let o = L(this.vertexCode);
|
|
239
249
|
if (o && (i = i.replace(/(struct Input \{[^}]*?)\n\}/, (e, t) => (a++, t + o + "\n}"))), this.varyingFields && (i = i.replace(/(struct Out \{[^}]*?)\n\}/, (e, t) => (a++, t + this.varyingFields + "\n}"))), a !== 2 + +!!o + +!!this.varyingFields + +!!this.rederived) throw new s("unsupported", "NodeMaterial requires the standard gltf/pbr shader; use matching @margoengine/gltf/pbr and shader versions");
|
|
240
250
|
return i = this.program.declare(t) + "\n" + i, this.patched.set(r, i), i;
|
|
241
251
|
}
|
|
242
252
|
depthShader(e, t) {
|
|
243
253
|
let n = /var o:(\w+);/.exec(e)?.[1], r = /(\w+\.model)\*vec4f\(i\.position\.xyz,1\)/.exec(e)?.[1], i = /@vertex fn vertex\(i:(\w+)\)/.exec(e)?.[1];
|
|
244
254
|
if (!n || !r || !i) throw new s("unsupported", "NodeMaterial requires a standard caster depth shader; use matching @margoengine/shader and effect package versions");
|
|
245
|
-
return this.program.declare(t) + "\n" + e.replace("vec4f(i.position.xyz,1)", "vec4f(margoPosition,1)").replace(`var o:${n};`, () => `var o:${n};let margoWorld=(${r}*vec4f(i.position.xyz,1)).xyz;let margoWorldNormal=normalize((${r}*vec4f(i.normal.xyz,0)).xyz);var margoPosition=i.position.xyz;{\n${this.depthVertex}\n}`).replace(RegExp(`(struct ${n} \\{[^}]*)\\}`), (e, t) => t + this.depthFields + "}").replace(RegExp(`(struct ${i} \\{[^}]*)\\}`), (e, t) => t +
|
|
255
|
+
return this.program.declare(t) + "\n" + e.replace("vec4f(i.position.xyz,1)", "vec4f(margoPosition,1)").replace(`var o:${n};`, () => `var o:${n};let margoWorld=(${r}*vec4f(i.position.xyz,1)).xyz;let margoWorldNormal=normalize((${r}*vec4f(i.normal.xyz,0)).xyz);var margoPosition=i.position.xyz;{\n${this.depthVertex}\n}`).replace(RegExp(`(struct ${n} \\{[^}]*)\\}`), (e, t) => t + this.depthFields + "}").replace(RegExp(`(struct ${i} \\{[^}]*)\\}`), (e, t) => t + L(this.depthVertex) + "}").replace(/@fragment fn fragment\((i:\w+)\)([^{]*)\{/, (e, t, n) => `@fragment fn fragment(${t},@builtin(front_facing) front:bool)${n}{\n${this.depthFragment}\n`);
|
|
246
256
|
}
|
|
247
257
|
velocityShader(e, t) {
|
|
248
258
|
let n = "{var o:Out;";
|
|
249
259
|
if (!e.includes(n) || !e.includes("@vertex fn vertex(i:Input,@builtin(vertex_index) index:u32)")) throw new s("unsupported", "NodeMaterial requires the standard velocity shader; use matching @margoengine/shader and @margoengine/taa versions");
|
|
250
|
-
return this.program.declare(t) + "\n" + e.replace(n, () => `${n}let margoWorld=(f.model*vec4f(i.position.xyz,1)).xyz;let margoWorldNormal=normalize((f.model*vec4f(i.normal.xyz,0)).xyz);var margoPosition=i.position.xyz;{\n${this.depthVertex.replaceAll("i.ndVertex", "index")}\n}`).replace(/(struct Out \{[^}]*)\}/, (e, t) => t + this.depthFields + "}").replace(/(struct Input \{[^}]*)\}/, (e, t) => t + (this.depthVertex.includes("i.ndInstance") ?
|
|
260
|
+
return this.program.declare(t) + "\n" + e.replace(n, () => `${n}let margoWorld=(f.model*vec4f(i.position.xyz,1)).xyz;let margoWorldNormal=normalize((f.model*vec4f(i.normal.xyz,0)).xyz);var margoPosition=i.position.xyz;{\n${this.depthVertex.replaceAll("i.ndVertex", "index")}\n}`).replace(/(struct Out \{[^}]*)\}/, (e, t) => t + this.depthFields + "}").replace(/(struct Input \{[^}]*)\}/, (e, t) => t + (this.depthVertex.includes("i.ndInstance") ? L("i.ndInstance") : "") + "}").replace(/@fragment fn fragment\((i:\w+)\)([^{]*)\{/, (e, t, n) => `@fragment fn fragment(${t},@builtin(front_facing) front:bool)${n}{\n${this.depthFragment}\n`);
|
|
261
|
+
}
|
|
262
|
+
meshData(e) {
|
|
263
|
+
return this.program.attributes?.read(e);
|
|
251
264
|
}
|
|
252
|
-
caster(e, t) {
|
|
265
|
+
caster(e, t, n, r) {
|
|
253
266
|
if (!this.depthVertex) return;
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
267
|
+
let i = this.depthRecords.get(r ?? this);
|
|
268
|
+
if (i?.device === e) return i.bound.length && l(i.record.entries(), i.bound) && (i.record = { ...i.record }), i.record;
|
|
269
|
+
let a = [], o = this.depthVertex + this.depthFragment, s = H(this.program.declare(0), o), c, u = {
|
|
270
|
+
key: this.depthKey ??= `n${++z}`,
|
|
257
271
|
cuts: !!this.depthFragment,
|
|
258
272
|
depth: (e, t) => this.depthShader(e, t),
|
|
259
273
|
velocity: (e, t) => this.velocityShader(e, t),
|
|
260
274
|
move: this.depthMove,
|
|
261
275
|
program: this.program,
|
|
262
|
-
layout: () =>
|
|
263
|
-
entries: () =>
|
|
276
|
+
layout: () => c ??= e.createBindGroupLayout({ entries: [...s.values()] }),
|
|
277
|
+
entries: () => {
|
|
278
|
+
let n = this.program.entries(e, {
|
|
279
|
+
...P(t),
|
|
280
|
+
meshData: r
|
|
281
|
+
}).filter((e) => s.has(e.binding));
|
|
282
|
+
return a.length || l(n, a), n;
|
|
283
|
+
}
|
|
264
284
|
};
|
|
265
|
-
return this.
|
|
285
|
+
return this.depthRecords.set(r ?? this, {
|
|
266
286
|
device: e,
|
|
267
|
-
record:
|
|
268
|
-
|
|
287
|
+
record: u,
|
|
288
|
+
bound: a
|
|
289
|
+
}), u;
|
|
269
290
|
}
|
|
270
|
-
bindings(e, t) {
|
|
271
|
-
return [...super.bindings(e, t), ...this.program.entries(e,
|
|
291
|
+
bindings(e, t, n) {
|
|
292
|
+
return [...super.bindings(e, t), ...this.program.entries(e, {
|
|
293
|
+
...P(t),
|
|
294
|
+
meshData: n
|
|
295
|
+
}).filter((e) => e.binding < this.colourBindings)];
|
|
272
296
|
}
|
|
273
297
|
};
|
|
274
298
|
//#endregion
|
|
275
|
-
export {
|
|
299
|
+
export { m as a, f as c, u as i, T as n, p as o, h as r, d as s, U as t };
|
package/dist/material.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Material, type MaterialOptions } from '@margoengine/materials';
|
|
1
|
+
import { Material, type MaterialMeshData, type MaterialOptions, type MeshGeometry } from '@margoengine/materials';
|
|
2
2
|
import type { CasterDisplacement } from './shared/caster.js';
|
|
3
3
|
import type { SurfaceGraph } from './graph.js';
|
|
4
4
|
import type { Engine } from '@margoengine/core/extension';
|
|
@@ -56,7 +56,10 @@ export declare class NodeMaterial extends Material {
|
|
|
56
56
|
private readonly depthMove;
|
|
57
57
|
private depthFragment;
|
|
58
58
|
private depthFields;
|
|
59
|
-
|
|
59
|
+
/** One depth record per mesh that has data of its own (else the material's), for the device it was made on. */
|
|
60
|
+
private readonly depthRecords;
|
|
61
|
+
/** The pipeline key of every depth record: their shader and layout are the same. */
|
|
62
|
+
private depthKey?;
|
|
60
63
|
private readonly colourBindings;
|
|
61
64
|
/** `extra` is for subclasses: it compiles nodes of its own after the fragment stage and before the vertex stage. */
|
|
62
65
|
constructor(options?: NodeMaterialOptions, extra?: (program: Program) => void);
|
|
@@ -68,7 +71,9 @@ export declare class NodeMaterial extends Material {
|
|
|
68
71
|
* this graph. A graph that changes between frames adds no velocity of its own: a surface moves as the mesh and camera do.
|
|
69
72
|
*/
|
|
70
73
|
private velocityShader;
|
|
74
|
+
/** @internal The mesh attributes this graph reads with `attribute()`, checked and packed for one mesh. */
|
|
75
|
+
meshData(geometry: MeshGeometry): MaterialMeshData | undefined;
|
|
71
76
|
/** @internal What a depth pass draws: the record of gltf\/pbr's `displacement`, undefined for an unchanged shape. */
|
|
72
|
-
caster(device: GPUDevice, engine?: Engine): CasterDisplacement | undefined;
|
|
73
|
-
bindings(device: GPUDevice, engine?: Engine): GPUBindGroupEntry[];
|
|
77
|
+
caster(device: GPUDevice, engine?: Engine, _map?: CasterDisplacement, data?: MaterialMeshData): CasterDisplacement | undefined;
|
|
78
|
+
bindings(device: GPUDevice, engine?: Engine, data?: MaterialMeshData): GPUBindGroupEntry[];
|
|
74
79
|
}
|