rapid-render 0.0.1

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/rapid.js ADDED
@@ -0,0 +1,784 @@
1
+ var g = Object.defineProperty;
2
+ var _ = (s, e, t) => e in s ? g(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
3
+ var a = (s, e, t) => (_(s, typeof e != "symbol" ? e + "" : e, t), t);
4
+ class p {
5
+ constructor(e) {
6
+ a(this, "usedElemNum");
7
+ a(this, "typedArray");
8
+ a(this, "arrayType");
9
+ a(this, "maxElemNum");
10
+ a(this, "bytePerElem");
11
+ this.usedElemNum = 0, this.maxElemNum = 64, this.bytePerElem = e.BYTES_PER_ELEMENT, this.arrayType = e, this.typedArray = new e(this.maxElemNum);
12
+ }
13
+ clear() {
14
+ this.usedElemNum = 0;
15
+ }
16
+ /**
17
+ * resize the array
18
+ * @param size
19
+ * @returns
20
+ */
21
+ resize(e = 0) {
22
+ if (e += this.usedElemNum, e > this.maxElemNum) {
23
+ for (; e > this.maxElemNum; )
24
+ this.maxElemNum <<= 1;
25
+ const t = this.typedArray;
26
+ return this.typedArray = new this.arrayType(this.maxElemNum), this.typedArray.set(t), this;
27
+ }
28
+ }
29
+ /**
30
+ * push a new element
31
+ * @param value
32
+ */
33
+ push(e) {
34
+ this.typedArray[this.usedElemNum] = e, this.usedElemNum += 1;
35
+ }
36
+ /**
37
+ * pop a element
38
+ * @param num
39
+ */
40
+ pop(e) {
41
+ this.usedElemNum -= e;
42
+ }
43
+ /**
44
+ * get the array
45
+ * @param begin
46
+ * @param end
47
+ * @returns
48
+ */
49
+ getArray(e = 0, t) {
50
+ return t == null ? this.typedArray : this.typedArray.subarray(e, t);
51
+ }
52
+ /**
53
+ * length of the array
54
+ */
55
+ get length() {
56
+ return this.typedArray.length;
57
+ }
58
+ }
59
+ class b extends p {
60
+ constructor() {
61
+ super(Float32Array);
62
+ }
63
+ /**
64
+ * push a matrix to the stack
65
+ */
66
+ pushMat() {
67
+ const e = this.usedElemNum - 6, t = this.typedArray;
68
+ this.resize(6), this.push(t[e + 0]), this.push(t[e + 1]), this.push(t[e + 2]), this.push(t[e + 3]), this.push(t[e + 4]), this.push(t[e + 5]);
69
+ }
70
+ /**
71
+ * pop a matrix from the stack
72
+ */
73
+ popMat() {
74
+ this.pop(6);
75
+ }
76
+ /**
77
+ * push a matrix and indentiy it
78
+ */
79
+ pushIdentity() {
80
+ this.resize(6), this.push(1), this.push(0), this.push(0), this.push(1), this.push(0), this.push(0);
81
+ }
82
+ /**
83
+ * Translates the current matrix by the specified x and y values.
84
+ * @param x - The amount to translate horizontally.
85
+ * @param y - The amount to translate vertically.
86
+ */
87
+ translate(e, t) {
88
+ const r = this.usedElemNum - 6, i = this.typedArray;
89
+ i[r + 4] = i[r + 0] * e + i[r + 2] * t + i[r + 4], i[r + 5] = i[r + 1] * e + i[r + 3] * t + i[r + 5];
90
+ }
91
+ /**
92
+ * Rotates the current matrix by the specified angle.
93
+ * @param angle - The angle, in radians, to rotate the matrix by.
94
+ */
95
+ rotate(e) {
96
+ const t = this.usedElemNum - 6, r = this.typedArray, i = Math.cos(e), h = Math.sin(e), n = r[t + 0], l = r[t + 1], d = r[t + 2], E = r[t + 3];
97
+ r[t + 0] = n * i - l * h, r[t + 1] = n * h + l * i, r[t + 2] = d * i - E * h, r[t + 3] = d * h + E * i;
98
+ }
99
+ /**
100
+ * Multiplies the current matrix on the top of the stack by a scaling transformation.
101
+ * @param x - The amount to scale the matrix horizontally.
102
+ * @param y - The amount to scale the matrix vertically. If not specified, x is used for both horizontal and vertical scaling.
103
+ */
104
+ scale(e, t = e) {
105
+ const r = this.usedElemNum - 6, i = this.typedArray;
106
+ i[r + 0] = i[r + 0] * e, i[r + 1] = i[r + 1] * e, i[r + 2] = i[r + 2] * t, i[r + 3] = i[r + 3] * t;
107
+ }
108
+ apply(e, t) {
109
+ const r = this.usedElemNum - 6, i = this.typedArray;
110
+ return [
111
+ i[r + 0] * e + i[r + 2] * t + i[r + 4],
112
+ i[r + 1] * e + i[r + 3] * t + i[r + 5]
113
+ ];
114
+ }
115
+ }
116
+ class o {
117
+ constructor(e, t, r, i) {
118
+ a(this, "r");
119
+ a(this, "g");
120
+ a(this, "b");
121
+ a(this, "a");
122
+ this.r = e, this.g = t, this.b = r, this.a = i;
123
+ }
124
+ /**
125
+ * is equal to another color instance
126
+ * @param color
127
+ * @returns
128
+ */
129
+ equals(e) {
130
+ return e.r == this.r && e.g == this.g && e.b == this.b && e.a == this.a;
131
+ }
132
+ /**
133
+ * create a new color by hex string
134
+ * @param hexString
135
+ * @returns
136
+ */
137
+ static fromHex(e) {
138
+ e.startsWith("#") && (e = e.slice(1));
139
+ const t = parseInt(e.slice(0, 2), 16) / 255, r = parseInt(e.slice(2, 4), 16) / 255, i = parseInt(e.slice(4, 6), 16) / 255;
140
+ let h = 1;
141
+ return e.length >= 8 && (h = parseInt(e.slice(6, 8), 16) / 255), new o(t, r, i, h);
142
+ }
143
+ }
144
+ class T extends p {
145
+ constructor(t, r, i = t.ARRAY_BUFFER) {
146
+ super(r);
147
+ a(this, "buffer");
148
+ a(this, "gl");
149
+ a(this, "type");
150
+ a(this, "bufferSize", 0);
151
+ this.gl = t, this.buffer = t.createBuffer(), this.type = i;
152
+ }
153
+ /**
154
+ * bind buffer to gpu
155
+ */
156
+ bindBuffer() {
157
+ this.gl.bindBuffer(this.type, this.buffer);
158
+ }
159
+ /**
160
+ * array data to gpu
161
+ */
162
+ bufferData() {
163
+ const t = this.gl;
164
+ this.usedElemNum > this.bufferSize ? (t.bufferData(this.type, this.getArray(0, this.usedElemNum), t.STATIC_DRAW), this.bufferSize = this.usedElemNum) : t.bufferSubData(this.type, 0, this.getArray(0, this.usedElemNum));
165
+ }
166
+ /**
167
+ * clear
168
+ */
169
+ clear() {
170
+ super.clear(), this.bufferSize = 0;
171
+ }
172
+ }
173
+ class A extends T {
174
+ constructor(t, r, i) {
175
+ super(t, Uint16Array, t.ELEMENT_ARRAY_BUFFER);
176
+ a(this, "elemVertPerObj");
177
+ a(this, "objects", 0);
178
+ a(this, "vertexPerObject");
179
+ a(this, "dirty", !0);
180
+ this.elemVertPerObj = r, this.vertexPerObject = i;
181
+ }
182
+ addObject(t) {
183
+ this.objects++, this.resize(this.elemVertPerObj);
184
+ }
185
+ /**
186
+ * set number of objects
187
+ * @param obj
188
+ */
189
+ setObjects(t) {
190
+ if (t > this.objects) {
191
+ for (; t > this.objects; )
192
+ this.addObject(this.objects * this.vertexPerObject);
193
+ this.dirty = !0;
194
+ }
195
+ }
196
+ /**
197
+ * array data to gpu
198
+ */
199
+ bufferData() {
200
+ this.dirty && super.bufferData(), this.dirty = !1;
201
+ }
202
+ clear() {
203
+ super.clear(), this.objects = 0;
204
+ }
205
+ }
206
+ const y = (s) => {
207
+ const e = s.getContext("webgl2") || s.getContext("webgl");
208
+ if (!e)
209
+ throw new Error("TODO");
210
+ return e;
211
+ }, f = (s, e, t) => {
212
+ const r = s.createShader(t);
213
+ if (!r)
214
+ throw new Error("Unable to create webgl shader");
215
+ if (s.shaderSource(r, e), s.compileShader(r), !s.getShaderParameter(r, s.COMPILE_STATUS)) {
216
+ const h = s.getShaderInfoLog(r);
217
+ throw console.error("Shader compilation failed:", h), new Error("Unable to compile shader: " + h + e);
218
+ }
219
+ return r;
220
+ }, m = (s, e, t) => {
221
+ var r = s.createProgram(), i = f(s, e, 35633), h = f(s, t, 35632);
222
+ if (!r)
223
+ throw new Error("Unable to create program shader");
224
+ return s.attachShader(r, i), s.attachShader(r, h), s.linkProgram(r), r;
225
+ };
226
+ function S(s, e, t) {
227
+ const r = s.createTexture();
228
+ if (s.bindTexture(s.TEXTURE_2D, r), s.texParameteri(s.TEXTURE_2D, s.TEXTURE_MIN_FILTER, t ? s.LINEAR : s.NEAREST), s.texParameteri(s.TEXTURE_2D, s.TEXTURE_MAG_FILTER, t ? s.LINEAR : s.NEAREST), s.texParameteri(s.TEXTURE_2D, s.TEXTURE_WRAP_S, s.CLAMP_TO_EDGE), s.texParameteri(s.TEXTURE_2D, s.TEXTURE_WRAP_T, s.CLAMP_TO_EDGE), s.texImage2D(s.TEXTURE_2D, 0, s.RGBA, s.RGBA, s.UNSIGNED_BYTE, e), !r)
229
+ throw new Error("unable to create texture");
230
+ return r;
231
+ }
232
+ class R {
233
+ constructor(e) {
234
+ /** shader to render */
235
+ a(this, "shader");
236
+ /** If there is no new shader, use the default shader */
237
+ a(this, "defaultShader");
238
+ a(this, "render");
239
+ a(this, "gl");
240
+ /** buffer */
241
+ a(this, "attributeBuffer");
242
+ /** shader attribute */
243
+ a(this, "attribute");
244
+ this.render = e, this.gl = e.gl, this.attributeBuffer = new T(this.gl, Float32Array);
245
+ }
246
+ /**
247
+ * Create a default shader that is called by subclasses
248
+ * @param options
249
+ */
250
+ createDefaultShader(e) {
251
+ this.defaultShader = m(this.render.gl, e.vs, e.fs), this.attribute = e.attribute;
252
+ }
253
+ /**
254
+ * add a vertex
255
+ * @param x
256
+ * @param y
257
+ */
258
+ addVertex(e, t, ...r) {
259
+ const [i, h] = this.render.matrix.apply(e, t);
260
+ this.attributeBuffer.push(i), this.attributeBuffer.push(h);
261
+ }
262
+ flush() {
263
+ this.doFlush(), this.reset();
264
+ }
265
+ doFlush() {
266
+ this.attributeBuffer.bufferData();
267
+ }
268
+ /**
269
+ * reset this region
270
+ * called when flush or entering this region
271
+ */
272
+ reset() {
273
+ this.attributeBuffer.clear();
274
+ }
275
+ /**
276
+ * enter this region
277
+ * @param shader costum shader
278
+ */
279
+ enter(e) {
280
+ this.shader = e || this.defaultShader;
281
+ const t = this.gl;
282
+ t.useProgram(this.shader), this.attributeBuffer.bindBuffer(), this.attribute.forEach((i) => {
283
+ const h = t.getAttribLocation(this.shader, i.name);
284
+ t.enableVertexAttribArray(h), t.vertexAttribPointer(
285
+ h,
286
+ i.size,
287
+ i.type,
288
+ i.normalized || !1,
289
+ i.stride,
290
+ i.offset || 0
291
+ );
292
+ });
293
+ const r = t.getUniformLocation(this.shader, "uProjectionMatrix");
294
+ t.uniformMatrix4fv(
295
+ r,
296
+ !1,
297
+ this.render.projection
298
+ );
299
+ }
300
+ /**
301
+ * exit this reigon
302
+ */
303
+ exit() {
304
+ }
305
+ }
306
+ const x = `precision mediump float;\r
307
+ \r
308
+ uniform vec4 uColor;\r
309
+ \r
310
+ void main(void) {\r
311
+ gl_FragColor = uColor;\r
312
+ }\r
313
+ `, I = `precision mediump float;\r
314
+ \r
315
+ attribute vec2 aPosition;\r
316
+ uniform mat4 uProjectionMatrix;\r
317
+ uniform vec4 uColor;\r
318
+ \r
319
+ void main(void) {\r
320
+ gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r
321
+ }\r
322
+ `;
323
+ class v extends R {
324
+ constructor(t) {
325
+ super(t);
326
+ /** color to draw shapes */
327
+ a(this, "color", new o(0, 0, 0, 1));
328
+ /** is the color needs to be updated */
329
+ a(this, "colorDirty", !0);
330
+ /** number of vertex to render */
331
+ a(this, "count", 0);
332
+ const r = this.gl, i = 2 * Float32Array.BYTES_PER_ELEMENT;
333
+ this.createDefaultShader({
334
+ fs: x,
335
+ vs: I,
336
+ attribute: [
337
+ { name: "aPosition", size: 2, type: r.FLOAT, stride: i }
338
+ ]
339
+ });
340
+ }
341
+ /**
342
+ * set a color
343
+ * @param color
344
+ */
345
+ setColor(t) {
346
+ t.equals(this.color) || (this.color = t, this.colorDirty = !0);
347
+ }
348
+ /**
349
+ * draw a shape
350
+ * @param vertexs
351
+ * @param color
352
+ */
353
+ drawShape(t, r = new o(0, 0, 0, 1)) {
354
+ this.setColor(r);
355
+ for (let i = 0; i < t.length; i += 2)
356
+ this.addVertex(t[i], t[i + 1]), this.count++;
357
+ this.flush();
358
+ }
359
+ /**
360
+ * render
361
+ */
362
+ doFlush() {
363
+ if (this.count > 0) {
364
+ super.doFlush();
365
+ const t = this.gl;
366
+ if (this.colorDirty) {
367
+ const r = this.color, i = this.gl, h = i.getUniformLocation(this.shader, "uColor");
368
+ i.uniform4f(h, r.r, r.g, r.b, r.a), this.colorDirty = !1;
369
+ }
370
+ t.drawArrays(t.TRIANGLE_FAN, 0, this.count);
371
+ }
372
+ }
373
+ /**
374
+ * reset this region
375
+ * called when flush or entering this region
376
+ */
377
+ reset() {
378
+ super.reset(), this.count = 0;
379
+ }
380
+ /**
381
+ * enter this region
382
+ * @param shader costum shader
383
+ */
384
+ enter(t) {
385
+ super.enter(t), this.colorDirty = !0;
386
+ }
387
+ }
388
+ const P = `precision lowp float;\r
389
+ uniform sampler2D uTextures[%TEXTURE_NUM%];\r
390
+ \r
391
+ uniform vec4 uColor;\r
392
+ varying vec2 vRegion;\r
393
+ varying float vTextureId;\r
394
+ \r
395
+ void main(void) {\r
396
+ vec4 color;\r
397
+ %GET_COLOR%\r
398
+ \r
399
+ gl_FragColor = color;\r
400
+ }`, w = `precision lowp float;\r
401
+ attribute vec2 aPosition;\r
402
+ attribute vec2 aRegion;\r
403
+ attribute float aTextureId;\r
404
+ \r
405
+ uniform mat4 uProjectionMatrix;\r
406
+ varying vec2 vRegion;\r
407
+ varying float vTextureId;\r
408
+ \r
409
+ void main(void) {\r
410
+ gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r
411
+ vRegion = aRegion;\r
412
+ vTextureId = aTextureId;\r
413
+ }`;
414
+ class N extends A {
415
+ constructor(e) {
416
+ super(e, 6, 4);
417
+ }
418
+ addObject(e) {
419
+ super.addObject(), this.push(e), this.push(e + 3), this.push(e + 2), this.push(e), this.push(e + 1), this.push(e + 2);
420
+ }
421
+ }
422
+ class M extends R {
423
+ constructor(t) {
424
+ super(t);
425
+ a(this, "objects", 0);
426
+ a(this, "usedTextures", []);
427
+ a(this, "elementArray", new N(this.gl));
428
+ a(this, "textureUnits");
429
+ const r = this.gl, i = 5 * Float32Array.BYTES_PER_ELEMENT;
430
+ this.createDefaultShader({
431
+ fs: this.generateFragShader(P, t.MAX_TEXTURE_IMAGE_UNITS),
432
+ vs: w,
433
+ attribute: [
434
+ { name: "aPosition", size: 2, type: r.FLOAT, stride: i },
435
+ { name: "aRegion", size: 2, type: r.FLOAT, stride: i, offset: 2 * Float32Array.BYTES_PER_ELEMENT },
436
+ { name: "aTextureId", size: 1, type: r.FLOAT, stride: i, offset: 4 * Float32Array.BYTES_PER_ELEMENT }
437
+ ]
438
+ }), this.textureUnits = Array.from({ length: t.MAX_TEXTURE_IMAGE_UNITS }, (h, n) => n);
439
+ }
440
+ addVertex(t, r, i, h, n) {
441
+ super.addVertex(t, r), this.attributeBuffer.push(i), this.attributeBuffer.push(h), this.attributeBuffer.push(n);
442
+ }
443
+ /**
444
+ * draw a sprite
445
+ * @param texture sprite texture
446
+ * @param offsetX
447
+ * @param offsetY
448
+ */
449
+ drawSprite(t, r, i) {
450
+ this.objects > 10922 && this.flush();
451
+ const h = t.base, n = this.useTexture(h.texture);
452
+ this.objects++, this.elementArray.setObjects(this.objects), this.attributeBuffer.resize(2 * 6), this.addVertex(r, i, t.clipX, t.clipY, n), this.addVertex(r + t.width, i, t.clipW, t.clipY, n), this.addVertex(r + t.width, i + t.height, t.clipW, t.clipH, n), this.addVertex(r, i + t.height, t.clipX, t.clipH, n);
453
+ }
454
+ /**
455
+ * use a texture
456
+ * @return texture unit id
457
+ */
458
+ useTexture(t) {
459
+ let r = this.usedTextures.indexOf(t);
460
+ return r === -1 && (this.usedTextures.length >= this.render.MAX_TEXTURE_IMAGE_UNITS && this.flush(), this.usedTextures.push(t), r = this.usedTextures.length - 1), r;
461
+ }
462
+ /**
463
+ * render
464
+ */
465
+ doFlush() {
466
+ if (this.objects > 0) {
467
+ super.doFlush();
468
+ const t = this.gl;
469
+ for (let r = 0; r < this.usedTextures.length; r++) {
470
+ const i = this.usedTextures[r];
471
+ t.activeTexture(t.TEXTURE0 + r), t.bindTexture(t.TEXTURE_2D, i);
472
+ }
473
+ this.elementArray.bufferData(), t.drawElements(t.TRIANGLES, this.objects * 6, t.UNSIGNED_SHORT, 0);
474
+ }
475
+ }
476
+ /**
477
+ * reset this region
478
+ */
479
+ reset() {
480
+ super.reset(), this.elementArray.clear(), this.objects = 0, this.usedTextures = [];
481
+ }
482
+ /**
483
+ * enter this region
484
+ * @param shader costum shader
485
+ */
486
+ enter(t) {
487
+ super.enter(t);
488
+ const r = this.gl, i = r.getUniformLocation(this.shader, "uTextures");
489
+ r.uniform1iv(i, this.textureUnits), this.elementArray.bindBuffer();
490
+ }
491
+ /**
492
+ * Generate shader based on the maximum texture units supported by the GPU
493
+ * @param fs
494
+ * @param max
495
+ * @returns
496
+ */
497
+ generateFragShader(t, r) {
498
+ let i = "";
499
+ t = t.replace("%TEXTURE_NUM%", r.toString());
500
+ for (let h = 0; h < r; h++)
501
+ h == 0 ? i += `if(vTextureId == ${h}.0)` : h == r - 1 ? i += "else" : i += `else if(vTextureId == ${h}.0)`, i += `{color = texture2D(uTextures[${h}], vRegion);}`;
502
+ return t = t.replace("%GET_COLOR%", i), t;
503
+ }
504
+ }
505
+ class U {
506
+ constructor(e) {
507
+ a(this, "render");
508
+ a(this, "cache", /* @__PURE__ */ new Map());
509
+ this.render = e;
510
+ }
511
+ /**
512
+ * create texture from url
513
+ * Equivalent to {@link Texture.fromUrl} method
514
+ * @param url
515
+ * @param antialias
516
+ * @returns
517
+ */
518
+ async textureFromUrl(e, t = !1) {
519
+ let r = this.cache.get(e);
520
+ if (!r) {
521
+ const i = await this.loadImage(e);
522
+ r = u.fromImageSource(this.render, i, t), this.cache.set(e, r);
523
+ }
524
+ return new c(r);
525
+ }
526
+ async loadImage(e) {
527
+ return new Promise((t) => {
528
+ const r = new Image();
529
+ r.onload = () => {
530
+ t(r);
531
+ }, r.src = e;
532
+ });
533
+ }
534
+ }
535
+ class u {
536
+ constructor(e, t, r) {
537
+ a(this, "texture");
538
+ a(this, "width");
539
+ a(this, "height");
540
+ this.texture = e, this.width = t, this.height = r;
541
+ }
542
+ static fromImageSource(e, t, r = !1) {
543
+ return new u(
544
+ S(e.gl, t, r),
545
+ t.width,
546
+ t.height
547
+ );
548
+ }
549
+ }
550
+ class c {
551
+ /**
552
+ * Creates a new `Texture` instance with the specified base texture reference.
553
+ * @param base - The {@link BaseTexture} to be used by the texture.
554
+ */
555
+ constructor(e) {
556
+ /**
557
+ * Reference to the {@link BaseTexture} used by this texture.
558
+ */
559
+ a(this, "base");
560
+ /**
561
+ * The x-coordinate of the top-left corner of the clipped region.
562
+ */
563
+ a(this, "clipX");
564
+ /**
565
+ * The y-coordinate of the top-left corner of the clipped region.
566
+ */
567
+ a(this, "clipY");
568
+ /**
569
+ * The width of the clipped region.
570
+ */
571
+ a(this, "clipW");
572
+ /**
573
+ * The height of the clipped region.
574
+ */
575
+ a(this, "clipH");
576
+ /**
577
+ * The width of the texture.
578
+ */
579
+ a(this, "width");
580
+ /**
581
+ * The height of the texture.
582
+ */
583
+ a(this, "height");
584
+ this.base = e, this.setClipRegion(0, 0, e.width, e.height), console.log(this);
585
+ }
586
+ /**
587
+ * Sets the region of the texture to be used for rendering.
588
+ * @param x - The x-coordinate of the top-left corner of the region.
589
+ * @param y - The y-coordinate of the top-left corner of the region.
590
+ * @param w - The width of the region.
591
+ * @param h - The height of the region.
592
+ */
593
+ setClipRegion(e, t, r, i) {
594
+ this.clipX = e / this.base.width, this.clipY = t / this.base.width, this.clipW = this.clipX + r / this.base.width, this.clipH = this.clipY + i / this.base.width, this.width = r, this.height = i;
595
+ }
596
+ /**
597
+ * Creates a new `Texture` instance from the specified image source.
598
+ * @param rapid - The Rapid instance to use.
599
+ * @param image - The image source to create the texture from.
600
+ * @param antialias - Whether to enable antialiasing for the texture. Default is `false`.
601
+ * @returns A new `Texture` instance created from the image source.
602
+ */
603
+ static fromImageSource(e, t, r = !1) {
604
+ return new c(
605
+ u.fromImageSource(e, t, r)
606
+ );
607
+ }
608
+ /**
609
+ * Creates a new `Texture` instance from the specified URL.
610
+ * @param rapid - The Rapid instance to use.
611
+ * @param url - The URL of the image to create the texture from.
612
+ * @returns A new `Texture` instance created from the specified URL.
613
+ */
614
+ static fromUrl(e, t) {
615
+ return e.texture.textureFromUrl(t);
616
+ }
617
+ }
618
+ class C {
619
+ constructor(e) {
620
+ /**
621
+ * canvas element
622
+ */
623
+ a(this, "canvas");
624
+ /**
625
+ * WebGLContext
626
+ */
627
+ a(this, "gl");
628
+ /**
629
+ * projection matrix
630
+ */
631
+ a(this, "projection");
632
+ /**
633
+ * matrix stack
634
+ */
635
+ a(this, "matrix", new b());
636
+ /**
637
+ * texture manager
638
+ */
639
+ a(this, "texture", new U(this));
640
+ a(this, "devicePixelRatio");
641
+ a(this, "nativeWdith");
642
+ a(this, "nativeHeight");
643
+ /** background color */
644
+ a(this, "backgroundColor");
645
+ a(this, "currentRegion");
646
+ a(this, "regions", /* @__PURE__ */ new Map());
647
+ a(this, "MAX_TEXTURE_IMAGE_UNITS");
648
+ this.canvas = e.canvas;
649
+ const t = y(this.canvas);
650
+ this.gl = t, this.backgroundColor = e.backgroundColor || new o(0.2, 0.2, 0.2, 1), this.devicePixelRatio = e.pixelDensity || window.devicePixelRatio || 1, this.projection = this.createOrthMatrix(0, this.canvas.width, this.canvas.height, 0), this.nativeHeight = e.height || 500, this.nativeWdith = e.width || 500, this.updateNativeSize(), this.resize(), t.enable(t.BLEND), t.blendFunc(t.SRC_ALPHA, t.ONE_MINUS_SRC_ALPHA), this.MAX_TEXTURE_IMAGE_UNITS = this.gl.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS), this.registerRegion("sprite", M), this.registerRegion("graphic", v);
651
+ }
652
+ /**
653
+ * register a region, use {@link setRegion} to use it
654
+ * @param name
655
+ * @param region
656
+ */
657
+ registerRegion(e, t) {
658
+ this.regions.set(e, new t(this));
659
+ }
660
+ /**
661
+ * Sets the current render region and its associated shader.
662
+ *
663
+ * @param regionName - The name of the render region to use.
664
+ * @param shader - The shader to use with the render region, or undefined to use the default shader.
665
+ */
666
+ setRegion(e, t) {
667
+ var i, h;
668
+ const r = this.regions.get(e);
669
+ (r != this.currentRegion || t && t !== this.currentRegion.shader) && ((i = this.currentRegion) == null || i.flush(), (h = this.currentRegion) == null || h.exit(), this.currentRegion = r, r.enter(t));
670
+ }
671
+ /**
672
+ * Draws a sprite on the screen.
673
+ *
674
+ * @param texture - The texture to use for the sprite.
675
+ * @param offsetX - The horizontal offset of the sprite from the origin.
676
+ * @param offsetY - The vertical offset of the sprite from the origin.
677
+ */
678
+ drawSprite(e, t = 0, r = 0, i) {
679
+ this.setRegion("sprite", i), this.currentRegion.drawSprite(e, t, r);
680
+ }
681
+ /**
682
+ * Draws a shape on the screen.
683
+ *
684
+ * @param vertexs - An array of vertex coordinates, where each vertex is a 2-element array of X and Y coordinates.
685
+ * @param color - The color of the shape
686
+ */
687
+ drawShape(e, t, r) {
688
+ this.setRegion("graphic", r), this.currentRegion.drawShape(e, t);
689
+ }
690
+ /**
691
+ * render
692
+ */
693
+ flush() {
694
+ this.currentRegion && this.currentRegion.flush();
695
+ }
696
+ /**
697
+ * Saves the current state
698
+ */
699
+ save() {
700
+ this.matrix.pushMat();
701
+ }
702
+ /**
703
+ * Call this before rendering
704
+ */
705
+ start() {
706
+ this.matrix.clear(), this.matrix.pushIdentity(), this.currentRegion = void 0, this.clear();
707
+ }
708
+ /**
709
+ * Call this after rendering
710
+ */
711
+ end() {
712
+ var e;
713
+ (e = this.currentRegion) == null || e.flush();
714
+ }
715
+ /**
716
+ * Restore to the previous saved state
717
+ */
718
+ restore() {
719
+ this.matrix.popMat();
720
+ }
721
+ /**
722
+ * clear the canvas
723
+ */
724
+ clear() {
725
+ const e = this.gl, t = this.backgroundColor;
726
+ e.clearColor(t.r, t.g, t.b, t.a), e.clear(e.COLOR_BUFFER_BIT);
727
+ }
728
+ resize() {
729
+ const e = this.canvas, t = this.devicePixelRatio;
730
+ e.width = this.nativeWdith * t, e.height = this.nativeHeight * t, this.gl.viewport(0, 0, e.width, e.height);
731
+ }
732
+ updateNativeSize() {
733
+ this.projection = this.createOrthMatrix(
734
+ 0,
735
+ this.nativeWdith,
736
+ this.nativeHeight,
737
+ 0
738
+ );
739
+ }
740
+ setNativeSize(e, t) {
741
+ this.nativeWdith = e / this.devicePixelRatio, this.nativeHeight = t / this.devicePixelRatio, this.updateNativeSize();
742
+ }
743
+ createOrthMatrix(e, t, r, i) {
744
+ return new Float32Array([
745
+ 2 / (t - e),
746
+ 0,
747
+ 0,
748
+ 0,
749
+ 0,
750
+ 2 / (i - r),
751
+ 0,
752
+ 0,
753
+ 0,
754
+ 0,
755
+ -1,
756
+ 0,
757
+ -(t + e) / (t - e),
758
+ -(i + r) / (i - r),
759
+ 0,
760
+ 1
761
+ ]);
762
+ }
763
+ createShader(e, t) {
764
+ return m(this.gl, e, t);
765
+ }
766
+ }
767
+ export {
768
+ u as BaseTexture,
769
+ T as BufferArray,
770
+ o as Color,
771
+ p as DynamicArrayBuffer,
772
+ A as ElementArray,
773
+ v as GraphicRegion,
774
+ b as MatrixStack,
775
+ C as Rapid,
776
+ R as RenderRegion,
777
+ M as SpriteRegion,
778
+ c as Texture,
779
+ U as TextureCache,
780
+ f as compileShader,
781
+ m as createShaderProgram,
782
+ S as createTexture,
783
+ y as getContext
784
+ };