@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
|
@@ -0,0 +1,299 @@
|
|
|
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
|
+
import { t as o } from "./clock-BLGNexlt.js";
|
|
3
|
+
import { MargoError as s } from "@margoengine/math/errors";
|
|
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
|
|
15
|
+
//#region src/material.ts
|
|
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 = {
|
|
17
|
+
modelMatrix: "f.model",
|
|
18
|
+
viewMatrix: "f.view",
|
|
19
|
+
projectionMatrix: y,
|
|
20
|
+
cameraNear: (e) => {
|
|
21
|
+
let t = y(e);
|
|
22
|
+
return `select(${t}[3].z/(${t}[2].z+1.0),(${t}[3].z-1.0)/${t}[2].z,${t}[3].w>0.5)`;
|
|
23
|
+
},
|
|
24
|
+
cameraFar: (e) => {
|
|
25
|
+
let t = y(e);
|
|
26
|
+
return `(${t}[3].z/${t}[2].z)`;
|
|
27
|
+
}
|
|
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) => {
|
|
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";
|
|
34
|
+
return {
|
|
35
|
+
kind: "vertex",
|
|
36
|
+
builtins: {
|
|
37
|
+
uv: "i.uv.xy",
|
|
38
|
+
vertexColor: "i.color",
|
|
39
|
+
positionLocal: e,
|
|
40
|
+
normalLocal: "i.normal.xyz",
|
|
41
|
+
positionWorld: t,
|
|
42
|
+
normalWorld: n,
|
|
43
|
+
cameraPosition: "f.eye.xyz",
|
|
44
|
+
viewDirection: `normalize(f.eye.xyz - ${t})`,
|
|
45
|
+
positionView: `(f.view * vec4f(${t}, 1.0)).xyz`,
|
|
46
|
+
normalView: "normalize((f.view * vec4f((f.normalMatrix * vec4f(i.normal.xyz, 0.0)).xyz, 0.0)).xyz)",
|
|
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)"),
|
|
50
|
+
linearDepth: `(-(f.view * vec4f(${t}, 1.0)).z)`,
|
|
51
|
+
vertexIndex: "i.ndVertex",
|
|
52
|
+
instanceIndex: "i.ndInstance",
|
|
53
|
+
...b,
|
|
54
|
+
...S
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}, w = /*@__PURE__*/ C("i.position.xyz"), T = {
|
|
58
|
+
kind: "fragment",
|
|
59
|
+
builtins: {
|
|
60
|
+
uv: "i.uv.xy",
|
|
61
|
+
vertexColor: "i.color",
|
|
62
|
+
positionWorld: "i.position",
|
|
63
|
+
normalWorld: "normalize(i.normal)",
|
|
64
|
+
cameraPosition: "f.eye.xyz",
|
|
65
|
+
viewDirection: "normalize(f.eye.xyz - i.position)",
|
|
66
|
+
screenCoordinate: "i.clip.xy",
|
|
67
|
+
screenUV: (e) => `(${e.local("i.clip.xy")} / ${e.frame("resolution", "vec2f")})`,
|
|
68
|
+
positionLocal: (e) => e.stage.varying(e, a),
|
|
69
|
+
normalLocal: (e) => e.stage.varying(e, i),
|
|
70
|
+
positionView: "(f.view * vec4f(i.position, 1.0)).xyz",
|
|
71
|
+
normalView: "normalize((f.view * vec4f(normalize(i.normal), 0.0)).xyz)",
|
|
72
|
+
tangentLocal: (e) => e.stage.varying(e, n("tangentLocal", "vec3f")),
|
|
73
|
+
tangentWorld: (e) => _(e, "normalize(i.normal)", "i.tangent.xyz"),
|
|
74
|
+
bitangentWorld: (e) => v(e, "normalize(i.normal)", "i.tangent.xyz", "i.tangent.w"),
|
|
75
|
+
linearDepth: "(-(f.view * vec4f(i.position, 1.0)).z)",
|
|
76
|
+
vertexIndex: (e) => e.stage.varying(e, n("vertexIndex", "u32")),
|
|
77
|
+
instanceIndex: (e) => e.stage.varying(e, n("instanceIndex", "u32")),
|
|
78
|
+
instanceColor: (e) => e.stage.varying(e, n("instanceColor", "vec4f")),
|
|
79
|
+
instanceUV: (e) => e.stage.varying(e, n("instanceUV", "vec4f")),
|
|
80
|
+
...b,
|
|
81
|
+
hdrTarget: "f.settings.w",
|
|
82
|
+
materialColor: "s.base",
|
|
83
|
+
materialNormal: "s.n",
|
|
84
|
+
materialRoughness: "s.roughness",
|
|
85
|
+
materialMetalness: "s.metal",
|
|
86
|
+
materialEmissive: "s.emissive",
|
|
87
|
+
frontFacing: "front"
|
|
88
|
+
},
|
|
89
|
+
varying(e, t) {
|
|
90
|
+
let n = e.program.varyings, r = e.program.canon(t), i = n.findIndex((t) => e.program.canon(t) === r);
|
|
91
|
+
if (i < 0) {
|
|
92
|
+
if (n.length >= g) throw RangeError(`NodeMaterial supports at most ${g} varyings; use fewer varying nodes.`);
|
|
93
|
+
i = n.push(t) - 1;
|
|
94
|
+
}
|
|
95
|
+
return e.local("i.ndv" + i);
|
|
96
|
+
}
|
|
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) {
|
|
99
|
+
let t = e?.canvas, n = o.find(e);
|
|
100
|
+
return {
|
|
101
|
+
width: t?.width ?? 1,
|
|
102
|
+
height: t?.height ?? 1,
|
|
103
|
+
time: n?.time ?? 0,
|
|
104
|
+
delta: n?.delta ?? 0,
|
|
105
|
+
frames: F(e)
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function F(e) {
|
|
109
|
+
let t = e?.camera;
|
|
110
|
+
return t && {
|
|
111
|
+
cameraEye: t.position,
|
|
112
|
+
cameraView: t.view,
|
|
113
|
+
cameraProjection: t.projection,
|
|
114
|
+
cameraNear: [t.near],
|
|
115
|
+
cameraFar: [t.far]
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function I(e, t) {
|
|
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) => ({
|
|
120
|
+
cameraPosition: M,
|
|
121
|
+
viewMatrix: N,
|
|
122
|
+
projectionMatrix: (e) => e.frame("cameraProjection", "mat4x4f"),
|
|
123
|
+
cameraNear: (e) => e.frame("cameraNear", "f32"),
|
|
124
|
+
cameraFar: (e) => e.frame("cameraFar", "f32"),
|
|
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)`
|
|
129
|
+
});
|
|
130
|
+
return [{
|
|
131
|
+
kind: "vertex",
|
|
132
|
+
builtins: {
|
|
133
|
+
...o(w, "vertex"),
|
|
134
|
+
uv: "i.uv.xy",
|
|
135
|
+
vertexColor: "i.color",
|
|
136
|
+
positionLocal: "i.position.xyz",
|
|
137
|
+
normalLocal: "i.normal.xyz",
|
|
138
|
+
positionWorld: "margoWorld",
|
|
139
|
+
normalWorld: "margoWorldNormal",
|
|
140
|
+
vertexIndex: "i.ndVertex",
|
|
141
|
+
instanceIndex: "i.ndInstance",
|
|
142
|
+
...c(() => "margoWorld", () => "margoWorldNormal")
|
|
143
|
+
}
|
|
144
|
+
}, {
|
|
145
|
+
kind: "fragment",
|
|
146
|
+
builtins: {
|
|
147
|
+
...o(T, "fragment"),
|
|
148
|
+
uv: s(n("uv", "vec2f")),
|
|
149
|
+
vertexColor: s(n("vertexColor", "vec4f")),
|
|
150
|
+
positionWorld: s(n("positionWorld", "vec3f")),
|
|
151
|
+
normalWorld: (e) => `normalize(${e.stage.varying(e, n("normalWorld", "vec3f"))})`,
|
|
152
|
+
positionLocal: s(a),
|
|
153
|
+
normalLocal: s(i),
|
|
154
|
+
vertexIndex: s(n("vertexIndex", "u32")),
|
|
155
|
+
instanceIndex: s(n("instanceIndex", "u32")),
|
|
156
|
+
screenCoordinate: T.builtins.screenCoordinate,
|
|
157
|
+
screenUV: T.builtins.screenUV,
|
|
158
|
+
frontFacing: "front",
|
|
159
|
+
...c((e) => e.stage.varying(e, n("positionWorld", "vec3f")), (e) => `normalize(${e.stage.varying(e, n("normalWorld", "vec3f"))})`)
|
|
160
|
+
},
|
|
161
|
+
varying(t, n) {
|
|
162
|
+
let r = t.program.canon(n), i = e.findIndex((e) => t.program.canon(e) === r);
|
|
163
|
+
if (i < 0) {
|
|
164
|
+
if (e.length >= g) throw RangeError(`NodeMaterial supports at most ${g} varyings; use fewer varying nodes.`);
|
|
165
|
+
i = e.push(n) - 1;
|
|
166
|
+
}
|
|
167
|
+
return t.local("i.ndv" + i);
|
|
168
|
+
}
|
|
169
|
+
}];
|
|
170
|
+
}
|
|
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) {
|
|
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);
|
|
174
|
+
return t ? {
|
|
175
|
+
binding: e,
|
|
176
|
+
visibility: 3,
|
|
177
|
+
buffer: { type: t.includes("uniform") ? "uniform" : "read-only-storage" }
|
|
178
|
+
} : i ? {
|
|
179
|
+
binding: e,
|
|
180
|
+
visibility: B,
|
|
181
|
+
storageTexture: {
|
|
182
|
+
format: i[2],
|
|
183
|
+
access: i[3] === "read" ? "read-only" : i[3] === "write" ? "write-only" : "read-write",
|
|
184
|
+
viewDimension: i[1].replace("_", "-")
|
|
185
|
+
}
|
|
186
|
+
} : r ? {
|
|
187
|
+
binding: e,
|
|
188
|
+
visibility: 3,
|
|
189
|
+
texture: {
|
|
190
|
+
sampleType: r[1] ? "depth" : r[4] === "u32" ? "uint" : r[4] === "i32" ? "sint" : "float",
|
|
191
|
+
viewDimension: r[3].replace("_", "-"),
|
|
192
|
+
multisampled: !!r[2]
|
|
193
|
+
}
|
|
194
|
+
} : {
|
|
195
|
+
binding: e,
|
|
196
|
+
visibility: 3,
|
|
197
|
+
sampler: { type: n.includes("comparison") ? "comparison" : "filtering" }
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function H(e, t) {
|
|
201
|
+
let n = /* @__PURE__ */ new Map();
|
|
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));
|
|
203
|
+
return n;
|
|
204
|
+
}
|
|
205
|
+
var U = class extends c {
|
|
206
|
+
constructor(e = {}, n) {
|
|
207
|
+
let { colorNode: i, opacityNode: a, roughnessNode: o, metalnessNode: s, emissiveNode: c, normalNode: l, positionNode: u, positionNormals: d, ...f } = e;
|
|
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");
|
|
209
|
+
let p = [
|
|
210
|
+
[i, (e) => i.type === "vec4f" ? `s.base = ${e};` : `s.base = vec4f(${e}, s.base.a);`],
|
|
211
|
+
[a === void 0 ? void 0 : t(a, "f32", "opacityNode"), (e) => `s.base.a = ${e};`],
|
|
212
|
+
[o === void 0 ? void 0 : t(o, "f32", "roughnessNode"), (e) => `s.roughness = clamp(${e}, 0.045, 1.0);`],
|
|
213
|
+
[s === void 0 ? void 0 : t(s, "f32", "metalnessNode"), (e) => `s.metal = clamp(${e}, 0.0, 1.0);`],
|
|
214
|
+
[c && t(c, "vec3f", "emissiveNode"), (e) => `s.emissive = ${e};`],
|
|
215
|
+
[l && t(l, "vec3f", "normalNode"), (e) => `s.n = normalize(${e});`]
|
|
216
|
+
].filter((e) => e[0]), m = this.program.stage(T, p.map((e) => e[0]));
|
|
217
|
+
n?.(this.program);
|
|
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 = [];
|
|
219
|
+
if (y && b === "neighbours") {
|
|
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);");
|
|
222
|
+
}
|
|
223
|
+
this.rederived = x.length > 0, this.fragmentCode = [
|
|
224
|
+
...b === "faces" && !l ? [k] : [],
|
|
225
|
+
...m.lines,
|
|
226
|
+
...p.map((e, t) => e[1](m.values[t]))
|
|
227
|
+
].map((e) => " " + e + "\n").join(""), this.vertexCode = [
|
|
228
|
+
...g.lines,
|
|
229
|
+
...x,
|
|
230
|
+
`let p=f.model*vec4f(${v},1);`,
|
|
231
|
+
..._.map((e, t) => `o.ndv${t} = ${e};`)
|
|
232
|
+
].map((e) => " " + e + "\n").join(""), this.varyingFields = R(this.program.varyings), this.colourBindings = this.program.next;
|
|
233
|
+
let S = [], E = {
|
|
234
|
+
vertex: !1,
|
|
235
|
+
fragment: !1
|
|
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]};`] : [];
|
|
239
|
+
E.vertex = !1;
|
|
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}` : "";
|
|
242
|
+
}
|
|
243
|
+
shader(e, t) {
|
|
244
|
+
let n = super.shader(e, t), r = t + n, i = this.patched.get(r);
|
|
245
|
+
if (i) return i;
|
|
246
|
+
let a = 0;
|
|
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);
|
|
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");
|
|
250
|
+
return i = this.program.declare(t) + "\n" + i, this.patched.set(r, i), i;
|
|
251
|
+
}
|
|
252
|
+
depthShader(e, t) {
|
|
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];
|
|
254
|
+
if (!n || !r || !i) throw new s("unsupported", "NodeMaterial requires a standard caster depth shader; use matching @margoengine/shader and effect package versions");
|
|
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`);
|
|
256
|
+
}
|
|
257
|
+
velocityShader(e, t) {
|
|
258
|
+
let n = "{var o:Out;";
|
|
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");
|
|
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);
|
|
264
|
+
}
|
|
265
|
+
caster(e, t, n, r) {
|
|
266
|
+
if (!this.depthVertex) return;
|
|
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}`,
|
|
271
|
+
cuts: !!this.depthFragment,
|
|
272
|
+
depth: (e, t) => this.depthShader(e, t),
|
|
273
|
+
velocity: (e, t) => this.velocityShader(e, t),
|
|
274
|
+
move: this.depthMove,
|
|
275
|
+
program: this.program,
|
|
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
|
+
}
|
|
284
|
+
};
|
|
285
|
+
return this.depthRecords.set(r ?? this, {
|
|
286
|
+
device: e,
|
|
287
|
+
record: u,
|
|
288
|
+
bound: a
|
|
289
|
+
}), u;
|
|
290
|
+
}
|
|
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)];
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
//#endregion
|
|
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 };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Material, type MaterialMeshData, type MaterialOptions, type MeshGeometry } from '@margoengine/materials';
|
|
2
|
+
import type { CasterDisplacement } from './shared/caster.js';
|
|
3
|
+
import type { SurfaceGraph } from './graph.js';
|
|
4
|
+
import type { Engine } from '@margoengine/core/extension';
|
|
5
|
+
import { Program, type Node, type ShaderStage, type NodeInput } from './node.js';
|
|
6
|
+
/** Material slots a node replaces. Nodes read `@margoengine/materials` maps and factors through `materialColor` and friends. */
|
|
7
|
+
export interface NodeMaterialOptions extends MaterialOptions {
|
|
8
|
+
/** Base colour (linear); vec3f keeps the computed alpha. Replaces color x map x vertex colour. */
|
|
9
|
+
colorNode?: Node<'vec3f' | 'vec4f'>;
|
|
10
|
+
opacityNode?: NodeInput<'f32'>;
|
|
11
|
+
roughnessNode?: NodeInput<'f32'>;
|
|
12
|
+
metalnessNode?: NodeInput<'f32'>;
|
|
13
|
+
emissiveNode?: Node<'vec3f'>;
|
|
14
|
+
/** World-space shading normal. */
|
|
15
|
+
normalNode?: Node<'vec3f'>;
|
|
16
|
+
/**
|
|
17
|
+
* Object-space vertex position, e.g. `positionLocal.add(normalLocal.mul(offset))`. Every pass sees it: colour, G-buffer,
|
|
18
|
+
* shadows and picking.
|
|
19
|
+
*/
|
|
20
|
+
positionNode?: Node<'vec3f'>;
|
|
21
|
+
/**
|
|
22
|
+
* How normals follow `positionNode`. `'neighbours'` evaluates the graph 0.001 units to two sides of each vertex: smooth,
|
|
23
|
+
* for shapes the graph works out from `positionLocal`. `'faces'` takes each triangle's own normal in the fragment, for
|
|
24
|
+
* graphs that read `uv()`, whose neighbours are unknown. `'keep'` leaves the mesh's normals. Default: `'faces'` when the
|
|
25
|
+
* graph reads `uv()`, else `'neighbours'`.
|
|
26
|
+
*/
|
|
27
|
+
positionNormals?: 'neighbours' | 'faces' | 'keep';
|
|
28
|
+
}
|
|
29
|
+
/** The surface value this material computed before its nodes ran (fragment only). */
|
|
30
|
+
export declare const materialColor: Node<"vec4f">, materialNormal: Node<"vec3f">;
|
|
31
|
+
/** The material's own roughness (0..1), metalness (0..1) and emissive (linear RGB) before the nodes ran (fragment only). */
|
|
32
|
+
export declare const materialRoughness: Node<"f32">, materialMetalness: Node<"f32">, materialEmissive: Node<"vec3f">;
|
|
33
|
+
/** True when the fragment belongs to a front face (fragment only). */
|
|
34
|
+
export declare const frontFacing: Node<"bool">;
|
|
35
|
+
/** @internal The fragment stage, for entries that compile more nodes into the same shader. */
|
|
36
|
+
export declare const fragmentStage: ShaderStage;
|
|
37
|
+
/**
|
|
38
|
+
* A `@margoengine/materials` Material whose colour, opacity, roughness, metalness, emissive, normal and vertex position come from
|
|
39
|
+
* shader nodes. Every Material option still applies; nodes are fixed at construction, uniform values stay live.
|
|
40
|
+
*/
|
|
41
|
+
export declare class NodeMaterial extends Material {
|
|
42
|
+
readonly program: Program;
|
|
43
|
+
/** Generated statements spliced into gltf\/pbr's surface() and vertex() (for inspection and tests). */
|
|
44
|
+
readonly fragmentCode: string;
|
|
45
|
+
readonly vertexCode: string;
|
|
46
|
+
readonly varyingFields: string;
|
|
47
|
+
/**
|
|
48
|
+
* The validated surface graph a `createGraphMaterial` material was built from, so a baking host can evaluate the
|
|
49
|
+
* same pattern with `evaluateSurfaceGraph`. Undefined for other node materials.
|
|
50
|
+
*/
|
|
51
|
+
surfaceGraph?: SurfaceGraph;
|
|
52
|
+
private readonly patched;
|
|
53
|
+
private readonly rederived;
|
|
54
|
+
private readonly depthVertex;
|
|
55
|
+
/** The depth vertex's move of `margoPosition` alone; TAA runs it again on last frame's uniforms. */
|
|
56
|
+
private readonly depthMove;
|
|
57
|
+
private depthFragment;
|
|
58
|
+
private depthFields;
|
|
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?;
|
|
63
|
+
private readonly colourBindings;
|
|
64
|
+
/** `extra` is for subclasses: it compiles nodes of its own after the fragment stage and before the vertex stage. */
|
|
65
|
+
constructor(options?: NodeMaterialOptions, extra?: (program: Program) => void);
|
|
66
|
+
shader(source: string, group: number): string;
|
|
67
|
+
/** `code`, a caster depth shader with the mesh normal in its input, drawing this graph's position and discards. */
|
|
68
|
+
private depthShader;
|
|
69
|
+
/**
|
|
70
|
+
* `code`, TAA's object velocity shader with the mesh normal in its input and `margoPosition` as its vertex, moved and cut by
|
|
71
|
+
* this graph. A graph that changes between frames adds no velocity of its own: a surface moves as the mesh and camera do.
|
|
72
|
+
*/
|
|
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;
|
|
76
|
+
/** @internal What a depth pass draws: the record of gltf\/pbr's `displacement`, undefined for an unchanged shape. */
|
|
77
|
+
caster(device: GPUDevice, engine?: Engine, _map?: CasterDisplacement, data?: MaterialMeshData): CasterDisplacement | undefined;
|
|
78
|
+
bindings(device: GPUDevice, engine?: Engine, data?: MaterialMeshData): GPUBindGroupEntry[];
|
|
79
|
+
}
|