@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,1184 @@
|
|
|
1
|
+
import { t as e } from "./errors-CsQwshHA.js";
|
|
2
|
+
import { r as t, s as n, t as r } from "./raw-0AYej3Ak.js";
|
|
3
|
+
import { n as i, r as a, t as o } from "./preprocess-CWGWlBWQ.js";
|
|
4
|
+
//#region src/glsl/presets.ts
|
|
5
|
+
var s = [
|
|
6
|
+
"position",
|
|
7
|
+
"normal",
|
|
8
|
+
"uv",
|
|
9
|
+
"uv1",
|
|
10
|
+
"color",
|
|
11
|
+
"tangent",
|
|
12
|
+
"instanceMatrix",
|
|
13
|
+
"instanceColor"
|
|
14
|
+
], c = [
|
|
15
|
+
"modelViewMatrix",
|
|
16
|
+
"worldViewProjection",
|
|
17
|
+
"matrix_model"
|
|
18
|
+
], l = "fn margoGLProjection()->mat4x4f{var p=camera.projection;for(var c=0;c<4;c++){p[c].z=p[c].w-2.0*p[c].z;}return p;}\n", u = "mat3x3f(camera.view[0].xyz,camera.view[1].xyz,camera.view[2].xyz)", d = "fn margoViewInverse()->mat4x4f{let r=transpose(" + u + ");return mat4x4f(vec4f(r[0],0.0),vec4f(r[1],0.0),vec4f(r[2],0.0),vec4f(camera.position,1.0));}\n", f = "fn margoCameraParams()->vec4f{let p=camera.projection;let a=p[2].z;let b=p[3].z;if(p[3].w>0.5){return vec4f(a/b,b/a,(b-1.0)/a,1.0);}let f=select(b/a,3.4e38,a==0.0);return vec4f(a/b,f,b/(1.0+a),0.0);}\n", ee = {
|
|
19
|
+
modelViewMatrix: (e) => {
|
|
20
|
+
let t = e ? "margoGLProjection()" : "camera.projection", n = e ? [l] : [], r = ["vertex", "fragment"], i = ["vertex"];
|
|
21
|
+
return {
|
|
22
|
+
uniforms: {
|
|
23
|
+
modelMatrix: {
|
|
24
|
+
glsl: "mat4",
|
|
25
|
+
code: "mesh.world",
|
|
26
|
+
implicit: i
|
|
27
|
+
},
|
|
28
|
+
viewMatrix: {
|
|
29
|
+
glsl: "mat4",
|
|
30
|
+
code: "camera.view",
|
|
31
|
+
implicit: r
|
|
32
|
+
},
|
|
33
|
+
modelViewMatrix: {
|
|
34
|
+
glsl: "mat4",
|
|
35
|
+
code: "(camera.view*mesh.world)",
|
|
36
|
+
implicit: i
|
|
37
|
+
},
|
|
38
|
+
projectionMatrix: {
|
|
39
|
+
glsl: "mat4",
|
|
40
|
+
code: t,
|
|
41
|
+
implicit: i,
|
|
42
|
+
helpers: n
|
|
43
|
+
},
|
|
44
|
+
normalMatrix: {
|
|
45
|
+
glsl: "mat3",
|
|
46
|
+
code: `(${u}*mesh.normal)`,
|
|
47
|
+
implicit: i
|
|
48
|
+
},
|
|
49
|
+
cameraPosition: {
|
|
50
|
+
glsl: "vec3",
|
|
51
|
+
code: "camera.position",
|
|
52
|
+
implicit: r
|
|
53
|
+
},
|
|
54
|
+
isOrthographic: {
|
|
55
|
+
glsl: "bool",
|
|
56
|
+
code: "(camera.projection[3].w>0.5)",
|
|
57
|
+
implicit: r
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
attributes: {
|
|
61
|
+
position: {
|
|
62
|
+
glsl: "vec3",
|
|
63
|
+
semantic: "position",
|
|
64
|
+
implicit: !0
|
|
65
|
+
},
|
|
66
|
+
normal: {
|
|
67
|
+
glsl: "vec3",
|
|
68
|
+
semantic: "normal",
|
|
69
|
+
implicit: !0
|
|
70
|
+
},
|
|
71
|
+
uv: {
|
|
72
|
+
glsl: "vec2",
|
|
73
|
+
semantic: "uv",
|
|
74
|
+
implicit: !0,
|
|
75
|
+
default: [0, 0]
|
|
76
|
+
},
|
|
77
|
+
uv1: {
|
|
78
|
+
glsl: "vec2",
|
|
79
|
+
semantic: "uv1",
|
|
80
|
+
implicit: !0,
|
|
81
|
+
default: [0, 0]
|
|
82
|
+
},
|
|
83
|
+
color: {
|
|
84
|
+
glsl: "vec3",
|
|
85
|
+
semantic: "color",
|
|
86
|
+
implicit: !0,
|
|
87
|
+
default: [
|
|
88
|
+
1,
|
|
89
|
+
1,
|
|
90
|
+
1
|
|
91
|
+
]
|
|
92
|
+
},
|
|
93
|
+
tangent: {
|
|
94
|
+
glsl: "vec4",
|
|
95
|
+
semantic: "tangent",
|
|
96
|
+
implicit: !0
|
|
97
|
+
},
|
|
98
|
+
instanceMatrix: {
|
|
99
|
+
glsl: "mat4",
|
|
100
|
+
semantic: "instanceMatrix",
|
|
101
|
+
implicit: !0
|
|
102
|
+
},
|
|
103
|
+
instanceColor: {
|
|
104
|
+
glsl: "vec3",
|
|
105
|
+
semantic: "instanceColor",
|
|
106
|
+
implicit: !0,
|
|
107
|
+
default: [
|
|
108
|
+
1,
|
|
109
|
+
1,
|
|
110
|
+
1
|
|
111
|
+
]
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
worldViewProjection: (e) => {
|
|
117
|
+
let t = e ? "margoGLProjection()" : "camera.projection", n = e ? [l] : [];
|
|
118
|
+
return {
|
|
119
|
+
uniforms: {
|
|
120
|
+
world: {
|
|
121
|
+
glsl: "mat4",
|
|
122
|
+
code: "mesh.world"
|
|
123
|
+
},
|
|
124
|
+
view: {
|
|
125
|
+
glsl: "mat4",
|
|
126
|
+
code: "camera.view"
|
|
127
|
+
},
|
|
128
|
+
projection: {
|
|
129
|
+
glsl: "mat4",
|
|
130
|
+
code: t,
|
|
131
|
+
helpers: n
|
|
132
|
+
},
|
|
133
|
+
worldView: {
|
|
134
|
+
glsl: "mat4",
|
|
135
|
+
code: "(camera.view*mesh.world)"
|
|
136
|
+
},
|
|
137
|
+
worldViewProjection: {
|
|
138
|
+
glsl: "mat4",
|
|
139
|
+
code: `(${t}*camera.view*mesh.world)`,
|
|
140
|
+
helpers: n
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
attributes: {
|
|
144
|
+
position: {
|
|
145
|
+
glsl: "vec3",
|
|
146
|
+
semantic: "position"
|
|
147
|
+
},
|
|
148
|
+
normal: {
|
|
149
|
+
glsl: "vec3",
|
|
150
|
+
semantic: "normal"
|
|
151
|
+
},
|
|
152
|
+
uv: {
|
|
153
|
+
glsl: "vec2",
|
|
154
|
+
semantic: "uv"
|
|
155
|
+
},
|
|
156
|
+
uv2: {
|
|
157
|
+
glsl: "vec2",
|
|
158
|
+
semantic: "uv1"
|
|
159
|
+
},
|
|
160
|
+
color: {
|
|
161
|
+
glsl: "vec4",
|
|
162
|
+
semantic: "color"
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
},
|
|
167
|
+
matrix_model: (e) => {
|
|
168
|
+
let t = e ? "margoGLProjection()" : "camera.projection", n = e ? [l] : [];
|
|
169
|
+
return {
|
|
170
|
+
uniforms: {
|
|
171
|
+
matrix_model: {
|
|
172
|
+
glsl: "mat4",
|
|
173
|
+
code: "mesh.world"
|
|
174
|
+
},
|
|
175
|
+
matrix_normal: {
|
|
176
|
+
glsl: "mat3",
|
|
177
|
+
code: "mesh.normal"
|
|
178
|
+
},
|
|
179
|
+
matrix_view: {
|
|
180
|
+
glsl: "mat4",
|
|
181
|
+
code: "camera.view"
|
|
182
|
+
},
|
|
183
|
+
matrix_viewInverse: {
|
|
184
|
+
glsl: "mat4",
|
|
185
|
+
code: "margoViewInverse()",
|
|
186
|
+
helpers: [d]
|
|
187
|
+
},
|
|
188
|
+
matrix_view3: {
|
|
189
|
+
glsl: "mat3",
|
|
190
|
+
code: u
|
|
191
|
+
},
|
|
192
|
+
matrix_projection: {
|
|
193
|
+
glsl: "mat4",
|
|
194
|
+
code: t,
|
|
195
|
+
helpers: n
|
|
196
|
+
},
|
|
197
|
+
matrix_viewProjection: {
|
|
198
|
+
glsl: "mat4",
|
|
199
|
+
code: `(${t}*camera.view)`,
|
|
200
|
+
helpers: n
|
|
201
|
+
},
|
|
202
|
+
view_position: {
|
|
203
|
+
glsl: "vec3",
|
|
204
|
+
code: "camera.position"
|
|
205
|
+
},
|
|
206
|
+
camera_params: {
|
|
207
|
+
glsl: "vec4",
|
|
208
|
+
code: "margoCameraParams()",
|
|
209
|
+
helpers: [f]
|
|
210
|
+
},
|
|
211
|
+
screen_size: {
|
|
212
|
+
glsl: "vec4",
|
|
213
|
+
code: "vec4f(camera.resolution,1.0/camera.resolution)"
|
|
214
|
+
},
|
|
215
|
+
viewport_size: {
|
|
216
|
+
glsl: "vec4",
|
|
217
|
+
code: "vec4f(camera.viewport.zw,1.0/camera.viewport.zw)"
|
|
218
|
+
},
|
|
219
|
+
view_index: {
|
|
220
|
+
glsl: "uint",
|
|
221
|
+
code: "0u"
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
attributes: {
|
|
225
|
+
vertex_position: {
|
|
226
|
+
glsl: "vec3",
|
|
227
|
+
semantic: "position"
|
|
228
|
+
},
|
|
229
|
+
vertex_normal: {
|
|
230
|
+
glsl: "vec3",
|
|
231
|
+
semantic: "normal"
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}, p = {
|
|
237
|
+
sampler2DShadow: ["2d", "depth"],
|
|
238
|
+
samplerCubeShadow: ["cube", "depth"],
|
|
239
|
+
sampler2DArrayShadow: ["2d-array", "depth"]
|
|
240
|
+
};
|
|
241
|
+
for (let [e, t] of [
|
|
242
|
+
["2D", "2d"],
|
|
243
|
+
["Cube", "cube"],
|
|
244
|
+
["3D", "3d"],
|
|
245
|
+
["2DArray", "2d-array"]
|
|
246
|
+
]) p["sampler" + e] = [t], p["isampler" + e] = [t, "sint"], p["usampler" + e] = [t, "uint"];
|
|
247
|
+
var m = {
|
|
248
|
+
float: "f32",
|
|
249
|
+
int: "i32",
|
|
250
|
+
uint: "u32",
|
|
251
|
+
bool: "u32",
|
|
252
|
+
mat2: "vec4f",
|
|
253
|
+
mat3: "mat3x3f",
|
|
254
|
+
mat4: "mat4x4f"
|
|
255
|
+
};
|
|
256
|
+
for (let e of [
|
|
257
|
+
2,
|
|
258
|
+
3,
|
|
259
|
+
4
|
|
260
|
+
]) m["vec" + e] = `vec${e}f`, m["ivec" + e] = `vec${e}i`, m["uvec" + e] = `vec${e}u`, m["bvec" + e] = `vec${e}u`;
|
|
261
|
+
var h = (e) => Number(/(?:vec|mat)([234])/.exec(e)?.[1] ?? 1), g = (e) => /^mat([234])x([234])$/.exec(e)?.slice(1).map(Number), _ = (e) => g(e) ? g(e)[0] * g(e)[1] : e.startsWith("mat") ? h(e) ** 2 : h(e), v = /^(?:[iu]?vec[34]|mat[34])\[\d+\]$/, y = (e) => /^(int|ivec)/.test(e) || /^vec\di$|^i32$/.test(e) ? "i" : /^(uint|uvec)/.test(e) || /^vec\du$|^u32$/.test(e) ? "u" : "f", b = (e) => {
|
|
262
|
+
let t = h(e), n = y(e);
|
|
263
|
+
return t === 1 ? {
|
|
264
|
+
f: "f32",
|
|
265
|
+
i: "i32",
|
|
266
|
+
u: "u32"
|
|
267
|
+
}[n] : `vec${t}${n}`;
|
|
268
|
+
}, x = /^[A-Za-z_]\w*$/, te = {
|
|
269
|
+
gl_MaxVertexAttribs: [8, 16],
|
|
270
|
+
gl_MaxVertexUniformVectors: [128, 256],
|
|
271
|
+
gl_MaxVaryingVectors: [8, 15],
|
|
272
|
+
gl_MaxVertexOutputVectors: [16, 16],
|
|
273
|
+
gl_MaxFragmentInputVectors: [15, 15],
|
|
274
|
+
gl_MaxVertexTextureImageUnits: [0, 16],
|
|
275
|
+
gl_MaxCombinedTextureImageUnits: [8, 32],
|
|
276
|
+
gl_MaxTextureImageUnits: [8, 16],
|
|
277
|
+
gl_MaxFragmentUniformVectors: [16, 224],
|
|
278
|
+
gl_MaxDrawBuffers: [1, 4],
|
|
279
|
+
gl_MinProgramTexelOffset: [-8, -8],
|
|
280
|
+
gl_MaxProgramTexelOffset: [7, 7]
|
|
281
|
+
}, ne = { count: 0 }, S = /* @__PURE__ */ new Map(), C = (e) => `${e}Shader`;
|
|
282
|
+
function w(t, n, r) {
|
|
283
|
+
let i = e("failed", `GLSL program: ${t ? C(t.stage) + ": " : ""}${n}`);
|
|
284
|
+
if (t) {
|
|
285
|
+
let e = (r ? RegExp(`\\b${r}\\b`).exec(t.text) : null)?.index ?? 0, n = t.text.slice(0, e);
|
|
286
|
+
Object.assign(i, {
|
|
287
|
+
stage: t.stage,
|
|
288
|
+
line: Math.max(1, n.split("\n").length - t.header),
|
|
289
|
+
column: e - n.lastIndexOf("\n")
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
throw i;
|
|
293
|
+
}
|
|
294
|
+
var T = {
|
|
295
|
+
tonemapping_fragment: "{vec3 margo_x=max(MARGO_OUT.rgb,vec3(0.0));float margo_m=min(margo_x.r,min(margo_x.g,margo_x.b));vec3 margo_v=margo_x-(margo_m<0.08?margo_m-6.25*margo_m*margo_m:0.04);float margo_p=max(margo_v.r,max(margo_v.g,margo_v.b));float margo_n=1.0-0.0576/(margo_p-0.52);vec3 margo_t=margo_p<0.76?margo_v:mix(margo_v*(margo_n/margo_p),vec3(margo_n),1.0-1.0/(0.15*(margo_p-margo_n)+1.0));if(margo_encoding!=0.0){MARGO_OUT.rgb=margo_t;}}",
|
|
296
|
+
colorspace_fragment: "{vec3 margo_x=max(MARGO_OUT.rgb,vec3(0.0));if(margo_encoding==1.0){MARGO_OUT.rgb=mix(margo_x*12.92,1.055*pow(margo_x,vec3(1.0/2.4))-0.055,step(vec3(0.0031308),margo_x));}}"
|
|
297
|
+
};
|
|
298
|
+
function E(t, n, r, i, a = 0) {
|
|
299
|
+
let o = r.includes ?? {};
|
|
300
|
+
return n.replace(/^[ \t]*#[ \t]*include[ \t]*(?:<([^>\n]*)>|"([^"\n]*)")[^\n]*$/gm, (s, c, l) => {
|
|
301
|
+
let u = (c ?? l).trim();
|
|
302
|
+
if (Object.hasOwn(o, u)) {
|
|
303
|
+
if (a >= 8) throw e("failed", `GLSL program: ${C(t)}: #include <${u}> nests more than 8 deep; an include includes itself`);
|
|
304
|
+
return E(t, o[u], r, i, a + 1).replace(/\r?\n/g, " ");
|
|
305
|
+
}
|
|
306
|
+
if (T[u]) {
|
|
307
|
+
if (t !== "fragment") throw e("failed", `GLSL program: ${C(t)}: #include <${u}> belongs in the fragmentShader`);
|
|
308
|
+
i.add(u);
|
|
309
|
+
let a = /^\s*#\s*version\s+300/m.test(r.fragmentShader) ? /^\s*(?:layout\s*\([^)]*\)\s*)?out\s+(?:(?:highp|mediump|lowp)\s+)?vec4\s+([A-Za-z_]\w*)/m.exec(n)?.[1] ?? /\bout\s+(?:(?:highp|mediump|lowp)\s+)?vec4\s+([A-Za-z_]\w*)/.exec(r.fragmentShader)?.[1] ?? "gl_FragColor" : "gl_FragColor";
|
|
310
|
+
return T[u].replaceAll("MARGO_OUT", a);
|
|
311
|
+
}
|
|
312
|
+
throw Object.assign(e("failed", `GLSL program: ${C(t)}: #include <${u}> is not known; pass its text in includes, or remove it`), {
|
|
313
|
+
stage: t,
|
|
314
|
+
line: n.slice(0, n.indexOf(s)).split("\n").length,
|
|
315
|
+
column: 1
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
function re(t, n, r, i = []) {
|
|
320
|
+
if (typeof n != "string" || !n.trim() || n.length > 65536) throw TypeError(`ShaderMaterial ${C(t)} needs 1..65536 code units of GLSL with void main()`);
|
|
321
|
+
let s = r.defines ?? {}, c = /^\s*#\s*version\b/m.test(n), l = /* @__PURE__ */ new Set(), u = [
|
|
322
|
+
...!c && r.glslVersion === 300 ? ["#version 300 es"] : [],
|
|
323
|
+
...Object.entries(s).map(([e, t]) => `#define ${e} ${typeof t == "boolean" ? +t : t}`),
|
|
324
|
+
...i
|
|
325
|
+
];
|
|
326
|
+
n = E(t, n, r, l);
|
|
327
|
+
let d = { version: 100 }, f = o(u.length ? u.join("\n") + "\n" + n : n, d);
|
|
328
|
+
if (c && r.glslVersion !== void 0 && r.glslVersion !== d.version) throw e("failed", `GLSL program: ${C(t)} declares #version ${d.version} but glslVersion is ${r.glslVersion}; drop one`);
|
|
329
|
+
try {
|
|
330
|
+
return {
|
|
331
|
+
stage: t,
|
|
332
|
+
text: f,
|
|
333
|
+
header: u.length,
|
|
334
|
+
version: d.version,
|
|
335
|
+
included: l,
|
|
336
|
+
program: new a(f, C(t)).parse()
|
|
337
|
+
};
|
|
338
|
+
} catch (e) {
|
|
339
|
+
throw typeof e?.line == "number" && (e.line -= u.length, e.stage = t), e;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
var D = (e, t) => RegExp(`\\b${t}\\b`).test(e), O = (e, t) => RegExp(`\\b${t}\\b`).test(e.text);
|
|
343
|
+
function k(e) {
|
|
344
|
+
let t = `uniforms.${e.member}`, n = g(e.glsl);
|
|
345
|
+
return n ? `mat${n[0]}x${n[1]}f(${Array.from({ length: n[0] }, (e, r) => `${t}[${r}].${"xyzw".slice(0, n[1])}`).join(",")})` : e.glsl === "bool" ? `(${t}!=0u)` : e.glsl.startsWith("bvec") ? `(${t}!=vec${h(e.glsl)}u(0u))` : e.glsl === "mat2" ? `mat2x2f(${t}.xy,${t}.zw)` : t;
|
|
346
|
+
}
|
|
347
|
+
var A = (e, t, n) => (r, i, a, o) => {
|
|
348
|
+
let s = i === "sampler2D" ? n?.(r) : void 0;
|
|
349
|
+
if (s && s !== "0u" && s !== "1u") {
|
|
350
|
+
let n = o.offset ? `vec2i(${o.offset})` : "vec2i(0)", c = o.lod ? `margoTaps(${r},${a},vec4f(0.0),vec2f(${o.lod},1.0),${n},${s})` : o.ddx ? `margoTaps(${r},${a},vec4f(${o.ddx},${o.ddy}),vec2f(0.0),${n},${s})` : e ? `margoTapsAuto(${r},${a},${o.bias ?? "0.0"},${n},${s})` : `margoTaps(${r},${a},vec4f(0.0),vec2f(0.0,1.0),${n},${s})`;
|
|
351
|
+
return /^\d+u$/.test(s) ? c : `select(${A(e, t)(r, i, a, o)},${c},${s}>1u)`;
|
|
352
|
+
}
|
|
353
|
+
let c = i.endsWith("2DArray") || i === "sampler2DArrayShadow", l = c ? `${a}.xy,i32(round(${a}.z))` : a, u = o.offset ? `,${o.offset}` : "", d = `${r},${t(r)},${l}`;
|
|
354
|
+
if (o.compare) return `textureSampleCompare${!e || o.lod || o.ddx ? "Level" : ""}(${d},${o.compare}${u})`;
|
|
355
|
+
if (/^[iu]/.test(i)) {
|
|
356
|
+
let e = i.includes("3D") ? 3 : 2, t = `vec${e}f(textureDimensions(${r},0))`, n = `vec${e}i(clamp(floor(${e === 3 ? a : `(${a}).xy`}*${t})${o.offset ? `+vec${e}f(${o.offset})` : ""},vec${e}f(0.0),${t}-1.0))`;
|
|
357
|
+
return c ? `textureLoad(${r},${n},clamp(i32(round((${a}).z)),0,i32(textureNumLayers(${r}))-1),0)` : `textureLoad(${r},${n},0)`;
|
|
358
|
+
}
|
|
359
|
+
return o.lod ? `textureSampleLevel(${d},${o.lod}${u})` : o.ddx ? `textureSampleGrad(${d},${o.ddx},${o.ddy}${u})` : e ? o.bias ? `textureSampleBias(${d},${o.bias}${u})` : `textureSample(${d}${u})` : `textureSampleLevel(${d},0.0${u})`;
|
|
360
|
+
}, j = (e) => e.startsWith("argument_") ? e + "s" : e + "Sampler", M = "fn margoTexel(t:texture_2d<f32>,i:vec2i,l:i32,m:u32)->vec4f{let n=vec2i(textureDimensions(t,l));return textureLoad(t,select(clamp(i,vec2i(0),n-1),((i%n)+n)%n,(m&1u)==1u),l);}\nfn margoBilinear(t:texture_2d<f32>,uv:vec2f,l:i32,o:vec2i,m:u32)->vec4f{let f=uv*vec2f(textureDimensions(t,l))-0.5;let b=floor(f);let w=f-b;let i=vec2i(b)+o;\nreturn mix(mix(margoTexel(t,i,l,m),margoTexel(t,i+vec2i(1,0),l,m),w.x),mix(margoTexel(t,i+vec2i(0,1),l,m),margoTexel(t,i+vec2i(1,1),l,m),w.x),w.y);}\nfn margoTaps(t:texture_2d<f32>,uv:vec2f,g:vec4f,b:vec2f,o:vec2i,m:u32)->vec4f{let n=vec2f(textureDimensions(t,0));\nlet lambda=select(log2(max(length(g.xy*n),length(g.zw*n)))+b.x,b.x,b.y>0.5);\nif(m<4u||lambda<=0.0){return margoBilinear(t,uv,0,o,m);}\nlet q=f32(textureNumLevels(t))-1.0;let l=min(lambda,q);let d=floor(l);let a=margoBilinear(t,uv,i32(d),o,m);\nif(d>=q){return a;}\nreturn mix(a,margoBilinear(t,uv,i32(d)+1,o,m),l-d);}\nfn margoTapsAuto(t:texture_2d<f32>,uv:vec2f,bias:f32,o:vec2i,m:u32)->vec4f{return margoTaps(t,uv,vec4f(dpdx(uv),dpdy(uv)),vec2f(bias,0.0),o,m);}\n", ie = (e) => (e.filter === "nearest" ? 0 : e.filter === "linear" ? 2 : 4) + +(e.wrap === "repeat");
|
|
361
|
+
function ae(e, t, n, r = [
|
|
362
|
+
0,
|
|
363
|
+
0,
|
|
364
|
+
0,
|
|
365
|
+
1
|
|
366
|
+
]) {
|
|
367
|
+
let i = h(n), a = b(n);
|
|
368
|
+
if (!e || !t) return `${a}(${[
|
|
369
|
+
0,
|
|
370
|
+
1,
|
|
371
|
+
2,
|
|
372
|
+
3
|
|
373
|
+
].map((e) => r[e] ?? +(e === 3)).slice(0, i).map((e) => y(n) === "f" ? Number.isInteger(e) ? e.toFixed(1) : String(e) : Math.trunc(e)).join(",")})`;
|
|
374
|
+
let o = h(t), s = o >= i ? e : `vec4${y(t)}(${e}${[
|
|
375
|
+
"",
|
|
376
|
+
",0,0,1",
|
|
377
|
+
",0,1",
|
|
378
|
+
",1"
|
|
379
|
+
][o]})`, c = (o >= i ? o : 4) === i ? s : `(${s}).${"xyzw".slice(0, i)}`;
|
|
380
|
+
return y(t) === y(n) ? c : `${a}(${c})`;
|
|
381
|
+
}
|
|
382
|
+
function oe(e, t) {
|
|
383
|
+
return /^(float|u?int|[iu]?vec[234])$/.test(t.type) || w(e, `varying ${t.name} has type ${t.type}; pass float, int or uint scalars and vectors between the stages`, t.name), y(t.type) !== "f" && !D(t.qualifier, "flat") && w(e, `integer varying ${t.name} must be flat (GLSL ES 3.00 4.3.6)`, t.name), b(t.type);
|
|
384
|
+
}
|
|
385
|
+
var se = (e) => D(e, "flat") ? "flat" : D(e, "centroid") ? "centroid" : "perspective", ce = (e) => e === "flat" ? "@interpolate(flat) " : e === "centroid" ? "@interpolate(perspective,centroid) " : "";
|
|
386
|
+
function N(e, t) {
|
|
387
|
+
if (!e || typeof e != "object") throw TypeError("ShaderMaterial needs {vertexShader, fragmentShader}");
|
|
388
|
+
let { defines: n = {}, flipY: r = !0, clipSpace: a = "gl", strict: o = !1, glslVersion: l, builtins: u = "auto", uniformNames: d = [], attributes: f = {}, defaultAttributeValues: y = {}, includes: T = {}, outputColorSpace: E = "srgb", unfilterable: N = {} } = e, P = t?.(e);
|
|
389
|
+
if (!P && (e.lights || e.fog || e.velocity)) throw TypeError("lights, fog and velocity are options of ShaderMaterial from shader/glsl-material; draw with that ShaderMaterial to use them");
|
|
390
|
+
for (let [e, t] of Object.entries(N)) if (!t || ![
|
|
391
|
+
"nearest",
|
|
392
|
+
"linear",
|
|
393
|
+
"mipmap"
|
|
394
|
+
].includes(t.filter) || !["clamp", "repeat"].includes(t.wrap)) throw TypeError(`ShaderMaterial unfilterable.${e} needs filter 'nearest', 'linear' or 'mipmap' and wrap 'clamp' or 'repeat'`);
|
|
395
|
+
if (u !== !1 && u !== "auto" && !c.includes(u)) throw TypeError(`ShaderMaterial builtins must be ${c.map((e) => `'${e}'`).join(", ")}, 'auto' or false`);
|
|
396
|
+
if (E !== "srgb" && E !== "linear") throw TypeError("ShaderMaterial outputColorSpace must be 'srgb' or 'linear'");
|
|
397
|
+
if (!Array.isArray(d)) throw TypeError("ShaderMaterial uniformNames must be an array of names");
|
|
398
|
+
for (let [e, t] of Object.entries(f)) if (!s.includes(t)) throw TypeError(`ShaderMaterial attributes.${e} must be one of ${s.join(", ")}`);
|
|
399
|
+
for (let [e, t] of Object.entries(y)) if (!Array.isArray(t) || !t.length || t.length > 4 || t.some((e) => !Number.isFinite(e))) throw TypeError(`ShaderMaterial defaultAttributeValues.${e} needs 1 to 4 finite numbers`);
|
|
400
|
+
for (let [e, t] of Object.entries(T)) if (typeof t != "string") throw TypeError(`ShaderMaterial includes.${e} must be GLSL text`);
|
|
401
|
+
if (typeof n != "object" || !n) throw TypeError("ShaderMaterial defines must be an object");
|
|
402
|
+
for (let [e, t] of Object.entries(n)) if (!x.test(e) || ![
|
|
403
|
+
"string",
|
|
404
|
+
"number",
|
|
405
|
+
"boolean"
|
|
406
|
+
].includes(typeof t) || /[\r\n\\]/.test(String(t))) throw TypeError(`ShaderMaterial define ${e} needs an identifier and a one-line string, number or boolean`);
|
|
407
|
+
if (typeof r != "boolean" || typeof o != "boolean") throw TypeError("ShaderMaterial flipY and strict must be booleans");
|
|
408
|
+
if (a !== "gl" && a !== "webgpu") throw TypeError("ShaderMaterial clipSpace must be 'gl' or 'webgpu'");
|
|
409
|
+
if (l !== void 0 && l !== 100 && l !== 300) throw TypeError("ShaderMaterial glslVersion must be 100 or 300");
|
|
410
|
+
let F = JSON.stringify([
|
|
411
|
+
e.vertexShader,
|
|
412
|
+
e.fragmentShader,
|
|
413
|
+
n,
|
|
414
|
+
l ?? 0,
|
|
415
|
+
r,
|
|
416
|
+
a,
|
|
417
|
+
o,
|
|
418
|
+
u,
|
|
419
|
+
[...d].sort(),
|
|
420
|
+
f,
|
|
421
|
+
y,
|
|
422
|
+
T,
|
|
423
|
+
E,
|
|
424
|
+
N,
|
|
425
|
+
e.lights,
|
|
426
|
+
e.fog,
|
|
427
|
+
e.velocity
|
|
428
|
+
]), I = S.get(F);
|
|
429
|
+
if (I) return I;
|
|
430
|
+
let L = re("vertex", e.vertexShader, e), R = re("fragment", e.fragmentShader, e), z = new Set(d), B = a === "gl", V, H = !1, le = (e) => {
|
|
431
|
+
let t = [];
|
|
432
|
+
for (let n of [L, R]) {
|
|
433
|
+
let r = new Map(n.program.globals.map((e) => [e.name, e]));
|
|
434
|
+
for (let [i, a] of Object.entries(e.uniforms)) {
|
|
435
|
+
if (z.has(i)) continue;
|
|
436
|
+
let e = r.get(i);
|
|
437
|
+
(e ? D(e.qualifier, "uniform") && e.type === a.glsl : a.implicit?.includes(n.stage) && O(n, i)) && t.push(i);
|
|
438
|
+
}
|
|
439
|
+
if (n.stage === "vertex") for (let [i, a] of Object.entries(e.attributes)) a.implicit && !r.has(i) && O(n, i) && t.push(i);
|
|
440
|
+
}
|
|
441
|
+
return t;
|
|
442
|
+
};
|
|
443
|
+
if (u === "auto") {
|
|
444
|
+
let e = c.map((e) => [e, le(ee[e](B))]).filter(([, e]) => e.length);
|
|
445
|
+
e.length > 1 && w(void 0, `the program uses built-in names of ${e.length} conventions (${e.map(([e, t]) => `'${e}': ${t.slice(0, 3).join(", ")}`).join("; ")}); choose one with builtins, or list your own names in uniforms`), e[0] && (H = e[0][0]);
|
|
446
|
+
} else H = u;
|
|
447
|
+
let U = [];
|
|
448
|
+
if (H) {
|
|
449
|
+
V = ee[H](B);
|
|
450
|
+
let t = (e) => {
|
|
451
|
+
let t = new Set(e.program.globals.map((e) => e.name)), n = [];
|
|
452
|
+
for (let [r, i] of Object.entries(V.uniforms)) !z.has(r) && i.implicit?.includes(e.stage) && !t.has(r) && O(e, r) && n.push(`uniform ${i.glsl} ${r};`);
|
|
453
|
+
if (e.stage === "vertex") for (let [r, i] of Object.entries(V.attributes)) i.implicit && !t.has(r) && O(e, r) && n.push(`${e.version === 300 ? "in" : "attribute"} ${i.glsl} ${r};`);
|
|
454
|
+
return n;
|
|
455
|
+
};
|
|
456
|
+
U.push(...t(L));
|
|
457
|
+
let n = t(R);
|
|
458
|
+
n.length && (R = re("fragment", e.fragmentShader, e, n));
|
|
459
|
+
}
|
|
460
|
+
P && U.push(...P.declare(L, R, z, V)), U.length && (L = re("vertex", e.vertexShader, e, U)), L.version !== R.version && w(R, `the vertexShader is GLSL ES ${L.version === 300 ? "3.00" : "1.00"} and the fragmentShader ${R.version === 300 ? "3.00" : "1.00"}; use one version in both`);
|
|
461
|
+
let W = /* @__PURE__ */ new Map(), G = /* @__PURE__ */ new Map(), ue = {}, K = /* @__PURE__ */ new Set(), de = [], q = /* @__PURE__ */ new Map(), fe = (e, t) => {
|
|
462
|
+
let [, n, r] = /^(.*?)((?:\[\d+\])*)$/.exec(t), i = e.program.structs.get(n);
|
|
463
|
+
return (i ? `${n}{${i.map((t) => `${t.name}:${fe(e, t.type)}`).join(";")}}` : n) + r;
|
|
464
|
+
}, J = (e, t) => {
|
|
465
|
+
let n = /^(.*)\[(\d+)\]$/.exec(t);
|
|
466
|
+
if (n) return `array<${J(e, n[1])},${n[2]}>`;
|
|
467
|
+
if (e.program.structs.has(t)) return "user_" + t;
|
|
468
|
+
let r = g(t);
|
|
469
|
+
return r ? `mat${r[0]}x${r[1]}f` : t === "bool" ? "bool" : t.startsWith("bvec") ? `vec${h(t)}<bool>` : t.startsWith("mat") ? `mat${h(t)}x${h(t)}f` : b(t);
|
|
470
|
+
}, pe = (e, t, n, r, i, a) => {
|
|
471
|
+
let o = /^(.*)\[(\d+)\]$/.exec(n);
|
|
472
|
+
if (o) {
|
|
473
|
+
let [, n, s] = o, c = e.program.structs.has(n) ? 0 : _(n);
|
|
474
|
+
return `array<${J(e, n)},${s}>(${Array.from({ length: +s }, (o, s) => pe(e, t, n, `${r}[${s}]`, `${i}__${s}`, [...a, [s, c]])).join(",")})`;
|
|
475
|
+
}
|
|
476
|
+
let s = e.program.structs.get(n);
|
|
477
|
+
if (s) return s.some((e) => /sampler/.test(e.type)) && w(e, `uniform ${t} holds a sampler in a struct; declare each sampler as its own uniform`, t), `user_${n}(${s.map((n) => pe(e, t, n.type, `${r}.${n.name}`, `${i}__${n.name}`, [...a, n.name])).join(",")})`;
|
|
478
|
+
let c = m[n] ?? (g(n) ? "mat4x4f" : void 0);
|
|
479
|
+
c || w(e, `uniform ${t} has a member of type ${n}; use scalars, vectors, matrices, structs and arrays of them`, t);
|
|
480
|
+
let l = {
|
|
481
|
+
name: r,
|
|
482
|
+
glsl: n,
|
|
483
|
+
type: c,
|
|
484
|
+
member: i,
|
|
485
|
+
root: t,
|
|
486
|
+
path: a
|
|
487
|
+
};
|
|
488
|
+
return W.set(r, l), k(l);
|
|
489
|
+
};
|
|
490
|
+
for (let e of [L, R]) for (let t of e.program.globals) {
|
|
491
|
+
if (!D(t.qualifier, "uniform")) continue;
|
|
492
|
+
let n = z.has(t.name) ? void 0 : V?.uniforms[t.name];
|
|
493
|
+
if (n) {
|
|
494
|
+
n.glsl !== t.type && w(e, `uniform ${t.name} is the engine's ${n.glsl} under builtins '${H}'; declare it ${n.glsl}, or list it in uniforms to set it yourself`, t.name), ue[t.name] = {
|
|
495
|
+
code: n.code,
|
|
496
|
+
type: n.glsl
|
|
497
|
+
}, n.helpers?.forEach((e) => K.add(e));
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
let r = q.get(t.name), i = fe(e, t.type);
|
|
501
|
+
if (r) {
|
|
502
|
+
r.shape !== i && w(e, `uniform ${t.name} is ${r.type} in the vertexShader and ${t.type} here; declare one type`, t.name);
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
P?.uniform(e, t, z) && de.push(t.name);
|
|
506
|
+
let a = {
|
|
507
|
+
type: t.type,
|
|
508
|
+
shape: i
|
|
509
|
+
};
|
|
510
|
+
q.set(t.name, a);
|
|
511
|
+
let o = "user_" + t.name, s = /^(\w*sampler\w*)\[(\d+)\]$/.exec(t.type), c = (e, t, n, r = {}) => {
|
|
512
|
+
let [i, a] = p[t], o = t === "sampler2D" && Object.hasOwn(N, e) ? "unfilterable-float" : a;
|
|
513
|
+
G.set(e, {
|
|
514
|
+
name: e,
|
|
515
|
+
glsl: t,
|
|
516
|
+
dimension: i,
|
|
517
|
+
...o ? { sampleType: o } : {},
|
|
518
|
+
binding: n,
|
|
519
|
+
...r
|
|
520
|
+
});
|
|
521
|
+
};
|
|
522
|
+
if (p[t.type]) c(t.name, t.type, o);
|
|
523
|
+
else if (s && p[s[1]]) for (let e = 0; e < +s[2]; e++) c(`${t.name}[${e}]`, s[1], `${o}__${e}`, {
|
|
524
|
+
root: t.name,
|
|
525
|
+
index: e
|
|
526
|
+
});
|
|
527
|
+
else if (m[t.type] || g(t.type)) W.set(t.name, {
|
|
528
|
+
name: t.name,
|
|
529
|
+
glsl: t.type,
|
|
530
|
+
type: m[t.type] ?? "mat4x4f",
|
|
531
|
+
member: o
|
|
532
|
+
});
|
|
533
|
+
else if (v.test(t.type)) {
|
|
534
|
+
let [, e, n] = /^(.*)\[(\d+)\]$/.exec(t.type);
|
|
535
|
+
W.set(t.name, {
|
|
536
|
+
name: t.name,
|
|
537
|
+
glsl: t.type,
|
|
538
|
+
type: m[e],
|
|
539
|
+
member: o,
|
|
540
|
+
count: +n
|
|
541
|
+
});
|
|
542
|
+
} else /\[\d+\]$/.test(t.type) || e.program.structs.has(t.type) ? a.copy = [J(e, t.type), pe(e, t.name, t.type, t.name, o, [])] : w(e, `uniform ${t.name} has type ${t.type}; use a scalar, vector, matrix, sampler, struct or array of them`, t.name);
|
|
543
|
+
}
|
|
544
|
+
let Y = [], X = [], Z = [];
|
|
545
|
+
for (let e of L.program.globals) {
|
|
546
|
+
let t = e.qualifier;
|
|
547
|
+
if (D(t, "attribute") || D(t, "in")) {
|
|
548
|
+
/^(float|u?int|[iu]?vec[234]|mat[234])$/.test(e.type) || w(L, `attribute ${e.name} has type ${e.type}; use a float, int or uint scalar or vector, or a mat2, mat3 or mat4`, e.name);
|
|
549
|
+
let t = f[e.name] ?? V?.attributes[e.name]?.semantic ?? (s.includes(e.name) ? e.name : void 0);
|
|
550
|
+
Y.push(t ? {
|
|
551
|
+
name: e.name,
|
|
552
|
+
glsl: e.type,
|
|
553
|
+
semantic: t
|
|
554
|
+
} : {
|
|
555
|
+
name: e.name,
|
|
556
|
+
glsl: e.type
|
|
557
|
+
});
|
|
558
|
+
} else (D(t, "varying") || D(t, "out")) && (oe(L, e), X.push({
|
|
559
|
+
name: e.name,
|
|
560
|
+
glsl: e.type,
|
|
561
|
+
location: X.length,
|
|
562
|
+
interpolation: se(t)
|
|
563
|
+
}));
|
|
564
|
+
}
|
|
565
|
+
let me = [];
|
|
566
|
+
for (let e of R.program.globals) {
|
|
567
|
+
let t = e.qualifier;
|
|
568
|
+
if (D(t, "varying") || D(t, "in")) {
|
|
569
|
+
oe(R, e);
|
|
570
|
+
let n = X.find((t) => t.name === e.name);
|
|
571
|
+
if (!n) {
|
|
572
|
+
RegExp(`\\b${e.name}\\b`, "g").exec(R.text) && [...R.text.matchAll(RegExp(`\\b${e.name}\\b`, "g"))].length > 1 && w(R, `varying ${e.name} is read but the vertexShader does not write it; declare it there too`, e.name);
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
n.glsl !== e.type && w(R, `varying ${e.name} is ${n.glsl} in the vertexShader and ${e.type} here`, e.name), L.version === 300 && n.interpolation !== se(t) && w(R, `varying ${e.name} has other interpolation qualifiers than in the vertexShader`, e.name), me.push(n);
|
|
576
|
+
} else D(t, "out") && Z.push(e);
|
|
577
|
+
}
|
|
578
|
+
Z.length > 1 && w(R, `${Z.length} colour outputs; the scene pass has one colour target, so declare one output`, Z[1].name);
|
|
579
|
+
let Q = Z[0];
|
|
580
|
+
Q && !/^(float|vec[234])$/.test(Q.type) && w(R, `output ${Q.name} has type ${Q.type}; the scene's colour target takes float vectors`, Q.name), /\bgl_FragData\s*\[\s*[^0\s]/.test(R.text) && w(R, "gl_FragData above 0 needs more colour targets; the scene pass has one", "gl_FragData");
|
|
581
|
+
let $ = { ...ue };
|
|
582
|
+
for (let e of W.values()) e.root || ($[e.name] = {
|
|
583
|
+
code: e.count ? `uniforms.${e.member}` : k(e),
|
|
584
|
+
type: e.glsl
|
|
585
|
+
});
|
|
586
|
+
for (let e of G.values()) e.root === void 0 && ($[e.name] = {
|
|
587
|
+
code: e.binding,
|
|
588
|
+
type: e.glsl
|
|
589
|
+
});
|
|
590
|
+
for (let [e, t] of q) (t.copy || /sampler\w*\[/.test(t.type)) && ($[e] = {
|
|
591
|
+
code: "user_" + e,
|
|
592
|
+
type: t.type
|
|
593
|
+
});
|
|
594
|
+
let he = (e) => [...q].filter(([t, n]) => n.copy && O(e, t)), ge = (e) => he(e).map(([e, t]) => `var<private> user_${e}:${t.copy[0]};\n`).join(""), _e = (e) => he(e).map(([e, t]) => `user_${e}=${t.copy[1]};\n`).join(""), ve = new Map([...G.values()].filter((e) => e.sampleType === "unfilterable-float").map((e) => [e.binding, ie(N[e.name])]).filter(([, e]) => e > 1));
|
|
595
|
+
ve.size && K.add(M);
|
|
596
|
+
let ye = ve.size ? (e) => e.startsWith("argument_") ? e + "m" : `${ve.get(e) ?? 0}u` : void 0, be = [...K].join(""), xe = "", Se = "", Ce = (e, t, n, a) => {
|
|
597
|
+
e.text;
|
|
598
|
+
for (let [n, [r, i]] of Object.entries(te)) O(e, n) && (t[n] = {
|
|
599
|
+
code: `i32(${e.version === 300 ? i : r})`,
|
|
600
|
+
type: "int"
|
|
601
|
+
});
|
|
602
|
+
O(e, "gl_DepthRange") && (e.program.structs.set("gl_DepthRangeParameters", [
|
|
603
|
+
"near",
|
|
604
|
+
"far",
|
|
605
|
+
"diff"
|
|
606
|
+
].map((e) => ({
|
|
607
|
+
name: e,
|
|
608
|
+
type: "float",
|
|
609
|
+
qualifier: ""
|
|
610
|
+
}))), t.gl_DepthRange = {
|
|
611
|
+
code: "user_gl_DepthRangeParameters(0.0,1.0,1.0)",
|
|
612
|
+
type: "gl_DepthRangeParameters"
|
|
613
|
+
});
|
|
614
|
+
let s = {
|
|
615
|
+
uniforms: {},
|
|
616
|
+
flipY: r,
|
|
617
|
+
yUp: B,
|
|
618
|
+
names: t,
|
|
619
|
+
colorReaders: {},
|
|
620
|
+
prologue: n + be,
|
|
621
|
+
sample: A(e.stage === "fragment", j, ye),
|
|
622
|
+
samplerOf: j,
|
|
623
|
+
...ye ? { samplerMode: ye } : {},
|
|
624
|
+
entry: {
|
|
625
|
+
name: "main",
|
|
626
|
+
type: "void",
|
|
627
|
+
params: []
|
|
628
|
+
},
|
|
629
|
+
missingEntry: `Define void main() in the ${C(e.stage)}`,
|
|
630
|
+
epilogue: (e) => (a(e), "")
|
|
631
|
+
}, c;
|
|
632
|
+
try {
|
|
633
|
+
c = new i(e.program, s, e.version).emit();
|
|
634
|
+
} catch (t) {
|
|
635
|
+
throw t?.code === "failed" && (t.message = t.message.replace("GLSL translation: ", `GLSL translation: ${C(e.stage)}: `)), t;
|
|
636
|
+
}
|
|
637
|
+
return o ? c : c.replace(/^fn /gm, "@diagnostic(off,derivative_uniformity) fn ");
|
|
638
|
+
}, we = {
|
|
639
|
+
...$,
|
|
640
|
+
gl_Position: {
|
|
641
|
+
code: "glsl_Position",
|
|
642
|
+
type: "vec4"
|
|
643
|
+
},
|
|
644
|
+
gl_PointSize: {
|
|
645
|
+
code: "glsl_PointSize",
|
|
646
|
+
type: "float"
|
|
647
|
+
},
|
|
648
|
+
gl_VertexID: {
|
|
649
|
+
code: "glsl_VertexID",
|
|
650
|
+
type: "int"
|
|
651
|
+
},
|
|
652
|
+
gl_InstanceID: {
|
|
653
|
+
code: "glsl_InstanceID",
|
|
654
|
+
type: "int"
|
|
655
|
+
}
|
|
656
|
+
}, Te = P?.motion(L, ue, W, we, J, K) ?? "", Ee = Ce(L, we, "var<private> glsl_Position:vec4f;\nvar<private> glsl_PointSize:f32;\nvar<private> glsl_VertexID:i32;\nvar<private> glsl_InstanceID:i32;\n" + ge(L) + Te, (e) => xe = e), De = {
|
|
657
|
+
...$,
|
|
658
|
+
margo_encoding: {
|
|
659
|
+
code: "camera.encoding",
|
|
660
|
+
type: "float"
|
|
661
|
+
},
|
|
662
|
+
gl_FragCoord: {
|
|
663
|
+
code: "glsl_FragCoord",
|
|
664
|
+
type: "vec4"
|
|
665
|
+
},
|
|
666
|
+
gl_FrontFacing: {
|
|
667
|
+
code: "glsl_FrontFacing",
|
|
668
|
+
type: "bool"
|
|
669
|
+
},
|
|
670
|
+
gl_PointCoord: {
|
|
671
|
+
code: "glsl_PointCoord",
|
|
672
|
+
type: "vec2"
|
|
673
|
+
},
|
|
674
|
+
gl_FragColor: {
|
|
675
|
+
code: "glsl_FragColor",
|
|
676
|
+
type: "vec4"
|
|
677
|
+
},
|
|
678
|
+
gl_FragData: {
|
|
679
|
+
code: "glsl_FragData",
|
|
680
|
+
type: "vec4[1]"
|
|
681
|
+
},
|
|
682
|
+
gl_FragDepth: {
|
|
683
|
+
code: "glsl_FragDepth",
|
|
684
|
+
type: "float"
|
|
685
|
+
},
|
|
686
|
+
gl_FragDepthEXT: {
|
|
687
|
+
code: "glsl_FragDepth",
|
|
688
|
+
type: "float"
|
|
689
|
+
}
|
|
690
|
+
}, Oe = /\bgl_FragDepth(EXT)?\b/.test(R.text), ke = /\bgl_FragData\b/.test(R.text), Ae = Ce(R, De, "var<private> glsl_FragCoord:vec4f;\nvar<private> glsl_FrontFacing:bool;\nvar<private> glsl_PointCoord:vec2f=vec2f(0.5);\nvar<private> glsl_FragColor:vec4f;\nvar<private> glsl_FragData:array<vec4f,1>;\nvar<private> glsl_FragDepth:f32;\n" + ge(R), (e) => Se = e), je = (e) => e.map((e) => `,@location(${e.location}) ${ce(e.interpolation)}v${e.location}:${b(e.glsl)}`).join(""), Me = "let c=o.clip;\nif(any(abs(c.xy)>vec2f(c.w))||c.z<0.0||c.z>c.w){o.clip=vec4f(0.0,0.0,-1.0,1.0);}\nelse{o.clip=vec4f(c.xy+a.gl_PointCorner*max(glsl_PointSize,1.0)/camera.viewport.zw*c.w,c.zw);}\n", Ne = O(L, "gl_PointSize"), Pe = O(R, "gl_PointCoord") ? `,@location(${X.length}) margoPointCoord:vec2f` : "", Fe = !!P?.velocity, Ie = P?.now ?? "", Le = `struct GLSLVaryings{@builtin(position)${Fe ? " @invariant" : ""} clip:vec4f${je(X)}${Pe}}\n`, Re = O(L, "gl_VertexID") || O(L, "gl_InstanceID"), ze = (e, t = (e) => e) => {
|
|
691
|
+
let n = Y.map((n) => {
|
|
692
|
+
let r = (t) => e?.find((e) => e.name === t), i = y[n.name] ?? V?.attributes[n.name]?.default;
|
|
693
|
+
if (n.glsl.startsWith("mat")) {
|
|
694
|
+
let e = h(n.glsl), i = Array.from({ length: e }, (i, a) => {
|
|
695
|
+
let o = r(t(n.name) + a);
|
|
696
|
+
return ae(o && "a." + o.name, o?.type, `vec${e}`, [
|
|
697
|
+
0,
|
|
698
|
+
1,
|
|
699
|
+
2,
|
|
700
|
+
3
|
|
701
|
+
].map((e) => +(a === e)));
|
|
702
|
+
});
|
|
703
|
+
return `user_${n.name}=mat${e}x${e}f(${i.join(",")});`;
|
|
704
|
+
}
|
|
705
|
+
let a = r(t(n.name));
|
|
706
|
+
return `user_${n.name}=${ae(a && "a." + a.name, a?.type, n.glsl, i)};`;
|
|
707
|
+
}), r = Ne && !!e?.some((e) => e.name === "gl_PointCorner");
|
|
708
|
+
Re && n.push(r ? "glsl_VertexID=i32(margoInstanceIndex);glsl_InstanceID=0;" : "glsl_VertexID=i32(margoVertexIndex);glsl_InstanceID=i32(margoInstanceIndex);");
|
|
709
|
+
let i = [...e?.length ? ["a:Attributes"] : [], ...Re ? ["@builtin(vertex_index) margoVertexIndex:u32", "@builtin(instance_index) margoInstanceIndex:u32"] : []], a = Fe ? P.entry(i.join(","), n.join("\n"), _e(L), xe, B, r ? Me : "", L, we) : "";
|
|
710
|
+
return `${Ee}\n${Le}${a}@vertex fn margoGLSLVertex(${i.join(",")})->GLSLVaryings{
|
|
711
|
+
${n.join("\n")}
|
|
712
|
+
${Ie}${_e(L)}${xe}();
|
|
713
|
+
var o:GLSLVaryings;
|
|
714
|
+
o.clip=glsl_Position;${B ? "\n// GL clip z runs from -w to w with depth (z/w+1)/2; Margo stores 1 minus that depth (reversed Z).\no.clip.z=(o.clip.w-o.clip.z)*0.5;" : ""}
|
|
715
|
+
${r ? Me : ""}${Pe ? r ? "\no.margoPointCoord=vec2f(a.gl_PointCorner.x,-a.gl_PointCorner.y)*0.5+0.5;\n" : "o.margoPointCoord=vec2f(0.5);\n" : ""}${X.map((e) => `o.v${e.location}=user_${e.name};`).join("\n")}
|
|
716
|
+
return o;
|
|
717
|
+
}
|
|
718
|
+
`;
|
|
719
|
+
}, Be = Q ? {
|
|
720
|
+
float: `vec4f(user_${Q.name},0.0,0.0,1.0)`,
|
|
721
|
+
vec2: `vec4f(user_${Q.name},0.0,1.0)`,
|
|
722
|
+
vec3: `vec4f(user_${Q.name},1.0)`,
|
|
723
|
+
vec4: `user_${Q.name}`
|
|
724
|
+
}[Q.type] : ke ? "glsl_FragData[0]" : "glsl_FragColor", Ve = R.included.has("colorspace_fragment") ? Be : E === "linear" ? `margoDisplay(${Be})` : `margoGLSLOutput(${Be})`, He = `${Ae}
|
|
725
|
+
${Ve.startsWith("margoGLSLOutput") ? "fn margoGLSLOutput(c:vec4f)->vec4f{if(camera.encoding==1.0){return c;}let x=clamp(c.rgb,vec3f(0),vec3f(1));return vec4f(select(x/12.92,pow((x+0.055)/1.055,vec3f(2.4)),x>vec3f(0.04045)),c.a);}\n" : ""}
|
|
726
|
+
struct GLSLFragmentIn{@builtin(position) margoPosition:vec4f,@builtin(front_facing) margoFront:bool${je(me)}${Pe}}
|
|
727
|
+
struct GLSLFragmentOut{@location(0) color:vec4f${Oe ? ",@builtin(frag_depth) depth:f32" : ""}}
|
|
728
|
+
@fragment fn margoGLSLFragment(i:GLSLFragmentIn)->GLSLFragmentOut{
|
|
729
|
+
let p=i.margoPosition;
|
|
730
|
+
glsl_FragCoord=${B ? "vec4f(p.x,camera.resolution.y-p.y,1.0-p.z,p.w)" : "p"};
|
|
731
|
+
glsl_FrontFacing=i.margoFront;${Pe ? "\nglsl_PointCoord=i.margoPointCoord;" : ""}
|
|
732
|
+
${_e(R)}${me.map((e) => `user_${e.name}=i.v${e.location};`).join("\n")}
|
|
733
|
+
${Oe ? "glsl_FragDepth=glsl_FragCoord.z;\n" : ""}${Se}();
|
|
734
|
+
var o:GLSLFragmentOut;
|
|
735
|
+
o.color=${Ve};
|
|
736
|
+
${Oe ? `o.depth=${B ? "clamp(1.0-glsl_FragDepth,0.0,1.0)" : "glsl_FragDepth"};\n` : ""}return o;
|
|
737
|
+
}
|
|
738
|
+
`;
|
|
739
|
+
ne.count++;
|
|
740
|
+
let Ue = {
|
|
741
|
+
vertex: ze(Y.map((e) => ({
|
|
742
|
+
name: e.name,
|
|
743
|
+
type: b(e.glsl)
|
|
744
|
+
}))),
|
|
745
|
+
fragment: He,
|
|
746
|
+
uniforms: [...W.values()],
|
|
747
|
+
textures: [...G.values()],
|
|
748
|
+
attributes: Y,
|
|
749
|
+
varyings: X,
|
|
750
|
+
version: L.version,
|
|
751
|
+
builtins: H,
|
|
752
|
+
points: Ne,
|
|
753
|
+
scene: de,
|
|
754
|
+
velocity: Fe,
|
|
755
|
+
vertexFor: ze,
|
|
756
|
+
display: Ve.startsWith("margoGLSLOutput")
|
|
757
|
+
};
|
|
758
|
+
return S.size >= 64 && S.delete(S.keys().next().value), S.set(F, Ue), Ue;
|
|
759
|
+
}
|
|
760
|
+
//#endregion
|
|
761
|
+
//#region src/glsl/material.ts
|
|
762
|
+
var P = {
|
|
763
|
+
never: "never",
|
|
764
|
+
less: "greater",
|
|
765
|
+
equal: "equal",
|
|
766
|
+
"less-equal": "greater-equal",
|
|
767
|
+
greater: "less",
|
|
768
|
+
"not-equal": "not-equal",
|
|
769
|
+
"greater-equal": "less-equal",
|
|
770
|
+
always: "always"
|
|
771
|
+
}, F = (e) => {
|
|
772
|
+
let t = P[e];
|
|
773
|
+
if (!t) throw TypeError(`ShaderMaterial depthFunc must be one of ${Object.keys(P).join(", ")}`);
|
|
774
|
+
return t;
|
|
775
|
+
}, I = (e) => {
|
|
776
|
+
let t = /^(.*)\[(\d+)\]$/.exec(e);
|
|
777
|
+
if (t) return Array.from({ length: +t[2] }, () => I(t[1]));
|
|
778
|
+
let n = /^mat([234])(?:x([234]))?$/.exec(e);
|
|
779
|
+
return n ? Array(+n[1] * (n[2] ?? n[1])).fill(0) : /sampler/.test(e) ? null : e === "bool" ? !1 : /^[ibu]?vec(\d)$/.test(e) ? Array(Number(e.at(-1))).fill(0) : 0;
|
|
780
|
+
};
|
|
781
|
+
function L(e, t = []) {
|
|
782
|
+
let n = e;
|
|
783
|
+
for (let e of t) {
|
|
784
|
+
if (typeof n != "object" || !n) return null;
|
|
785
|
+
if (typeof e == "string") n = n[e];
|
|
786
|
+
else {
|
|
787
|
+
let [t, r] = e;
|
|
788
|
+
n = r && typeof n[0] != "object" ? r === 1 ? n[t] : Array.prototype.slice.call(n, t * r, t * r + r) : n[t];
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
return n ?? null;
|
|
792
|
+
}
|
|
793
|
+
function R(e) {
|
|
794
|
+
let t = {};
|
|
795
|
+
for (let n of e) {
|
|
796
|
+
let e = t, r = "v";
|
|
797
|
+
for (let t of n.path ?? []) e = e[r] ??= typeof t == "string" ? {} : [], r = typeof t == "string" ? t : t[0];
|
|
798
|
+
e[r] = I(n.glsl);
|
|
799
|
+
}
|
|
800
|
+
return t.v;
|
|
801
|
+
}
|
|
802
|
+
function z(e, t) {
|
|
803
|
+
if (t.count) {
|
|
804
|
+
let n = typeof e != "object" || !e || !("length" in e) ? I(t.glsl) : e, r = {
|
|
805
|
+
...t,
|
|
806
|
+
glsl: t.glsl.replace(/\[\d+\]$/, ""),
|
|
807
|
+
count: void 0
|
|
808
|
+
};
|
|
809
|
+
return typeof n[0] == "number" ? Array.from(n) : Array.from(n, (e) => [z(e, r)].flat(2));
|
|
810
|
+
}
|
|
811
|
+
let n = /^mat([234])x([234])$/.exec(t.glsl);
|
|
812
|
+
if (n) {
|
|
813
|
+
let [r, i] = [+n[1], +n[2]], a = z(e, {
|
|
814
|
+
...t,
|
|
815
|
+
glsl: "vec4"
|
|
816
|
+
}), o = Array(16).fill(0);
|
|
817
|
+
for (let e = 0; e < r; e++) for (let t = 0; t < i; t++) o[e * 4 + t] = a[e * i + t] ?? 0;
|
|
818
|
+
return o;
|
|
819
|
+
}
|
|
820
|
+
let r = /^b/.test(t.glsl), i = (e, n) => {
|
|
821
|
+
if (typeof e == "boolean") return +e;
|
|
822
|
+
if (typeof e == "number") return r ? +(e !== 0) : e;
|
|
823
|
+
throw RangeError(`ShaderMaterial uniform ${t.name}${n} needs numbers or booleans for ${t.glsl}; assign a matching value`);
|
|
824
|
+
};
|
|
825
|
+
if (e == null) return z(I(t.glsl), t);
|
|
826
|
+
if (typeof e == "number" || typeof e == "boolean") return i(e, "");
|
|
827
|
+
if (typeof e == "string") return t.glsl === "vec3" ? e : i(e, "");
|
|
828
|
+
let a = e;
|
|
829
|
+
return (typeof a.length == "number" ? Array.from(a) : "x" in a ? [
|
|
830
|
+
a.x,
|
|
831
|
+
a.y,
|
|
832
|
+
a.z,
|
|
833
|
+
a.w
|
|
834
|
+
].filter((e) => e !== void 0) : []).map((e, t) => i(e, `[${t}]`));
|
|
835
|
+
}
|
|
836
|
+
var B = class e extends t {
|
|
837
|
+
constructor(t, n) {
|
|
838
|
+
if (!t || typeof t != "object") throw TypeError("ShaderMaterial needs {vertexShader, fragmentShader}");
|
|
839
|
+
let { uniforms: r = {}, builtins: i, defines: a, glslVersion: o, flipY: s, clipSpace: c, strict: l, attributes: u, defaultAttributeValues: d, includes: f, outputColorSpace: ee, unfilterable: p, lights: m, fog: h, velocity: g, side: _, blending: v, blendSrc: y = "src-alpha", blendDst: b = "one-minus-src-alpha", blendEquation: x = "add", blendSrcAlpha: te, blendDstAlpha: ne, blendEquationAlpha: S, depthFunc: C, polygonOffset: w = !1, polygonOffsetFactor: T = 0, polygonOffsetUnits: E = 0, vertexShader: re, fragmentShader: D, ...O } = t;
|
|
840
|
+
if (!r || typeof r != "object") throw TypeError("ShaderMaterial uniforms must be an object of {value}");
|
|
841
|
+
if (_ !== void 0 && O.cull !== void 0) throw TypeError("ShaderMaterial takes side or cull, not both; pass only one of them");
|
|
842
|
+
let k = {
|
|
843
|
+
vertexShader: t.vertexShader,
|
|
844
|
+
fragmentShader: t.fragmentShader,
|
|
845
|
+
defines: { ...a },
|
|
846
|
+
glslVersion: o,
|
|
847
|
+
flipY: s,
|
|
848
|
+
clipSpace: c,
|
|
849
|
+
strict: l,
|
|
850
|
+
builtins: i,
|
|
851
|
+
uniformNames: Object.keys(r),
|
|
852
|
+
attributes: u,
|
|
853
|
+
defaultAttributeValues: d,
|
|
854
|
+
includes: f,
|
|
855
|
+
outputColorSpace: ee,
|
|
856
|
+
unfilterable: p,
|
|
857
|
+
lights: m,
|
|
858
|
+
fog: h,
|
|
859
|
+
velocity: g
|
|
860
|
+
}, A = N(k, n?.link), j = {}, M = {};
|
|
861
|
+
for (let [e, t] of Object.entries(r)) {
|
|
862
|
+
if (!t || typeof t != "object" || !("value" in t)) throw TypeError(`ShaderMaterial uniform ${e} must be {value}`);
|
|
863
|
+
j[e] = t;
|
|
864
|
+
}
|
|
865
|
+
n?.uniforms(A, j, M), super({
|
|
866
|
+
...O,
|
|
867
|
+
...v === "custom" ? { blending: {
|
|
868
|
+
color: {
|
|
869
|
+
srcFactor: y,
|
|
870
|
+
dstFactor: b,
|
|
871
|
+
operation: x
|
|
872
|
+
},
|
|
873
|
+
alpha: {
|
|
874
|
+
srcFactor: te ?? y,
|
|
875
|
+
dstFactor: ne ?? b,
|
|
876
|
+
operation: S ?? x
|
|
877
|
+
}
|
|
878
|
+
} } : v === void 0 ? {} : { blending: v },
|
|
879
|
+
...C === void 0 ? {} : { depthCompare: F(C) },
|
|
880
|
+
...e.parts(A, j),
|
|
881
|
+
vertexEntry: "margoGLSLVertex",
|
|
882
|
+
fragmentEntry: "margoGLSLFragment",
|
|
883
|
+
...A.velocity ? { velocityEntry: "margoGLSLVelocity" } : {}
|
|
884
|
+
}), this.isShaderMaterial = !0, this.offset = [
|
|
885
|
+
!1,
|
|
886
|
+
0,
|
|
887
|
+
0
|
|
888
|
+
], M.material = this, this.holder = M, this.scene = n, this.linked = A, this.source = k, this.publish(j), _ !== void 0 && (this.side = _), this.offset = [
|
|
889
|
+
w,
|
|
890
|
+
T,
|
|
891
|
+
E
|
|
892
|
+
], this.applyOffset();
|
|
893
|
+
}
|
|
894
|
+
applyOffset() {
|
|
895
|
+
let [e, t, n] = this.offset;
|
|
896
|
+
if (!Number.isFinite(t) || !Number.isFinite(n)) throw RangeError("ShaderMaterial polygonOffsetFactor and polygonOffsetUnits must be finite");
|
|
897
|
+
this.depthBias = e ? -n : 0, this.depthBiasSlopeScale = e ? -t : 0;
|
|
898
|
+
}
|
|
899
|
+
get side() {
|
|
900
|
+
return this.cull === "none" ? "double" : this.cull === "front" ? "back" : "front";
|
|
901
|
+
}
|
|
902
|
+
set side(e) {
|
|
903
|
+
let t = {
|
|
904
|
+
front: "back",
|
|
905
|
+
back: "front",
|
|
906
|
+
double: "none"
|
|
907
|
+
}[e];
|
|
908
|
+
if (!t) throw TypeError("ShaderMaterial side must be 'front', 'back' or 'double'");
|
|
909
|
+
this.cull = t;
|
|
910
|
+
}
|
|
911
|
+
get depthFunc() {
|
|
912
|
+
return F(this.depthCompare);
|
|
913
|
+
}
|
|
914
|
+
set depthFunc(e) {
|
|
915
|
+
this.depthCompare = F(e);
|
|
916
|
+
}
|
|
917
|
+
get polygonOffset() {
|
|
918
|
+
return this.offset[0];
|
|
919
|
+
}
|
|
920
|
+
set polygonOffset(e) {
|
|
921
|
+
this.offset[0] = !!e, this.applyOffset();
|
|
922
|
+
}
|
|
923
|
+
get polygonOffsetFactor() {
|
|
924
|
+
return this.offset[1];
|
|
925
|
+
}
|
|
926
|
+
set polygonOffsetFactor(e) {
|
|
927
|
+
this.offset[1] = e, this.applyOffset();
|
|
928
|
+
}
|
|
929
|
+
get polygonOffsetUnits() {
|
|
930
|
+
return this.offset[2];
|
|
931
|
+
}
|
|
932
|
+
set polygonOffsetUnits(e) {
|
|
933
|
+
this.offset[2] = e, this.applyOffset();
|
|
934
|
+
}
|
|
935
|
+
get program() {
|
|
936
|
+
let { vertexFor: e, display: t, ...n } = this.linked;
|
|
937
|
+
return n;
|
|
938
|
+
}
|
|
939
|
+
static parts(e, t) {
|
|
940
|
+
let r = {}, i = {}, a = /* @__PURE__ */ new Map();
|
|
941
|
+
for (let n of e.uniforms) n.root ? a.set(n.root, [...a.get(n.root) ?? [], n]) : t[n.name] ??= { value: I(n.glsl) }, r[n.member] = {
|
|
942
|
+
type: n.type,
|
|
943
|
+
...n.count ? { count: n.count } : {},
|
|
944
|
+
get value() {
|
|
945
|
+
return z(n.root ? L(t[n.root]?.value, n.path) : t[n.name]?.value, n);
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
for (let [e, n] of a) t[e] ??= { value: R(n) };
|
|
949
|
+
for (let r of e.textures) {
|
|
950
|
+
let e = r.root ?? r.name;
|
|
951
|
+
t[e] ??= { value: r.root ? [] : null };
|
|
952
|
+
let a = () => {
|
|
953
|
+
let n = t[e]?.value;
|
|
954
|
+
return r.index === void 0 ? n : n?.[r.index];
|
|
955
|
+
}, o = () => {
|
|
956
|
+
let e = a();
|
|
957
|
+
return n(e) && "texture" in e ? e : void 0;
|
|
958
|
+
};
|
|
959
|
+
i[r.binding] = {
|
|
960
|
+
dimension: r.dimension,
|
|
961
|
+
...r.sampleType ? { sampleType: r.sampleType } : {},
|
|
962
|
+
get texture() {
|
|
963
|
+
return (o() ? o().texture : a()) ?? null;
|
|
964
|
+
},
|
|
965
|
+
get sampler() {
|
|
966
|
+
return o()?.sampler;
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
}
|
|
970
|
+
return {
|
|
971
|
+
vertexShader: e.vertex,
|
|
972
|
+
fragmentShader: e.fragment,
|
|
973
|
+
uniforms: r,
|
|
974
|
+
textures: i
|
|
975
|
+
};
|
|
976
|
+
}
|
|
977
|
+
publish(e) {
|
|
978
|
+
let t = this.uniforms;
|
|
979
|
+
for (let e of Object.keys(t)) Object.defineProperty(t, e, { enumerable: !1 });
|
|
980
|
+
Object.assign(t, e);
|
|
981
|
+
for (let n of Object.keys(e)) Object.defineProperty(e, n, {
|
|
982
|
+
get: () => t[n],
|
|
983
|
+
set: (e) => t[n] = e,
|
|
984
|
+
enumerable: !0,
|
|
985
|
+
configurable: !0
|
|
986
|
+
});
|
|
987
|
+
}
|
|
988
|
+
get vertexShader() {
|
|
989
|
+
return this.source.vertexShader;
|
|
990
|
+
}
|
|
991
|
+
set vertexShader(e) {
|
|
992
|
+
this.relink({
|
|
993
|
+
...this.source,
|
|
994
|
+
vertexShader: e
|
|
995
|
+
});
|
|
996
|
+
}
|
|
997
|
+
get fragmentShader() {
|
|
998
|
+
return this.source.fragmentShader;
|
|
999
|
+
}
|
|
1000
|
+
set fragmentShader(e) {
|
|
1001
|
+
this.relink({
|
|
1002
|
+
...this.source,
|
|
1003
|
+
fragmentShader: e
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
get defines() {
|
|
1007
|
+
return this.source.defines;
|
|
1008
|
+
}
|
|
1009
|
+
set defines(e) {
|
|
1010
|
+
this.relink({
|
|
1011
|
+
...this.source,
|
|
1012
|
+
defines: { ...e }
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
set needsUpdate(e) {
|
|
1016
|
+
e && this.relink({
|
|
1017
|
+
...this.source,
|
|
1018
|
+
defines: { ...this.source.defines }
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
code(e, t) {
|
|
1022
|
+
return this.prelude(t) + (e === "fragment" ? this.linked.fragment : this.linked.vertexFor(t?.layout ?? []));
|
|
1023
|
+
}
|
|
1024
|
+
relink(n) {
|
|
1025
|
+
let r = N(n, this.scene?.link), i = this, a = this.uniforms, o = {};
|
|
1026
|
+
for (let e of Object.keys(a)) o[e] = a[e];
|
|
1027
|
+
this.scene?.uniforms(r, o, this.holder);
|
|
1028
|
+
let s = e.parts(r, o), c = (e) => JSON.stringify([e.uniforms, e.textures]);
|
|
1029
|
+
if (c(r) !== c(this.linked)) {
|
|
1030
|
+
let e = new t({
|
|
1031
|
+
...s,
|
|
1032
|
+
vertexEntry: "margoGLSLVertex",
|
|
1033
|
+
fragmentEntry: "margoGLSLFragment"
|
|
1034
|
+
});
|
|
1035
|
+
this.dispose();
|
|
1036
|
+
for (let t of [
|
|
1037
|
+
"members",
|
|
1038
|
+
"size",
|
|
1039
|
+
"bytes",
|
|
1040
|
+
"data",
|
|
1041
|
+
"textures",
|
|
1042
|
+
"uniforms"
|
|
1043
|
+
]) Object.defineProperty(i, t, {
|
|
1044
|
+
value: e[t],
|
|
1045
|
+
writable: !0,
|
|
1046
|
+
configurable: !0
|
|
1047
|
+
});
|
|
1048
|
+
this.publish(o);
|
|
1049
|
+
}
|
|
1050
|
+
this.linked = r, this.source = n, i.setCode.call(this, r.vertex, r.fragment);
|
|
1051
|
+
}
|
|
1052
|
+
}, V = {
|
|
1053
|
+
position: ["positions", 3],
|
|
1054
|
+
normal: ["normals", 3],
|
|
1055
|
+
uv: ["uvs", 2],
|
|
1056
|
+
uv1: ["uvs1", 2],
|
|
1057
|
+
tangent: ["tangents", 4],
|
|
1058
|
+
color: ["colors", 0]
|
|
1059
|
+
}, H = (e, t) => {
|
|
1060
|
+
let n = /^(int|ivec)/.test(e) ? "i" : /^(uint|uvec)/.test(e) ? "u" : "f";
|
|
1061
|
+
return t === 1 ? {
|
|
1062
|
+
f: "f32",
|
|
1063
|
+
i: "i32",
|
|
1064
|
+
u: "u32"
|
|
1065
|
+
}[n] : `vec${t}${n}`;
|
|
1066
|
+
};
|
|
1067
|
+
function le(e, t, n = {}) {
|
|
1068
|
+
if (!(t instanceof B)) throw TypeError("shaderMesh needs a ShaderMaterial");
|
|
1069
|
+
if (!e || typeof e != "object") throw TypeError("shaderMesh needs geometry: {positions, ...} or a core Geometry");
|
|
1070
|
+
let { attributes: i = {}, instances: a = {}, index: o, ...s } = n, c = t.program, l = e.vertices, u = l ? l.length / 6 : (e.positions?.length ?? 0) / 3, d = {}, f = {
|
|
1071
|
+
vertex: /* @__PURE__ */ new Set(),
|
|
1072
|
+
instance: /* @__PURE__ */ new Set()
|
|
1073
|
+
}, ee = (t) => {
|
|
1074
|
+
let n = V[t];
|
|
1075
|
+
if (!n) return;
|
|
1076
|
+
if (l && (t === "position" || t === "normal")) {
|
|
1077
|
+
let e = t === "position" ? 0 : 3, n = new Float32Array(u * 3);
|
|
1078
|
+
for (let t = 0; t < u; t++) for (let r = 0; r < 3; r++) n[t * 3 + r] = l[t * 6 + e + r];
|
|
1079
|
+
return [n, 3];
|
|
1080
|
+
}
|
|
1081
|
+
let r = e[n[0]];
|
|
1082
|
+
if (r) return [r, n[1] || (u ? r.length / u : 0)];
|
|
1083
|
+
}, p;
|
|
1084
|
+
for (let e of c.attributes) {
|
|
1085
|
+
let t = Number(/(?:vec|mat)([234])/.exec(e.glsl)?.[1] ?? 1), n = e.glsl.startsWith("mat"), r = a[e.name] ?? (e.semantic ? a[e.semantic] : void 0), o = r ?? i[e.name], s = o ? [o, n ? t * t : t] : e.semantic ? ee(e.semantic) : void 0;
|
|
1086
|
+
if (!s) continue;
|
|
1087
|
+
let [c, l] = s, u = r ? "instance" : "vertex";
|
|
1088
|
+
if (!(l >= 1) || c.length % l) throw RangeError(`shaderMesh attribute ${e.name} needs a multiple of ${l} numbers for ${e.glsl}`);
|
|
1089
|
+
if (f[u].add(c.length / l), e.semantic === "position" && u === "vertex" && l === 3 && (p = c), n) {
|
|
1090
|
+
let n = c.length / (t * t);
|
|
1091
|
+
for (let r = 0; r < t; r++) {
|
|
1092
|
+
let i = new Float32Array(n * t);
|
|
1093
|
+
for (let e = 0; e < n; e++) for (let n = 0; n < t; n++) i[e * t + n] = c[e * t * t + r * t + n];
|
|
1094
|
+
d[e.name + r] = {
|
|
1095
|
+
data: i,
|
|
1096
|
+
type: H("vec", t),
|
|
1097
|
+
step: u
|
|
1098
|
+
};
|
|
1099
|
+
}
|
|
1100
|
+
} else d[e.name] = {
|
|
1101
|
+
data: c,
|
|
1102
|
+
type: H(e.glsl, l),
|
|
1103
|
+
step: u
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
if (f.vertex.size > 1) throw RangeError(`shaderMesh per-vertex attributes hold ${[...f.vertex].join(" and ")} vertices; give each one element a vertex`);
|
|
1107
|
+
if (f.instance.size > 1) throw RangeError(`shaderMesh per-instance attributes hold ${[...f.instance].join(" and ")} instances; give each one element an instance`);
|
|
1108
|
+
let m = s.instanceCount ?? [...f.instance][0], h = s.bounds;
|
|
1109
|
+
if (h === void 0 && p && !f.instance.size && p.length) {
|
|
1110
|
+
let e = [
|
|
1111
|
+
Infinity,
|
|
1112
|
+
Infinity,
|
|
1113
|
+
Infinity
|
|
1114
|
+
], t = [
|
|
1115
|
+
-Infinity,
|
|
1116
|
+
-Infinity,
|
|
1117
|
+
-Infinity
|
|
1118
|
+
];
|
|
1119
|
+
for (let n = 0; n < p.length; n += 3) for (let r = 0; r < 3; r++) e[r] = Math.min(e[r], p[n + r]), t[r] = Math.max(t[r], p[n + r]);
|
|
1120
|
+
let n = [
|
|
1121
|
+
0,
|
|
1122
|
+
1,
|
|
1123
|
+
2
|
|
1124
|
+
].map((n) => (e[n] + t[n]) / 2), r = 0;
|
|
1125
|
+
for (let e = 0; e < p.length; e += 3) r = Math.max(r, Math.hypot(p[e] - n[0], p[e + 1] - n[1], p[e + 2] - n[2]));
|
|
1126
|
+
h = {
|
|
1127
|
+
center: n,
|
|
1128
|
+
radius: r * (1 + 1e-6) + 1e-7
|
|
1129
|
+
};
|
|
1130
|
+
}
|
|
1131
|
+
let g = s.topology ?? "triangle-list", _ = o ? Array.from(o) : e.indices ? Array.from(e.indices) : null, v = m, y = [...f.vertex][0] ?? 0;
|
|
1132
|
+
if (c.varyings.some((e) => e.interpolation === "flat") && g !== "point-list") {
|
|
1133
|
+
let t = (e) => _ ? _[e] : e, n = _ ? _.length : y, r = (o ?? e.indices) instanceof Uint16Array ? 65535 : 4294967295, i = [], a = g.startsWith("line"), s = g.endsWith("strip"), c = a ? 2 : 3;
|
|
1134
|
+
for (let e = 0, o = 0; o <= n; o++) if (!(o < n && !(s && _ && t(o) === r))) {
|
|
1135
|
+
for (let n = e; n + c <= o; n += s ? 1 : c) {
|
|
1136
|
+
let r = Array.from({ length: c }, (e, r) => t(n + r));
|
|
1137
|
+
a ? i.push(r[1], r[0]) : s && (n - e) % 2 ? i.push(r[2], r[1], r[0]) : i.push(r[2], r[0], r[1]);
|
|
1138
|
+
}
|
|
1139
|
+
e = o + 1;
|
|
1140
|
+
}
|
|
1141
|
+
_ = i, g = a ? "line-list" : "triangle-list";
|
|
1142
|
+
}
|
|
1143
|
+
if (g === "point-list" && c.points) {
|
|
1144
|
+
if (f.instance.size) throw RangeError("shaderMesh draws sized points one instance a point; give a point list with gl_PointSize no per-instance attributes");
|
|
1145
|
+
let e = _;
|
|
1146
|
+
for (let [t, n] of Object.entries(d)) {
|
|
1147
|
+
let r = Number(/vec([234])/.exec(n.type)?.[1] ?? 1), i = e ? Float32Array.from(e.flatMap((e) => Array.from({ length: r }, (t, i) => n.data[e * r + i]))) : n.data;
|
|
1148
|
+
d[t] = {
|
|
1149
|
+
...n,
|
|
1150
|
+
data: i,
|
|
1151
|
+
step: "instance"
|
|
1152
|
+
};
|
|
1153
|
+
}
|
|
1154
|
+
d.gl_PointCorner = {
|
|
1155
|
+
data: [
|
|
1156
|
+
-1,
|
|
1157
|
+
-1,
|
|
1158
|
+
1,
|
|
1159
|
+
-1,
|
|
1160
|
+
1,
|
|
1161
|
+
1,
|
|
1162
|
+
-1,
|
|
1163
|
+
-1,
|
|
1164
|
+
1,
|
|
1165
|
+
1,
|
|
1166
|
+
-1,
|
|
1167
|
+
1
|
|
1168
|
+
],
|
|
1169
|
+
type: "vec2f"
|
|
1170
|
+
}, v = e ? e.length : y, _ = null, g = "triangle-list";
|
|
1171
|
+
}
|
|
1172
|
+
return new r({
|
|
1173
|
+
frustumCulled: c.builtins !== !1,
|
|
1174
|
+
...s,
|
|
1175
|
+
topology: g,
|
|
1176
|
+
...h === void 0 ? {} : { bounds: h },
|
|
1177
|
+
...v === void 0 ? {} : { instanceCount: v },
|
|
1178
|
+
material: t,
|
|
1179
|
+
attributes: d,
|
|
1180
|
+
index: _
|
|
1181
|
+
});
|
|
1182
|
+
}
|
|
1183
|
+
//#endregion
|
|
1184
|
+
export { x as a, g as c, O as d, l as f, D as i, w as l, le as n, N as o, d as p, ne as r, k as s, B as t, m as u };
|