micugl 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -1
- package/dist/MarbleScene.d.ts +27 -0
- package/dist/RippleScene.d.ts +20 -0
- package/dist/examples/Marble/MarbleScene.d.ts +20 -0
- package/dist/examples/Marble/MarbleScene.js +1 -0
- package/dist/examples/Marble/MarbleScene.mjs +71 -0
- package/dist/examples/Marble/marbleShaders.d.ts +2 -0
- package/dist/{MarbleScene-B1gMFEHc.js → examples/Marble/marbleShaders.js} +3 -3
- package/dist/{MarbleScene-DmZt4s6L.mjs → examples/Marble/marbleShaders.mjs} +5 -70
- package/dist/examples/Ripple/RippleScene.d.ts +13 -0
- package/dist/examples/Ripple/RippleScene.js +1 -0
- package/dist/examples/Ripple/RippleScene.mjs +109 -0
- package/dist/examples/Ripple/rippleShaders.d.ts +3 -0
- package/dist/examples/Ripple/rippleShaders.js +80 -0
- package/dist/examples/Ripple/rippleShaders.mjs +94 -0
- package/dist/examples/index.d.ts +4 -4
- package/dist/examples/index.js +1 -1
- package/dist/examples/index.mjs +9 -7
- package/dist/index.d.ts +35 -9
- package/dist/index.js +1 -1
- package/dist/index.mjs +35 -146
- package/dist/marbleShaders.d.ts +5 -0
- package/dist/rippleShaders.d.ts +7 -0
- package/dist/src/core/lib/createShaderConfig.js +1 -0
- package/dist/src/core/lib/createShaderConfig.mjs +34 -0
- package/dist/src/core/lib/vectorUtils.js +1 -0
- package/dist/src/core/lib/vectorUtils.mjs +36 -0
- package/dist/src/core/managers/FBOManager.js +1 -0
- package/dist/src/core/managers/FBOManager.mjs +120 -0
- package/dist/src/core/managers/WebGLManager.js +1 -0
- package/dist/src/core/managers/WebGLManager.mjs +273 -0
- package/dist/src/core/systems/Passes.js +1 -0
- package/dist/src/core/systems/Passes.mjs +78 -0
- package/dist/src/core/systems/Postprocessing.js +1 -0
- package/dist/src/core/systems/Postprocessing.mjs +128 -0
- package/dist/src/react/components/base/BasePingPongShaderComponent.js +1 -0
- package/dist/src/react/components/base/BasePingPongShaderComponent.mjs +48 -0
- package/dist/src/react/components/base/BaseShaderComponent.js +1 -0
- package/dist/src/react/components/base/BaseShaderComponent.mjs +34 -0
- package/dist/src/react/components/engine/PingPongShaderEngine.js +1 -0
- package/dist/src/react/components/engine/PingPongShaderEngine.mjs +68 -0
- package/dist/src/react/components/engine/ShaderEngine.js +1 -0
- package/dist/src/react/components/engine/ShaderEngine.mjs +106 -0
- package/dist/src/react/hooks/useDarkMode.js +1 -0
- package/dist/src/react/hooks/useDarkMode.mjs +22 -0
- package/dist/src/react/hooks/usePingPongPasses.js +1 -0
- package/dist/src/react/hooks/usePingPongPasses.mjs +93 -0
- package/dist/src/react/hooks/useUniformUpdaters.js +1 -0
- package/dist/src/react/hooks/useUniformUpdaters.mjs +12 -0
- package/dist/src/react/lib/createUniformUpdater.js +1 -0
- package/dist/src/react/lib/createUniformUpdater.mjs +28 -0
- package/dist/types.d.ts +204 -0
- package/dist/types.js +1 -0
- package/dist/types.mjs +1 -0
- package/package.json +44 -9
- package/dist/BasePingPongShaderComponent-BvRm7-g5.js +0 -1
- package/dist/BasePingPongShaderComponent-FY62Kl02.mjs +0 -273
- package/dist/BaseShaderComponent-BSBIvn4B.js +0 -1
- package/dist/BaseShaderComponent-Be_evz2F.mjs +0 -151
- package/dist/SimpleRippleScene--B1BrlxP.js +0 -80
- package/dist/SimpleRippleScene-Ba-EPdTJ.mjs +0 -197
- package/dist/examples/Marble/index.d.ts +0 -3
- package/dist/examples/Marble/index.js +0 -1
- package/dist/examples/Marble/index.mjs +0 -6
- package/dist/examples/SimpleRipple/index.d.ts +0 -3
- package/dist/examples/SimpleRipple/index.js +0 -1
- package/dist/examples/SimpleRipple/index.mjs +0 -7
- package/dist/useUniformUpdaters-D5WtqZpp.mjs +0 -456
- package/dist/useUniformUpdaters-DGX0mf9g.js +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("../../MarbleScene-B1gMFEHc.js");exports.Marble=e.Marble;exports.marbleFragmentShader=e.marbleFragmentShader;exports.marbleVertexShader=e.marbleVertexShader;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("../../SimpleRippleScene--B1BrlxP.js");exports.SimpleRipple=e.SimpleRipple;exports.rippleRenderShader=e.rippleRenderShader;exports.rippleSimulationShader=e.rippleSimulationShader;exports.rippleVertexShader=e.rippleVertexShader;
|
|
@@ -1,456 +0,0 @@
|
|
|
1
|
-
import { useMemo as g } from "react";
|
|
2
|
-
class w {
|
|
3
|
-
constructor(s) {
|
|
4
|
-
this.resources = /* @__PURE__ */ new Map(), this.floatTextureExtension = null, this.gl = s, this.floatTextureExtension = s.getExtension("OES_texture_float"), this.floatTexturesSupported = !!this.floatTextureExtension, s.getExtension("OES_texture_float_linear");
|
|
5
|
-
}
|
|
6
|
-
createFramebuffer(s, e) {
|
|
7
|
-
const t = this.gl, { width: r, height: o, textureCount: a = 2, textureOptions: i = {} } = e, c = [];
|
|
8
|
-
for (let h = 0; h < a; h++) {
|
|
9
|
-
const l = this.createTexture({
|
|
10
|
-
width: r,
|
|
11
|
-
height: o,
|
|
12
|
-
...i
|
|
13
|
-
});
|
|
14
|
-
c.push(l);
|
|
15
|
-
}
|
|
16
|
-
const u = t.createFramebuffer();
|
|
17
|
-
if (!u)
|
|
18
|
-
throw new Error("Failed to create framebuffer");
|
|
19
|
-
const f = {
|
|
20
|
-
framebuffer: u,
|
|
21
|
-
textures: c,
|
|
22
|
-
currentTextureIndex: 0,
|
|
23
|
-
width: r,
|
|
24
|
-
height: o
|
|
25
|
-
};
|
|
26
|
-
return this.resources.set(s, f), f;
|
|
27
|
-
}
|
|
28
|
-
createTexture(s) {
|
|
29
|
-
const e = this.gl, {
|
|
30
|
-
width: t,
|
|
31
|
-
height: r,
|
|
32
|
-
internalFormat: o = e.RGBA,
|
|
33
|
-
format: a = e.RGBA,
|
|
34
|
-
type: i = this.floatTexturesSupported ? e.FLOAT : e.UNSIGNED_BYTE,
|
|
35
|
-
minFilter: c = e.NEAREST,
|
|
36
|
-
magFilter: u = e.NEAREST,
|
|
37
|
-
wrapS: f = e.CLAMP_TO_EDGE,
|
|
38
|
-
wrapT: h = e.CLAMP_TO_EDGE,
|
|
39
|
-
generateMipmap: l = !1
|
|
40
|
-
} = s, n = e.createTexture();
|
|
41
|
-
if (!n)
|
|
42
|
-
throw new Error("Failed to create texture");
|
|
43
|
-
return e.bindTexture(e.TEXTURE_2D, n), e.texImage2D(e.TEXTURE_2D, 0, o, t, r, 0, a, i, null), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, c), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, u), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, f), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, h), l && e.generateMipmap(e.TEXTURE_2D), n;
|
|
44
|
-
}
|
|
45
|
-
bindFramebuffer(s, e) {
|
|
46
|
-
const t = this.gl;
|
|
47
|
-
if (s === null) {
|
|
48
|
-
t.bindFramebuffer(t.FRAMEBUFFER, null);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
const r = this.resources.get(s);
|
|
52
|
-
if (!r)
|
|
53
|
-
throw new Error(`Framebuffer with id ${s} not found`);
|
|
54
|
-
const o = e ?? r.currentTextureIndex;
|
|
55
|
-
t.bindFramebuffer(t.FRAMEBUFFER, r.framebuffer), t.framebufferTexture2D(
|
|
56
|
-
t.FRAMEBUFFER,
|
|
57
|
-
t.COLOR_ATTACHMENT0,
|
|
58
|
-
t.TEXTURE_2D,
|
|
59
|
-
r.textures[o],
|
|
60
|
-
0
|
|
61
|
-
);
|
|
62
|
-
const a = t.checkFramebufferStatus(t.FRAMEBUFFER);
|
|
63
|
-
if (a !== t.FRAMEBUFFER_COMPLETE)
|
|
64
|
-
throw new Error(`Framebuffer is not complete: ${a}`);
|
|
65
|
-
}
|
|
66
|
-
bindTexture(s, e, t) {
|
|
67
|
-
const r = this.gl, o = this.resources.get(s);
|
|
68
|
-
if (!o)
|
|
69
|
-
throw new Error(`Framebuffer with id ${s} not found`);
|
|
70
|
-
const a = t ?? o.currentTextureIndex;
|
|
71
|
-
r.activeTexture(r.TEXTURE0 + e), r.bindTexture(r.TEXTURE_2D, o.textures[a]);
|
|
72
|
-
}
|
|
73
|
-
swapTextures(s) {
|
|
74
|
-
const e = this.resources.get(s);
|
|
75
|
-
if (!e)
|
|
76
|
-
throw new Error(`Framebuffer with id ${s} not found`);
|
|
77
|
-
e.currentTextureIndex = (e.currentTextureIndex + 1) % e.textures.length;
|
|
78
|
-
}
|
|
79
|
-
getPingPongIndices(s) {
|
|
80
|
-
const e = this.resources.get(s);
|
|
81
|
-
if (!e)
|
|
82
|
-
throw new Error(`Framebuffer with id ${s} not found`);
|
|
83
|
-
const t = e.currentTextureIndex, r = (t + 1) % e.textures.length;
|
|
84
|
-
return { read: t, write: r };
|
|
85
|
-
}
|
|
86
|
-
resizeFramebuffer(s, e, t) {
|
|
87
|
-
const r = this.gl, o = this.resources.get(s);
|
|
88
|
-
if (!o)
|
|
89
|
-
throw new Error(`Framebuffer with id ${s} not found`);
|
|
90
|
-
o.width === e && o.height === t || (o.textures.forEach((a) => {
|
|
91
|
-
r.bindTexture(r.TEXTURE_2D, a), r.texImage2D(
|
|
92
|
-
r.TEXTURE_2D,
|
|
93
|
-
0,
|
|
94
|
-
r.RGBA,
|
|
95
|
-
e,
|
|
96
|
-
t,
|
|
97
|
-
0,
|
|
98
|
-
r.RGBA,
|
|
99
|
-
this.floatTexturesSupported ? r.FLOAT : r.UNSIGNED_BYTE,
|
|
100
|
-
null
|
|
101
|
-
);
|
|
102
|
-
}), o.width = e, o.height = t);
|
|
103
|
-
}
|
|
104
|
-
destroy(s) {
|
|
105
|
-
const e = this.gl, t = this.resources.get(s);
|
|
106
|
-
t && (t.textures.forEach((r) => {
|
|
107
|
-
e.deleteTexture(r);
|
|
108
|
-
}), e.deleteFramebuffer(t.framebuffer), this.resources.delete(s));
|
|
109
|
-
}
|
|
110
|
-
destroyAll() {
|
|
111
|
-
Array.from(this.resources.keys()).forEach((s) => {
|
|
112
|
-
this.destroy(s);
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
isFloatTexturesSupported() {
|
|
116
|
-
return this.floatTexturesSupported;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
class T {
|
|
120
|
-
constructor(s, e) {
|
|
121
|
-
this.resources = /* @__PURE__ */ new Map(), this.compileCache = /* @__PURE__ */ new Map(), this.uniformUpdateFns = /* @__PURE__ */ new Map(), this.extensions = /* @__PURE__ */ new Map();
|
|
122
|
-
const t = {
|
|
123
|
-
alpha: !1,
|
|
124
|
-
depth: !1,
|
|
125
|
-
stencil: !1,
|
|
126
|
-
antialias: !1,
|
|
127
|
-
powerPreference: "low-power",
|
|
128
|
-
preserveDrawingBuffer: !1
|
|
129
|
-
}, r = s.getContext("webgl", { ...t, ...e });
|
|
130
|
-
if (!r)
|
|
131
|
-
throw new Error("WebGL not supported");
|
|
132
|
-
this.gl = r, this.fboManager = new w(r), this.getExtension("OES_texture_float"), this.getExtension("OES_texture_float_linear"), this.getExtension("OES_vertex_array_object"), this.getExtension("ANGLE_instanced_arrays");
|
|
133
|
-
}
|
|
134
|
-
getExtension(s) {
|
|
135
|
-
if (this.extensions.has(s))
|
|
136
|
-
return this.extensions.get(s);
|
|
137
|
-
const e = this.gl.getExtension(s);
|
|
138
|
-
return this.extensions.set(s, e), e;
|
|
139
|
-
}
|
|
140
|
-
createProgram(s, e) {
|
|
141
|
-
const { vertexShader: t, fragmentShader: r, uniforms: o, attributes: a } = e, i = this.gl, c = this.getOrCompileShader("vertex:" + t, i.VERTEX_SHADER, t), u = this.getOrCompileShader("fragment:" + r, i.FRAGMENT_SHADER, r), f = i.createProgram();
|
|
142
|
-
if (!f)
|
|
143
|
-
throw new Error("Failed to create WebGL program");
|
|
144
|
-
if (i.attachShader(f, c), i.attachShader(f, u), i.linkProgram(f), !i.getProgramParameter(f, i.LINK_STATUS)) {
|
|
145
|
-
const d = i.getProgramInfoLog(f);
|
|
146
|
-
throw i.deleteProgram(f), new Error(`Could not link shader program: ${d}`);
|
|
147
|
-
}
|
|
148
|
-
const h = {};
|
|
149
|
-
for (const d of o)
|
|
150
|
-
h[d.name] = i.getUniformLocation(f, d.name);
|
|
151
|
-
const l = {};
|
|
152
|
-
if (a)
|
|
153
|
-
for (const d of a)
|
|
154
|
-
l[d.name] = i.getAttribLocation(f, d.name);
|
|
155
|
-
const n = {
|
|
156
|
-
program: f,
|
|
157
|
-
uniforms: h,
|
|
158
|
-
attributes: l,
|
|
159
|
-
buffers: {}
|
|
160
|
-
};
|
|
161
|
-
return this.resources.set(s, n), this.uniformUpdateFns.set(s, /* @__PURE__ */ new Map()), n;
|
|
162
|
-
}
|
|
163
|
-
getOrCompileShader(s, e, t) {
|
|
164
|
-
if (this.compileCache.has(s)) {
|
|
165
|
-
const o = this.compileCache.get(s);
|
|
166
|
-
if (o)
|
|
167
|
-
return o;
|
|
168
|
-
}
|
|
169
|
-
const r = this.compileShader(e, t);
|
|
170
|
-
return this.compileCache.set(s, r), r;
|
|
171
|
-
}
|
|
172
|
-
compileShader(s, e) {
|
|
173
|
-
const t = this.gl, r = t.createShader(s);
|
|
174
|
-
if (!r)
|
|
175
|
-
throw new Error("Failed to create shader");
|
|
176
|
-
if (t.shaderSource(r, e), t.compileShader(r), !t.getShaderParameter(r, t.COMPILE_STATUS)) {
|
|
177
|
-
const o = t.getShaderInfoLog(r);
|
|
178
|
-
throw t.deleteShader(r), new Error(`Shader compilation failed: ${o}`);
|
|
179
|
-
}
|
|
180
|
-
return r;
|
|
181
|
-
}
|
|
182
|
-
createBuffer(s, e, t) {
|
|
183
|
-
const r = this.gl, o = this.resources.get(s);
|
|
184
|
-
if (!o)
|
|
185
|
-
throw new Error(`Program with id ${s} not found`);
|
|
186
|
-
const a = r.createBuffer();
|
|
187
|
-
if (!a)
|
|
188
|
-
throw new Error("Failed to create buffer");
|
|
189
|
-
return r.bindBuffer(r.ARRAY_BUFFER, a), r.bufferData(r.ARRAY_BUFFER, t, r.STATIC_DRAW), o.buffers[e] = { buffer: a, data: t }, a;
|
|
190
|
-
}
|
|
191
|
-
updateBuffer(s, e, t) {
|
|
192
|
-
const r = this.gl, o = this.resources.get(s);
|
|
193
|
-
if (!o)
|
|
194
|
-
throw new Error(`Program with id ${s} not found`);
|
|
195
|
-
const a = o.buffers[e];
|
|
196
|
-
if (!a)
|
|
197
|
-
throw new Error(`Buffer for attribute ${e} not found`);
|
|
198
|
-
r.bindBuffer(r.ARRAY_BUFFER, a.buffer), r.bufferData(r.ARRAY_BUFFER, t, r.STATIC_DRAW), a.data = t;
|
|
199
|
-
}
|
|
200
|
-
registerUniformUpdater(s, e, t, r) {
|
|
201
|
-
const o = this.resources.get(s);
|
|
202
|
-
if (!o)
|
|
203
|
-
throw new Error(`Program with id ${s} not found`);
|
|
204
|
-
const a = this.uniformUpdateFns.get(s);
|
|
205
|
-
if (!a)
|
|
206
|
-
throw new Error(`Program uniforms for id ${s} not found`);
|
|
207
|
-
const i = o.uniforms[e];
|
|
208
|
-
if (i === null)
|
|
209
|
-
return;
|
|
210
|
-
const c = this.gl;
|
|
211
|
-
let u;
|
|
212
|
-
switch (t) {
|
|
213
|
-
case "float":
|
|
214
|
-
u = (f, h, l) => {
|
|
215
|
-
const n = r(f, h, l);
|
|
216
|
-
return c.uniform1f(i, n), n;
|
|
217
|
-
};
|
|
218
|
-
break;
|
|
219
|
-
case "vec2":
|
|
220
|
-
u = (f, h, l) => {
|
|
221
|
-
let n = r(f, h, l);
|
|
222
|
-
return Array.isArray(n) && (n = new Float32Array(n)), c.uniform2fv(i, n), n;
|
|
223
|
-
};
|
|
224
|
-
break;
|
|
225
|
-
case "vec3":
|
|
226
|
-
u = (f, h, l) => {
|
|
227
|
-
let n = r(f, h, l);
|
|
228
|
-
return Array.isArray(n) && (n = new Float32Array(n)), c.uniform3fv(i, n), n;
|
|
229
|
-
};
|
|
230
|
-
break;
|
|
231
|
-
case "vec4":
|
|
232
|
-
u = (f, h, l) => {
|
|
233
|
-
let n = r(f, h, l);
|
|
234
|
-
return Array.isArray(n) && (n = new Float32Array(n)), c.uniform4fv(i, n), n;
|
|
235
|
-
};
|
|
236
|
-
break;
|
|
237
|
-
case "int":
|
|
238
|
-
u = (f, h, l) => {
|
|
239
|
-
const n = r(f, h, l);
|
|
240
|
-
return c.uniform1i(i, n), n;
|
|
241
|
-
};
|
|
242
|
-
break;
|
|
243
|
-
case "mat2":
|
|
244
|
-
u = (f, h, l) => {
|
|
245
|
-
let n = r(f, h, l);
|
|
246
|
-
return Array.isArray(n) && (n = new Float32Array(n)), c.uniformMatrix2fv(i, !1, n), n;
|
|
247
|
-
};
|
|
248
|
-
break;
|
|
249
|
-
case "mat3":
|
|
250
|
-
u = (f, h, l) => {
|
|
251
|
-
let n = r(f, h, l);
|
|
252
|
-
return Array.isArray(n) && (n = new Float32Array(n)), c.uniformMatrix3fv(i, !1, n), n;
|
|
253
|
-
};
|
|
254
|
-
break;
|
|
255
|
-
case "mat4":
|
|
256
|
-
u = (f, h, l) => {
|
|
257
|
-
let n = r(f, h, l);
|
|
258
|
-
return Array.isArray(n) && (n = new Float32Array(n)), c.uniformMatrix4fv(i, !1, n), n;
|
|
259
|
-
};
|
|
260
|
-
break;
|
|
261
|
-
case "sampler2D":
|
|
262
|
-
u = (f, h, l) => {
|
|
263
|
-
const n = r(f, h, l);
|
|
264
|
-
return c.uniform1i(i, n), n;
|
|
265
|
-
};
|
|
266
|
-
break;
|
|
267
|
-
default:
|
|
268
|
-
throw new Error(`Unsupported uniform type: ${t}`);
|
|
269
|
-
}
|
|
270
|
-
a.set(e, u);
|
|
271
|
-
}
|
|
272
|
-
updateUniforms(s, e) {
|
|
273
|
-
const t = this.uniformUpdateFns.get(s);
|
|
274
|
-
if (!t)
|
|
275
|
-
return;
|
|
276
|
-
const r = this.gl.canvas, o = r.width, a = r.height;
|
|
277
|
-
t.forEach((i) => {
|
|
278
|
-
i(e, o, a);
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
setSize(s, e, t = !0) {
|
|
282
|
-
const r = this.gl.canvas, o = t && window.devicePixelRatio || 1, a = Math.floor(s * o), i = Math.floor(e * o);
|
|
283
|
-
(r.width !== a || r.height !== i) && (r.width = a, r.height = i, r.style.width = `${s}px`, r.style.height = `${e}px`, this.gl.viewport(0, 0, a, i));
|
|
284
|
-
}
|
|
285
|
-
prepareRender(s, e = {}) {
|
|
286
|
-
const { clear: t = !0, clearColor: r = [0, 0, 0, 1] } = e, o = this.gl, a = this.resources.get(s);
|
|
287
|
-
if (!a)
|
|
288
|
-
throw new Error(`Program with id ${s} not found`);
|
|
289
|
-
o.useProgram(a.program), t && (o.clearColor(...r), o.clear(o.COLOR_BUFFER_BIT));
|
|
290
|
-
}
|
|
291
|
-
fastRender(s, e, t = !0) {
|
|
292
|
-
const r = this.gl, o = this.resources.get(s);
|
|
293
|
-
if (!o)
|
|
294
|
-
throw new Error(`Program with id ${s} not found`);
|
|
295
|
-
r.useProgram(o.program), t && r.clear(r.COLOR_BUFFER_BIT), this.updateUniforms(s, e);
|
|
296
|
-
}
|
|
297
|
-
setUniform(s, e, t, r) {
|
|
298
|
-
const o = this.gl, a = this.resources.get(s);
|
|
299
|
-
if (!a)
|
|
300
|
-
throw new Error(`Program with id ${s} not found`);
|
|
301
|
-
const i = a.uniforms[e];
|
|
302
|
-
if (i !== null)
|
|
303
|
-
switch (o.useProgram(a.program), r) {
|
|
304
|
-
case "float":
|
|
305
|
-
o.uniform1f(i, t);
|
|
306
|
-
break;
|
|
307
|
-
case "vec2":
|
|
308
|
-
o.uniform2fv(i, t);
|
|
309
|
-
break;
|
|
310
|
-
case "vec3":
|
|
311
|
-
o.uniform3fv(i, t);
|
|
312
|
-
break;
|
|
313
|
-
case "vec4":
|
|
314
|
-
o.uniform4fv(i, t);
|
|
315
|
-
break;
|
|
316
|
-
case "int":
|
|
317
|
-
o.uniform1i(i, t);
|
|
318
|
-
break;
|
|
319
|
-
case "mat2":
|
|
320
|
-
o.uniformMatrix2fv(i, !1, t);
|
|
321
|
-
break;
|
|
322
|
-
case "mat3":
|
|
323
|
-
o.uniformMatrix3fv(i, !1, t);
|
|
324
|
-
break;
|
|
325
|
-
case "mat4":
|
|
326
|
-
o.uniformMatrix4fv(i, !1, t);
|
|
327
|
-
break;
|
|
328
|
-
case "sampler2D":
|
|
329
|
-
o.uniform1i(i, t);
|
|
330
|
-
break;
|
|
331
|
-
default:
|
|
332
|
-
throw new Error(`Unsupported uniform type: ${r}`);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
setAttributeOnce(s, e, t) {
|
|
336
|
-
const r = this.gl, o = this.resources.get(s);
|
|
337
|
-
if (!o)
|
|
338
|
-
throw new Error(`Program with id ${s} not found`);
|
|
339
|
-
const a = o.attributes[e];
|
|
340
|
-
if (a === -1) {
|
|
341
|
-
console.warn(`Attribute ${e} not found or is unused`);
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
344
|
-
const i = o.buffers[e];
|
|
345
|
-
if (!i)
|
|
346
|
-
throw new Error(`Buffer for attribute ${e} not found`);
|
|
347
|
-
if (r.bindBuffer(r.ARRAY_BUFFER, i.buffer), r.enableVertexAttribArray(a), r.vertexAttribPointer(
|
|
348
|
-
a,
|
|
349
|
-
t.size,
|
|
350
|
-
r[t.type],
|
|
351
|
-
t.normalized,
|
|
352
|
-
t.stride,
|
|
353
|
-
t.offset
|
|
354
|
-
), t.instanced) {
|
|
355
|
-
const c = this.getExtension("ANGLE_instanced_arrays");
|
|
356
|
-
if (c != null && c.vertexAttribDivisorANGLE)
|
|
357
|
-
c.vertexAttribDivisorANGLE(a, 1);
|
|
358
|
-
else if (r.vertexAttribDivisor)
|
|
359
|
-
r.vertexAttribDivisor(a, 1);
|
|
360
|
-
else
|
|
361
|
-
throw new Error("Instanced rendering not supported");
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
drawArrays(s, e, t) {
|
|
365
|
-
this.gl.drawArrays(s, e, t);
|
|
366
|
-
}
|
|
367
|
-
drawElements(s, e, t, r) {
|
|
368
|
-
this.gl.drawElements(s, e, t, r);
|
|
369
|
-
}
|
|
370
|
-
destroy(s) {
|
|
371
|
-
const e = this.gl, t = this.resources.get(s);
|
|
372
|
-
t && (Object.values(t.buffers).forEach(({ buffer: r }) => {
|
|
373
|
-
e.deleteBuffer(r);
|
|
374
|
-
}), e.deleteProgram(t.program), this.resources.delete(s), this.uniformUpdateFns.delete(s));
|
|
375
|
-
}
|
|
376
|
-
destroyAll() {
|
|
377
|
-
for (const s of Array.from(this.resources.keys()))
|
|
378
|
-
this.destroy(s);
|
|
379
|
-
this.compileCache.clear(), this.fboManager.destroyAll();
|
|
380
|
-
}
|
|
381
|
-
get context() {
|
|
382
|
-
return this.gl;
|
|
383
|
-
}
|
|
384
|
-
get fbo() {
|
|
385
|
-
return this.fboManager;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
const A = (m) => {
|
|
389
|
-
const { vertexShader: s, fragmentShader: e, uniformNames: t = {}, attributeConfigs: r = [] } = m, a = { ...{
|
|
390
|
-
u_time: "float",
|
|
391
|
-
u_resolution: "vec2"
|
|
392
|
-
}, ...t }, i = Object.entries(a).map(([u, f]) => ({
|
|
393
|
-
name: u,
|
|
394
|
-
type: f
|
|
395
|
-
})), c = r.map((u) => ({
|
|
396
|
-
name: u.name,
|
|
397
|
-
size: u.size,
|
|
398
|
-
type: u.type,
|
|
399
|
-
normalized: u.normalized ?? !1,
|
|
400
|
-
stride: u.stride ?? 0,
|
|
401
|
-
offset: u.offset ?? 0,
|
|
402
|
-
instanced: u.instanced
|
|
403
|
-
}));
|
|
404
|
-
return c.some((u) => u.name === "a_position") || c.push({
|
|
405
|
-
name: "a_position",
|
|
406
|
-
size: 2,
|
|
407
|
-
type: "FLOAT",
|
|
408
|
-
normalized: !1,
|
|
409
|
-
stride: 0,
|
|
410
|
-
offset: 0,
|
|
411
|
-
instanced: !1
|
|
412
|
-
}), {
|
|
413
|
-
vertexShader: s,
|
|
414
|
-
fragmentShader: e,
|
|
415
|
-
uniforms: i,
|
|
416
|
-
attributes: c
|
|
417
|
-
};
|
|
418
|
-
};
|
|
419
|
-
function E(m, s, e) {
|
|
420
|
-
return {
|
|
421
|
-
name: m,
|
|
422
|
-
type: s,
|
|
423
|
-
updateFn: typeof e == "function" ? e : (t) => e
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
function p(m) {
|
|
427
|
-
return m.map(
|
|
428
|
-
({ name: s, type: e, value: t }) => E(s, e, t)
|
|
429
|
-
);
|
|
430
|
-
}
|
|
431
|
-
function x() {
|
|
432
|
-
return [
|
|
433
|
-
E("u_time", "float", (m) => m * 1e-3),
|
|
434
|
-
E(
|
|
435
|
-
"u_resolution",
|
|
436
|
-
"vec2",
|
|
437
|
-
(m, s = 0, e = 0) => new Float32Array([s, e])
|
|
438
|
-
)
|
|
439
|
-
];
|
|
440
|
-
}
|
|
441
|
-
const _ = (m, s) => g(() => {
|
|
442
|
-
const e = x();
|
|
443
|
-
return Object.entries(s).forEach(([t, r]) => {
|
|
444
|
-
const o = t.startsWith("u_") ? t : `u_${t}`;
|
|
445
|
-
e.push(E(o, r.type, r.value));
|
|
446
|
-
}), { [m]: e };
|
|
447
|
-
}, [m, s]);
|
|
448
|
-
export {
|
|
449
|
-
w as F,
|
|
450
|
-
T as W,
|
|
451
|
-
x as a,
|
|
452
|
-
E as b,
|
|
453
|
-
A as c,
|
|
454
|
-
p as d,
|
|
455
|
-
_ as u
|
|
456
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";const x=require("react");class g{constructor(s){this.resources=new Map,this.floatTextureExtension=null,this.gl=s,this.floatTextureExtension=s.getExtension("OES_texture_float"),this.floatTexturesSupported=!!this.floatTextureExtension,s.getExtension("OES_texture_float_linear")}createFramebuffer(s,e){const t=this.gl,{width:r,height:o,textureCount:a=2,textureOptions:i={}}=e,c=[];for(let h=0;h<a;h++){const l=this.createTexture({width:r,height:o,...i});c.push(l)}const u=t.createFramebuffer();if(!u)throw new Error("Failed to create framebuffer");const f={framebuffer:u,textures:c,currentTextureIndex:0,width:r,height:o};return this.resources.set(s,f),f}createTexture(s){const e=this.gl,{width:t,height:r,internalFormat:o=e.RGBA,format:a=e.RGBA,type:i=this.floatTexturesSupported?e.FLOAT:e.UNSIGNED_BYTE,minFilter:c=e.NEAREST,magFilter:u=e.NEAREST,wrapS:f=e.CLAMP_TO_EDGE,wrapT:h=e.CLAMP_TO_EDGE,generateMipmap:l=!1}=s,n=e.createTexture();if(!n)throw new Error("Failed to create texture");return e.bindTexture(e.TEXTURE_2D,n),e.texImage2D(e.TEXTURE_2D,0,o,t,r,0,a,i,null),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,c),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,u),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,f),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,h),l&&e.generateMipmap(e.TEXTURE_2D),n}bindFramebuffer(s,e){const t=this.gl;if(s===null){t.bindFramebuffer(t.FRAMEBUFFER,null);return}const r=this.resources.get(s);if(!r)throw new Error(`Framebuffer with id ${s} not found`);const o=e??r.currentTextureIndex;t.bindFramebuffer(t.FRAMEBUFFER,r.framebuffer),t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,r.textures[o],0);const a=t.checkFramebufferStatus(t.FRAMEBUFFER);if(a!==t.FRAMEBUFFER_COMPLETE)throw new Error(`Framebuffer is not complete: ${a}`)}bindTexture(s,e,t){const r=this.gl,o=this.resources.get(s);if(!o)throw new Error(`Framebuffer with id ${s} not found`);const a=t??o.currentTextureIndex;r.activeTexture(r.TEXTURE0+e),r.bindTexture(r.TEXTURE_2D,o.textures[a])}swapTextures(s){const e=this.resources.get(s);if(!e)throw new Error(`Framebuffer with id ${s} not found`);e.currentTextureIndex=(e.currentTextureIndex+1)%e.textures.length}getPingPongIndices(s){const e=this.resources.get(s);if(!e)throw new Error(`Framebuffer with id ${s} not found`);const t=e.currentTextureIndex,r=(t+1)%e.textures.length;return{read:t,write:r}}resizeFramebuffer(s,e,t){const r=this.gl,o=this.resources.get(s);if(!o)throw new Error(`Framebuffer with id ${s} not found`);o.width===e&&o.height===t||(o.textures.forEach(a=>{r.bindTexture(r.TEXTURE_2D,a),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,e,t,0,r.RGBA,this.floatTexturesSupported?r.FLOAT:r.UNSIGNED_BYTE,null)}),o.width=e,o.height=t)}destroy(s){const e=this.gl,t=this.resources.get(s);t&&(t.textures.forEach(r=>{e.deleteTexture(r)}),e.deleteFramebuffer(t.framebuffer),this.resources.delete(s))}destroyAll(){Array.from(this.resources.keys()).forEach(s=>{this.destroy(s)})}isFloatTexturesSupported(){return this.floatTexturesSupported}}class b{constructor(s,e){this.resources=new Map,this.compileCache=new Map,this.uniformUpdateFns=new Map,this.extensions=new Map;const t={alpha:!1,depth:!1,stencil:!1,antialias:!1,powerPreference:"low-power",preserveDrawingBuffer:!1},r=s.getContext("webgl",{...t,...e});if(!r)throw new Error("WebGL not supported");this.gl=r,this.fboManager=new g(r),this.getExtension("OES_texture_float"),this.getExtension("OES_texture_float_linear"),this.getExtension("OES_vertex_array_object"),this.getExtension("ANGLE_instanced_arrays")}getExtension(s){if(this.extensions.has(s))return this.extensions.get(s);const e=this.gl.getExtension(s);return this.extensions.set(s,e),e}createProgram(s,e){const{vertexShader:t,fragmentShader:r,uniforms:o,attributes:a}=e,i=this.gl,c=this.getOrCompileShader("vertex:"+t,i.VERTEX_SHADER,t),u=this.getOrCompileShader("fragment:"+r,i.FRAGMENT_SHADER,r),f=i.createProgram();if(!f)throw new Error("Failed to create WebGL program");if(i.attachShader(f,c),i.attachShader(f,u),i.linkProgram(f),!i.getProgramParameter(f,i.LINK_STATUS)){const d=i.getProgramInfoLog(f);throw i.deleteProgram(f),new Error(`Could not link shader program: ${d}`)}const h={};for(const d of o)h[d.name]=i.getUniformLocation(f,d.name);const l={};if(a)for(const d of a)l[d.name]=i.getAttribLocation(f,d.name);const n={program:f,uniforms:h,attributes:l,buffers:{}};return this.resources.set(s,n),this.uniformUpdateFns.set(s,new Map),n}getOrCompileShader(s,e,t){if(this.compileCache.has(s)){const o=this.compileCache.get(s);if(o)return o}const r=this.compileShader(e,t);return this.compileCache.set(s,r),r}compileShader(s,e){const t=this.gl,r=t.createShader(s);if(!r)throw new Error("Failed to create shader");if(t.shaderSource(r,e),t.compileShader(r),!t.getShaderParameter(r,t.COMPILE_STATUS)){const o=t.getShaderInfoLog(r);throw t.deleteShader(r),new Error(`Shader compilation failed: ${o}`)}return r}createBuffer(s,e,t){const r=this.gl,o=this.resources.get(s);if(!o)throw new Error(`Program with id ${s} not found`);const a=r.createBuffer();if(!a)throw new Error("Failed to create buffer");return r.bindBuffer(r.ARRAY_BUFFER,a),r.bufferData(r.ARRAY_BUFFER,t,r.STATIC_DRAW),o.buffers[e]={buffer:a,data:t},a}updateBuffer(s,e,t){const r=this.gl,o=this.resources.get(s);if(!o)throw new Error(`Program with id ${s} not found`);const a=o.buffers[e];if(!a)throw new Error(`Buffer for attribute ${e} not found`);r.bindBuffer(r.ARRAY_BUFFER,a.buffer),r.bufferData(r.ARRAY_BUFFER,t,r.STATIC_DRAW),a.data=t}registerUniformUpdater(s,e,t,r){const o=this.resources.get(s);if(!o)throw new Error(`Program with id ${s} not found`);const a=this.uniformUpdateFns.get(s);if(!a)throw new Error(`Program uniforms for id ${s} not found`);const i=o.uniforms[e];if(i===null)return;const c=this.gl;let u;switch(t){case"float":u=(f,h,l)=>{const n=r(f,h,l);return c.uniform1f(i,n),n};break;case"vec2":u=(f,h,l)=>{let n=r(f,h,l);return Array.isArray(n)&&(n=new Float32Array(n)),c.uniform2fv(i,n),n};break;case"vec3":u=(f,h,l)=>{let n=r(f,h,l);return Array.isArray(n)&&(n=new Float32Array(n)),c.uniform3fv(i,n),n};break;case"vec4":u=(f,h,l)=>{let n=r(f,h,l);return Array.isArray(n)&&(n=new Float32Array(n)),c.uniform4fv(i,n),n};break;case"int":u=(f,h,l)=>{const n=r(f,h,l);return c.uniform1i(i,n),n};break;case"mat2":u=(f,h,l)=>{let n=r(f,h,l);return Array.isArray(n)&&(n=new Float32Array(n)),c.uniformMatrix2fv(i,!1,n),n};break;case"mat3":u=(f,h,l)=>{let n=r(f,h,l);return Array.isArray(n)&&(n=new Float32Array(n)),c.uniformMatrix3fv(i,!1,n),n};break;case"mat4":u=(f,h,l)=>{let n=r(f,h,l);return Array.isArray(n)&&(n=new Float32Array(n)),c.uniformMatrix4fv(i,!1,n),n};break;case"sampler2D":u=(f,h,l)=>{const n=r(f,h,l);return c.uniform1i(i,n),n};break;default:throw new Error(`Unsupported uniform type: ${t}`)}a.set(e,u)}updateUniforms(s,e){const t=this.uniformUpdateFns.get(s);if(!t)return;const r=this.gl.canvas,o=r.width,a=r.height;t.forEach(i=>{i(e,o,a)})}setSize(s,e,t=!0){const r=this.gl.canvas,o=t&&window.devicePixelRatio||1,a=Math.floor(s*o),i=Math.floor(e*o);(r.width!==a||r.height!==i)&&(r.width=a,r.height=i,r.style.width=`${s}px`,r.style.height=`${e}px`,this.gl.viewport(0,0,a,i))}prepareRender(s,e={}){const{clear:t=!0,clearColor:r=[0,0,0,1]}=e,o=this.gl,a=this.resources.get(s);if(!a)throw new Error(`Program with id ${s} not found`);o.useProgram(a.program),t&&(o.clearColor(...r),o.clear(o.COLOR_BUFFER_BIT))}fastRender(s,e,t=!0){const r=this.gl,o=this.resources.get(s);if(!o)throw new Error(`Program with id ${s} not found`);r.useProgram(o.program),t&&r.clear(r.COLOR_BUFFER_BIT),this.updateUniforms(s,e)}setUniform(s,e,t,r){const o=this.gl,a=this.resources.get(s);if(!a)throw new Error(`Program with id ${s} not found`);const i=a.uniforms[e];if(i!==null)switch(o.useProgram(a.program),r){case"float":o.uniform1f(i,t);break;case"vec2":o.uniform2fv(i,t);break;case"vec3":o.uniform3fv(i,t);break;case"vec4":o.uniform4fv(i,t);break;case"int":o.uniform1i(i,t);break;case"mat2":o.uniformMatrix2fv(i,!1,t);break;case"mat3":o.uniformMatrix3fv(i,!1,t);break;case"mat4":o.uniformMatrix4fv(i,!1,t);break;case"sampler2D":o.uniform1i(i,t);break;default:throw new Error(`Unsupported uniform type: ${r}`)}}setAttributeOnce(s,e,t){const r=this.gl,o=this.resources.get(s);if(!o)throw new Error(`Program with id ${s} not found`);const a=o.attributes[e];if(a===-1){console.warn(`Attribute ${e} not found or is unused`);return}const i=o.buffers[e];if(!i)throw new Error(`Buffer for attribute ${e} not found`);if(r.bindBuffer(r.ARRAY_BUFFER,i.buffer),r.enableVertexAttribArray(a),r.vertexAttribPointer(a,t.size,r[t.type],t.normalized,t.stride,t.offset),t.instanced){const c=this.getExtension("ANGLE_instanced_arrays");if(c!=null&&c.vertexAttribDivisorANGLE)c.vertexAttribDivisorANGLE(a,1);else if(r.vertexAttribDivisor)r.vertexAttribDivisor(a,1);else throw new Error("Instanced rendering not supported")}}drawArrays(s,e,t){this.gl.drawArrays(s,e,t)}drawElements(s,e,t,r){this.gl.drawElements(s,e,t,r)}destroy(s){const e=this.gl,t=this.resources.get(s);t&&(Object.values(t.buffers).forEach(({buffer:r})=>{e.deleteBuffer(r)}),e.deleteProgram(t.program),this.resources.delete(s),this.uniformUpdateFns.delete(s))}destroyAll(){for(const s of Array.from(this.resources.keys()))this.destroy(s);this.compileCache.clear(),this.fboManager.destroyAll()}get context(){return this.gl}get fbo(){return this.fboManager}}const T=m=>{const{vertexShader:s,fragmentShader:e,uniformNames:t={},attributeConfigs:r=[]}=m,a={...{u_time:"float",u_resolution:"vec2"},...t},i=Object.entries(a).map(([u,f])=>({name:u,type:f})),c=r.map(u=>({name:u.name,size:u.size,type:u.type,normalized:u.normalized??!1,stride:u.stride??0,offset:u.offset??0,instanced:u.instanced}));return c.some(u=>u.name==="a_position")||c.push({name:"a_position",size:2,type:"FLOAT",normalized:!1,stride:0,offset:0,instanced:!1}),{vertexShader:s,fragmentShader:e,uniforms:i,attributes:c}};function E(m,s,e){return{name:m,type:s,updateFn:typeof e=="function"?e:t=>e}}function p(m){return m.map(({name:s,type:e,value:t})=>E(s,e,t))}function w(){return[E("u_time","float",m=>m*.001),E("u_resolution","vec2",(m,s=0,e=0)=>new Float32Array([s,e]))]}const A=(m,s)=>x.useMemo(()=>{const e=w();return Object.entries(s).forEach(([t,r])=>{const o=t.startsWith("u_")?t:`u_${t}`;e.push(E(o,r.type,r.value))}),{[m]:e}},[m,s]);exports.FBOManager=g;exports.WebGLManager=b;exports.createCommonUpdaters=w;exports.createShaderConfig=T;exports.createUniformUpdater=E;exports.createUniformUpdaters=p;exports.useUniformUpdaters=A;
|