modern-canvas 0.0.2 → 0.0.3

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/dist/index.mjs CHANGED
@@ -1,120 +1,9 @@
1
- function v(o) {
1
+ function w(o) {
2
2
  return typeof o == "function" ? o() : o;
3
3
  }
4
- const U = v(() => ({
5
- name: "canvas:base",
6
- register(o) {
7
- o.registerShape({
8
- name: "rectangle",
9
- type: "2d",
10
- mode: "triangles",
11
- data: new Float32Array([
12
- -1,
13
- 1,
14
- -1,
15
- -1,
16
- 1,
17
- -1,
18
- 1,
19
- -1,
20
- 1,
21
- 1,
22
- -1,
23
- 1
24
- ])
25
- }), o.registerMaterial({
26
- name: "baseMaterial",
27
- vertexShader: `attribute vec2 aPosition;
28
- varying vec2 vTextureCoord;
29
- void main() {
30
- vTextureCoord = step(0.0, aPosition);
31
- gl_Position = vec4(aPosition, 0, 1);
32
- }`,
33
- fragmentShader: `uniform sampler2D uSampler;
34
- varying vec2 vTextureCoord;
35
- void main() {
36
- gl_FragColor = texture2D(uSampler, vTextureCoord);
37
- }`
38
- });
39
- }
40
- })), D = v(() => ({
41
- name: "canvas:node-image",
42
- register(o) {
43
- o.registerNodeRenderer({
44
- name: "image",
45
- include: (r) => Boolean(r.image),
46
- shape: "rectangle",
47
- material: "baseMaterial",
48
- update(r) {
49
- if (!o.resources.has(r.image)) {
50
- const e = new Image();
51
- e.src = r.image, o.registerResource({
52
- name: r.image,
53
- data: e
54
- });
55
- }
56
- return {
57
- uSampler: r.image
58
- };
59
- }
60
- });
61
- }
62
- })), N = v(() => {
63
- const o = document.createElement("canvas").getContext("2d");
64
- return {
65
- name: "canvas:node-text",
66
- register(r) {
67
- r.registerNodeRenderer({
68
- name: "text",
69
- include: (e) => Boolean(e.text),
70
- shape: "rectangle",
71
- material: "baseMaterial",
72
- update(e) {
73
- const { width: a = 100, height: t = 100, text: c } = e;
74
- return r.resources.has(c) || (o.canvas.width = a, o.canvas.height = t, o.font = "20px monospace", o.textAlign = "center", o.textBaseline = "middle", o.fillStyle = "black", o.clearRect(0, 0, o.canvas.width, o.canvas.height), o.fillText(c, a / 2, t / 2), r.registerResource({
75
- name: c,
76
- data: o.canvas
77
- })), {
78
- uSampler: e.text
79
- };
80
- }
81
- });
82
- }
83
- };
84
- }), F = v(() => ({
85
- name: "canvas:fade",
86
- register(o) {
87
- o.registerNodeRenderer({
88
- name: "fade",
89
- include: (r) => Boolean(r.fade),
90
- shape: "rectangle",
91
- material: {
92
- vertexShader: `attribute vec2 aPosition;
93
- varying vec2 vTextureCoord;
94
- void main() {
95
- vTextureCoord = step(0.0, aPosition);
96
- gl_Position = vec4(aPosition, 0, 1);
97
- }`,
98
- fragmentShader: `uniform sampler2D uSampler;
99
- varying vec2 vTextureCoord;
100
- uniform float uTime;
101
- float linear(float time, float offset, float change, float duration) {
102
- return change * (time / duration) + offset;
103
- }
104
- void main(void) {
105
- vec4 color = texture2D(uSampler, vTextureCoord);
106
- gl_FragColor = vec4(color.rgb, linear(uTime, 0.0, 1.0, 3.0));
107
- }`
108
- },
109
- update: (r, e) => ({
110
- uTime: e
111
- })
112
- });
113
- }
114
- }));
115
- class _ extends Float32Array {
4
+ class x extends Float32Array {
116
5
  static identity() {
117
- return new _([
6
+ return new x([
118
7
  1,
119
8
  0,
120
9
  0,
@@ -126,8 +15,8 @@ class _ extends Float32Array {
126
15
  1
127
16
  ]);
128
17
  }
129
- static translation(r, e) {
130
- return new _([
18
+ static translation(r, c) {
19
+ return new x([
131
20
  1,
132
21
  0,
133
22
  0,
@@ -135,31 +24,31 @@ class _ extends Float32Array {
135
24
  1,
136
25
  0,
137
26
  r,
138
- e,
27
+ c,
139
28
  1
140
29
  ]);
141
30
  }
142
31
  static rotation(r) {
143
- const e = Math.cos(r), a = Math.sin(r);
144
- return new _([
145
- e,
146
- -a,
32
+ const c = Math.cos(r), t = Math.sin(r);
33
+ return new x([
34
+ c,
35
+ -t,
147
36
  0,
148
- a,
149
- e,
37
+ t,
38
+ c,
150
39
  0,
151
40
  0,
152
41
  0,
153
42
  1
154
43
  ]);
155
44
  }
156
- static scaling(r, e) {
157
- return new _([
45
+ static scaling(r, c) {
46
+ return new x([
158
47
  r,
159
48
  0,
160
49
  0,
161
50
  0,
162
- e,
51
+ c,
163
52
  0,
164
53
  0,
165
54
  0,
@@ -167,91 +56,97 @@ class _ extends Float32Array {
167
56
  ]);
168
57
  }
169
58
  multiply(r) {
170
- const e = this[0], a = this[1], t = this[2], c = this[3], u = this[4], i = this[5], l = this[6], s = this[7], n = this[8], f = r[0], R = r[1], g = r[2], T = r[3], d = r[4], E = r[5], m = r[6], A = r[7], x = r[8];
171
- return this[0] = f * e + R * c + g * l, this[1] = f * a + R * u + g * s, this[2] = f * t + R * i + g * n, this[3] = T * e + d * c + E * l, this[4] = T * a + d * u + E * s, this[5] = T * t + d * i + E * n, this[6] = m * e + A * c + x * l, this[7] = m * a + A * u + x * s, this[8] = m * t + A * i + x * n, this;
59
+ const c = this[0], t = this[1], n = this[2], d = this[3], m = this[4], a = this[5], s = this[6], e = this[7], i = this[8], u = r[0], f = r[1], h = r[2], E = r[3], T = r[4], l = r[5], g = r[6], R = r[7], A = r[8];
60
+ return this[0] = u * c + f * d + h * s, this[1] = u * t + f * m + h * e, this[2] = u * n + f * a + h * i, this[3] = E * c + T * d + l * s, this[4] = E * t + T * m + l * e, this[5] = E * n + T * a + l * i, this[6] = g * c + R * d + A * s, this[7] = g * t + R * m + A * e, this[8] = g * n + R * a + A * i, this;
172
61
  }
173
62
  }
174
- const L = v(() => {
175
- const o = { x: 0, y: 0 };
176
- return {
177
- name: "canvas:selector2d",
178
- register(r) {
179
- const { view: e } = r;
180
- e.addEventListener("mousemove", (a) => {
181
- const t = e.getBoundingClientRect();
182
- o.x = a.clientX - t.left, o.y = a.clientY - t.top;
183
- }), e.addEventListener("click", () => {
184
- var t;
185
- const a = r.get("hovered");
186
- a && ((t = r.get("onSelect")) == null || t(a));
187
- }), r.registerNodeRenderer({
188
- name: "selector2d",
189
- include: () => !1,
190
- shape: "rectangle",
191
- material: {
192
- vertexShader: `attribute vec2 aPosition;
193
- varying vec2 vTextureCoord;
194
- uniform mat3 uModelMatrix;
195
- void main() {
196
- vTextureCoord = step(0.0, aPosition);
197
- vec2 position = aPosition;
198
- gl_Position = vec4((uModelMatrix * vec3(position, 1)).xy, 0, 1);
199
- }`,
200
- fragmentShader: `uniform vec4 uColor;
201
- void main() {
202
- gl_FragColor = uColor;
203
- }`
204
- },
205
- update: (a, t) => {
206
- const { width: c, height: u } = r;
207
- let {
208
- x: i = 0,
209
- y: l = 0,
210
- width: s = c,
211
- height: n = u
212
- } = a;
213
- const {
214
- rotation: f = 0
215
- } = a;
216
- i = i / c, l = l / u, s = s / c, n = n / u;
217
- const R = f / 180 * Math.PI;
218
- return {
219
- uModelMatrix: _.identity().multiply(_.translation(2 * i - (1 - s), 1 - n - 2 * l)).multiply(_.scaling(s, n)).multiply(_.rotation(R)),
220
- uColor: [(t >>> 16 & 255) / 255, (t >>> 8 & 255) / 255, (t & 255) / 255, 1]
221
- };
222
- }
223
- });
224
- },
225
- beforeRender(r) {
226
- var R, g;
227
- const { width: e, height: a, gl: t, children: c, nodeRenderers: u } = r;
228
- let i = 0;
229
- const l = {};
230
- t.bindFramebuffer(t.FRAMEBUFFER, r.glFramebuffers[0].buffer), t.viewport(0, 0, e, a), t.clear(t.COLOR_BUFFER_BIT | t.DEPTH_BUFFER_BIT), t.enable(t.DEPTH_TEST), r.forEachNode((T, d) => {
231
- var m;
232
- const E = i++;
233
- l[E] = d, (m = u.get("selector2d")) == null || m.render(T, E);
234
- });
235
- const s = new Uint8Array(4);
236
- t.readPixels(o.x, a - o.y, 1, 1, t.RGBA, t.UNSIGNED_BYTE, s);
237
- const n = l[(s[0] << 16) + (s[1] << 8) + s[2]];
238
- if (((R = r.get("hoveredPath")) == null ? void 0 : R.join("")) === (n == null ? void 0 : n.join("")))
239
- return;
240
- let f;
241
- n && (f = { children: c }, n.forEach((T) => {
242
- var d;
243
- return f = (d = f.children) == null ? void 0 : d[T];
244
- })), r.set("hovered", f), r.set("hoveredPath", n), f && ((g = r.get("onHover")) == null || g(f, n));
63
+ function P(o) {
64
+ const r = [];
65
+ for (let c = o.length, t = 0; t < c; t++) {
66
+ const n = o[t], d = ~~(n / 16);
67
+ for (let m = 0; m <= d; m++)
68
+ m in r || (r[m] = 0);
69
+ r[d] |= 1 << n % 16;
70
+ }
71
+ return r;
72
+ }
73
+ function B(o) {
74
+ const r = document.createElement("video");
75
+ return r.playsInline = !0, r.muted = !0, r.loop = !0, r.src = o, r;
76
+ }
77
+ function O(o) {
78
+ const r = new Image();
79
+ return r.decoding = "sync", r.loading = "eager", r.crossOrigin = "anonymous", r.src = o, r;
80
+ }
81
+ let b;
82
+ const y = /* @__PURE__ */ new Map();
83
+ function X() {
84
+ var o;
85
+ return b || (b = document.createElement("iframe"), b.id = `__SANDBOX__${Math.random()}`, b.width = "0", b.height = "0", b.style.visibility = "hidden", b.style.position = "fixed", document.body.appendChild(b), (o = b.contentWindow) == null || o.document.write('<!DOCTYPE html><meta charset="UTF-8"><title></title><body>')), b;
86
+ }
87
+ function U(o) {
88
+ switch (!0) {
89
+ case (o.startsWith("#") && o.length === 7):
90
+ return o = o.substring(1), [
91
+ parseInt(`${o[0]}${o[1]}`, 16) / 255,
92
+ parseInt(`${o[2]}${o[3]}`, 16) / 255,
93
+ parseInt(`${o[4]}${o[5]}`, 16) / 255,
94
+ 1
95
+ ];
96
+ case (o.startsWith("#") && o.length === 9):
97
+ return o = o.substring(1), [
98
+ parseInt(`${o[0]}${o[1]}`, 16) / 255,
99
+ parseInt(`${o[2]}${o[3]}`, 16) / 255,
100
+ parseInt(`${o[4]}${o[5]}`, 16) / 255,
101
+ parseInt(`${o[6]}${o[7]}`, 16) / 255
102
+ ];
103
+ case o.startsWith("rgb("):
104
+ return [
105
+ ...o.replace(/rgb\((.+?)\)/, "$1").split(",").slice(0, 3).map((r) => Number(r.trim()) / 255),
106
+ 1
107
+ ];
108
+ case o.startsWith("rgba("):
109
+ return o.replace(/rgba\((.+?)\)/, "$1").split(",").slice(0, 4).map((r) => Number(r.trim()) / 255);
110
+ }
111
+ }
112
+ function G(o, r = [0, 0, 0, 0]) {
113
+ if (y.has(o))
114
+ return y.get(o);
115
+ let c = U(o);
116
+ if (!c) {
117
+ const t = X();
118
+ if (t) {
119
+ const n = t.contentWindow;
120
+ if (n) {
121
+ const d = n.document, m = d.createElement("div");
122
+ d.body.appendChild(m), m.textContent = " ", m.style.color = o, c = U(n.getComputedStyle(m).color), d.body.removeChild(m);
123
+ }
245
124
  }
246
- };
247
- }), S = v(() => ({
248
- name: "canvas:transform2d",
125
+ }
126
+ return c && y.set(o, c), c ?? r;
127
+ }
128
+ const j = w({
129
+ name: "renderer2d",
249
130
  register(o) {
250
- o.registerNodeRenderer({
251
- name: "transform2d",
252
- shape: "rectangle",
253
- material: {
254
- vertexShader: `attribute vec2 aPosition;
131
+ o.registerShape("rectangle", {
132
+ type: "2d",
133
+ mode: "triangles",
134
+ buffer: new Float32Array([
135
+ -1,
136
+ 1,
137
+ -1,
138
+ -1,
139
+ 1,
140
+ -1,
141
+ 1,
142
+ -1,
143
+ 1,
144
+ 1,
145
+ -1,
146
+ 1
147
+ ])
148
+ }), o.registerMaterial("material2d", {
149
+ vertexShader: `attribute vec2 aPosition;
255
150
  varying vec2 vTextureCoord;
256
151
  uniform mat3 uModelMatrix;
257
152
  void main() {
@@ -259,262 +154,422 @@ void main() {
259
154
  vec2 position = aPosition;
260
155
  gl_Position = vec4((uModelMatrix * vec3(position, 1)).xy, 0, 1);
261
156
  }`,
262
- fragmentShader: `uniform sampler2D uSampler;
157
+ fragmentShader: `uniform sampler2D uSampler;
263
158
  varying vec2 vTextureCoord;
159
+ uniform vec4 uBackgroundColor;
264
160
  void main() {
265
- gl_FragColor = texture2D(uSampler, vTextureCoord);
161
+ vec4 color = texture2D(uSampler, vTextureCoord);
162
+ gl_FragColor = mix(uBackgroundColor, color, color.a);
266
163
  }`
267
- },
268
- update: (r) => {
269
- const { width: e, height: a } = o;
270
- let {
271
- x: t = 0,
272
- y: c = 0,
273
- width: u = e,
274
- height: i = a
275
- } = r;
164
+ });
165
+ const r = document.createElement("canvas").getContext("2d");
166
+ o.registerSystem({
167
+ update(c = 0) {
168
+ var d;
169
+ const { textures: t } = o, n = o.query("type");
170
+ for (let m = n.length, a = 0; a < m; a++) {
171
+ const s = n[a], { type: e } = s;
172
+ if (e === "image" && s.src)
173
+ t.has(s.id) || o.registerTexture(s.id, O(s.src));
174
+ else if (e === "video")
175
+ if (!t.has(s.id))
176
+ o.registerTexture(s.id, B(s.src));
177
+ else {
178
+ const i = (d = t.get(s.id)) == null ? void 0 : d.source;
179
+ i.duration && (i.currentTime = c % i.duration);
180
+ }
181
+ else if (e === "text") {
182
+ const {
183
+ width: i,
184
+ height: u,
185
+ color: f = "black",
186
+ fontSize: h = 14,
187
+ fontWeight: E = 400,
188
+ direction: T = "inherit",
189
+ fontFamily: l = "monospace",
190
+ fontKerning: g = "normal",
191
+ textAlign: R = "center",
192
+ textBaseline: A = "middle"
193
+ } = s.style ?? {};
194
+ if (!t.has(s.id)) {
195
+ const _ = `${E} ${h}px ${l}`;
196
+ r.font = _;
197
+ const p = r.measureText(s.content), I = u ?? p.actualBoundingBoxAscent + p.actualBoundingBoxDescent, v = i ?? p.width;
198
+ s.style.width = v, s.style.height = I, r.canvas.width = v, r.canvas.height = I, r.fillStyle = f, r.direction = T, r.font = _, r.fontKerning = g, r.textAlign = R, r.textBaseline = A, r.clearRect(0, 0, v, I), r.fillText(s.content, v / 2, I / 2), o.registerTexture(s.id, r.canvas);
199
+ }
200
+ }
201
+ }
202
+ }
203
+ }), o.registerSystem({
204
+ update() {
205
+ var m;
276
206
  const {
277
- rotation: l = 0
278
- } = r;
279
- t = t / e, c = c / a, u = u / e, i = i / a;
280
- const s = l / 180 * Math.PI;
281
- return {
282
- uModelMatrix: _.identity().multiply(_.translation(2 * t - (1 - u), 1 - i - 2 * c)).multiply(_.scaling(u, i)).multiply(_.rotation(s))
283
- };
207
+ width: c,
208
+ height: t,
209
+ textures: n
210
+ } = o, d = o.query();
211
+ for (let a = d.length, s = 0; s < a; s++) {
212
+ const e = d[s], {
213
+ shape: i = "rectangle",
214
+ material: u = "material2d",
215
+ filters: f = []
216
+ } = e, {
217
+ left: h = 0,
218
+ top: E = 0,
219
+ width: T = 0,
220
+ height: l = 0,
221
+ rotation: g = 0,
222
+ background: R
223
+ } = e.style ?? {}, A = h / c, _ = E / t, p = T / c, I = l / t, v = g / 180 * Math.PI, D = R != null && R.color ? G(R.color) : [0, 0, 0, 0];
224
+ o.renderNode({
225
+ shape: i,
226
+ material: u,
227
+ uniforms: {
228
+ uSampler: (m = n.get(e.id)) == null ? void 0 : m.value,
229
+ uBackgroundColor: D,
230
+ uModelMatrix: x.identity().multiply(x.translation(2 * A - (1 - p), 1 - I - 2 * _)).multiply(x.scaling(p, I)).multiply(x.rotation(v))
231
+ },
232
+ extraRenderers: f == null ? void 0 : f.map((N) => {
233
+ const {
234
+ shape: C,
235
+ material: M,
236
+ type: S,
237
+ ...F
238
+ } = N;
239
+ return {
240
+ shape: C ?? i,
241
+ material: M ?? S ?? u,
242
+ uniforms: F
243
+ };
244
+ })
245
+ });
246
+ }
284
247
  }
285
248
  });
286
249
  }
287
- })), X = [
288
- U,
289
- D,
290
- N,
291
- F,
292
- S,
293
- L
294
- ];
295
- function P(o, r = 0) {
296
- const {
297
- gl: e,
298
- width: a,
299
- height: t,
300
- beforeRenderPlugins: c,
301
- afterRenderPlugins: u,
302
- nodeRenderers: i,
303
- forEachNode: l
304
- } = o, s = Array.from(i.values());
305
- c.forEach((n) => {
306
- var f;
307
- return (f = n.beforeRender) == null ? void 0 : f.call(n, o);
308
- }), e.viewport(0, 0, a, t), e.clear(e.COLOR_BUFFER_BIT | e.DEPTH_BUFFER_BIT), l((n, f) => {
309
- e.bindFramebuffer(e.FRAMEBUFFER, null), e.bindTexture(e.TEXTURE_2D, null);
310
- const R = s.filter((g) => !g.include && !g.exclude || g.include && g.include(n, f) || g.exclude && !g.exclude(n, f));
311
- for (let g = R.length, T = 0; T < g; T++) {
312
- let d = null;
313
- T < g - 1 && (d = o.glFramebuffers[T % 2]), e.bindFramebuffer(e.FRAMEBUFFER, (d == null ? void 0 : d.buffer) ?? null), d && e.clear(e.COLOR_BUFFER_BIT | e.DEPTH_BUFFER_BIT), R[T].render(n, r), d && e.bindTexture(e.TEXTURE_2D, d.texture);
314
- }
315
- }), u.forEach((n) => {
316
- var f;
317
- return (f = n.afterRender) == null ? void 0 : f.call(n, o);
318
- }), e.flush();
250
+ });
251
+ function $(o, r) {
252
+ const { width: c, height: t, context: n, shapes: d, materials: m, framebuffers: a, lastState: s } = o, { extraRenderers: e = [] } = r, i = [
253
+ r,
254
+ ...e
255
+ ].filter((u) => d.has(u.shape) && m.has(u.material));
256
+ for (let u = i.length, f = 0; f < u; f++) {
257
+ const h = i[f], {
258
+ shape: E,
259
+ material: T,
260
+ uniforms: l
261
+ } = h, g = d.get(E), R = m.get(T);
262
+ (s == null ? void 0 : s.material) !== T && (s == null ? void 0 : s.shape) !== E && R.setupAttributes({ aPosition: g.buffer });
263
+ const A = f < u - 1 ? a[f % 2] : null;
264
+ n.bindFramebuffer(n.FRAMEBUFFER, (A == null ? void 0 : A.buffer) ?? null), n.viewport(0, 0, c, t), A && n.clear(n.COLOR_BUFFER_BIT | n.DEPTH_BUFFER_BIT), (s == null ? void 0 : s.material) !== T && n.useProgram(R.program), l && R.setupUniforms(l), n.drawArrays(g.mode, 0, g.count), A && n.bindTexture(n.TEXTURE_2D, A.texture), o.lastState = {
265
+ material: T,
266
+ shape: E
267
+ };
268
+ }
269
+ }
270
+ function L(o, r = 0) {
271
+ var d, m;
272
+ const { context: c, systems: t, framebuffers: n } = o;
273
+ n.forEach((a) => a.resize()), c.clear(c.COLOR_BUFFER_BIT | c.DEPTH_BUFFER_BIT);
274
+ for (let a = t.length, s = 0; s < a; s++)
275
+ (m = (d = t[s]).update) == null || m.call(d, r);
276
+ c.flush();
319
277
  }
320
- function C() {
278
+ function W() {
321
279
  const o = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map();
322
- function e(i) {
323
- if (o.has(i))
324
- return o.get(i);
325
- if (r.has(i)) {
326
- const { value: l, shared: s } = r.get(i), n = l();
327
- return s && t(i, n), n;
280
+ function c(a) {
281
+ if (o.has(a))
282
+ return o.get(a);
283
+ if (r.has(a)) {
284
+ const { value: s, shared: e } = r.get(a), i = s();
285
+ return e && n(a, i), i;
328
286
  }
329
287
  }
330
- function a(i) {
331
- return o.has(i) || r.has(i);
288
+ function t(a) {
289
+ return o.has(a) || r.has(a);
332
290
  }
333
- function t(i, l) {
334
- o.set(i, l);
291
+ function n(a, s) {
292
+ o.set(a, s);
335
293
  }
336
- function c(i, l) {
337
- u(i, l, !0);
294
+ function d(a, s) {
295
+ m(a, s, !0);
338
296
  }
339
- function u(i, l, s = !1) {
340
- r.set(i, { value: l, shared: s });
297
+ function m(a, s, e = !1) {
298
+ r.set(a, { value: s, shared: e });
341
299
  }
342
300
  return new Proxy(
343
- { instances: o, bindings: r, get: e, has: a, set: t, bind: u, singleton: c },
301
+ { instances: o, bindings: r, get: c, has: t, set: n, bind: m, singleton: d },
344
302
  {
345
- get(i, l, s) {
346
- return typeof l == "symbol" || l in i ? Reflect.get(i, l, s) : i.get(l);
303
+ get(a, s, e) {
304
+ return typeof s == "symbol" || s in a ? Reflect.get(a, s, e) : a.get(s);
347
305
  },
348
- has(i, l) {
349
- return typeof l == "symbol" || l in i ? Reflect.has(i, l) : i.has(l);
306
+ has(a, s) {
307
+ return typeof s == "symbol" || s in a ? Reflect.has(a, s) : a.has(s);
350
308
  },
351
- set(i, l, s, n) {
352
- return typeof l == "symbol" || l in i ? Reflect.set(i, l, s, n) : (i.set(l, s), !0);
309
+ set(a, s, e, i) {
310
+ return typeof s == "symbol" || s in a ? Reflect.set(a, s, e, i) : (a.set(s, e), !0);
353
311
  }
354
312
  }
355
313
  );
356
314
  }
357
- function M(o, r) {
358
- const { gl: e, materials: a, glSlTypes: t } = o, {
359
- name: c,
360
- vertexShader: u,
361
- fragmentShader: i,
362
- uniforms: l
363
- } = r;
364
- if (a.has(c))
315
+ function V(o, r, c) {
316
+ const { context: t, materials: n, slTypes: d } = o, {
317
+ vertexShader: m,
318
+ fragmentShader: a,
319
+ uniforms: s
320
+ } = c;
321
+ if (n.has(r))
365
322
  return;
366
- const s = [
367
- { type: e.VERTEX_SHADER, source: u },
368
- { type: e.FRAGMENT_SHADER, source: i.includes("precision") ? i : `precision mediump float;
369
- ${i}` }
370
- ].map(({ type: T, source: d }) => {
371
- const E = e.createShader(T);
372
- if (!E)
323
+ const e = [
324
+ { type: t.VERTEX_SHADER, source: m },
325
+ { type: t.FRAGMENT_SHADER, source: a.includes("precision") ? a : `precision mediump float;
326
+ ${a}` }
327
+ ].map(({ type: E, source: T }) => {
328
+ const l = t.createShader(E);
329
+ if (!l)
373
330
  throw new Error("failed to create shader");
374
- if (e.shaderSource(E, d), e.compileShader(E), !e.getShaderParameter(E, e.COMPILE_STATUS))
331
+ if (t.shaderSource(l, T), t.compileShader(l), !t.getShaderParameter(l, t.COMPILE_STATUS))
375
332
  throw new Error(`failed to compiling shader:
376
- ${d}
377
- ${e.getShaderInfoLog(E)}`);
378
- return E;
379
- }), n = e.createProgram();
380
- if (!n)
333
+ ${T}
334
+ ${t.getShaderInfoLog(l)}`);
335
+ return l;
336
+ }), i = t.createProgram();
337
+ if (!i)
381
338
  throw new Error("failed to create program");
382
- if (s.forEach((T) => e.attachShader(n, T)), e.linkProgram(n), s.forEach((T) => e.deleteShader(T)), !e.getProgramParameter(n, e.LINK_STATUS))
383
- throw new Error(`failed to initing program: ${e.getProgramInfoLog(n)}`);
384
- const f = {};
385
- for (let T = e.getProgramParameter(n, e.ACTIVE_ATTRIBUTES), d = 0; d < T; d++) {
386
- const E = e.getActiveAttrib(n, d);
387
- if (!E)
339
+ if (e.forEach((E) => t.attachShader(i, E)), t.linkProgram(i), e.forEach((E) => t.deleteShader(E)), !t.getProgramParameter(i, t.LINK_STATUS))
340
+ throw new Error(`failed to initing program: ${t.getProgramInfoLog(i)}`);
341
+ const u = {};
342
+ for (let E = t.getProgramParameter(i, t.ACTIVE_ATTRIBUTES), T = 0; T < E; T++) {
343
+ const l = t.getActiveAttrib(i, T);
344
+ if (!l)
388
345
  continue;
389
- const m = E.name.replace(/\[.*?]$/, "");
390
- f[m] = {
391
- type: t[E.type],
392
- isArray: m !== E.name,
393
- location: e.getAttribLocation(n, m)
346
+ const g = l.name.replace(/\[.*?]$/, "");
347
+ u[g] = {
348
+ type: d[l.type],
349
+ isArray: g !== l.name,
350
+ location: t.getAttribLocation(i, g)
394
351
  };
395
352
  }
396
- const R = {};
397
- for (let T = e.getProgramParameter(n, e.ACTIVE_UNIFORMS), d = 0; d < T; d++) {
398
- const E = e.getActiveUniform(n, d);
399
- if (!E)
353
+ const f = {};
354
+ for (let E = t.getProgramParameter(i, t.ACTIVE_UNIFORMS), T = 0; T < E; T++) {
355
+ const l = t.getActiveUniform(i, T);
356
+ if (!l)
400
357
  continue;
401
- const m = E.name.replace(/\[.*?]$/, "");
402
- R[m] = {
403
- type: t[E.type],
404
- isArray: m !== E.name,
405
- location: e.getUniformLocation(n, m)
358
+ const g = l.name.replace(/\[.*?]$/, "");
359
+ f[g] = {
360
+ type: d[l.type],
361
+ isArray: g !== l.name,
362
+ location: t.getUniformLocation(i, g)
406
363
  };
407
364
  }
408
- const g = {
409
- ...r,
410
- program: n,
411
- attributes: f,
412
- uniforms: R,
413
- setAttributes(T) {
414
- for (const [d, E] of Object.entries(T)) {
415
- const m = f[d];
416
- if (!m)
365
+ const h = {
366
+ ...c,
367
+ program: i,
368
+ attributes: u,
369
+ uniforms: f,
370
+ setupAttributes(E) {
371
+ for (const [T, l] of Object.entries(E)) {
372
+ const g = u[T];
373
+ if (!g)
417
374
  continue;
418
- const { type: A, isArray: x, location: b } = m;
419
- if (E instanceof WebGLBuffer) {
420
- e.bindBuffer(e.ARRAY_BUFFER, E);
421
- const h = e.getAttribLocation(n, d);
422
- switch (A) {
375
+ const { type: R, isArray: A, location: _ } = g;
376
+ if (l instanceof WebGLBuffer) {
377
+ t.bindBuffer(t.ARRAY_BUFFER, l);
378
+ const p = t.getAttribLocation(i, T);
379
+ switch (R) {
423
380
  case "vec2":
424
- e.vertexAttribPointer(h, 2, e.FLOAT, !1, 0, 0), e.enableVertexAttribArray(h);
381
+ t.vertexAttribPointer(p, 2, t.FLOAT, !1, 0, 0), t.enableVertexAttribArray(p);
425
382
  break;
426
383
  case "vec3":
427
- e.vertexAttribPointer(h, 3, e.FLOAT, !1, 0, 0), e.enableVertexAttribArray(h);
384
+ t.vertexAttribPointer(p, 3, t.FLOAT, !1, 0, 0), t.enableVertexAttribArray(p);
428
385
  break;
429
386
  }
430
387
  } else
431
- switch (A) {
388
+ switch (R) {
432
389
  case "float":
433
- x ? e.vertexAttrib1fv(b, E) : e.vertexAttrib1f(b, E);
390
+ A ? t.vertexAttrib1fv(_, l) : t.vertexAttrib1f(_, l);
434
391
  break;
435
392
  case "vec2":
436
- e.vertexAttrib2fv(b, E);
393
+ t.vertexAttrib2fv(_, l);
437
394
  break;
438
395
  case "vec3":
439
- e.vertexAttrib3fv(b, E);
396
+ t.vertexAttrib3fv(_, l);
440
397
  break;
441
398
  case "vec4":
442
- e.vertexAttrib4fv(b, E);
399
+ t.vertexAttrib4fv(_, l);
443
400
  break;
444
401
  }
445
402
  }
446
403
  },
447
- setUniforms(T) {
448
- var d;
449
- for (const [E, m] of Object.entries(T)) {
450
- const A = R[E];
451
- if (!A)
404
+ setupUniforms(E) {
405
+ for (const [T, l] of Object.entries(E)) {
406
+ const g = f[T];
407
+ if (!g)
452
408
  continue;
453
- const { type: x, isArray: b, location: h } = A;
454
- switch (x) {
409
+ const { type: R, isArray: A, location: _ } = g;
410
+ switch (R) {
455
411
  case "float":
456
- b ? e.uniform1fv(h, m) : e.uniform1f(h, m);
412
+ A ? t.uniform1fv(_, l) : t.uniform1f(_, l);
457
413
  break;
458
414
  case "bool":
459
415
  case "int":
460
- b ? e.uniform1iv(h, m) : e.uniform1i(h, m);
416
+ A ? t.uniform1iv(_, l) : t.uniform1i(_, l);
461
417
  break;
462
418
  case "vec2":
463
- e.uniform2fv(h, m);
419
+ t.uniform2fv(_, l);
464
420
  break;
465
421
  case "vec3":
466
- e.uniform3fv(h, m);
422
+ t.uniform3fv(_, l);
467
423
  break;
468
424
  case "vec4":
469
- e.uniform4fv(h, m);
425
+ t.uniform4fv(_, l);
470
426
  break;
471
427
  case "mat2":
472
- e.uniformMatrix2fv(h, !1, m);
428
+ t.uniformMatrix2fv(_, !1, l);
473
429
  break;
474
430
  case "mat3":
475
- e.uniformMatrix3fv(h, !1, m);
431
+ t.uniformMatrix3fv(_, !1, l);
476
432
  break;
477
433
  case "mat4":
478
- e.uniformMatrix4fv(h, !1, m);
434
+ t.uniformMatrix4fv(_, !1, l);
479
435
  break;
480
436
  case "sampler2D":
481
- e.bindTexture(
482
- e.TEXTURE_2D,
483
- ((d = o.resources.get(m)) == null ? void 0 : d.texture) ?? o.glDefaultTexture
484
- ), e.uniform1i(h, 0);
437
+ t.bindTexture(
438
+ t.TEXTURE_2D,
439
+ l ?? o.defaultTexture
440
+ ), t.uniform1i(_, 0);
485
441
  break;
486
442
  }
487
443
  }
488
444
  }
489
445
  };
490
- l && g.setUniforms(l), a.set(c, g);
446
+ s && h.setupUniforms(s), n.set(r, h);
447
+ }
448
+ function H(o, r, c) {
449
+ const { context: t, shapes: n, drawModes: d } = o, {
450
+ type: m = "2d",
451
+ mode: a = "triangles",
452
+ buffer: s = new Float32Array([])
453
+ } = c, e = t.createBuffer();
454
+ t.bindBuffer(t.ARRAY_BUFFER, e), t.bufferData(t.ARRAY_BUFFER, s, t.STATIC_DRAW), n.set(r, {
455
+ mode: d[a],
456
+ count: s.byteLength / s.BYTES_PER_ELEMENT / (m === "2d" ? 2 : 3),
457
+ buffer: e
458
+ });
491
459
  }
492
- function I(o, r) {
493
- o.singleton("gl", () => {
494
- const { view: e } = o, a = e.getContext("webgl", r) || e.getContext("experimental-webgl", r);
495
- if (!a)
460
+ function Y(o) {
461
+ const { entities: r, children: c, components: t, archetypes: n } = o;
462
+ r.clear(), t.clear(), n.clear();
463
+ const d = function() {
464
+ let e = 0;
465
+ return () => ++e;
466
+ }(), m = function() {
467
+ let e = 0;
468
+ return () => ++e;
469
+ }();
470
+ function a(e, i) {
471
+ var R, A;
472
+ e.id || (e.id = d());
473
+ const u = e.id, f = [];
474
+ for (const _ in e) {
475
+ let p = t.get(_);
476
+ p || (p = m(), t.set(_, p)), f.push(p);
477
+ }
478
+ const h = P(f), E = h.map((_) => String.fromCharCode(_)).join(""), T = r.get(u);
479
+ T && ((A = (R = n.get(T.archetypeId)) == null ? void 0 : R.entityIds) == null || A.delete(u));
480
+ const l = n.get(E);
481
+ let g = l == null ? void 0 : l.entityIds;
482
+ l || (g = /* @__PURE__ */ new Set(), n.set(E, {
483
+ entityIds: g,
484
+ codePoints: h
485
+ })), g.add(u), r.set(u, { path: i, archetypeId: E });
486
+ }
487
+ function s(e, i) {
488
+ for (let u = e.length, f = 0; f < u; f++) {
489
+ const h = e[f], E = [...i, f];
490
+ a(h, E), h.children && s(h.children, E);
491
+ }
492
+ }
493
+ s(c, []);
494
+ }
495
+ function k(o, r) {
496
+ const { entities: c, components: t, children: n, archetypes: d } = o;
497
+ if (!r)
498
+ return n.flatMap(function i(u) {
499
+ var f;
500
+ return [u, (f = u.children) == null ? void 0 : f.flatMap(i)].filter(Boolean);
501
+ });
502
+ const m = typeof r == "string" ? [r] : r, a = [];
503
+ for (let i = m.length, u = 0; u < i; u++) {
504
+ const f = t.get(m[u]);
505
+ f && a.push(f);
506
+ }
507
+ const s = P(a), e = [];
508
+ for (const [, { entityIds: i, codePoints: u }] of d.entries())
509
+ if (u.some((f) => s.some((h) => h & f)))
510
+ for (const [f] of i.entries()) {
511
+ const { path: h } = c.get(f);
512
+ if (!h || !h.length)
513
+ continue;
514
+ let E = n[h[0]];
515
+ for (let T = h.length, l = 1; l < T; l++)
516
+ E = E[l];
517
+ e.push(E);
518
+ }
519
+ return e;
520
+ }
521
+ function q(o, r, c) {
522
+ const { context: t, textures: n } = o, d = {
523
+ loading: !0,
524
+ value: null,
525
+ source: c
526
+ };
527
+ return n.set(r, d), new Promise((m) => {
528
+ c instanceof HTMLImageElement ? c.addEventListener("load", a, { once: !0 }) : c instanceof HTMLVideoElement ? (c.addEventListener("canplay", a, { once: !0 }), c.addEventListener("timeupdate", () => {
529
+ t.bindTexture(t.TEXTURE_2D, d.value), t.texImage2D(t.TEXTURE_2D, 0, t.RGBA, t.RGBA, t.UNSIGNED_BYTE, c);
530
+ })) : a();
531
+ function a() {
532
+ d.value = t.createTexture(), d.value && (t.bindTexture(t.TEXTURE_2D, d.value), t.texParameteri(t.TEXTURE_2D, t.TEXTURE_MIN_FILTER, t.LINEAR), t.texParameteri(t.TEXTURE_2D, t.TEXTURE_MAG_FILTER, t.LINEAR), t.texParameteri(t.TEXTURE_2D, t.TEXTURE_WRAP_S, t.CLAMP_TO_EDGE), t.texParameteri(t.TEXTURE_2D, t.TEXTURE_WRAP_T, t.CLAMP_TO_EDGE), t.texImage2D(t.TEXTURE_2D, 0, t.RGBA, t.RGBA, t.UNSIGNED_BYTE, c)), d.loading = !1, c instanceof HTMLVideoElement && (c.currentTime = 0.01), m();
533
+ }
534
+ });
535
+ }
536
+ function K(o, r) {
537
+ const { systems: c } = o;
538
+ c.push(r);
539
+ }
540
+ function z(o = {}) {
541
+ var a, s;
542
+ const {
543
+ view: r = document.createElement("canvas"),
544
+ children: c = [],
545
+ plugins: t = []
546
+ } = o, n = W();
547
+ n.view = r, n.singleton("context", () => {
548
+ const { view: e } = n, i = e.getContext("webgl", o) || e.getContext("experimental-webgl", o);
549
+ if (!i)
496
550
  throw new Error("failed to getContext for webgl");
497
- return a.pixelStorei(a.UNPACK_FLIP_Y_WEBGL, !0), a.pixelStorei(a.UNPACK_PREMULTIPLY_ALPHA_WEBGL, !0), a.clearColor(0, 0, 0, 0), a.enable(a.DEPTH_TEST), a.enable(a.CULL_FACE), a.enable(a.BLEND), a.blendFunc(a.SRC_ALPHA, a.ONE_MINUS_SRC_ALPHA), a;
498
- }), o.singleton("glDefaultTexture", () => {
499
- const { gl: e, width: a, height: t } = o, c = e.createTexture();
500
- return e.bindTexture(e.TEXTURE_2D, c), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), e.texImage2D(e.TEXTURE_2D, 0, e.RGBA, a, t, 0, e.RGBA, e.UNSIGNED_BYTE, null), c;
501
- }), o.singleton("glFramebuffers", () => {
502
- const { gl: e } = o;
551
+ const u = i.drawingBufferWidth, f = i.drawingBufferHeight;
552
+ return i.viewport(0, 0, u, f), i.pixelStorei(i.UNPACK_FLIP_Y_WEBGL, !0), i.pixelStorei(i.UNPACK_PREMULTIPLY_ALPHA_WEBGL, !0), i.clearColor(0, 0, 0, 0), i.enable(i.DEPTH_TEST), i.enable(i.CULL_FACE), i.enable(i.BLEND), i.blendFunc(i.ONE, i.ONE_MINUS_SRC_ALPHA), i.depthMask(!1), i;
553
+ }), n.singleton("defaultTexture", () => {
554
+ const { context: e, width: i, height: u } = n, f = e.createTexture();
555
+ return e.bindTexture(e.TEXTURE_2D, f), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), e.texImage2D(e.TEXTURE_2D, 0, e.RGBA, i, u, 0, e.RGBA, e.UNSIGNED_BYTE, null), f;
556
+ }), n.singleton("framebuffers", () => {
557
+ const { context: e } = n;
503
558
  return Array.from({ length: 2 }, () => {
504
- const a = e.createTexture(), t = e.createFramebuffer(), c = e.createRenderbuffer();
505
- function u() {
506
- const { width: i, height: l } = o;
507
- e.bindTexture(e.TEXTURE_2D, a), e.texImage2D(e.TEXTURE_2D, 0, e.RGBA, i, l, 0, e.RGBA, e.UNSIGNED_BYTE, null), e.bindRenderbuffer(e.RENDERBUFFER, c), e.renderbufferStorage(e.RENDERBUFFER, e.DEPTH_COMPONENT16, i, l);
559
+ const i = e.createTexture(), u = e.createFramebuffer(), f = e.createRenderbuffer();
560
+ function h() {
561
+ const { width: E, height: T } = n;
562
+ e.bindTexture(e.TEXTURE_2D, i), e.texImage2D(e.TEXTURE_2D, 0, e.RGBA, E, T, 0, e.RGBA, e.UNSIGNED_BYTE, null), e.bindRenderbuffer(e.RENDERBUFFER, f), e.renderbufferStorage(e.RENDERBUFFER, e.DEPTH_COMPONENT16, E, T);
508
563
  }
509
- return u(), e.bindTexture(e.TEXTURE_2D, a), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), e.bindFramebuffer(e.FRAMEBUFFER, t), e.framebufferTexture2D(e.FRAMEBUFFER, e.COLOR_ATTACHMENT0, e.TEXTURE_2D, a, 0), e.bindRenderbuffer(e.RENDERBUFFER, c), e.framebufferRenderbuffer(e.FRAMEBUFFER, e.DEPTH_ATTACHMENT, e.RENDERBUFFER, c), {
510
- buffer: t,
511
- depthBuffer: c,
512
- texture: a,
513
- resize: u
564
+ return h(), e.bindTexture(e.TEXTURE_2D, i), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), e.bindFramebuffer(e.FRAMEBUFFER, u), e.framebufferTexture2D(e.FRAMEBUFFER, e.COLOR_ATTACHMENT0, e.TEXTURE_2D, i, 0), e.bindRenderbuffer(e.RENDERBUFFER, f), e.framebufferRenderbuffer(e.FRAMEBUFFER, e.DEPTH_ATTACHMENT, e.RENDERBUFFER, f), {
565
+ buffer: u,
566
+ depthBuffer: f,
567
+ texture: i,
568
+ resize: h
514
569
  };
515
570
  });
516
- }), o.singleton("glDrawModes", () => {
517
- const { gl: e } = o;
571
+ }), n.singleton("drawModes", () => {
572
+ const { context: e } = n;
518
573
  return {
519
574
  points: e.POINTS,
520
575
  linear: e.LINEAR,
@@ -522,8 +577,8 @@ function I(o, r) {
522
577
  triangleStrip: e.TRIANGLE_STRIP,
523
578
  triangleFan: e.TRIANGLE_FAN
524
579
  };
525
- }), o.singleton("glSlTypes", () => {
526
- const e = o.gl;
580
+ }), n.singleton("slTypes", () => {
581
+ const { context: e } = n;
527
582
  return {
528
583
  [e.FLOAT]: "float",
529
584
  [e.FLOAT_VEC2]: "vec2",
@@ -534,9 +589,9 @@ function I(o, r) {
534
589
  [e.INT_VEC3]: "ivec3",
535
590
  [e.INT_VEC4]: "ivec4",
536
591
  [e.UNSIGNED_INT]: "uint",
537
- [e.UNSIGNED_INT_VEC2]: "uvec2",
538
- [e.UNSIGNED_INT_VEC3]: "uvec3",
539
- [e.UNSIGNED_INT_VEC4]: "uvec4",
592
+ [e.UNSIGNED_INT_VEC2 ?? -1]: "uvec2",
593
+ [e.UNSIGNED_INT_VEC3 ?? -1]: "uvec3",
594
+ [e.UNSIGNED_INT_VEC4 ?? -1]: "uvec4",
540
595
  [e.BOOL]: "bool",
541
596
  [e.BOOL_VEC2]: "bvec2",
542
597
  [e.BOOL_VEC3]: "bvec3",
@@ -545,128 +600,48 @@ function I(o, r) {
545
600
  [e.FLOAT_MAT3]: "mat3",
546
601
  [e.FLOAT_MAT4]: "mat4",
547
602
  [e.SAMPLER_2D]: "sampler2D",
548
- [e.INT_SAMPLER_2D]: "sampler2D",
549
- [e.UNSIGNED_INT_SAMPLER_2D]: "sampler2D",
603
+ [e.INT_SAMPLER_2D ?? -1]: "sampler2D",
604
+ [e.UNSIGNED_INT_SAMPLER_2D ?? -1]: "sampler2D",
550
605
  [e.SAMPLER_CUBE]: "samplerCube",
551
- [e.INT_SAMPLER_CUBE]: "samplerCube",
552
- [e.UNSIGNED_INT_SAMPLER_CUBE]: "samplerCube",
553
- [e.SAMPLER_2D_ARRAY]: "sampler2DArray",
554
- [e.INT_SAMPLER_2D_ARRAY]: "sampler2DArray",
555
- [e.UNSIGNED_INT_SAMPLER_2D_ARRAY]: "sampler2DArray"
606
+ [e.INT_SAMPLER_CUBE ?? -1]: "samplerCube",
607
+ [e.UNSIGNED_INT_SAMPLER_CUBE ?? -1]: "samplerCube",
608
+ [e.SAMPLER_2D_ARRAY ?? -1]: "sampler2DArray",
609
+ [e.INT_SAMPLER_2D_ARRAY ?? -1]: "sampler2DArray",
610
+ [e.UNSIGNED_INT_SAMPLER_2D_ARRAY ?? -1]: "sampler2DArray"
556
611
  };
557
- }), o.singleton("glExtensions", () => ({
558
- loseContext: o.gl.getExtension("WEBGL_lose_context")
559
- }));
560
- }
561
- function B(o, r) {
562
- const { gl: e, shapes: a, glDrawModes: t } = o, {
563
- name: c,
564
- type: u = "2d",
565
- mode: i = "triangles",
566
- data: l = new Float32Array([])
567
- } = r, s = e.createBuffer();
568
- e.bindBuffer(e.ARRAY_BUFFER, s), e.bufferData(e.ARRAY_BUFFER, l, e.STATIC_DRAW), a.set(c, {
569
- mode: t[i],
570
- count: l.byteLength / l.BYTES_PER_ELEMENT / (u === "2d" ? 2 : 3),
571
- buffer: s
572
- });
573
- }
574
- function y(o, r) {
575
- const { gl: e, shapes: a, materials: t, nodeRenderers: c } = o, { name: u, shape: i, material: l } = r, s = typeof i == "string" ? i : `${u}-shape`;
576
- typeof i != "string" && o.registerShape({ ...i, name: s });
577
- const n = typeof l == "string" ? l : `${u}-material`;
578
- typeof l != "string" && o.registerMaterial({ ...l, name: n });
579
- const f = a.get(s), R = t.get(n);
580
- if (!f || !R)
581
- return;
582
- const { buffer: g } = f;
583
- R.setAttributes({ aPosition: g }), c.set(u, {
584
- ...r,
585
- shape: s,
586
- material: n,
587
- render: (T, d) => {
588
- var p;
589
- const E = a.get(s), m = t.get(n);
590
- if (!E || !m)
591
- return;
592
- const { program: A } = m, { mode: x, count: b } = E;
593
- e.useProgram(A);
594
- const h = (p = r.update) == null ? void 0 : p.call(r, T, d);
595
- h && m.setUniforms(h), e.drawArrays(x, 0, b);
612
+ }), n.singleton("extensions", () => ({
613
+ loseContext: n.context.getExtension("WEBGL_lose_context")
614
+ })), n.bind("width", () => n.context.drawingBufferWidth), n.bind("height", () => n.context.drawingBufferHeight), n.plugins = /* @__PURE__ */ new Map(), n.children = c, n.shapes = /* @__PURE__ */ new Map(), n.registerShape = (e, i) => H(n, e, i), n.materials = /* @__PURE__ */ new Map(), n.registerMaterial = (e, i) => V(n, e, i), n.textures = /* @__PURE__ */ new Map(), n.registerTexture = (e, i) => q(n, e, i), n.entities = /* @__PURE__ */ new Map(), n.components = /* @__PURE__ */ new Map(), n.archetypes = /* @__PURE__ */ new Map(), n.systems = [], n.registerSystem = (e) => K(n, e), n.query = (e) => k(n, e), n.setup = () => Y(n), n.load = async () => {
615
+ var u, f;
616
+ const { systems: e, textures: i } = n;
617
+ for (let h = e.length, E = 0; E < h; E++)
618
+ (f = (u = e[E]).update) == null || f.call(u, 0);
619
+ for (; Array.from(i.values()).some((h) => h.loading); )
620
+ await new Promise((h) => setTimeout(h, 100));
621
+ }, n.renderNode = (e) => $(n, e), n.render = (e) => L(n, e), n.start = () => {
622
+ let e = 0, i = 0;
623
+ u(0);
624
+ function u(f) {
625
+ requestAnimationFrame(u), L(n, i), f *= 1e-3, i += f - e, e = f;
596
626
  }
597
- });
598
- }
599
- function w(o, r) {
600
- const { children: e } = o;
601
- function a(t, c = []) {
602
- for (let u = t.length - 1; u >= 0; u--) {
603
- const i = [...c, u], l = t[u];
604
- a(l.children ?? [], i), r(l, i);
605
- }
606
- }
607
- a(e);
608
- }
609
- function O(o, r) {
610
- const { gl: e, resources: a } = o, { name: t, data: c } = r, u = {
611
- loading: !0,
612
- texture: null
613
627
  };
614
- function i() {
615
- u.texture = e.createTexture(), u.texture && (e.bindTexture(e.TEXTURE_2D, u.texture), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.LINEAR), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), e.texImage2D(e.TEXTURE_2D, 0, e.RGBA, e.RGBA, e.UNSIGNED_BYTE, c)), u.loading = !1;
616
- }
617
- c instanceof HTMLImageElement ? c.addEventListener("load", i, { once: !0 }) : i(), a.set(t, u);
618
- }
619
- function G(o = {}) {
620
- var i, l;
621
- const {
622
- view: r = document.createElement("canvas"),
623
- children: e = [],
624
- plugins: a = []
625
- } = o, t = C();
626
- t.set("view", r), I(t, o), t.bind("width", () => t.gl.drawingBufferWidth), t.bind("height", () => t.gl.drawingBufferHeight), t.singleton("plugins", () => {
627
- const s = /* @__PURE__ */ new Map();
628
- return a.forEach((n) => {
629
- var f;
630
- (f = n.register) == null || f.call(n, t), s.set(n.name, n);
631
- }), s;
632
- }), t.singleton("beforeRenderPlugins", () => Array.from(t.plugins.values()).filter((s) => "beforeRender" in s)), t.singleton("afterRenderPlugins", () => Array.from(t.plugins.values()).filter((s) => "afterRender" in s)), t.set("children", e), t.set("shapes", /* @__PURE__ */ new Map()), t.set("registerShape", (s) => B(t, s)), t.set("materials", /* @__PURE__ */ new Map()), t.set("registerMaterial", (s) => M(t, s)), t.set("resources", /* @__PURE__ */ new Map()), t.set("registerResource", (s) => O(t, s)), t.set("nodeRenderers", /* @__PURE__ */ new Map()), t.set("registerNodeRenderer", (s) => y(t, s)), t.set("forEachNode", (s) => w(t, s)), t.set("load", async () => {
633
- t.get("plugins");
634
- const s = Array.from(t.nodeRenderers.values());
635
- for (t.forEachNode((n, f) => {
636
- s.filter((g) => !g.include && !g.exclude || g.include && g.include(n, f) || g.exclude && !g.exclude(n, f)).forEach((g) => {
637
- var T;
638
- return (T = g.update) == null ? void 0 : T.call(g, n, 0);
639
- });
640
- }); Array.from(t.resources.values()).some((n) => n.loading); )
641
- await new Promise((n) => setTimeout(n, 100));
642
- return !0;
643
- }), t.set("render", (s) => P(t, s)), t.set("startRenderLoop", () => {
644
- let s = 0, n = 0;
645
- f(0);
646
- function f(R) {
647
- requestAnimationFrame(f), P(t, n), R *= 1e-3, n += R - s, s = R;
648
- }
649
- });
650
- function c(s) {
651
- s.preventDefault(), setTimeout(() => {
652
- var n;
653
- t.gl.isContextLost() && ((n = t.glExtensions.loseContext) == null || n.restoreContext());
628
+ function d(e) {
629
+ e.preventDefault(), setTimeout(() => {
630
+ var i;
631
+ n.context.isContextLost() && ((i = n.extensions.loseContext) == null || i.restoreContext());
654
632
  }, 0);
655
633
  }
656
- function u() {
634
+ function m() {
657
635
  }
658
- return (i = r.addEventListener) == null || i.call(r, "webglcontextlost", c, !1), (l = r.addEventListener) == null || l.call(r, "webglcontextrestored", u, !1), t.set("destroy", () => {
659
- var s, n, f;
660
- (s = r.removeEventListener) == null || s.call(r, "webglcontextlost", c), (n = r.removeEventListener) == null || n.call(r, "webglcontextrestored", u), t.gl.useProgram(null), (f = t.glExtensions.loseContext) == null || f.loseContext();
661
- }), t;
636
+ return (a = r.addEventListener) == null || a.call(r, "webglcontextlost", d, !1), (s = r.addEventListener) == null || s.call(r, "webglcontextrestored", m, !1), n.destroy = () => {
637
+ var e, i, u;
638
+ (e = r.removeEventListener) == null || e.call(r, "webglcontextlost", d), (i = r.removeEventListener) == null || i.call(r, "webglcontextrestored", m), n.context.useProgram(null), (u = n.extensions.loseContext) == null || u.loseContext();
639
+ }, t.forEach((e) => {
640
+ var i;
641
+ (i = e.register) == null || i.call(e, n), n.plugins.set(e.name, e);
642
+ }), n.setup(), n;
662
643
  }
663
644
  export {
664
- U as basePlugin,
665
- G as createCanvas,
666
- F as fadePlugin,
667
- D as nodeImagePlugin,
668
- N as nodeTextPlugin,
669
- X as plugins,
670
- L as selector2dPlugin,
671
- S as transform2dPlugin
645
+ j as Renderer2d,
646
+ z as createApp
672
647
  };