rapid-render 0.1.12 → 0.1.13
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 +9 -0
- package/dist/index.d.ts +13 -11
- package/dist/interface.d.ts +137 -3
- package/dist/math.d.ts +44 -0
- package/dist/particle.d.ts +70 -11
- package/dist/rapid.global.js +1 -1
- package/dist/rapid.js +1 -1
- package/dist/rapid.umd.cjs +1 -1
- package/dist/render.d.ts +8 -3
- package/dist/utils.d.ts +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -12,6 +12,8 @@ A highly efficient ([stress-test](https://nightre.github.io/Rapid.js/docs/exampl
|
|
|
12
12
|
* **Fast Rendering** ⚡
|
|
13
13
|
* **TileMap** - YSort, isometric 🗺️
|
|
14
14
|
* **Light Shadow** 💡
|
|
15
|
+
* **Particle** 🎆
|
|
16
|
+
* **Camera** 🎥
|
|
15
17
|
* **Graphics Drawing** ✏️
|
|
16
18
|
* **Text Rendering** 📝
|
|
17
19
|
* **Line Drawing** - line texture 〰️
|
|
@@ -19,6 +21,12 @@ A highly efficient ([stress-test](https://nightre.github.io/Rapid.js/docs/exampl
|
|
|
19
21
|
* **Mask** 🎭
|
|
20
22
|
* **Frame Buffer Object** 🖼️
|
|
21
23
|
|
|
24
|
+
## Performance Testing
|
|
25
|
+
|
|
26
|
+
32x32 Texture Sprites 60FPS
|
|
27
|
+
|
|
28
|
+
* `Intel® Iris® Xe Graphics` : 42K sprites
|
|
29
|
+
|
|
22
30
|
## Install
|
|
23
31
|
|
|
24
32
|
```bash
|
|
@@ -69,3 +77,4 @@ Issues and PRs are welcome!
|
|
|
69
77
|

|
|
70
78
|

|
|
71
79
|

|
|
80
|
+

|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import Rapid from "./render";
|
|
2
|
-
import GLShader from "./webgl/glshader";
|
|
3
|
-
import { Text } from "./texture";
|
|
4
|
-
import { TileMapRender, TileSet } from "./tilemap";
|
|
5
|
-
import { Uniform } from "./webgl/uniform";
|
|
6
|
-
import { spriteAttributes, graphicAttributes } from "./regions/attributes";
|
|
7
|
-
|
|
8
|
-
export
|
|
9
|
-
export * from "./
|
|
10
|
-
export * from "./
|
|
11
|
-
export * from "./
|
|
1
|
+
import Rapid from "./render";
|
|
2
|
+
import GLShader from "./webgl/glshader";
|
|
3
|
+
import { Text } from "./texture";
|
|
4
|
+
import { TileMapRender, TileSet } from "./tilemap";
|
|
5
|
+
import { Uniform } from "./webgl/uniform";
|
|
6
|
+
import { spriteAttributes, graphicAttributes } from "./regions/attributes";
|
|
7
|
+
import { ParticleEmitter } from "./particle";
|
|
8
|
+
export { Text, Rapid, GLShader, TileMapRender, TileSet, Uniform, ParticleEmitter, graphicAttributes, spriteAttributes, };
|
|
9
|
+
export * from "./math";
|
|
10
|
+
export * from "./interface";
|
|
11
|
+
export * from "./texture";
|
|
12
|
+
export * from "./render";
|
|
13
|
+
export * from "./particle";
|
package/dist/interface.d.ts
CHANGED
|
@@ -155,9 +155,6 @@ export declare enum ShaderType {
|
|
|
155
155
|
SPRITE = "sprite",
|
|
156
156
|
GRAPHIC = "graphic"
|
|
157
157
|
}
|
|
158
|
-
export interface IParticleEmitterOptions extends ITransformOptions, IShaderRenderOptions {
|
|
159
|
-
texture: Texture | Texture[] | [Texture, number][];
|
|
160
|
-
}
|
|
161
158
|
export declare enum BlendMode {
|
|
162
159
|
Additive = "additive",
|
|
163
160
|
Subtractive = "subtractive",
|
|
@@ -176,3 +173,140 @@ export interface ILightRenderOptions {
|
|
|
176
173
|
/** Type of mask to apply */
|
|
177
174
|
type?: MaskType;
|
|
178
175
|
}
|
|
176
|
+
export interface ICameraOptions extends ITransformOptions {
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Defines particle emitter shape types
|
|
180
|
+
*/
|
|
181
|
+
export declare enum ParticleShape {
|
|
182
|
+
/**
|
|
183
|
+
* Point emitter, emits particles from a single point
|
|
184
|
+
*/
|
|
185
|
+
POINT = "point",
|
|
186
|
+
/**
|
|
187
|
+
* Circle emitter, emits particles randomly from a circular area
|
|
188
|
+
*/
|
|
189
|
+
CIRCLE = "circle",
|
|
190
|
+
/**
|
|
191
|
+
* Rectangle emitter, emits particles randomly from a rectangular area
|
|
192
|
+
*/
|
|
193
|
+
RECT = "rect"
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Defines particle attribute animation
|
|
197
|
+
* @template T Attribute type, can be number, vector or color
|
|
198
|
+
*/
|
|
199
|
+
export interface ParticleAttribute<T extends number | Vec2 | Color> {
|
|
200
|
+
/**
|
|
201
|
+
* Damping coefficient, controls attribute decay rate over time
|
|
202
|
+
*/
|
|
203
|
+
damping?: number;
|
|
204
|
+
/**
|
|
205
|
+
* Initial attribute value
|
|
206
|
+
*/
|
|
207
|
+
start: T;
|
|
208
|
+
/**
|
|
209
|
+
* Final attribute value, uses initial value if not specified
|
|
210
|
+
*/
|
|
211
|
+
end?: T;
|
|
212
|
+
/**
|
|
213
|
+
* Attribute change rate, automatically calculated from start and end if not specified
|
|
214
|
+
*/
|
|
215
|
+
delta?: T;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Particle system configuration options
|
|
219
|
+
*/
|
|
220
|
+
export interface IParticleOptions extends ITransformOptions, IShaderRenderOptions {
|
|
221
|
+
/**
|
|
222
|
+
* Particle texture, can be a single texture, array of textures, or weighted texture array
|
|
223
|
+
*/
|
|
224
|
+
texture: Texture | Texture[] | [Texture, number][];
|
|
225
|
+
/**
|
|
226
|
+
* Particle emission rate (particles per second)
|
|
227
|
+
*/
|
|
228
|
+
emitRate?: number;
|
|
229
|
+
/**
|
|
230
|
+
* Emission time interval in seconds
|
|
231
|
+
*/
|
|
232
|
+
emitTime?: number;
|
|
233
|
+
/**
|
|
234
|
+
* Maximum number of particles limit
|
|
235
|
+
*/
|
|
236
|
+
maxParticles?: number;
|
|
237
|
+
/**
|
|
238
|
+
* Particle lifetime in seconds, can be fixed value or range
|
|
239
|
+
*/
|
|
240
|
+
life?: number | [number, number];
|
|
241
|
+
/**
|
|
242
|
+
* Particle animation properties collection
|
|
243
|
+
*/
|
|
244
|
+
animation: {
|
|
245
|
+
/**
|
|
246
|
+
* Velocity vector, controls particle movement direction and speed
|
|
247
|
+
*/
|
|
248
|
+
velocity?: ParticleAttribute<Vec2>;
|
|
249
|
+
/**
|
|
250
|
+
* Acceleration vector, controls particle velocity changes
|
|
251
|
+
*/
|
|
252
|
+
acceleration?: ParticleAttribute<Vec2>;
|
|
253
|
+
/**
|
|
254
|
+
* Speed scalar, used in combination with rotation direction
|
|
255
|
+
*/
|
|
256
|
+
speed?: ParticleAttribute<number>;
|
|
257
|
+
/**
|
|
258
|
+
* Scale factor, controls particle size
|
|
259
|
+
*/
|
|
260
|
+
scale?: ParticleAttribute<number>;
|
|
261
|
+
/**
|
|
262
|
+
* Rotation angle (in radians)
|
|
263
|
+
*/
|
|
264
|
+
rotation?: ParticleAttribute<number>;
|
|
265
|
+
/**
|
|
266
|
+
* Color and transparency
|
|
267
|
+
*/
|
|
268
|
+
color?: ParticleAttribute<Color>;
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* Emitter shape
|
|
272
|
+
*/
|
|
273
|
+
emitShape?: ParticleShape;
|
|
274
|
+
/**
|
|
275
|
+
* Circular emitter radius
|
|
276
|
+
*/
|
|
277
|
+
emitRadius?: number;
|
|
278
|
+
/**
|
|
279
|
+
* Rectangular emitter dimensions
|
|
280
|
+
*/
|
|
281
|
+
emitRect?: {
|
|
282
|
+
width: number;
|
|
283
|
+
height: number;
|
|
284
|
+
};
|
|
285
|
+
/**
|
|
286
|
+
* Whether to use local coordinate system, true means particles are relative to emitter position,
|
|
287
|
+
* false means using global coordinates
|
|
288
|
+
*/
|
|
289
|
+
localSpace?: boolean;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Particle attribute data types
|
|
293
|
+
*/
|
|
294
|
+
export type ParticleAttributeTypes = number | Vec2 | Color;
|
|
295
|
+
/**
|
|
296
|
+
* Particle attribute runtime data
|
|
297
|
+
* @template T Attribute type
|
|
298
|
+
*/
|
|
299
|
+
export type ParticleAttributeData<T extends ParticleAttributeTypes> = {
|
|
300
|
+
/**
|
|
301
|
+
* Attribute change rate per second
|
|
302
|
+
*/
|
|
303
|
+
delta?: T;
|
|
304
|
+
/**
|
|
305
|
+
* Current attribute value
|
|
306
|
+
*/
|
|
307
|
+
value: T;
|
|
308
|
+
/**
|
|
309
|
+
* Damping coefficient
|
|
310
|
+
*/
|
|
311
|
+
damping?: number;
|
|
312
|
+
};
|
package/dist/math.d.ts
CHANGED
|
@@ -289,6 +289,9 @@ export declare class Color implements IMathObject<Color> {
|
|
|
289
289
|
* @returns A new Color instance with the result of the subtraction.
|
|
290
290
|
*/
|
|
291
291
|
subtract(color: Color): Color;
|
|
292
|
+
divide(color: Color | number): Color;
|
|
293
|
+
multiply(color: Color | number): Color;
|
|
294
|
+
clamp(): void;
|
|
292
295
|
static Red: Color;
|
|
293
296
|
static Green: Color;
|
|
294
297
|
static Blue: Color;
|
|
@@ -444,6 +447,7 @@ export declare class Vec2 implements IMathObject<Vec2> {
|
|
|
444
447
|
* @returns An array of Vec2 instances.
|
|
445
448
|
*/
|
|
446
449
|
static FromArray(array: number[][]): Vec2[];
|
|
450
|
+
static fromAngle(angle: number): Vec2;
|
|
447
451
|
/**
|
|
448
452
|
* Calculates the angle between two vectors.
|
|
449
453
|
* @param v - The other vector.
|
|
@@ -474,3 +478,43 @@ export declare class MathUtils {
|
|
|
474
478
|
*/
|
|
475
479
|
static normalizeDegrees(degrees: number): number;
|
|
476
480
|
}
|
|
481
|
+
export declare class Random {
|
|
482
|
+
/**
|
|
483
|
+
* 生成指定范围内的随机浮点数
|
|
484
|
+
* @param min - 最小值
|
|
485
|
+
* @param max - 最大值
|
|
486
|
+
* @returns 随机浮点数
|
|
487
|
+
*/
|
|
488
|
+
static float(min: number, max: number): number;
|
|
489
|
+
/**
|
|
490
|
+
* 生成指定范围内的随机整数
|
|
491
|
+
* @param min - 最小值
|
|
492
|
+
* @param max - 最大值
|
|
493
|
+
* @returns 随机整数
|
|
494
|
+
*/
|
|
495
|
+
static int(min: number, max: number): number;
|
|
496
|
+
/**
|
|
497
|
+
* 生成随机角度(0-360度)
|
|
498
|
+
* @returns 随机角度(弧度)
|
|
499
|
+
*/
|
|
500
|
+
static angle(): number;
|
|
501
|
+
/**
|
|
502
|
+
* 生成指定范围内的随机向量
|
|
503
|
+
* @param minX - 最小X值
|
|
504
|
+
* @param maxX - 最大X值
|
|
505
|
+
* @param minY - 最小Y值
|
|
506
|
+
* @param maxY - 最大Y值
|
|
507
|
+
* @returns 随机向量
|
|
508
|
+
*/
|
|
509
|
+
static vector(minX: number, maxX: number, minY: number, maxY: number): Vec2;
|
|
510
|
+
/**
|
|
511
|
+
* 生成具有随机方向和指定长度的向量
|
|
512
|
+
* @param length - 向量长度
|
|
513
|
+
* @returns 随机方向向量
|
|
514
|
+
*/
|
|
515
|
+
static direction(length: number): Vec2;
|
|
516
|
+
static randomColor(minColor: Color, maxColor: Color): Color;
|
|
517
|
+
static pick(array: any[]): any;
|
|
518
|
+
static pickWeight(array: [any, number][]): any;
|
|
519
|
+
static scalarOrRange<T extends number | Vec2 | Color>(range?: T | [T, T], defaultValue?: T): T;
|
|
520
|
+
}
|
package/dist/particle.d.ts
CHANGED
|
@@ -1,14 +1,73 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
1
|
+
import Rapid from "./render";
|
|
2
|
+
import { IParticleOptions, ITransformOptions } from "./interface";
|
|
3
|
+
import { Vec2 } from "./math";
|
|
4
|
+
/**
|
|
5
|
+
* Particle emitter for creating and managing particle systems
|
|
6
|
+
*/
|
|
8
7
|
export declare class ParticleEmitter {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
private rapid;
|
|
9
|
+
private particles;
|
|
10
|
+
private options;
|
|
11
|
+
private emitting;
|
|
12
|
+
private emitTimer;
|
|
13
|
+
private emitRate;
|
|
14
|
+
private emitTime;
|
|
15
|
+
private emitTimeCounter;
|
|
16
|
+
localSpace: boolean;
|
|
17
|
+
position: Vec2;
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new particle emitter
|
|
20
|
+
* @param rapid - The Rapid renderer instance
|
|
21
|
+
* @param options - Emitter configuration options
|
|
22
|
+
*/
|
|
23
|
+
constructor(rapid: Rapid, options: IParticleOptions);
|
|
24
|
+
/**
|
|
25
|
+
* Gets the transform options
|
|
26
|
+
*/
|
|
27
|
+
getTransform(): ITransformOptions;
|
|
28
|
+
/**
|
|
29
|
+
* Sets particle emission rate
|
|
30
|
+
* @param rate - Particles per second
|
|
31
|
+
*/
|
|
32
|
+
setEmitRate(rate: number): void;
|
|
33
|
+
/**
|
|
34
|
+
* Sets time interval between emissions
|
|
35
|
+
* @param time - Time interval in seconds
|
|
36
|
+
*/
|
|
37
|
+
setEmitTime(time: number): void;
|
|
38
|
+
/**
|
|
39
|
+
* Starts emitting particles
|
|
40
|
+
*/
|
|
41
|
+
start(): void;
|
|
42
|
+
/**
|
|
43
|
+
* Stops emitting new particles but allows existing ones to complete their lifecycle
|
|
44
|
+
*/
|
|
45
|
+
stop(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Clears all particles and resets the emitter
|
|
48
|
+
*/
|
|
49
|
+
clear(): void;
|
|
50
|
+
/**
|
|
51
|
+
* Emits specified number of particles
|
|
52
|
+
* @param count - Number of particles to emit
|
|
53
|
+
*/
|
|
54
|
+
emit(count: number): void;
|
|
55
|
+
/**
|
|
56
|
+
* Updates particle emitter state
|
|
57
|
+
* @param deltaTime - Time in seconds since last update
|
|
58
|
+
*/
|
|
13
59
|
update(deltaTime: number): void;
|
|
60
|
+
/**
|
|
61
|
+
* Renders all particles
|
|
62
|
+
*/
|
|
63
|
+
render(): void;
|
|
64
|
+
/**
|
|
65
|
+
* Gets current particle count
|
|
66
|
+
*/
|
|
67
|
+
getParticleCount(): number;
|
|
68
|
+
/**
|
|
69
|
+
* Checks if the particle emitter is active (has particles or is emitting)
|
|
70
|
+
*/
|
|
71
|
+
isActive(): boolean;
|
|
72
|
+
oneShot(): void;
|
|
14
73
|
}
|
package/dist/rapid.global.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var rapid=function(t){"use strict";var e,r,i,s,n,a;t.LineTextureMode=void 0,(e=t.LineTextureMode||(t.LineTextureMode={})).STRETCH="stretch",e.REPEAT="repeat",t.TextureWrapMode=void 0,(r=t.TextureWrapMode||(t.TextureWrapMode={})).REPEAT="repeat",r.CLAMP="clamp",r.MIRROR="mirror",t.MaskType=void 0,(i=t.MaskType||(t.MaskType={})).Include="normal",i.Exclude="inverse",t.TilemapShape=void 0,(s=t.TilemapShape||(t.TilemapShape={})).SQUARE="square",s.ISOMETRIC="isometric",t.ShaderType=void 0,(n=t.ShaderType||(t.ShaderType={})).SPRITE="sprite",n.GRAPHIC="graphic",t.BlendMode=void 0,(a=t.BlendMode||(t.BlendMode={})).Additive="additive",a.Subtractive="subtractive",a.Mix="mix";var h;t.ArrayType=void 0,(h=t.ArrayType||(t.ArrayType={}))[h.Float32=0]="Float32",h[h.Uint32=1]="Uint32",h[h.Uint16=2]="Uint16";class o{constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(e){switch(e){case t.ArrayType.Float32:return Float32Array;case t.ArrayType.Uint32:return Uint32Array;case t.ArrayType.Uint16:return Uint16Array}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case t.ArrayType.Float32:this.typedArray=this.float32;break;case t.ArrayType.Uint32:this.typedArray=this.uint32;break;case t.ArrayType.Uint16:this.typedArray=this.uint16}}clear(){this.usedElemNum=0}resize(t=0){if((t+=this.usedElemNum)>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum<<=1;this.setMaxSize(this.maxElemNum)}}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.float32[this.usedElemNum++]=t}pushUint16(t){this.uint16[this.usedElemNum++]=t}pop(t){this.usedElemNum-=t}getArray(t=0,e){return null==e?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.typedArray.length}}class u extends o{constructor(t,e,r=t.ARRAY_BUFFER,i=t.STATIC_DRAW){super(e),this.dirty=!0,this.webglBufferSize=0,this.gl=t,this.buffer=t.createBuffer(),this.type=r,this.usage=i}pushFloat32(t){super.pushFloat32(t),this.dirty=!0}pushUint32(t){super.pushUint32(t),this.dirty=!0}pushUint16(t){super.pushUint16(t),this.dirty=!0}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize?(t.bufferData(this.type,this.getArray(),this.usage),this.webglBufferSize=this.maxElemNum):t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class l extends o{constructor(){super(t.ArrayType.Float32)}pushMat(){const t=this.usedElemNum-6,e=this.typedArray;this.resize(6),this.pushFloat32(e[t+0]),this.pushFloat32(e[t+1]),this.pushFloat32(e[t+2]),this.pushFloat32(e[t+3]),this.pushFloat32(e[t+4]),this.pushFloat32(e[t+5])}popMat(){this.pop(6)}pushIdentity(){this.resize(6),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0)}translate(t,e){if("number"!=typeof t)return this.translate(t.x,t.y);const r=this.usedElemNum-6,i=this.typedArray;i[r+4]=i[r+0]*t+i[r+2]*e+i[r+4],i[r+5]=i[r+1]*t+i[r+3]*e+i[r+5]}rotate(t){const e=this.usedElemNum-6,r=this.typedArray,i=Math.cos(t),s=Math.sin(t),n=r[e+0],a=r[e+1],h=r[e+2],o=r[e+3];r[e+0]=n*i-a*s,r[e+1]=n*s+a*i,r[e+2]=h*i-o*s,r[e+3]=h*s+o*i}scale(t,e){if("number"!=typeof t)return this.scale(t.x,t.y);e||(e=t);const r=this.usedElemNum-6,i=this.typedArray;i[r+0]=i[r+0]*t,i[r+1]=i[r+1]*t,i[r+2]=i[r+2]*e,i[r+3]=i[r+3]*e}apply(t,e){if("number"!=typeof t)return new p(...this.apply(t.x,t.y));const r=this.usedElemNum-6,i=this.typedArray;return[i[r+0]*t+i[r+2]*e+i[r+4],i[r+1]*t+i[r+3]*e+i[r+5]]}getInverse(){const t=this.usedElemNum-6,e=this.typedArray,r=e[t+0],i=e[t+1],s=e[t+2],n=e[t+3],a=e[t+4],h=e[t+5],o=r*n-i*s;return new Float32Array([n/o,-i/o,-s/o,r/o,(s*h-n*a)/o,(i*a-r*h)/o])}getTransform(){const t=this.usedElemNum-6,e=this.typedArray;return new Float32Array([e[t+0],e[t+1],e[t+2],e[t+3],e[t+4],e[t+5]])}setTransform(t){const e=this.usedElemNum-6,r=this.typedArray;r[e+0]=t[0],r[e+1]=t[1],r[e+2]=t[2],r[e+3]=t[3],r[e+4]=t[4],r[e+5]=t[5]}getGlobalPosition(){const t=this.usedElemNum-6,e=this.typedArray;return new p(e[t+4],e[t+5])}setGlobalPosition(t,e){if("number"!=typeof t)return void this.setGlobalPosition(t.x,t.y);const r=this.usedElemNum-6,i=this.typedArray;i[r+4]=t,i[r+5]=e}getGlobalRotation(){const t=this.usedElemNum-6,e=this.typedArray;return Math.atan2(e[t+1],e[t+0])}setGlobalRotation(t){const e=this.usedElemNum-6,r=this.typedArray,i=this.getGlobalScale(),s=Math.cos(t),n=Math.sin(t);r[e+0]=s*i.x,r[e+1]=n*i.x,r[e+2]=-n*i.y,r[e+3]=s*i.y}getGlobalScale(){const t=this.usedElemNum-6,e=this.typedArray,r=Math.sqrt(e[t+0]*e[t+0]+e[t+1]*e[t+1]),i=Math.sqrt(e[t+2]*e[t+2]+e[t+3]*e[t+3]);return new p(r,i)}setGlobalScale(t,e){if("number"!=typeof t)return void this.setGlobalScale(t.x,t.y);const r=this.getGlobalRotation(),i=Math.cos(r),s=Math.sin(r),n=this.usedElemNum-6,a=this.typedArray;a[n+0]=i*t,a[n+1]=s*t,a[n+2]=-s*e,a[n+3]=i*e}globalToLocal(t){const e=this.getInverse();return new p(e[0]*t.x+e[2]*t.y+e[4],e[1]*t.x+e[3]*t.y+e[5])}localToGlobal(t){return this.apply(t)}toCSSTransform(){const t=this.usedElemNum-6,e=this.typedArray;return`matrix(${e[t+0]}, ${e[t+1]}, ${e[t+2]}, ${e[t+3]}, ${e[t+4]}, ${e[t+5]})`}identity(){const t=this.usedElemNum-6,e=this.typedArray;e[t+0]=1,e[t+1]=0,e[t+2]=0,e[t+3]=1,e[t+4]=0,e[t+5]=0}applyTransform(t,e=0,r=0){(t.saveTransform??1)&&this.pushMat(),t.afterSave&&t.afterSave();const i=t.x||0,s=t.y||0;(i||s)&&this.translate(i,s),t.position&&this.translate(t.position),t.rotation&&this.rotate(t.rotation),t.scale&&this.scale(t.scale);let n=t.offsetX||0,a=t.offsetY||0;t.offset&&(n+=t.offset.x,a+=t.offset.y);const h=t.origin;return h&&("number"==typeof h?(n-=h*e,a-=h*r):(n-=h.x*e,a-=h.y*r)),{offsetX:n,offsetY:a}}applyTransformAfter(t){t.beforRestore&&t.beforRestore(),(t.restoreTransform??1)&&this.popMat()}}class c extends u{constructor(e,r,i,s){super(e,t.ArrayType.Uint16,e.ELEMENT_ARRAY_BUFFER,e.STATIC_DRAW),this.setMaxSize(r*s);for(let t=0;t<s;t++)this.addObject(t*i);this.bindBuffer(),this.bufferData()}addObject(t){}}class d{constructor(t,e,r,i=255){this._r=t,this._g=e,this._b=r,this._a=i,this.updateUint()}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0}setRGBA(t,e,r,i){this.r=t,this.g=e,this.b=r,this.a=i,this.updateUint()}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new d(this._r,this._g,this._b,this._a)}equal(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),i=parseInt(t.slice(4,6),16);let s=255;return t.length>=8&&(s=parseInt(t.slice(6,8),16)),new d(e,r,i,s)}add(t){return new d(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new d(Math.max(this.r-t.r,0),Math.max(this.g-t.g,0),Math.max(this.b-t.b,0),Math.max(this.a-t.a,0))}}d.Red=new d(255,0,0,255),d.Green=new d(0,255,0,255),d.Blue=new d(0,0,255,255),d.Yellow=new d(255,255,0,255),d.Purple=new d(128,0,128,255),d.Orange=new d(255,165,0,255),d.Pink=new d(255,192,203,255),d.Gray=new d(128,128,128,255),d.Brown=new d(139,69,19,255),d.Cyan=new d(0,255,255,255),d.Magenta=new d(255,0,255,255),d.Lime=new d(192,255,0,255),d.White=new d(255,255,255,255),d.Black=new d(0,0,0,255),d.TRANSPARENT=new d(0,0,0,0);class p{constructor(t,e){this.x=void 0!==t?t:0,this.y=void 0!==e?e:0}add(t){return new p(this.x+t.x,this.y+t.y)}subtract(t){return new p(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof p?new p(this.x*t.x,this.y*t.y):new p(this.x*t,this.y*t)}divide(t){return t instanceof p?new p(this.x/t.x,this.y/t.y):new p(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,r=this.y-t.y;return Math.sqrt(e*e+r*r)}clone(){return new p(this.x,this.y)}copy(t){this.x=t.x,this.y=t.y}equal(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}angle(){return Math.atan2(this.y,this.x)}middle(t){return new p((this.x+t.x)/2,(this.y+t.y)/2)}abs(){return new p(Math.abs(this.x),Math.abs(this.y))}floor(){return new p(Math.floor(this.x),Math.floor(this.y))}ceil(){return new p(Math.ceil(this.x),Math.ceil(this.y))}snap(t){return new p(Math.round(this.x/t)*t,Math.round(this.y/t)*t)}stringify(){return`Vec2(${this.x}, ${this.y})`}static FromArray(t){return t.map((t=>new p(t[0],t[1])))}angleBetween(t){const e=this.dot(t),r=this.length()*t.length(),i=Math.max(-1,Math.min(1,e/r));return Math.acos(i)}}p.ZERO=new p(0,0),p.ONE=new p(1,1),p.UP=new p(0,1),p.DOWN=new p(0,-1),p.LEFT=new p(-1,0),p.RIGHT=new p(1,0);class f{constructor(t){this.render=t}createLightShadowMaskPolygon(t,e,r){const i=[];t.forEach((t=>{for(let e=0;e<t.length;e++){const r=t[e],s=t[(e+1)%t.length];i.push([r,s])}})),r=r||Math.sqrt(Math.pow(this.render.width,2)+Math.pow(this.render.height,2));const s=[];return i.forEach((([t,i])=>{const n=new p(t.x-e.x,t.y-e.y),a=new p(i.x-e.x,i.y-e.y),h=i.subtract(t).perpendicular(),o=Math.abs(h.dot(n))/(h.length()*n.length())+.01,u=Math.abs(h.dot(a))/(h.length()*a.length())+.01,l=r/o,c=r/u,d=new p(n.x,n.y).normalize(),f=new p(a.x,a.y).normalize(),g=new p(t.x+d.x*l,t.y+d.y*l),m=new p(i.x+f.x*c,i.y+f.y*c);s.push([t,i,m,g])})),s}}const g=(t,e,r,i)=>{const s=[],n=i?Math.atan2(e.y,e.x):Math.atan2(-e.y,-e.x),a=Math.PI;for(let e=0;e<10;e++){const i=n+e/10*a,h=n+(e+1)/10*a,o=Math.cos(i)*r,u=Math.sin(i)*r,l=Math.cos(h)*r,c=Math.sin(h)*r;s.push(t),s.push(t.add(new p(o,u))),s.push(t.add(new p(l,c)))}return s},m=e=>{const r=e.points;if(r.length<2)return{vertices:[],uv:[]};const{normals:i,length:s}=((t,e=!1)=>{const r=[];if(t.length<2||e&&t.length<3)return{normals:r,length:0};const i=t.length;let s=0;if(e)for(let e=0;e<i;e++){const r=t[e],n=t[(e+1)%i];s+=r.distanceTo(n)}else for(let e=0;e<i-1;e++)s+=t[e].distanceTo(t[e+1]);const n=(t,e,r)=>{const i=e.subtract(t).normalize(),s=e.subtract(r).normalize(),n=s.dot(i);if(n<-.999)return{normal:i.perpendicular(),miters:1};{let t=s.add(i).normalize();i.cross(s)<0&&(t=t.multiply(-1));let e=1/Math.sqrt((1-n)/2);return{normal:t,miters:Math.min(e,4)}}};if(e){for(let e=0;e<i-1;e++){const s=0===e?t[i-2]:t[e-1],a=t[e],h=t[e+1];r.push(n(s,a,h))}r.push(r[0])}else for(let e=0;e<i;e++)if(0===e){const e=t[1].subtract(t[0]).normalize();r.push({normal:e.perpendicular(),miters:1})}else if(e===i-1){const i=t[e].subtract(t[e-1]).normalize();r.push({normal:i.perpendicular(),miters:1})}else r.push(n(t[e-1],t[e],t[e+1]));return{normals:r,length:s}})(r,e.closed),n=(e.width||1)/2,a=[],h=[],o=e.roundCap||!1,u=e.textureMode||t.LineTextureMode.STRETCH;let l=0;const c=e.texture?.width||1;for(let e=0;e<r.length-1;e++){const o=r[e],d=i[e].normal,f=i[e].miters,g=o.add(d.multiply(f*n)),m=o.subtract(d.multiply(f*n)),y=r[e+1],x=i[e+1].normal,T=i[e+1].miters,E=y.add(x.multiply(T*n)),b=y.subtract(x.multiply(T*n)),w=o.distanceTo(y);let R=0,S=0;u===t.LineTextureMode.STRETCH?(R=l/s,S=(l+w)/s):(R=l/c,S=R+w/c);const A=new p(R,0),M=new p(R,1),v=new p(S,0),U=new p(S,1);a.push(g),h.push(A),a.push(m),h.push(M),a.push(E),h.push(v),a.push(E),h.push(v),a.push(b),h.push(U),a.push(m),h.push(M),l+=w}if(o&&!e.closed){const t=r[0],e=i[0].normal,s=g(t,e,n,!0);a.push(...s);const h=r[r.length-1],o=i[r.length-1].normal,u=g(h,o,n,!1);a.push(...u)}return{vertices:a,uv:h}};var y="precision mediump float;\r\nvarying vec2 vRegion;\r\nvarying vec4 vColor;\r\n\r\nuniform sampler2D uTexture;\r\nuniform int uUseTexture;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n if(uUseTexture > 0){\r\n color = texture2D(uTexture, vRegion) * vColor;\r\n }else{\r\n color = vColor;\r\n }\r\n // fragment\r\n gl_FragColor = color;\r\n}\r\n",x="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec4 aColor;\r\nattribute vec2 aRegion;\r\n\r\nvarying vec4 vColor;\r\nuniform mat4 uProjectionMatrix;\r\nuniform vec4 uColor;\r\nvarying vec2 vRegion;\r\n\r\nvoid main(void) {\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n vColor = aColor;\r\n vRegion = aRegion;\r\n}\r\n";const T=(t,e,r)=>{const i=t.createShader(r);if(!i)throw new Error("Unable to create webgl shader");t.shaderSource(i,e),t.compileShader(i);if(!t.getShaderParameter(i,t.COMPILE_STATUS)){const r=t.getShaderInfoLog(i);throw console.error("Shader compilation failed:",r),new Error("Unable to compile shader: "+r+e)}return i};function E(t,e,r,i=!1,s=!1,n="clamp"){const a=t.createTexture();if(!a)throw new Error("unable to create texture");let h;switch(t.bindTexture(t.TEXTURE_2D,a),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,r?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,r?t.LINEAR:t.NEAREST),n){case"repeat":h=t.REPEAT;break;case"mirror":h=t.MIRRORED_REPEAT;break;default:h=t.CLAMP_TO_EDGE}return t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,h),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,h),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,s),i?t.texImage2D(t.TEXTURE_2D,0,t.RGBA,e.width,e.height,0,t.RGBA,t.UNSIGNED_BYTE,null):t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),a}const b=5126;var w="precision mediump float;\r\nuniform sampler2D uTextures[%TEXTURE_NUM%];\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n %GET_COLOR%\r\n // fragment\r\n gl_FragColor = color * vColor;\r\n}",R="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec2 aRegion;\r\nattribute float aTextureId;\r\nattribute vec4 aColor;\r\n\r\nuniform mat4 uProjectionMatrix;\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vRegion = aRegion;\r\n vTextureId = aTextureId;\r\n vColor = aColor;\r\n\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n}";const S=[{name:"aPosition",size:2,type:b,stride:24},{name:"aRegion",size:2,type:b,stride:24,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:b,stride:24,offset:4*Float32Array.BYTES_PER_ELEMENT},{name:"aColor",size:4,type:5121,stride:24,offset:5*Float32Array.BYTES_PER_ELEMENT,normalized:!0}],A=[{name:"aPosition",size:2,type:b,stride:20},{name:"aColor",size:4,type:5121,stride:20,offset:2*Float32Array.BYTES_PER_ELEMENT,normalized:!0},{name:"aRegion",size:2,type:b,stride:20,offset:3*Float32Array.BYTES_PER_ELEMENT}];class M{constructor(t,e,r,i,s=0){this.attributeLoc={},this.uniformLoc={},this.textureUnitNum=0,this.attributes=[];const n=function(t,e){if(t.includes("%TEXTURE_NUM%")&&(t=t.replace("%TEXTURE_NUM%",e.toString())),t.includes("%GET_COLOR%")){let r="";for(let t=0;t<e;t++)r+=0==t?`if(vTextureId == ${t}.0)`:t==e-1?"else":`else if(vTextureId == ${t}.0)`,r+=`{color = texture2D(uTextures[${t}], vRegion);}`;t=t.replace("%GET_COLOR%",r)}return t}(r,t.maxTextureUnits-s);this.program=((t,e,r)=>{var i=t.createProgram(),s=T(t,e,35633),n=T(t,r,35632);if(!i)throw new Error("Unable to create program shader");if(t.attachShader(i,s),t.attachShader(i,n),t.linkProgram(i),!t.getProgramParameter(i,t.LINK_STATUS)){const e=t.getProgramInfoLog(i);throw new Error("Unable to link shader program: "+e)}return i})(t.gl,e,n),this.gl=t.gl,this.textureUnitNum=s,this.parseShader(e),this.parseShader(n),i&&this.setAttributes(i)}setUniforms(t,e){const r=this.gl;for(const i of t.getUnifromNames()){const s=this.getUniform(i);t.bind(r,i,s,e)}}getUniform(t){return this.uniformLoc[t]}use(){this.gl.useProgram(this.program)}parseShader(t){const e=this.gl,r=t.match(/attribute\s+\w+\s+(\w+)/g);if(r)for(const t of r){const r=t.split(" ")[2],i=e.getAttribLocation(this.program,r);-1!=i&&(this.attributeLoc[r]=i)}const i=t.match(/uniform\s+\w+\s+(\w+)/g);if(i)for(const t of i){const r=t.split(" ")[2];this.uniformLoc[r]=e.getUniformLocation(this.program,r)}}setAttribute(t){const e=this.attributeLoc[t.name];if(void 0!==e){const r=this.gl;r.vertexAttribPointer(e,t.size,t.type,t.normalized||!1,t.stride,t.offset||0),r.enableVertexAttribArray(e)}}setAttributes(t){this.attributes=t;for(const e of t)this.setAttribute(e)}updateAttributes(){this.setAttributes(this.attributes)}static createCostumShader(e,r,i,s,n=0){let a={[t.ShaderType.SPRITE]:w,[t.ShaderType.GRAPHIC]:y}[s],h={[t.ShaderType.SPRITE]:R,[t.ShaderType.GRAPHIC]:x}[s];const o={[t.ShaderType.SPRITE]:S,[t.ShaderType.GRAPHIC]:A}[s];return a=a.replace("void main(void) {",i+"\nvoid main(void) {"),h=h.replace("void main(void) {",r+"\nvoid main(void) {"),a=a.replace("// fragment","fragment(color);"),h=h.replace(/\/\/ vertex s[\s\S]*?\/\/ vertex e/,"vec2 position = aPosition;\n vertex(position, vRegion);\n gl_Position = uProjectionMatrix * vec4(position, 0.0, 1.0);"),new M(e,h,a,o,n)}}class v{constructor(e){this.usedTextures=[],this.shaders=new Map,this.isCostumShader=!1,this.freeTextureUnitNum=0,this.rapid=e,this.gl=e.gl,this.webglArrayBuffer=new u(e.gl,t.ArrayType.Float32,e.gl.ARRAY_BUFFER,e.gl.STREAM_DRAW),this.maxTextureUnits=e.maxTextureUnits}getTextureUnitList(){return Array.from({length:this.maxTextureUnits},((t,e)=>e))}addVertex(t,e,...r){const[i,s]=this.rapid.matrixStack.apply(t,e);this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(s)}useTexture(t){const e=this.usedTextures.indexOf(t);return-1==e?(this.usedTextures.push(t),this.freeTextureUnitNum=this.maxTextureUnits-this.usedTextures.length,[this.usedTextures.length-1,!0]):[e,!1]}enterRegion(t){this.currentShader=t??this.getShader("default"),this.currentShader.use(),this.initializeForNextRender(),this.webglArrayBuffer.bindBuffer(),this.currentShader.updateAttributes(),this.updateProjection(),this.isCostumShader=Boolean(t)}updateProjection(){this.gl.uniformMatrix4fv(this.currentShader.uniformLoc.uProjectionMatrix,!1,this.rapid.projection)}isUnifromChanged(t){return!!t&&(this.costumUnifrom!=t||!!t?.isDirty)}setCurrentUniform(t){t.clearDirty(),this.costumUnifrom=t}exitRegion(){}initDefaultShader(t,e,r){this.setShader("default",t,e,r)}setShader(t,e,r,i){this.webglArrayBuffer.bindBuffer(),this.shaders.set(t,new M(this.rapid,e,r,i)),"default"===t&&(this.defaultShader=this.shaders.get(t))}getShader(t){return this.shaders.get(t)}render(){this.executeRender(),this.initializeForNextRender()}executeRender(){const t=this.gl;for(let e=0;e<this.usedTextures.length;e++)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,this.usedTextures[e]);this.webglArrayBuffer.bufferData()}initializeForNextRender(){this.webglArrayBuffer.clear(),this.usedTextures.length=0,this.isCostumShader=!1,this.freeTextureUnitNum=this.maxTextureUnits}hasPendingContent(){return!1}isShaderChanged(t){return(t||this.defaultShader)!=this.currentShader}}class U extends v{constructor(t){super(t),this.vertex=0,this.offset=p.ZERO,this.drawType=t.gl.TRIANGLE_FAN,this.setShader("default",x,y,A)}startRender(t,e,r,i){i&&this.currentShader?.setUniforms(i,this),this.offset=new p(t,e),this.vertex=0,this.webglArrayBuffer.clear(),r&&r.base&&(this.texture=this.useTexture(r.base.texture)[0])}addVertex(t,e,r,i,s){this.webglArrayBuffer.resize(3),super.addVertex(t+this.offset.x,e+this.offset.y),this.webglArrayBuffer.pushUint32(s),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(i),this.vertex+=1}executeRender(){super.executeRender();const t=this.gl;t.uniform1i(this.currentShader.uniformLoc.uUseTexture,void 0===this.texture?0:1),this.texture&&t.uniform1i(this.currentShader.uniformLoc.uTexture,this.texture),t.drawArrays(this.drawType,0,this.vertex),this.drawType=this.rapid.gl.TRIANGLE_FAN,this.vertex=0,this.texture=void 0}}const F=Math.floor(16384);class _ extends c{constructor(t,e){super(t,6,4,e)}addObject(t){super.addObject(),this.pushUint16(t),this.pushUint16(t+1),this.pushUint16(t+2),this.pushUint16(t),this.pushUint16(t+3),this.pushUint16(t+2)}}class C extends v{constructor(t){const e=t.gl;super(t),this.batchSprite=0,this.spriteTextureUnits=[],this.spriteTextureUnitIndexOffset=0,this.setShader("default",R,w,S),this.indexBuffer=new _(e,F)}addVertex(t,e,r,i,s,n){super.addVertex(t,e),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(s),this.webglArrayBuffer.pushUint32(n)}renderSprite(t,e,r,i,s,n,a,h,o,u,l,c,d,p=0){(1+p>this.freeTextureUnitNum||this.batchSprite>=F||this.isUnifromChanged(l)||this.rapid.projectionDirty)&&(this.render(),l&&this.isUnifromChanged(l)&&(this.currentShader.setUniforms(l,this),this.setCurrentUniform(l)),this.rapid.projectionDirty&&this.updateProjection()),this.batchSprite++,this.webglArrayBuffer.resize(20);const[f,g]=this.useTexture(t);g&&(this.spriteTextureUnits.push(f),this.spriteTextureUnitIndexOffset=this.spriteTextureUnits[0]);const m=f-this.spriteTextureUnitIndexOffset,y=c?n:i,x=c?i:n,T=d?a:s,E=d?s:a,b=h,w=h+e,R=o,S=o+r;this.addVertex(b,R,y,T,m,u),this.addVertex(w,R,x,T,m,u),this.addVertex(w,S,x,E,m,u),this.addVertex(b,S,y,E,m,u)}executeRender(){if(super.executeRender(),this.batchSprite<=0)return;const t=this.gl;this.spriteTextureUnits.length>0&&this.gl.uniform1iv(this.currentShader.uniformLoc.uTextures,this.spriteTextureUnits),t.drawElements(t.TRIANGLES,6*this.batchSprite,t.UNSIGNED_SHORT,0)}enterRegion(t){super.enterRegion(t),this.indexBuffer.bindBuffer()}initializeForNextRender(){super.initializeForNextRender(),this.batchSprite=0,this.spriteTextureUnits.length=0}hasPendingContent(){return this.batchSprite>0}}class N{constructor(t,e){this.cache=new Map,this.render=t,this.antialias=e}async textureFromUrl(e,r=this.antialias,i=t.TextureWrapMode.CLAMP){let s=this.cache.get(e);if(!s){const t=await this.loadImage(e);s=B.fromImageSource(this.render,t,r,i),this.cache.set(e,s)}return new I(s)}textureFromFrameBufferObject(t){return new I(t)}async textureFromSource(e,r=this.antialias,i=t.TextureWrapMode.CLAMP){let s=this.cache.get(e);return s||(s=B.fromImageSource(this.render,e,r,i),this.cache.set(e,s)),new I(s)}async loadImage(t){return new Promise((e=>{const r=new Image;r.onload=()=>{e(r)},r.src=t}))}createText(t){return new P(this.render,t)}destroy(t){t instanceof I?(t.base?.destroy(this.render.gl),this.removeCache(t)):(t.destroy(this.render.gl),this.removeCache(t))}createFrameBufferObject(t,e,r=this.antialias){return new L(this.render,t,e,r)}removeCache(t){const e=t instanceof I?t.base?.texture:t.texture;e&&this.cache.forEach(((t,r)=>{t===e&&this.cache.delete(r)}))}}class B{constructor(e,r,i,s=t.TextureWrapMode.CLAMP){this.texture=e,this.width=r,this.height=i,this.wrapMode=s}static fromImageSource(e,r,i=!1,s=t.TextureWrapMode.CLAMP){return new B(E(e.gl,r,i,!1,!1,s),r.width,r.height)}destroy(t){t.deleteTexture(this.texture)}}class I{constructor(t){this.scale=1,this.setBaseTextur(t)}setBaseTextur(t){t&&(this.base=t,this.setClipRegion(0,0,t.width,t.height))}setClipRegion(t,e,r,i){if(this.base)return this.clipX=t/this.base.width,this.clipY=e/this.base.height,this.clipW=this.clipX+r/this.base.width,this.clipH=this.clipY+i/this.base.height,this.width=r*this.scale,this.height=i*this.scale,this}static fromImageSource(t,e,r=!1){return new I(B.fromImageSource(t,e,r))}static fromUrl(t,e){return t.textures.textureFromUrl(e)}createSpritesHeet(t,e){if(!this.base)return[];const r=[],i=Math.floor(this.base.width/t),s=Math.floor(this.base.height/e);for(let n=0;n<s;n++)for(let s=0;s<i;s++){const i=this.clone();i.setClipRegion(s*t,n*e,t,e),r.push(i)}return r}clone(){return new I(this.base)}}class P extends I{constructor(t,e){super(),this.scale=.5,this.rapid=t,this.options=e,this.text=e.text||" ",this.updateTextImage()}updateTextImage(){const t=this.createTextCanvas();this.setBaseTextur(B.fromImageSource(this.rapid,t,!0))}createTextCanvas(){const t=document.createElement("canvas"),e=t.getContext("2d");if(!e)throw new Error("Failed to get canvas context");e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";const r=this.text.split("\n");let i=0,s=0;for(const t of r){const r=e.measureText(t);i=Math.max(i,r.width),s+=this.options.fontSize||16}t.width=2*i,t.height=2*s,e.scale(2,2),e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";let n=0;for(const t of r)e.fillText(t,0,n),n+=this.options.fontSize||16;return t}setText(t){this.text!=t&&(this.text=t,this.updateTextImage())}}class L extends B{constructor(t,e,r,i=!1){const s=t.gl,n=E(s,{width:e,height:r},i,!0,!1),a=s.createFramebuffer();if(!a)throw s.deleteTexture(n),new Error("Failed to create WebGL framebuffer");s.bindFramebuffer(s.FRAMEBUFFER,a),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,n,0);const h=s.createRenderbuffer();if(!h)throw s.deleteFramebuffer(a),s.deleteTexture(n),new Error("Failed to create depth-stencil renderbuffer");s.bindRenderbuffer(s.RENDERBUFFER,h),s.renderbufferStorage(s.RENDERBUFFER,s.STENCIL_INDEX8,e,r),s.framebufferRenderbuffer(s.FRAMEBUFFER,s.STENCIL_ATTACHMENT,s.RENDERBUFFER,h),super(n,e,r),this.gl=s,this.framebuffer=a,s.bindTexture(s.TEXTURE_2D,null),s.bindFramebuffer(s.FRAMEBUFFER,null)}bind(){const t=this.gl;t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,this.framebuffer),t.clearColor(.5,.2,.5,.5),t.clear(t.COLOR_BUFFER_BIT)}unbind(){this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}resize(t,e){this.width=t,this.height=e,this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,t,e,0,this.gl.RGBA,this.gl.UNSIGNED_BYTE,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}destroy(t){t.deleteFramebuffer(this.framebuffer),super.destroy(t)}}const D=new Set;class k{constructor(t,e){this.textures=new Map,this.width=t,this.height=e}setTile(t,e){e instanceof I&&(e={texture:e}),this.textures.set(t,e)}getTile(t){return this.textures.get(t)}}class O{constructor(t){this.rapid=t}getYSortRow(t,e,r){if(!t)return[];const i=[];for(const r of t){const t=Math.floor(r.ySort/e);i[t]||(i[t]=[]),i[t].push(r)}return i}getOffset(t){let e=(t.errorX??2)+1,r=(t.errorY??2)+1;if("number"==typeof t.error){const i=(t.error??2)+1;e=i,r=i}else t.error&&(e=t.error.x+1,r=t.error.y+1);return{errorX:e,errorY:r}}getTileData(e,r){const i=r.shape??t.TilemapShape.SQUARE,s=e.width,n=i===t.TilemapShape.ISOMETRIC?e.height/2:e.height,a=this.rapid.matrixStack,h=a.globalToLocal(p.ZERO),o=a.getGlobalScale(),{errorX:u,errorY:l}=this.getOffset(r),c=Math.ceil(this.rapid.width/s/o.x)+2*u,d=Math.ceil(this.rapid.height/n/o.y)+2*l,f=new p(h.x<0?Math.ceil(h.x/s):Math.floor(h.x/s),h.y<0?Math.ceil(h.y/n):Math.floor(h.y/n));f.x-=u,f.y-=l;let g=new p(0-h.x%s-u*s,0-h.y%n-l*n);return g=g.add(h),{startTile:f,offset:g,viewportWidth:c,viewportHeight:d,height:n,width:s,shape:i}}renderYSortRow(t,e){for(const r of e)r.render?r.render():r.renderSprite&&t.renderSprite(r.renderSprite)}renderLayer(e,r){this.rapid.matrixStack.applyTransform(r);const i=r.tileSet,{startTile:s,offset:n,viewportWidth:a,viewportHeight:h,shape:o,width:u,height:l}=this.getTileData(i,r),c=this.getYSortRow(r.ySortCallback,l,h),d=r.ySortCallback&&r.ySortCallback.length>0;var p;0!==this.rapid.matrixStack.getGlobalRotation()&&(p="TileMapRender: tilemap is not supported rotation",D.has(p)||(D.add(p),console.warn(p)),this.rapid.matrixStack.setGlobalRotation(0));for(let p=0;p<h;p++){const h=p+s.y,f=c[h]??[];if(h<0||h>=e.length)this.renderYSortRow(this.rapid,f);else{for(let c=0;c<a;c++){const a=c+s.x;if(a<0||a>=e[h].length)continue;const d=e[h][a],g=i.getTile(d);if(!g)continue;let m=c*u+n.x,y=p*l+n.y,x=p*l+n.y+(g.ySortOffset??0);h%2!=0&&o===t.TilemapShape.ISOMETRIC&&(m+=u/2);const T=r.eachTile&&r.eachTile(d,a,h)||{};f.push({ySort:x,renderSprite:{...g,x:m+(g.x||0),y:y+(g.y||0),...T}})}d&&f.sort(((t,e)=>t.ySort-e.ySort)),this.renderYSortRow(this.rapid,f)}}this.rapid.matrixStack.applyTransform(r)}localToMap(e,r){const i=r.tileSet;if(r.shape===t.TilemapShape.ISOMETRIC){let t=0,r=0;const s=i.height/2,n=i.width/2;let a=Math.floor(e.y/s);const h=a%2==0;let o=Math.floor(e.x/n);const u=o%2==0,l=e.x%n/n,c=e.y%s/s,d=c<l,f=c<1-l;return h||(a-=1),d&&!u&&h?a-=1:d||!u||h?f&&u&&h?(o-=2,a-=1):f||u||h||(a+=1):(a+=1,o-=2),t=o,r=a,t=Math.floor(o/2),new p(t,r)}return new p(Math.floor(e.x/i.width),Math.floor(e.y/i.height))}mapToLocal(e,r){const i=r.tileSet;if(r.shape===t.TilemapShape.ISOMETRIC){let t=new p(e.x*i.width,e.y*i.height/2);return e.y%2!=0&&(t.x+=i.width/2),t}return new p(e.x*i.width,e.y*i.height)}}return t.BaseTexture=B,t.Color=d,t.DynamicArrayBuffer=o,t.FrameBufferObject=L,t.GLShader=M,t.MathUtils=class{static deg2rad(t){return t*(Math.PI/180)}static rad2deg(t){return t/(Math.PI/180)}static normalizeDegrees(t){return(t%360+360)%360}},t.MatrixStack=l,t.Rapid=class{constructor(t){this.projectionDirty=!0,this.matrixStack=new l,this.tileMap=new O(this),this.light=new f(this),this.devicePixelRatio=window.devicePixelRatio||1,this.defaultColor=new d(255,255,255,255),this.regions=new Map,this.currentMaskType=[],this.currentTransform=[],this.currentFBO=[];const e=(t=>{const e={stencil:!0},r=t.getContext("webgl2",e)||t.getContext("webgl",e);if(!r)throw new Error("Unable to initialize WebGL. Your browser may not support it.");return r})(t.canvas);this.gl=e,this.canvas=t.canvas,this.textures=new N(this,t.antialias??!1),this.maxTextureUnits=e.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.width=t.width||this.canvas.width,this.height=t.width||this.canvas.height,this.backgroundColor=t.backgroundColor||new d(255,255,255,255),this.registerBuildInRegion(),this.initWebgl(e),this.projectionDirty=!1}renderTileMapLayer(t,e){this.tileMap.renderLayer(t,e instanceof k?{tileSet:e}:e)}initWebgl(t){this.resize(this.width,this.height),t.enable(t.BLEND),t.disable(t.DEPTH_TEST),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.enable(t.STENCIL_TEST),t.enable(t.SCISSOR_TEST)}clearTextureUnit(){for(let t=0;t<this.maxTextureUnits;t++)this.gl.activeTexture(this.gl.TEXTURE0+t),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}registerBuildInRegion(){this.registerRegion("sprite",C),this.registerRegion("graphic",U)}registerRegion(t,e){this.regions.set(t,new e(this))}quitCurrentRegion(){this.currentRegion&&this.currentRegion.hasPendingContent()&&(this.currentRegion.render(),this.currentRegion.exitRegion())}setRegion(t,e){if(t!=this.currentRegionName||this.currentRegion&&this.currentRegion.isShaderChanged(e)){const r=this.regions.get(t);this.quitCurrentRegion(),this.currentRegion=r,this.currentRegionName=t,r.enterRegion(e)}}save(){this.matrixStack.pushMat()}restore(){this.matrixStack.popMat()}withTransform(t){this.save(),t(),this.restore()}startRender(t=!0){this.clear(),t&&this.matrixStack.clear(),this.matrixStack.pushIdentity(),this.currentRegion=void 0,this.currentRegionName=void 0}endRender(){this.currentRegion?.render(),this.projectionDirty=!1}render(t){this.startRender(),t(),this.endRender()}renderSprite(t){const e=t.texture;if(!e||!e.base)return;const{offsetX:r,offsetY:i}=this.startDraw(t,e.width,e.height);this.setRegion("sprite",t.shader),this.currentRegion.renderSprite(e.base.texture,e.width,e.height,e.clipX,e.clipY,e.clipW,e.clipH,r,i,(t.color||this.defaultColor).uint32,t.uniforms,t.flipX,t.flipY),this.afterDraw()}renderTexture(t){t.base&&this.renderSprite({texture:t})}renderLine(t){const e=t.closed?[...t.points,t.points[0]]:t.points,{vertices:r,uv:i}=m({...t,points:e});this.renderGraphic({...t,drawType:this.gl.TRIANGLES,points:r,uv:i})}renderGraphic(t){this.startGraphicDraw(t),t.points.forEach(((e,r)=>{const i=Array.isArray(t.color)?t.color[r]:t.color,s=t.uv?.[r];this.addGraphicVertex(e.x,e.y,s,i)})),this.endGraphicDraw()}startGraphicDraw(t){const{offsetX:e,offsetY:r}=this.startDraw(t);this.setRegion("graphic",t.shader);const i=this.currentRegion;i.startRender(e,r,t.texture,t.uniforms),t.drawType&&(i.drawType=t.drawType)}addGraphicVertex(t,e,r,i){this.currentRegion.addVertex(t,e,r?.x,r?.y,(i||this.defaultColor).uint32)}endGraphicDraw(){this.currentRegion.render(),this.afterDraw()}startDraw(t,e=0,r=0){return this.currentTransform.push(t),this.matrixStack.applyTransform(t,e,r)}afterDraw(){this.currentTransform.length>0&&this.matrixStack.applyTransformAfter(this.currentTransform.pop())}renderRect(t){const{width:e,height:r}=t,i=[new p(0,0),new p(e,0),new p(e,r),new p(0,r)];this.renderGraphic({...t,points:i,drawType:this.gl.TRIANGLE_FAN})}renderCircle(t){const e=t.segments||32,r=t.radius,i=t.color||this.defaultColor,s=[];for(let t=0;t<=e;t++){const i=t/e*Math.PI*2,n=Math.cos(i)*r,a=Math.sin(i)*r;s.push(new p(n,a))}this.renderGraphic({...t,points:s,color:i,drawType:this.gl.TRIANGLE_FAN})}resize(t,e){const r=t*this.devicePixelRatio,i=e*this.devicePixelRatio;this.canvas.width=r,this.canvas.height=i,this.resizeWebglSize(t,e),this.canvas.style.width=t+"px",this.canvas.style.height=e+"px",this.width=t,this.height=e}resizeWebglSize(t,e,r){const i=t*(r||this.devicePixelRatio),s=e*(r||this.devicePixelRatio);this.gl.viewport(0,0,i,s),this.updateProjection(0,t,e,0),this.gl.scissor(0,0,i,s)}updateProjection(t,e,r,i){this.projection=this.createOrthMatrix(t,e,r,i),this.projectionDirty=!0}clear(t){const e=this.gl,r=t||this.backgroundColor;e.clearColor(r.r/255,r.g/255,r.b/255,r.a/255),e.clear(e.COLOR_BUFFER_BIT),this.clearMask()}createOrthMatrix(t,e,r,i){return new Float32Array([2/(e-t),0,0,0,0,2/(i-r),0,0,0,0,-1,0,-(e+t)/(e-t),-(i+r)/(i-r),0,1])}drawMask(e=t.MaskType.Include,r){this.startDrawMask(e),r(),this.endDrawMask()}startDrawMask(e=t.MaskType.Include){const r=this.gl;this.currentMaskType.push(e),this.setMaskType(e,!0),r.stencilOp(r.KEEP,r.KEEP,r.REPLACE),r.colorMask(!1,!1,!1,!1)}endDrawMask(){const e=this.gl;this.quitCurrentRegion(),e.stencilOp(e.KEEP,e.KEEP,e.KEEP),e.colorMask(!0,!0,!0,!0),this.setMaskType(this.currentMaskType.pop()??t.MaskType.Include,!1)}setMaskType(e,r=!1){const i=this.gl;if(this.quitCurrentRegion(),r)this.clearMask(),i.stencilFunc(i.ALWAYS,1,255);else switch(e){case t.MaskType.Include:i.stencilFunc(i.EQUAL,1,255);break;case t.MaskType.Exclude:i.stencilFunc(i.NOTEQUAL,1,255)}}clearMask(){const t=this.gl;this.quitCurrentRegion(),t.clearStencil(0),t.clear(t.STENCIL_BUFFER_BIT),t.stencilFunc(t.ALWAYS,1,255)}createCostumShader(t,e,r,i=0){return M.createCostumShader(this,t,e,r,i)}startFBO(t){this.quitCurrentRegion(),t.bind(),this.clearTextureUnit(),this.resizeWebglSize(t.width,t.height,1),this.updateProjection(0,t.width,0,t.height),this.save(),this.matrixStack.identity(),this.currentFBO.push(t)}endFBO(){if(this.currentFBO.length>0){const t=this.currentFBO.pop();this.quitCurrentRegion(),t.unbind(),this.clearTextureUnit(),this.resizeWebglSize(this.width,this.height),this.updateProjection(0,this.width,this.height,0),this.restore()}}drawToFBO(t,e){this.startFBO(t),e(),this.endFBO()}setBlendMode(e){switch(e){case t.BlendMode.Additive:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE);break;case t.BlendMode.Subtractive:this.gl.blendFunc(this.gl.ZERO,this.gl.ONE_MINUS_SRC_COLOR);break;case t.BlendMode.Mix:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE_MINUS_SRC_ALPHA)}}drawLightShadowMask(e){this.startDrawMask(e.type||t.MaskType.Exclude);this.light.createLightShadowMaskPolygon(e.occlusion,e.lightSource,e.baseProjectionLength).forEach((t=>{this.renderGraphic({points:t,color:d.Black})})),this.endDrawMask()}},t.SCALEFACTOR=2,t.Text=P,t.Texture=I,t.TextureCache=N,t.TileMapRender=O,t.TileSet=k,t.Uniform=class{constructor(t){this.isDirty=!1,this.data=t}setUniform(t,e){this.data[t]!=e&&(this.isDirty=!0),this.data[t]=e}clearDirty(){this.isDirty=!1}getUnifromNames(){return Object.keys(this.data)}bind(t,e,r,i){if(!r)return;const s=this.data[e];if("number"==typeof s)t.uniform1f(r,s);else if(Array.isArray(s))switch(s.length){case 1:Number.isInteger(s[0])?t.uniform1i(r,s[0]):t.uniform1f(r,s[0]);break;case 2:Number.isInteger(s[0])?t.uniform2iv(r,s):t.uniform2fv(r,s);break;case 3:Number.isInteger(s[0])?t.uniform3iv(r,s):t.uniform3fv(r,s);break;case 4:Number.isInteger(s[0])?t.uniform4iv(r,s):t.uniform4fv(r,s);break;case 9:t.uniformMatrix3fv(r,!1,s);break;case 16:t.uniformMatrix4fv(r,!1,s);break;default:console.error(`Unsupported uniform array length for ${e}:`,s.length)}else if("boolean"==typeof s)t.uniform1i(r,s?1:0);else if(s.base?.texture){const e=i.useTexture(s.base.texture)[0];t.uniform1i(r,e)}else console.error(`Unsupported uniform type for ${e}:`,typeof s)}},t.Vec2=p,t.WebglBufferArray=u,t.WebglElementBufferArray=c,t.graphicAttributes=A,t.spriteAttributes=S,t}({});
|
|
1
|
+
var rapid=function(t){"use strict";var e,r,i,s,a,n,h;t.LineTextureMode=void 0,(e=t.LineTextureMode||(t.LineTextureMode={})).STRETCH="stretch",e.REPEAT="repeat",t.TextureWrapMode=void 0,(r=t.TextureWrapMode||(t.TextureWrapMode={})).REPEAT="repeat",r.CLAMP="clamp",r.MIRROR="mirror",t.MaskType=void 0,(i=t.MaskType||(t.MaskType={})).Include="normal",i.Exclude="inverse",t.TilemapShape=void 0,(s=t.TilemapShape||(t.TilemapShape={})).SQUARE="square",s.ISOMETRIC="isometric",t.ShaderType=void 0,(a=t.ShaderType||(t.ShaderType={})).SPRITE="sprite",a.GRAPHIC="graphic",t.BlendMode=void 0,(n=t.BlendMode||(t.BlendMode={})).Additive="additive",n.Subtractive="subtractive",n.Mix="mix",t.ParticleShape=void 0,(h=t.ParticleShape||(t.ParticleShape={})).POINT="point",h.CIRCLE="circle",h.RECT="rect";var o;t.ArrayType=void 0,(o=t.ArrayType||(t.ArrayType={}))[o.Float32=0]="Float32",o[o.Uint32=1]="Uint32",o[o.Uint16=2]="Uint16";class u{constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(e){switch(e){case t.ArrayType.Float32:return Float32Array;case t.ArrayType.Uint32:return Uint32Array;case t.ArrayType.Uint16:return Uint16Array}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case t.ArrayType.Float32:this.typedArray=this.float32;break;case t.ArrayType.Uint32:this.typedArray=this.uint32;break;case t.ArrayType.Uint16:this.typedArray=this.uint16}}clear(){this.usedElemNum=0}resize(t=0){if((t+=this.usedElemNum)>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum<<=1;this.setMaxSize(this.maxElemNum)}}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.float32[this.usedElemNum++]=t}pushUint16(t){this.uint16[this.usedElemNum++]=t}pop(t){this.usedElemNum-=t}getArray(t=0,e){return null==e?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.typedArray.length}}class l extends u{constructor(t,e,r=t.ARRAY_BUFFER,i=t.STATIC_DRAW){super(e),this.dirty=!0,this.webglBufferSize=0,this.gl=t,this.buffer=t.createBuffer(),this.type=r,this.usage=i}pushFloat32(t){super.pushFloat32(t),this.dirty=!0}pushUint32(t){super.pushUint32(t),this.dirty=!0}pushUint16(t){super.pushUint16(t),this.dirty=!0}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize?(t.bufferData(this.type,this.getArray(),this.usage),this.webglBufferSize=this.maxElemNum):t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class c extends u{constructor(){super(t.ArrayType.Float32)}pushMat(){const t=this.usedElemNum-6,e=this.typedArray;this.resize(6),this.pushFloat32(e[t+0]),this.pushFloat32(e[t+1]),this.pushFloat32(e[t+2]),this.pushFloat32(e[t+3]),this.pushFloat32(e[t+4]),this.pushFloat32(e[t+5])}popMat(){this.pop(6)}pushIdentity(){this.resize(6),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0)}translate(t,e){if("number"!=typeof t)return this.translate(t.x,t.y);const r=this.usedElemNum-6,i=this.typedArray;i[r+4]=i[r+0]*t+i[r+2]*e+i[r+4],i[r+5]=i[r+1]*t+i[r+3]*e+i[r+5]}rotate(t){const e=this.usedElemNum-6,r=this.typedArray,i=Math.cos(t),s=Math.sin(t),a=r[e+0],n=r[e+1],h=r[e+2],o=r[e+3];r[e+0]=a*i-n*s,r[e+1]=a*s+n*i,r[e+2]=h*i-o*s,r[e+3]=h*s+o*i}scale(t,e){if("number"!=typeof t)return this.scale(t.x,t.y);e||(e=t);const r=this.usedElemNum-6,i=this.typedArray;i[r+0]=i[r+0]*t,i[r+1]=i[r+1]*t,i[r+2]=i[r+2]*e,i[r+3]=i[r+3]*e}apply(t,e){if("number"!=typeof t)return new f(...this.apply(t.x,t.y));const r=this.usedElemNum-6,i=this.typedArray;return[i[r+0]*t+i[r+2]*e+i[r+4],i[r+1]*t+i[r+3]*e+i[r+5]]}getInverse(){const t=this.usedElemNum-6,e=this.typedArray,r=e[t+0],i=e[t+1],s=e[t+2],a=e[t+3],n=e[t+4],h=e[t+5],o=r*a-i*s;return new Float32Array([a/o,-i/o,-s/o,r/o,(s*h-a*n)/o,(i*n-r*h)/o])}getTransform(){const t=this.usedElemNum-6,e=this.typedArray;return new Float32Array([e[t+0],e[t+1],e[t+2],e[t+3],e[t+4],e[t+5]])}setTransform(t){const e=this.usedElemNum-6,r=this.typedArray;r[e+0]=t[0],r[e+1]=t[1],r[e+2]=t[2],r[e+3]=t[3],r[e+4]=t[4],r[e+5]=t[5]}getGlobalPosition(){const t=this.usedElemNum-6,e=this.typedArray;return new f(e[t+4],e[t+5])}setGlobalPosition(t,e){if("number"!=typeof t)return void this.setGlobalPosition(t.x,t.y);const r=this.usedElemNum-6,i=this.typedArray;i[r+4]=t,i[r+5]=e}getGlobalRotation(){const t=this.usedElemNum-6,e=this.typedArray;return Math.atan2(e[t+1],e[t+0])}setGlobalRotation(t){const e=this.usedElemNum-6,r=this.typedArray,i=this.getGlobalScale(),s=Math.cos(t),a=Math.sin(t);r[e+0]=s*i.x,r[e+1]=a*i.x,r[e+2]=-a*i.y,r[e+3]=s*i.y}getGlobalScale(){const t=this.usedElemNum-6,e=this.typedArray,r=Math.sqrt(e[t+0]*e[t+0]+e[t+1]*e[t+1]),i=Math.sqrt(e[t+2]*e[t+2]+e[t+3]*e[t+3]);return new f(r,i)}setGlobalScale(t,e){if("number"!=typeof t)return void this.setGlobalScale(t.x,t.y);const r=this.getGlobalRotation(),i=Math.cos(r),s=Math.sin(r),a=this.usedElemNum-6,n=this.typedArray;n[a+0]=i*t,n[a+1]=s*t,n[a+2]=-s*e,n[a+3]=i*e}globalToLocal(t){const e=this.getInverse();return new f(e[0]*t.x+e[2]*t.y+e[4],e[1]*t.x+e[3]*t.y+e[5])}localToGlobal(t){return this.apply(t)}toCSSTransform(){const t=this.usedElemNum-6,e=this.typedArray;return`matrix(${e[t+0]}, ${e[t+1]}, ${e[t+2]}, ${e[t+3]}, ${e[t+4]}, ${e[t+5]})`}identity(){const t=this.usedElemNum-6,e=this.typedArray;e[t+0]=1,e[t+1]=0,e[t+2]=0,e[t+3]=1,e[t+4]=0,e[t+5]=0}applyTransform(t,e=0,r=0){(t.saveTransform??1)&&this.pushMat(),t.afterSave&&t.afterSave();const i=t.x||0,s=t.y||0;(i||s)&&this.translate(i,s),t.position&&this.translate(t.position),t.rotation&&this.rotate(t.rotation),t.scale&&this.scale(t.scale);let a=t.offsetX||0,n=t.offsetY||0;t.offset&&(a+=t.offset.x,n+=t.offset.y);const h=t.origin;return h&&("number"==typeof h?(a-=h*e,n-=h*r):(a-=h.x*e,n-=h.y*r)),{offsetX:a,offsetY:n}}applyTransformAfter(t){t.beforRestore&&t.beforRestore(),(t.restoreTransform??1)&&this.popMat()}}class d extends l{constructor(e,r,i,s){super(e,t.ArrayType.Uint16,e.ELEMENT_ARRAY_BUFFER,e.STATIC_DRAW),this.setMaxSize(r*s);for(let t=0;t<s;t++)this.addObject(t*i);this.bindBuffer(),this.bufferData()}addObject(t){}}class p{constructor(t,e,r,i=255){this._r=t,this._g=e,this._b=r,this._a=i,this.updateUint()}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0}setRGBA(t,e,r,i){this.r=t,this.g=e,this.b=r,this.a=i,this.updateUint()}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new p(this._r,this._g,this._b,this._a)}equal(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),i=parseInt(t.slice(4,6),16);let s=255;return t.length>=8&&(s=parseInt(t.slice(6,8),16)),new p(e,r,i,s)}add(t){return new p(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new p(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)}divide(t){return t instanceof p?new p(this.r/t.r,this.g/t.g,this.b/t.b,this.a/t.a):new p(this.r/t,this.g/t,this.b/t,this.a/t)}multiply(t){return t instanceof p?new p(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a):new p(this.r*t,this.g*t,this.b*t,this.a*t)}clamp(){this.r=Math.max(0,Math.min(255,this.r)),this.g=Math.max(0,Math.min(255,this.g)),this.b=Math.max(0,Math.min(255,this.b)),this.a=Math.max(0,Math.min(255,this.a))}}p.Red=new p(255,0,0,255),p.Green=new p(0,255,0,255),p.Blue=new p(0,0,255,255),p.Yellow=new p(255,255,0,255),p.Purple=new p(128,0,128,255),p.Orange=new p(255,165,0,255),p.Pink=new p(255,192,203,255),p.Gray=new p(128,128,128,255),p.Brown=new p(139,69,19,255),p.Cyan=new p(0,255,255,255),p.Magenta=new p(255,0,255,255),p.Lime=new p(192,255,0,255),p.White=new p(255,255,255,255),p.Black=new p(0,0,0,255),p.TRANSPARENT=new p(0,0,0,0);class f{constructor(t,e){this.x=void 0!==t?t:0,this.y=void 0!==e?e:0}add(t){return new f(this.x+t.x,this.y+t.y)}subtract(t){return new f(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof f?new f(this.x*t.x,this.y*t.y):new f(this.x*t,this.y*t)}divide(t){return t instanceof f?new f(this.x/t.x,this.y/t.y):new f(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,r=this.y-t.y;return Math.sqrt(e*e+r*r)}clone(){return new f(this.x,this.y)}copy(t){this.x=t.x,this.y=t.y}equal(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}angle(){return Math.atan2(this.y,this.x)}middle(t){return new f((this.x+t.x)/2,(this.y+t.y)/2)}abs(){return new f(Math.abs(this.x),Math.abs(this.y))}floor(){return new f(Math.floor(this.x),Math.floor(this.y))}ceil(){return new f(Math.ceil(this.x),Math.ceil(this.y))}snap(t){return new f(Math.round(this.x/t)*t,Math.round(this.y/t)*t)}stringify(){return`Vec2(${this.x}, ${this.y})`}static FromArray(t){return t.map((t=>new f(t[0],t[1])))}static fromAngle(t){return new f(Math.cos(t),Math.sin(t))}angleBetween(t){const e=this.dot(t),r=this.length()*t.length(),i=Math.max(-1,Math.min(1,e/r));return Math.acos(i)}}f.ZERO=new f(0,0),f.ONE=new f(1,1),f.UP=new f(0,1),f.DOWN=new f(0,-1),f.LEFT=new f(-1,0),f.RIGHT=new f(1,0);class m{static float(t,e){return Math.random()*(e-t)+t}static int(t,e){return Math.floor(Math.random()*(e-t+1))+t}static angle(){return Math.random()*Math.PI*2}static vector(t,e,r,i){return new f(m.float(t,e),m.float(r,i))}static direction(t){const e=m.angle();return new f(Math.cos(e)*t,Math.sin(e)*t)}static randomColor(t,e){return new p(m.float(t.r,e.r),m.float(t.g,e.g),m.float(t.b,e.b),m.float(t.a,e.a))}static pick(t){return t[m.int(0,t.length-1)]}static pickWeight(t){if(!t||0===t.length)return null;let e=0;for(const r of t)e+=r[1];const r=Math.random()*e;let i=0;for(const e of t)if(i+=e[1],r<=i)return e[0];return t[t.length-1][0]}static scalarOrRange(t,e){if(void 0===t)return e;if(Array.isArray(t)){if("number"==typeof t[0])return m.float(t[0],t[1]);if(t[0]instanceof f)return m.vector(t[0].x,t[1].x,t[0].y,t[1].y);if(t[0]instanceof p)return m.randomColor(t[0],t[1])}return"number"==typeof t?t:t.clone()}}class g{constructor(t){this.render=t}createLightShadowMaskPolygon(t,e,r){const i=[];t.forEach((t=>{for(let e=0;e<t.length;e++){const r=t[e],s=t[(e+1)%t.length];i.push([r,s])}})),r=r||Math.sqrt(Math.pow(this.render.width,2)+Math.pow(this.render.height,2));const s=[];return i.forEach((([t,i])=>{const a=new f(t.x-e.x,t.y-e.y),n=new f(i.x-e.x,i.y-e.y),h=i.subtract(t).perpendicular(),o=Math.abs(h.dot(a))/(h.length()*a.length())+.01,u=Math.abs(h.dot(n))/(h.length()*n.length())+.01,l=r/o,c=r/u,d=new f(a.x,a.y).normalize(),p=new f(n.x,n.y).normalize(),m=new f(t.x+d.x*l,t.y+d.y*l),g=new f(i.x+p.x*c,i.y+p.y*c);s.push([t,i,g,m])})),s}}const y=(t,e,r,i)=>{const s=[],a=i?Math.atan2(e.y,e.x):Math.atan2(-e.y,-e.x),n=Math.PI;for(let e=0;e<10;e++){const i=a+e/10*n,h=a+(e+1)/10*n,o=Math.cos(i)*r,u=Math.sin(i)*r,l=Math.cos(h)*r,c=Math.sin(h)*r;s.push(t),s.push(t.add(new f(o,u))),s.push(t.add(new f(l,c)))}return s},x=e=>{const r=e.points;if(r.length<2)return{vertices:[],uv:[]};const{normals:i,length:s}=((t,e=!1)=>{const r=[];if(t.length<2||e&&t.length<3)return{normals:r,length:0};const i=t.length;let s=0;if(e)for(let e=0;e<i;e++){const r=t[e],a=t[(e+1)%i];s+=r.distanceTo(a)}else for(let e=0;e<i-1;e++)s+=t[e].distanceTo(t[e+1]);const a=(t,e,r)=>{const i=e.subtract(t).normalize(),s=e.subtract(r).normalize(),a=s.dot(i);if(a<-.999)return{normal:i.perpendicular(),miters:1};{let t=s.add(i).normalize();i.cross(s)<0&&(t=t.multiply(-1));let e=1/Math.sqrt((1-a)/2);return{normal:t,miters:Math.min(e,4)}}};if(e){for(let e=0;e<i-1;e++){const s=0===e?t[i-2]:t[e-1],n=t[e],h=t[e+1];r.push(a(s,n,h))}r.push(r[0])}else for(let e=0;e<i;e++)if(0===e){const e=t[1].subtract(t[0]).normalize();r.push({normal:e.perpendicular(),miters:1})}else if(e===i-1){const i=t[e].subtract(t[e-1]).normalize();r.push({normal:i.perpendicular(),miters:1})}else r.push(a(t[e-1],t[e],t[e+1]));return{normals:r,length:s}})(r,e.closed),a=(e.width||1)/2,n=[],h=[],o=e.roundCap||!1,u=e.textureMode||t.LineTextureMode.STRETCH;let l=0;const c=e.texture?.width||1;for(let e=0;e<r.length-1;e++){const o=r[e],d=i[e].normal,p=i[e].miters,m=o.add(d.multiply(p*a)),g=o.subtract(d.multiply(p*a)),y=r[e+1],x=i[e+1].normal,T=i[e+1].miters,b=y.add(x.multiply(T*a)),E=y.subtract(x.multiply(T*a)),R=o.distanceTo(y);let w=0,S=0;u===t.LineTextureMode.STRETCH?(w=l/s,S=(l+R)/s):(w=l/c,S=w+R/c);const M=new f(w,0),A=new f(w,1),v=new f(S,0),U=new f(S,1);n.push(m),h.push(M),n.push(g),h.push(A),n.push(b),h.push(v),n.push(b),h.push(v),n.push(E),h.push(U),n.push(g),h.push(A),l+=R}if(o&&!e.closed){const t=r[0],e=i[0].normal,s=y(t,e,a,!0);n.push(...s);const h=r[r.length-1],o=i[r.length-1].normal,u=y(h,o,a,!1);n.push(...u)}return{vertices:n,uv:h}};var T="precision mediump float;\r\nvarying vec2 vRegion;\r\nvarying vec4 vColor;\r\n\r\nuniform sampler2D uTexture;\r\nuniform int uUseTexture;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n if(uUseTexture > 0){\r\n color = texture2D(uTexture, vRegion) * vColor;\r\n }else{\r\n color = vColor;\r\n }\r\n // fragment\r\n gl_FragColor = color;\r\n}\r\n",b="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec4 aColor;\r\nattribute vec2 aRegion;\r\n\r\nvarying vec4 vColor;\r\nuniform mat4 uProjectionMatrix;\r\nuniform vec4 uColor;\r\nvarying vec2 vRegion;\r\n\r\nvoid main(void) {\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n vColor = aColor;\r\n vRegion = aRegion;\r\n}\r\n";const E=(t,e,r)=>{const i=t.createShader(r);if(!i)throw new Error("Unable to create webgl shader");t.shaderSource(i,e),t.compileShader(i);if(!t.getShaderParameter(i,t.COMPILE_STATUS)){const r=t.getShaderInfoLog(i);throw console.error("Shader compilation failed:",r),new Error("Unable to compile shader: "+r+e)}return i};function R(t,e,r,i=!1,s=!1,a="clamp"){const n=t.createTexture();if(!n)throw new Error("unable to create texture");let h;switch(t.bindTexture(t.TEXTURE_2D,n),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,r?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,r?t.LINEAR:t.NEAREST),a){case"repeat":h=t.REPEAT;break;case"mirror":h=t.MIRRORED_REPEAT;break;default:h=t.CLAMP_TO_EDGE}return t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,h),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,h),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,s),i?t.texImage2D(t.TEXTURE_2D,0,t.RGBA,e.width,e.height,0,t.RGBA,t.UNSIGNED_BYTE,null):t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),n}const w=5126;var S="precision mediump float;\r\nuniform sampler2D uTextures[%TEXTURE_NUM%];\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n %GET_COLOR%\r\n // fragment\r\n gl_FragColor = color * vColor;\r\n}",M="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec2 aRegion;\r\nattribute float aTextureId;\r\nattribute vec4 aColor;\r\n\r\nuniform mat4 uProjectionMatrix;\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vRegion = aRegion;\r\n vTextureId = aTextureId;\r\n vColor = aColor;\r\n\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n}";const A=[{name:"aPosition",size:2,type:w,stride:24},{name:"aRegion",size:2,type:w,stride:24,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:w,stride:24,offset:4*Float32Array.BYTES_PER_ELEMENT},{name:"aColor",size:4,type:5121,stride:24,offset:5*Float32Array.BYTES_PER_ELEMENT,normalized:!0}],v=[{name:"aPosition",size:2,type:w,stride:20},{name:"aColor",size:4,type:5121,stride:20,offset:2*Float32Array.BYTES_PER_ELEMENT,normalized:!0},{name:"aRegion",size:2,type:w,stride:20,offset:3*Float32Array.BYTES_PER_ELEMENT}];class U{constructor(t,e,r,i,s=0){this.attributeLoc={},this.uniformLoc={},this.textureUnitNum=0,this.attributes=[];const a=function(t,e){if(t.includes("%TEXTURE_NUM%")&&(t=t.replace("%TEXTURE_NUM%",e.toString())),t.includes("%GET_COLOR%")){let r="";for(let t=0;t<e;t++)r+=0==t?`if(vTextureId == ${t}.0)`:t==e-1?"else":`else if(vTextureId == ${t}.0)`,r+=`{color = texture2D(uTextures[${t}], vRegion);}`;t=t.replace("%GET_COLOR%",r)}return t}(r,t.maxTextureUnits-s);this.program=((t,e,r)=>{var i=t.createProgram(),s=E(t,e,35633),a=E(t,r,35632);if(!i)throw new Error("Unable to create program shader");if(t.attachShader(i,s),t.attachShader(i,a),t.linkProgram(i),!t.getProgramParameter(i,t.LINK_STATUS)){const e=t.getProgramInfoLog(i);throw new Error("Unable to link shader program: "+e)}return i})(t.gl,e,a),this.gl=t.gl,this.textureUnitNum=s,this.parseShader(e),this.parseShader(a),i&&this.setAttributes(i)}setUniforms(t,e){const r=this.gl;for(const i of t.getUnifromNames()){const s=this.getUniform(i);t.bind(r,i,s,e)}}getUniform(t){return this.uniformLoc[t]}use(){this.gl.useProgram(this.program)}parseShader(t){const e=this.gl,r=t.match(/attribute\s+\w+\s+(\w+)/g);if(r)for(const t of r){const r=t.split(" ")[2],i=e.getAttribLocation(this.program,r);-1!=i&&(this.attributeLoc[r]=i)}const i=t.match(/uniform\s+\w+\s+(\w+)/g);if(i)for(const t of i){const r=t.split(" ")[2];this.uniformLoc[r]=e.getUniformLocation(this.program,r)}}setAttribute(t){const e=this.attributeLoc[t.name];if(void 0!==e){const r=this.gl;r.vertexAttribPointer(e,t.size,t.type,t.normalized||!1,t.stride,t.offset||0),r.enableVertexAttribArray(e)}}setAttributes(t){this.attributes=t;for(const e of t)this.setAttribute(e)}updateAttributes(){this.setAttributes(this.attributes)}static createCostumShader(e,r,i,s,a=0){let n={[t.ShaderType.SPRITE]:S,[t.ShaderType.GRAPHIC]:T}[s],h={[t.ShaderType.SPRITE]:M,[t.ShaderType.GRAPHIC]:b}[s];const o={[t.ShaderType.SPRITE]:A,[t.ShaderType.GRAPHIC]:v}[s];return n=n.replace("void main(void) {",i+"\nvoid main(void) {"),h=h.replace("void main(void) {",r+"\nvoid main(void) {"),n=n.replace("// fragment","fragment(color);"),h=h.replace(/\/\/ vertex s[\s\S]*?\/\/ vertex e/,"vec2 position = aPosition;\n vertex(position, vRegion);\n gl_Position = uProjectionMatrix * vec4(position, 0.0, 1.0);"),new U(e,h,n,o,a)}}class C{constructor(e){this.usedTextures=[],this.shaders=new Map,this.isCostumShader=!1,this.freeTextureUnitNum=0,this.rapid=e,this.gl=e.gl,this.webglArrayBuffer=new l(e.gl,t.ArrayType.Float32,e.gl.ARRAY_BUFFER,e.gl.STREAM_DRAW),this.maxTextureUnits=e.maxTextureUnits}getTextureUnitList(){return Array.from({length:this.maxTextureUnits},((t,e)=>e))}addVertex(t,e,...r){const[i,s]=this.rapid.matrixStack.apply(t,e);this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(s)}useTexture(t){const e=this.usedTextures.indexOf(t);return-1==e?(this.usedTextures.push(t),this.freeTextureUnitNum=this.maxTextureUnits-this.usedTextures.length,[this.usedTextures.length-1,!0]):[e,!1]}enterRegion(t){this.currentShader=t??this.getShader("default"),this.currentShader.use(),this.initializeForNextRender(),this.webglArrayBuffer.bindBuffer(),this.currentShader.updateAttributes(),this.updateProjection(),this.isCostumShader=Boolean(t)}updateProjection(){this.gl.uniformMatrix4fv(this.currentShader.uniformLoc.uProjectionMatrix,!1,this.rapid.projection)}isUnifromChanged(t){return!!t&&(this.costumUnifrom!=t||!!t?.isDirty)}setCurrentUniform(t){t.clearDirty(),this.costumUnifrom=t}exitRegion(){}initDefaultShader(t,e,r){this.setShader("default",t,e,r)}setShader(t,e,r,i){this.webglArrayBuffer.bindBuffer(),this.shaders.set(t,new U(this.rapid,e,r,i)),"default"===t&&(this.defaultShader=this.shaders.get(t))}getShader(t){return this.shaders.get(t)}render(){this.executeRender(),this.initializeForNextRender()}executeRender(){const t=this.gl;for(let e=0;e<this.usedTextures.length;e++)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,this.usedTextures[e]);this.webglArrayBuffer.bufferData()}initializeForNextRender(){this.webglArrayBuffer.clear(),this.usedTextures.length=0,this.isCostumShader=!1,this.freeTextureUnitNum=this.maxTextureUnits}hasPendingContent(){return!1}isShaderChanged(t){return(t||this.defaultShader)!=this.currentShader}}class F extends C{constructor(t){super(t),this.vertex=0,this.offset=f.ZERO,this.drawType=t.gl.TRIANGLE_FAN,this.setShader("default",b,T,v)}startRender(t,e,r,i){i&&this.currentShader?.setUniforms(i,this),this.offset=new f(t,e),this.vertex=0,this.webglArrayBuffer.clear(),r&&r.base&&(this.texture=this.useTexture(r.base.texture)[0])}addVertex(t,e,r,i,s){this.webglArrayBuffer.resize(3),super.addVertex(t+this.offset.x,e+this.offset.y),this.webglArrayBuffer.pushUint32(s),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(i),this.vertex+=1}executeRender(){super.executeRender();const t=this.gl;t.uniform1i(this.currentShader.uniformLoc.uUseTexture,void 0===this.texture?0:1),this.texture&&t.uniform1i(this.currentShader.uniformLoc.uTexture,this.texture),t.drawArrays(this.drawType,0,this.vertex),this.drawType=this.rapid.gl.TRIANGLE_FAN,this.vertex=0,this.texture=void 0}}const P=Math.floor(16384);class _ extends d{constructor(t,e){super(t,6,4,e)}addObject(t){super.addObject(),this.pushUint16(t),this.pushUint16(t+1),this.pushUint16(t+2),this.pushUint16(t),this.pushUint16(t+3),this.pushUint16(t+2)}}class N extends C{constructor(t){const e=t.gl;super(t),this.batchSprite=0,this.spriteTextureUnits=[],this.spriteTextureUnitIndexOffset=0,this.setShader("default",M,S,A),this.indexBuffer=new _(e,P)}addVertex(t,e,r,i,s,a){super.addVertex(t,e),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(s),this.webglArrayBuffer.pushUint32(a)}renderSprite(t,e,r,i,s,a,n,h,o,u,l,c,d,p=0){(1+p>this.freeTextureUnitNum||this.batchSprite>=P||this.isUnifromChanged(l)||this.rapid.projectionDirty)&&(this.render(),l&&this.isUnifromChanged(l)&&(this.currentShader.setUniforms(l,this),this.setCurrentUniform(l)),this.rapid.projectionDirty&&this.updateProjection()),this.batchSprite++,this.webglArrayBuffer.resize(20);const[f,m]=this.useTexture(t);m&&(this.spriteTextureUnits.push(f),this.spriteTextureUnitIndexOffset=this.spriteTextureUnits[0]);const g=f-this.spriteTextureUnitIndexOffset,y=c?a:i,x=c?i:a,T=d?n:s,b=d?s:n,E=h,R=h+e,w=o,S=o+r;this.addVertex(E,w,y,T,g,u),this.addVertex(R,w,x,T,g,u),this.addVertex(R,S,x,b,g,u),this.addVertex(E,S,y,b,g,u)}executeRender(){if(super.executeRender(),this.batchSprite<=0)return;const t=this.gl;this.spriteTextureUnits.length>0&&this.gl.uniform1iv(this.currentShader.uniformLoc.uTextures,this.spriteTextureUnits),t.drawElements(t.TRIANGLES,6*this.batchSprite,t.UNSIGNED_SHORT,0)}enterRegion(t){super.enterRegion(t),this.indexBuffer.bindBuffer()}initializeForNextRender(){super.initializeForNextRender(),this.batchSprite=0,this.spriteTextureUnits.length=0}hasPendingContent(){return this.batchSprite>0}}class I{constructor(t,e){this.cache=new Map,this.render=t,this.antialias=e}async textureFromUrl(e,r=this.antialias,i=t.TextureWrapMode.CLAMP){let s=this.cache.get(e);if(!s){const t=await this.loadImage(e);s=B.fromImageSource(this.render,t,r,i),this.cache.set(e,s)}return new L(s)}textureFromFrameBufferObject(t){return new L(t)}async textureFromSource(e,r=this.antialias,i=t.TextureWrapMode.CLAMP){let s=this.cache.get(e);return s||(s=B.fromImageSource(this.render,e,r,i),this.cache.set(e,s)),new L(s)}async loadImage(t){return new Promise((e=>{const r=new Image;r.onload=()=>{e(r)},r.src=t}))}createText(t){return new D(this.render,t)}destroy(t){t instanceof L?(t.base?.destroy(this.render.gl),this.removeCache(t)):(t.destroy(this.render.gl),this.removeCache(t))}createFrameBufferObject(t,e,r=this.antialias){return new O(this.render,t,e,r)}removeCache(t){const e=t instanceof L?t.base?.texture:t.texture;e&&this.cache.forEach(((t,r)=>{t===e&&this.cache.delete(r)}))}}class B{constructor(e,r,i,s=t.TextureWrapMode.CLAMP){this.texture=e,this.width=r,this.height=i,this.wrapMode=s}static fromImageSource(e,r,i=!1,s=t.TextureWrapMode.CLAMP){return new B(R(e.gl,r,i,!1,!1,s),r.width,r.height)}destroy(t){t.deleteTexture(this.texture)}}class L{constructor(t){this.scale=1,this.setBaseTextur(t)}setBaseTextur(t){t&&(this.base=t,this.setClipRegion(0,0,t.width,t.height))}setClipRegion(t,e,r,i){if(this.base)return this.clipX=t/this.base.width,this.clipY=e/this.base.height,this.clipW=this.clipX+r/this.base.width,this.clipH=this.clipY+i/this.base.height,this.width=r*this.scale,this.height=i*this.scale,this}static fromImageSource(t,e,r=!1){return new L(B.fromImageSource(t,e,r))}static fromUrl(t,e){return t.textures.textureFromUrl(e)}createSpritesHeet(t,e){if(!this.base)return[];const r=[],i=Math.floor(this.base.width/t),s=Math.floor(this.base.height/e);for(let a=0;a<s;a++)for(let s=0;s<i;s++){const i=this.clone();i.setClipRegion(s*t,a*e,t,e),r.push(i)}return r}clone(){return new L(this.base)}}class D extends L{constructor(t,e){super(),this.scale=.5,this.rapid=t,this.options=e,this.text=e.text||" ",this.updateTextImage()}updateTextImage(){const t=this.createTextCanvas();this.setBaseTextur(B.fromImageSource(this.rapid,t,!0))}createTextCanvas(){const t=document.createElement("canvas"),e=t.getContext("2d");if(!e)throw new Error("Failed to get canvas context");e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";const r=this.text.split("\n");let i=0,s=0;for(const t of r){const r=e.measureText(t);i=Math.max(i,r.width),s+=this.options.fontSize||16}t.width=2*i,t.height=2*s,e.scale(2,2),e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";let a=0;for(const t of r)e.fillText(t,0,a),a+=this.options.fontSize||16;return t}setText(t){this.text!=t&&(this.text=t,this.updateTextImage())}}class O extends B{constructor(t,e,r,i=!1){const s=t.gl,a=R(s,{width:e,height:r},i,!0,!1),n=s.createFramebuffer();if(!n)throw s.deleteTexture(a),new Error("Failed to create WebGL framebuffer");s.bindFramebuffer(s.FRAMEBUFFER,n),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,a,0);const h=s.createRenderbuffer();if(!h)throw s.deleteFramebuffer(n),s.deleteTexture(a),new Error("Failed to create depth-stencil renderbuffer");s.bindRenderbuffer(s.RENDERBUFFER,h),s.renderbufferStorage(s.RENDERBUFFER,s.STENCIL_INDEX8,e,r),s.framebufferRenderbuffer(s.FRAMEBUFFER,s.STENCIL_ATTACHMENT,s.RENDERBUFFER,h),super(a,e,r),this.gl=s,this.framebuffer=n,s.bindTexture(s.TEXTURE_2D,null),s.bindFramebuffer(s.FRAMEBUFFER,null)}bind(){const t=this.gl;t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,this.framebuffer),t.clearColor(.5,.2,.5,.5),t.clear(t.COLOR_BUFFER_BIT)}unbind(){this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}resize(t,e){this.width=t,this.height=e,this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,t,e,0,this.gl.RGBA,this.gl.UNSIGNED_BYTE,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}destroy(t){t.deleteFramebuffer(this.framebuffer),super.destroy(t)}}const k=new Set;class G{constructor(t,e){this.textures=new Map,this.width=t,this.height=e}setTile(t,e){e instanceof L&&(e={texture:e}),this.textures.set(t,e)}getTile(t){return this.textures.get(t)}}class z{constructor(t){this.rapid=t}getYSortRow(t,e,r){if(!t)return[];const i=[];for(const r of t){const t=Math.floor(r.ySort/e);i[t]||(i[t]=[]),i[t].push(r)}return i}getOffset(t){let e=(t.errorX??2)+1,r=(t.errorY??2)+1;if("number"==typeof t.error){const i=(t.error??2)+1;e=i,r=i}else t.error&&(e=t.error.x+1,r=t.error.y+1);return{errorX:e,errorY:r}}getTileData(e,r){const i=r.shape??t.TilemapShape.SQUARE,s=e.width,a=i===t.TilemapShape.ISOMETRIC?e.height/2:e.height,n=this.rapid.matrixStack,h=n.globalToLocal(f.ZERO),o=n.getGlobalScale(),{errorX:u,errorY:l}=this.getOffset(r),c=Math.ceil(this.rapid.width/s/o.x)+2*u,d=Math.ceil(this.rapid.height/a/o.y)+2*l,p=new f(h.x<0?Math.ceil(h.x/s):Math.floor(h.x/s),h.y<0?Math.ceil(h.y/a):Math.floor(h.y/a));p.x-=u,p.y-=l;let m=new f(0-h.x%s-u*s,0-h.y%a-l*a);return m=m.add(h),{startTile:p,offset:m,viewportWidth:c,viewportHeight:d,height:a,width:s,shape:i}}renderYSortRow(t,e){for(const r of e)r.render?r.render():r.renderSprite&&t.renderSprite(r.renderSprite)}renderLayer(e,r){this.rapid.matrixStack.applyTransform(r);const i=r.tileSet,{startTile:s,offset:a,viewportWidth:n,viewportHeight:h,shape:o,width:u,height:l}=this.getTileData(i,r),c=this.getYSortRow(r.ySortCallback,l,h),d=r.ySortCallback&&r.ySortCallback.length>0;var p;0!==this.rapid.matrixStack.getGlobalRotation()&&(p="TileMapRender: tilemap is not supported rotation",k.has(p)||(k.add(p),console.warn(p)),this.rapid.matrixStack.setGlobalRotation(0));for(let p=0;p<h;p++){const h=p+s.y,f=c[h]??[];if(h<0||h>=e.length)this.renderYSortRow(this.rapid,f);else{for(let c=0;c<n;c++){const n=c+s.x;if(n<0||n>=e[h].length)continue;const d=e[h][n],m=i.getTile(d);if(!m)continue;let g=c*u+a.x,y=p*l+a.y,x=p*l+a.y+(m.ySortOffset??0);h%2!=0&&o===t.TilemapShape.ISOMETRIC&&(g+=u/2);const T=r.eachTile&&r.eachTile(d,n,h)||{};f.push({ySort:x,renderSprite:{...m,x:g+(m.x||0),y:y+(m.y||0),...T}})}d&&f.sort(((t,e)=>t.ySort-e.ySort)),this.renderYSortRow(this.rapid,f)}}this.rapid.matrixStack.applyTransform(r)}localToMap(e,r){const i=r.tileSet;if(r.shape===t.TilemapShape.ISOMETRIC){let t=0,r=0;const s=i.height/2,a=i.width/2;let n=Math.floor(e.y/s);const h=n%2==0;let o=Math.floor(e.x/a);const u=o%2==0,l=e.x%a/a,c=e.y%s/s,d=c<l,p=c<1-l;return h||(n-=1),d&&!u&&h?n-=1:d||!u||h?p&&u&&h?(o-=2,n-=1):p||u||h||(n+=1):(n+=1,o-=2),t=o,r=n,t=Math.floor(o/2),new f(t,r)}return new f(Math.floor(e.x/i.width),Math.floor(e.y/i.height))}mapToLocal(e,r){const i=r.tileSet;if(r.shape===t.TilemapShape.ISOMETRIC){let t=new f(e.x*i.width,e.y*i.height/2);return e.y%2!=0&&(t.x+=i.width/2),t}return new f(e.x*i.width,e.y*i.height)}}const X=!0;class j{constructor(t,e){this.life=0,this.datas={},this.rapid=t,this.options=e,e.texture instanceof L?this.texture=e.texture:e.texture instanceof Array&&e.texture[0]instanceof Array?this.texture=m.pickWeight(e.texture):e.texture instanceof Array&&(this.texture=m.pick(e.texture)),this.maxLife=m.scalarOrRange(e.life,1),this.datas={speed:this.processAttribute(e.animation.speed,0),rotation:this.processAttribute(e.animation.rotation,0),scale:this.processAttribute(e.animation.scale,1),color:this.processAttribute(e.animation.color,p.White),velocity:this.processAttribute(e.animation.velocity,f.ZERO),acceleration:this.processAttribute(e.animation.acceleration,f.ZERO)},this.position=f.ZERO,this.initializePosition()}processAttribute(t,e){if(!t)return{value:e};if("object"==typeof(r=t)&&null!==r&&Object.getPrototypeOf(r)===Object.prototype){const r=m.scalarOrRange(t.start,e),i=m.scalarOrRange(t.end||r,e);return{delta:t.delta??this.getDelta(r,i,this.maxLife),value:r,damping:t.damping}}return this.processAttribute({start:t},e);var r}updateDamping(t){for(const e of Object.values(this.datas))if(e.damping){const r=e.value,i=Math.pow(e.damping,t);e.value="number"==typeof r?r*i:r.multiply(i)}}updateDelta(t){const e=this.datas;for(const e of Object.values(this.datas))if(e.delta){const r=e.value;"number"==typeof r?e.value+=t*e.delta:e.value=r.add(e.delta.multiply(t))}e.color.value.clamp();const r=f.fromAngle(e.rotation.value).multiply(e.speed.value*t);this.position=this.position.add(r).add(e.velocity.value.multiply(t)).add(e.acceleration.value.multiply(t))}getDelta(t,e,r){return"number"==typeof t&&"number"==typeof e?(e-t)/r:t instanceof f&&e instanceof f||t instanceof p&&e instanceof p?e.subtract(t).divide(r):t}update(t){return this.life+=t,!(this.life>=this.maxLife)&&(this.updateDamping(t),this.updateDelta(t),!0)}render(){this.rapid.renderSprite({...this.options,position:this.position,scale:this.datas.scale.value,rotation:this.datas.rotation.value,color:this.datas.color.value,texture:this.texture})}initializePosition(){switch(this.options.emitShape){case t.ParticleShape.POINT:this.position=f.ZERO;break;case t.ParticleShape.CIRCLE:const e=Math.random()*Math.PI*2,r=(this.options.emitRadius||0)*Math.sqrt(Math.random());this.position=new f(Math.cos(e)*r,Math.sin(e)*r);break;case t.ParticleShape.RECT:this.position=new f((Math.random()-.5)*(this.options.emitRect?.width||0),(Math.random()-.5)*(this.options.emitRect?.height||0))}!this.options.localSpace&&this.options.position&&(this.position=this.position.add(this.options.position))}}class W{constructor(t,e){this.particles=[],this.emitting=!1,this.emitTimer=0,this.emitRate=10,this.emitTime=0,this.emitTimeCounter=0,this.localSpace=X,this.position=f.ZERO,this.rapid=t,this.options=e,this.emitRate=void 0!==e.emitRate?e.emitRate:10,this.emitTime=void 0!==e.emitTime?e.emitTime:0,this.localSpace=void 0!==e.localSpace?e.localSpace:X,this.position=e.position||f.ZERO}getTransform(){return this.options}setEmitRate(t){this.emitRate=t}setEmitTime(t){this.emitTime=t}start(){this.emitting=!0,this.emitTimeCounter=0}stop(){this.emitting=!1}clear(){this.particles=[],this.emitTimeCounter=0}emit(t){const e=Math.min(t,(this.options.maxParticles||1/0)-this.particles.length);for(let t=0;t<e;t++){const t={...this.options},e=new j(this.rapid,t);this.particles.unshift(e)}}update(t){if(this.emitting&&this.emitRate>0)if(this.emitTime>0){if(this.emitTimeCounter+=t,this.emitTimeCounter>=this.emitTime){const t=Math.floor(this.emitTimeCounter/this.emitTime);this.emit(this.emitRate*t),this.emitTimeCounter-=t*this.emitTime}}else{this.emitTimer+=t;const e=this.emitRate*t,r=Math.floor(e);r>0&&(this.emit(r),this.emitTimer-=r/this.emitRate);this.emitTimer*this.emitRate>=1&&(this.emit(1),this.emitTimer-=1/this.emitRate)}for(let e=this.particles.length-1;e>=0;e--)this.particles[e].update(t)||this.particles.splice(e,1)}render(){for(const t of this.particles)t.render()}getParticleCount(){return this.particles.length}isActive(){return this.emitting||this.particles.length>0}oneShot(){this.emit(this.emitRate)}}return t.BaseTexture=B,t.Color=p,t.DynamicArrayBuffer=u,t.FrameBufferObject=O,t.GLShader=U,t.MathUtils=class{static deg2rad(t){return t*(Math.PI/180)}static rad2deg(t){return t/(Math.PI/180)}static normalizeDegrees(t){return(t%360+360)%360}},t.MatrixStack=c,t.ParticleEmitter=W,t.Random=m,t.Rapid=class{constructor(t){this.projectionDirty=!0,this.matrixStack=new c,this.tileMap=new z(this),this.light=new g(this),this.devicePixelRatio=window.devicePixelRatio||1,this.defaultColor=new p(255,255,255,255),this.regions=new Map,this.currentMaskType=[],this.currentTransform=[],this.currentFBO=[],this.lastTime=0;const e=(t=>{const e={stencil:!0},r=t.getContext("webgl2",e)||t.getContext("webgl",e);if(!r)throw new Error("Unable to initialize WebGL. Your browser may not support it.");return r})(t.canvas);this.gl=e,this.canvas=t.canvas,this.textures=new I(this,t.antialias??!1),this.maxTextureUnits=e.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.width=t.width||this.canvas.width,this.height=t.width||this.canvas.height,this.backgroundColor=t.backgroundColor||new p(255,255,255,255),this.registerBuildInRegion(),this.initWebgl(e),this.projectionDirty=!1}renderTileMapLayer(t,e){this.tileMap.renderLayer(t,e instanceof G?{tileSet:e}:e)}initWebgl(t){this.resize(this.width,this.height),t.enable(t.BLEND),t.disable(t.DEPTH_TEST),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.enable(t.STENCIL_TEST),t.enable(t.SCISSOR_TEST)}clearTextureUnit(){for(let t=0;t<this.maxTextureUnits;t++)this.gl.activeTexture(this.gl.TEXTURE0+t),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}registerBuildInRegion(){this.registerRegion("sprite",N),this.registerRegion("graphic",F)}registerRegion(t,e){this.regions.set(t,new e(this))}quitCurrentRegion(){this.currentRegion&&this.currentRegion.hasPendingContent()&&(this.currentRegion.render(),this.currentRegion.exitRegion())}setRegion(t,e){if(t!=this.currentRegionName||this.currentRegion&&this.currentRegion.isShaderChanged(e)){const r=this.regions.get(t);this.quitCurrentRegion(),this.currentRegion=r,this.currentRegionName=t,r.enterRegion(e)}}save(){this.matrixStack.pushMat()}restore(){this.matrixStack.popMat()}withTransform(t){this.save(),t(),this.restore()}startRender(t=!0){this.clear(),t&&this.matrixStack.clear(),this.matrixStack.pushIdentity(),this.currentRegion=void 0,this.currentRegionName=void 0;const e=performance.now(),r=this.lastTime?(e-this.lastTime)/1e3:0;return this.lastTime=e,r}endRender(){this.currentRegion?.render(),this.projectionDirty=!1}render(t){t(this.startRender()),this.endRender()}renderCamera(t){this.matrixStack.applyTransform(t),this.matrixStack.setTransform(this.matrixStack.getInverse())}renderSprite(t){const e=t.texture;if(!e||!e.base)return;const{offsetX:r,offsetY:i}=this.startDraw(t,e.width,e.height);this.setRegion("sprite",t.shader),this.currentRegion.renderSprite(e.base.texture,e.width,e.height,e.clipX,e.clipY,e.clipW,e.clipH,r,i,(t.color||this.defaultColor).uint32,t.uniforms,t.flipX,t.flipY),this.afterDraw()}renderParticles(t){t.localSpace?(this.startDraw(t.getTransform()),t.render(),this.afterDraw()):t.render()}renderTexture(t){t.base&&this.renderSprite({texture:t})}renderLine(t){const e=t.closed?[...t.points,t.points[0]]:t.points,{vertices:r,uv:i}=x({...t,points:e});this.renderGraphic({...t,drawType:this.gl.TRIANGLES,points:r,uv:i})}renderGraphic(t){this.startGraphicDraw(t),t.points.forEach(((e,r)=>{const i=Array.isArray(t.color)?t.color[r]:t.color,s=t.uv?.[r];this.addGraphicVertex(e.x,e.y,s,i)})),this.endGraphicDraw()}startGraphicDraw(t){const{offsetX:e,offsetY:r}=this.startDraw(t);this.setRegion("graphic",t.shader);const i=this.currentRegion;i.startRender(e,r,t.texture,t.uniforms),t.drawType&&(i.drawType=t.drawType)}addGraphicVertex(t,e,r,i){this.currentRegion.addVertex(t,e,r?.x,r?.y,(i||this.defaultColor).uint32)}endGraphicDraw(){this.currentRegion.render(),this.afterDraw()}startDraw(t,e=0,r=0){return this.currentTransform.push(t),this.matrixStack.applyTransform(t,e,r)}afterDraw(){this.currentTransform.length>0&&this.matrixStack.applyTransformAfter(this.currentTransform.pop())}renderRect(t){const{width:e,height:r}=t,i=[new f(0,0),new f(e,0),new f(e,r),new f(0,r)];this.renderGraphic({...t,points:i,drawType:this.gl.TRIANGLE_FAN})}renderCircle(t){const e=t.segments||32,r=t.radius,i=t.color||this.defaultColor,s=[];for(let t=0;t<=e;t++){const i=t/e*Math.PI*2,a=Math.cos(i)*r,n=Math.sin(i)*r;s.push(new f(a,n))}this.renderGraphic({...t,points:s,color:i,drawType:this.gl.TRIANGLE_FAN})}resize(t,e){const r=t*this.devicePixelRatio,i=e*this.devicePixelRatio;this.canvas.width=r,this.canvas.height=i,this.resizeWebglSize(t,e),this.canvas.style.width=t+"px",this.canvas.style.height=e+"px",this.width=t,this.height=e}resizeWebglSize(t,e,r){const i=t*(r||this.devicePixelRatio),s=e*(r||this.devicePixelRatio);this.gl.viewport(0,0,i,s),this.updateProjection(0,t,e,0),this.gl.scissor(0,0,i,s)}updateProjection(t,e,r,i){this.projection=this.createOrthMatrix(t,e,r,i),this.projectionDirty=!0}clear(t){const e=this.gl,r=t||this.backgroundColor;e.clearColor(r.r/255,r.g/255,r.b/255,r.a/255),e.clear(e.COLOR_BUFFER_BIT),this.clearMask()}createOrthMatrix(t,e,r,i){return new Float32Array([2/(e-t),0,0,0,0,2/(i-r),0,0,0,0,-1,0,-(e+t)/(e-t),-(i+r)/(i-r),0,1])}drawMask(e=t.MaskType.Include,r){this.startDrawMask(e),r(),this.endDrawMask()}startDrawMask(e=t.MaskType.Include){const r=this.gl;this.currentMaskType.push(e),this.setMaskType(e,!0),r.stencilOp(r.KEEP,r.KEEP,r.REPLACE),r.colorMask(!1,!1,!1,!1)}endDrawMask(){const e=this.gl;this.quitCurrentRegion(),e.stencilOp(e.KEEP,e.KEEP,e.KEEP),e.colorMask(!0,!0,!0,!0),this.setMaskType(this.currentMaskType.pop()??t.MaskType.Include,!1)}setMaskType(e,r=!1){const i=this.gl;if(this.quitCurrentRegion(),r)this.clearMask(),i.stencilFunc(i.ALWAYS,1,255);else switch(e){case t.MaskType.Include:i.stencilFunc(i.EQUAL,1,255);break;case t.MaskType.Exclude:i.stencilFunc(i.NOTEQUAL,1,255)}}clearMask(){const t=this.gl;this.quitCurrentRegion(),t.clearStencil(0),t.clear(t.STENCIL_BUFFER_BIT),t.stencilFunc(t.ALWAYS,1,255)}createCostumShader(t,e,r,i=0){return U.createCostumShader(this,t,e,r,i)}startFBO(t){this.quitCurrentRegion(),t.bind(),this.clearTextureUnit(),this.resizeWebglSize(t.width,t.height,1),this.updateProjection(0,t.width,0,t.height),this.save(),this.matrixStack.identity(),this.currentFBO.push(t)}endFBO(){if(this.currentFBO.length>0){const t=this.currentFBO.pop();this.quitCurrentRegion(),t.unbind(),this.clearTextureUnit(),this.resizeWebglSize(this.width,this.height),this.updateProjection(0,this.width,this.height,0),this.restore()}}drawToFBO(t,e){this.startFBO(t),e(),this.endFBO()}setBlendMode(e){switch(e){case t.BlendMode.Additive:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE);break;case t.BlendMode.Subtractive:this.gl.blendFunc(this.gl.ZERO,this.gl.ONE_MINUS_SRC_COLOR);break;case t.BlendMode.Mix:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE_MINUS_SRC_ALPHA)}}drawLightShadowMask(e){this.startDrawMask(e.type||t.MaskType.Exclude);this.light.createLightShadowMaskPolygon(e.occlusion,e.lightSource,e.baseProjectionLength).forEach((t=>{this.renderGraphic({points:t,color:p.Black})})),this.endDrawMask()}createParticleEmitter(t){return new W(this,t)}},t.SCALEFACTOR=2,t.Text=D,t.Texture=L,t.TextureCache=I,t.TileMapRender=z,t.TileSet=G,t.Uniform=class{constructor(t){this.isDirty=!1,this.data=t}setUniform(t,e){this.data[t]!=e&&(this.isDirty=!0),this.data[t]=e}clearDirty(){this.isDirty=!1}getUnifromNames(){return Object.keys(this.data)}bind(t,e,r,i){if(!r)return;const s=this.data[e];if("number"==typeof s)t.uniform1f(r,s);else if(Array.isArray(s))switch(s.length){case 1:Number.isInteger(s[0])?t.uniform1i(r,s[0]):t.uniform1f(r,s[0]);break;case 2:Number.isInteger(s[0])?t.uniform2iv(r,s):t.uniform2fv(r,s);break;case 3:Number.isInteger(s[0])?t.uniform3iv(r,s):t.uniform3fv(r,s);break;case 4:Number.isInteger(s[0])?t.uniform4iv(r,s):t.uniform4fv(r,s);break;case 9:t.uniformMatrix3fv(r,!1,s);break;case 16:t.uniformMatrix4fv(r,!1,s);break;default:console.error(`Unsupported uniform array length for ${e}:`,s.length)}else if("boolean"==typeof s)t.uniform1i(r,s?1:0);else if(s.base?.texture){const e=i.useTexture(s.base.texture)[0];t.uniform1i(r,e)}else console.error(`Unsupported uniform type for ${e}:`,typeof s)}},t.Vec2=f,t.WebglBufferArray=l,t.WebglElementBufferArray=d,t.graphicAttributes=v,t.spriteAttributes=A,t}({});
|
package/dist/rapid.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t,e,r,i,s,n;!function(t){t.STRETCH="stretch",t.REPEAT="repeat"}(t||(t={})),function(t){t.REPEAT="repeat",t.CLAMP="clamp",t.MIRROR="mirror"}(e||(e={})),function(t){t.Include="normal",t.Exclude="inverse"}(r||(r={})),function(t){t.SQUARE="square",t.ISOMETRIC="isometric"}(i||(i={})),function(t){t.SPRITE="sprite",t.GRAPHIC="graphic"}(s||(s={})),function(t){t.Additive="additive",t.Subtractive="subtractive",t.Mix="mix"}(n||(n={}));var a;!function(t){t[t.Float32=0]="Float32",t[t.Uint32=1]="Uint32",t[t.Uint16=2]="Uint16"}(a||(a={}));class h{constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(t){switch(t){case a.Float32:return Float32Array;case a.Uint32:return Uint32Array;case a.Uint16:return Uint16Array}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case a.Float32:this.typedArray=this.float32;break;case a.Uint32:this.typedArray=this.uint32;break;case a.Uint16:this.typedArray=this.uint16}}clear(){this.usedElemNum=0}resize(t=0){if((t+=this.usedElemNum)>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum<<=1;this.setMaxSize(this.maxElemNum)}}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.float32[this.usedElemNum++]=t}pushUint16(t){this.uint16[this.usedElemNum++]=t}pop(t){this.usedElemNum-=t}getArray(t=0,e){return null==e?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.typedArray.length}}class o extends h{constructor(t,e,r=t.ARRAY_BUFFER,i=t.STATIC_DRAW){super(e),this.dirty=!0,this.webglBufferSize=0,this.gl=t,this.buffer=t.createBuffer(),this.type=r,this.usage=i}pushFloat32(t){super.pushFloat32(t),this.dirty=!0}pushUint32(t){super.pushUint32(t),this.dirty=!0}pushUint16(t){super.pushUint16(t),this.dirty=!0}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize?(t.bufferData(this.type,this.getArray(),this.usage),this.webglBufferSize=this.maxElemNum):t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class u extends h{constructor(){super(a.Float32)}pushMat(){const t=this.usedElemNum-6,e=this.typedArray;this.resize(6),this.pushFloat32(e[t+0]),this.pushFloat32(e[t+1]),this.pushFloat32(e[t+2]),this.pushFloat32(e[t+3]),this.pushFloat32(e[t+4]),this.pushFloat32(e[t+5])}popMat(){this.pop(6)}pushIdentity(){this.resize(6),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0)}translate(t,e){if("number"!=typeof t)return this.translate(t.x,t.y);const r=this.usedElemNum-6,i=this.typedArray;i[r+4]=i[r+0]*t+i[r+2]*e+i[r+4],i[r+5]=i[r+1]*t+i[r+3]*e+i[r+5]}rotate(t){const e=this.usedElemNum-6,r=this.typedArray,i=Math.cos(t),s=Math.sin(t),n=r[e+0],a=r[e+1],h=r[e+2],o=r[e+3];r[e+0]=n*i-a*s,r[e+1]=n*s+a*i,r[e+2]=h*i-o*s,r[e+3]=h*s+o*i}scale(t,e){if("number"!=typeof t)return this.scale(t.x,t.y);e||(e=t);const r=this.usedElemNum-6,i=this.typedArray;i[r+0]=i[r+0]*t,i[r+1]=i[r+1]*t,i[r+2]=i[r+2]*e,i[r+3]=i[r+3]*e}apply(t,e){if("number"!=typeof t)return new d(...this.apply(t.x,t.y));const r=this.usedElemNum-6,i=this.typedArray;return[i[r+0]*t+i[r+2]*e+i[r+4],i[r+1]*t+i[r+3]*e+i[r+5]]}getInverse(){const t=this.usedElemNum-6,e=this.typedArray,r=e[t+0],i=e[t+1],s=e[t+2],n=e[t+3],a=e[t+4],h=e[t+5],o=r*n-i*s;return new Float32Array([n/o,-i/o,-s/o,r/o,(s*h-n*a)/o,(i*a-r*h)/o])}getTransform(){const t=this.usedElemNum-6,e=this.typedArray;return new Float32Array([e[t+0],e[t+1],e[t+2],e[t+3],e[t+4],e[t+5]])}setTransform(t){const e=this.usedElemNum-6,r=this.typedArray;r[e+0]=t[0],r[e+1]=t[1],r[e+2]=t[2],r[e+3]=t[3],r[e+4]=t[4],r[e+5]=t[5]}getGlobalPosition(){const t=this.usedElemNum-6,e=this.typedArray;return new d(e[t+4],e[t+5])}setGlobalPosition(t,e){if("number"!=typeof t)return void this.setGlobalPosition(t.x,t.y);const r=this.usedElemNum-6,i=this.typedArray;i[r+4]=t,i[r+5]=e}getGlobalRotation(){const t=this.usedElemNum-6,e=this.typedArray;return Math.atan2(e[t+1],e[t+0])}setGlobalRotation(t){const e=this.usedElemNum-6,r=this.typedArray,i=this.getGlobalScale(),s=Math.cos(t),n=Math.sin(t);r[e+0]=s*i.x,r[e+1]=n*i.x,r[e+2]=-n*i.y,r[e+3]=s*i.y}getGlobalScale(){const t=this.usedElemNum-6,e=this.typedArray,r=Math.sqrt(e[t+0]*e[t+0]+e[t+1]*e[t+1]),i=Math.sqrt(e[t+2]*e[t+2]+e[t+3]*e[t+3]);return new d(r,i)}setGlobalScale(t,e){if("number"!=typeof t)return void this.setGlobalScale(t.x,t.y);const r=this.getGlobalRotation(),i=Math.cos(r),s=Math.sin(r),n=this.usedElemNum-6,a=this.typedArray;a[n+0]=i*t,a[n+1]=s*t,a[n+2]=-s*e,a[n+3]=i*e}globalToLocal(t){const e=this.getInverse();return new d(e[0]*t.x+e[2]*t.y+e[4],e[1]*t.x+e[3]*t.y+e[5])}localToGlobal(t){return this.apply(t)}toCSSTransform(){const t=this.usedElemNum-6,e=this.typedArray;return`matrix(${e[t+0]}, ${e[t+1]}, ${e[t+2]}, ${e[t+3]}, ${e[t+4]}, ${e[t+5]})`}identity(){const t=this.usedElemNum-6,e=this.typedArray;e[t+0]=1,e[t+1]=0,e[t+2]=0,e[t+3]=1,e[t+4]=0,e[t+5]=0}applyTransform(t,e=0,r=0){(t.saveTransform??1)&&this.pushMat(),t.afterSave&&t.afterSave();const i=t.x||0,s=t.y||0;(i||s)&&this.translate(i,s),t.position&&this.translate(t.position),t.rotation&&this.rotate(t.rotation),t.scale&&this.scale(t.scale);let n=t.offsetX||0,a=t.offsetY||0;t.offset&&(n+=t.offset.x,a+=t.offset.y);const h=t.origin;return h&&("number"==typeof h?(n-=h*e,a-=h*r):(n-=h.x*e,a-=h.y*r)),{offsetX:n,offsetY:a}}applyTransformAfter(t){t.beforRestore&&t.beforRestore(),(t.restoreTransform??1)&&this.popMat()}}class l extends o{constructor(t,e,r,i){super(t,a.Uint16,t.ELEMENT_ARRAY_BUFFER,t.STATIC_DRAW),this.setMaxSize(e*i);for(let t=0;t<i;t++)this.addObject(t*r);this.bindBuffer(),this.bufferData()}addObject(t){}}class c{constructor(t,e,r,i=255){this._r=t,this._g=e,this._b=r,this._a=i,this.updateUint()}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0}setRGBA(t,e,r,i){this.r=t,this.g=e,this.b=r,this.a=i,this.updateUint()}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new c(this._r,this._g,this._b,this._a)}equal(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),i=parseInt(t.slice(4,6),16);let s=255;return t.length>=8&&(s=parseInt(t.slice(6,8),16)),new c(e,r,i,s)}add(t){return new c(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new c(Math.max(this.r-t.r,0),Math.max(this.g-t.g,0),Math.max(this.b-t.b,0),Math.max(this.a-t.a,0))}}c.Red=new c(255,0,0,255),c.Green=new c(0,255,0,255),c.Blue=new c(0,0,255,255),c.Yellow=new c(255,255,0,255),c.Purple=new c(128,0,128,255),c.Orange=new c(255,165,0,255),c.Pink=new c(255,192,203,255),c.Gray=new c(128,128,128,255),c.Brown=new c(139,69,19,255),c.Cyan=new c(0,255,255,255),c.Magenta=new c(255,0,255,255),c.Lime=new c(192,255,0,255),c.White=new c(255,255,255,255),c.Black=new c(0,0,0,255),c.TRANSPARENT=new c(0,0,0,0);class d{constructor(t,e){this.x=void 0!==t?t:0,this.y=void 0!==e?e:0}add(t){return new d(this.x+t.x,this.y+t.y)}subtract(t){return new d(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof d?new d(this.x*t.x,this.y*t.y):new d(this.x*t,this.y*t)}divide(t){return t instanceof d?new d(this.x/t.x,this.y/t.y):new d(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,r=this.y-t.y;return Math.sqrt(e*e+r*r)}clone(){return new d(this.x,this.y)}copy(t){this.x=t.x,this.y=t.y}equal(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}angle(){return Math.atan2(this.y,this.x)}middle(t){return new d((this.x+t.x)/2,(this.y+t.y)/2)}abs(){return new d(Math.abs(this.x),Math.abs(this.y))}floor(){return new d(Math.floor(this.x),Math.floor(this.y))}ceil(){return new d(Math.ceil(this.x),Math.ceil(this.y))}snap(t){return new d(Math.round(this.x/t)*t,Math.round(this.y/t)*t)}stringify(){return`Vec2(${this.x}, ${this.y})`}static FromArray(t){return t.map((t=>new d(t[0],t[1])))}angleBetween(t){const e=this.dot(t),r=this.length()*t.length(),i=Math.max(-1,Math.min(1,e/r));return Math.acos(i)}}d.ZERO=new d(0,0),d.ONE=new d(1,1),d.UP=new d(0,1),d.DOWN=new d(0,-1),d.LEFT=new d(-1,0),d.RIGHT=new d(1,0);class f{static deg2rad(t){return t*(Math.PI/180)}static rad2deg(t){return t/(Math.PI/180)}static normalizeDegrees(t){return(t%360+360)%360}}class p{constructor(t){this.render=t}createLightShadowMaskPolygon(t,e,r){const i=[];t.forEach((t=>{for(let e=0;e<t.length;e++){const r=t[e],s=t[(e+1)%t.length];i.push([r,s])}})),r=r||Math.sqrt(Math.pow(this.render.width,2)+Math.pow(this.render.height,2));const s=[];return i.forEach((([t,i])=>{const n=new d(t.x-e.x,t.y-e.y),a=new d(i.x-e.x,i.y-e.y),h=i.subtract(t).perpendicular(),o=Math.abs(h.dot(n))/(h.length()*n.length())+.01,u=Math.abs(h.dot(a))/(h.length()*a.length())+.01,l=r/o,c=r/u,f=new d(n.x,n.y).normalize(),p=new d(a.x,a.y).normalize(),g=new d(t.x+f.x*l,t.y+f.y*l),m=new d(i.x+p.x*c,i.y+p.y*c);s.push([t,i,m,g])})),s}}const g=(t,e,r,i)=>{const s=[],n=i?Math.atan2(e.y,e.x):Math.atan2(-e.y,-e.x),a=Math.PI;for(let e=0;e<10;e++){const i=n+e/10*a,h=n+(e+1)/10*a,o=Math.cos(i)*r,u=Math.sin(i)*r,l=Math.cos(h)*r,c=Math.sin(h)*r;s.push(t),s.push(t.add(new d(o,u))),s.push(t.add(new d(l,c)))}return s},m=e=>{const r=e.points;if(r.length<2)return{vertices:[],uv:[]};const{normals:i,length:s}=((t,e=!1)=>{const r=[];if(t.length<2||e&&t.length<3)return{normals:r,length:0};const i=t.length;let s=0;if(e)for(let e=0;e<i;e++){const r=t[e],n=t[(e+1)%i];s+=r.distanceTo(n)}else for(let e=0;e<i-1;e++)s+=t[e].distanceTo(t[e+1]);const n=(t,e,r)=>{const i=e.subtract(t).normalize(),s=e.subtract(r).normalize(),n=s.dot(i);if(n<-.999)return{normal:i.perpendicular(),miters:1};{let t=s.add(i).normalize();i.cross(s)<0&&(t=t.multiply(-1));let e=1/Math.sqrt((1-n)/2);return{normal:t,miters:Math.min(e,4)}}};if(e){for(let e=0;e<i-1;e++){const s=0===e?t[i-2]:t[e-1],a=t[e],h=t[e+1];r.push(n(s,a,h))}r.push(r[0])}else for(let e=0;e<i;e++)if(0===e){const e=t[1].subtract(t[0]).normalize();r.push({normal:e.perpendicular(),miters:1})}else if(e===i-1){const i=t[e].subtract(t[e-1]).normalize();r.push({normal:i.perpendicular(),miters:1})}else r.push(n(t[e-1],t[e],t[e+1]));return{normals:r,length:s}})(r,e.closed),n=(e.width||1)/2,a=[],h=[],o=e.roundCap||!1,u=e.textureMode||t.STRETCH;let l=0;const c=e.texture?.width||1;for(let e=0;e<r.length-1;e++){const o=r[e],f=i[e].normal,p=i[e].miters,g=o.add(f.multiply(p*n)),m=o.subtract(f.multiply(p*n)),x=r[e+1],y=i[e+1].normal,T=i[e+1].miters,E=x.add(y.multiply(T*n)),b=x.subtract(y.multiply(T*n)),w=o.distanceTo(x);let R=0,S=0;u===t.STRETCH?(R=l/s,S=(l+w)/s):(R=l/c,S=R+w/c);const A=new d(R,0),v=new d(R,1),M=new d(S,0),U=new d(S,1);a.push(g),h.push(A),a.push(m),h.push(v),a.push(E),h.push(M),a.push(E),h.push(M),a.push(b),h.push(U),a.push(m),h.push(v),l+=w}if(o&&!e.closed){const t=r[0],e=i[0].normal,s=g(t,e,n,!0);a.push(...s);const h=r[r.length-1],o=i[r.length-1].normal,u=g(h,o,n,!1);a.push(...u)}return{vertices:a,uv:h}};var x="precision mediump float;\r\nvarying vec2 vRegion;\r\nvarying vec4 vColor;\r\n\r\nuniform sampler2D uTexture;\r\nuniform int uUseTexture;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n if(uUseTexture > 0){\r\n color = texture2D(uTexture, vRegion) * vColor;\r\n }else{\r\n color = vColor;\r\n }\r\n // fragment\r\n gl_FragColor = color;\r\n}\r\n",y="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec4 aColor;\r\nattribute vec2 aRegion;\r\n\r\nvarying vec4 vColor;\r\nuniform mat4 uProjectionMatrix;\r\nuniform vec4 uColor;\r\nvarying vec2 vRegion;\r\n\r\nvoid main(void) {\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n vColor = aColor;\r\n vRegion = aRegion;\r\n}\r\n";const T=(t,e,r)=>{const i=t.createShader(r);if(!i)throw new Error("Unable to create webgl shader");t.shaderSource(i,e),t.compileShader(i);if(!t.getShaderParameter(i,t.COMPILE_STATUS)){const r=t.getShaderInfoLog(i);throw console.error("Shader compilation failed:",r),new Error("Unable to compile shader: "+r+e)}return i};function E(t,e,r,i=!1,s=!1,n="clamp"){const a=t.createTexture();if(!a)throw new Error("unable to create texture");let h;switch(t.bindTexture(t.TEXTURE_2D,a),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,r?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,r?t.LINEAR:t.NEAREST),n){case"repeat":h=t.REPEAT;break;case"mirror":h=t.MIRRORED_REPEAT;break;default:h=t.CLAMP_TO_EDGE}return t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,h),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,h),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,s),i?t.texImage2D(t.TEXTURE_2D,0,t.RGBA,e.width,e.height,0,t.RGBA,t.UNSIGNED_BYTE,null):t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),a}const b=5126;var w="precision mediump float;\r\nuniform sampler2D uTextures[%TEXTURE_NUM%];\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n %GET_COLOR%\r\n // fragment\r\n gl_FragColor = color * vColor;\r\n}",R="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec2 aRegion;\r\nattribute float aTextureId;\r\nattribute vec4 aColor;\r\n\r\nuniform mat4 uProjectionMatrix;\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vRegion = aRegion;\r\n vTextureId = aTextureId;\r\n vColor = aColor;\r\n\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n}";const S=[{name:"aPosition",size:2,type:b,stride:24},{name:"aRegion",size:2,type:b,stride:24,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:b,stride:24,offset:4*Float32Array.BYTES_PER_ELEMENT},{name:"aColor",size:4,type:5121,stride:24,offset:5*Float32Array.BYTES_PER_ELEMENT,normalized:!0}],A=[{name:"aPosition",size:2,type:b,stride:20},{name:"aColor",size:4,type:5121,stride:20,offset:2*Float32Array.BYTES_PER_ELEMENT,normalized:!0},{name:"aRegion",size:2,type:b,stride:20,offset:3*Float32Array.BYTES_PER_ELEMENT}];class v{constructor(t,e,r,i,s=0){this.attributeLoc={},this.uniformLoc={},this.textureUnitNum=0,this.attributes=[];const n=function(t,e){if(t.includes("%TEXTURE_NUM%")&&(t=t.replace("%TEXTURE_NUM%",e.toString())),t.includes("%GET_COLOR%")){let r="";for(let t=0;t<e;t++)r+=0==t?`if(vTextureId == ${t}.0)`:t==e-1?"else":`else if(vTextureId == ${t}.0)`,r+=`{color = texture2D(uTextures[${t}], vRegion);}`;t=t.replace("%GET_COLOR%",r)}return t}(r,t.maxTextureUnits-s);this.program=((t,e,r)=>{var i=t.createProgram(),s=T(t,e,35633),n=T(t,r,35632);if(!i)throw new Error("Unable to create program shader");if(t.attachShader(i,s),t.attachShader(i,n),t.linkProgram(i),!t.getProgramParameter(i,t.LINK_STATUS)){const e=t.getProgramInfoLog(i);throw new Error("Unable to link shader program: "+e)}return i})(t.gl,e,n),this.gl=t.gl,this.textureUnitNum=s,this.parseShader(e),this.parseShader(n),i&&this.setAttributes(i)}setUniforms(t,e){const r=this.gl;for(const i of t.getUnifromNames()){const s=this.getUniform(i);t.bind(r,i,s,e)}}getUniform(t){return this.uniformLoc[t]}use(){this.gl.useProgram(this.program)}parseShader(t){const e=this.gl,r=t.match(/attribute\s+\w+\s+(\w+)/g);if(r)for(const t of r){const r=t.split(" ")[2],i=e.getAttribLocation(this.program,r);-1!=i&&(this.attributeLoc[r]=i)}const i=t.match(/uniform\s+\w+\s+(\w+)/g);if(i)for(const t of i){const r=t.split(" ")[2];this.uniformLoc[r]=e.getUniformLocation(this.program,r)}}setAttribute(t){const e=this.attributeLoc[t.name];if(void 0!==e){const r=this.gl;r.vertexAttribPointer(e,t.size,t.type,t.normalized||!1,t.stride,t.offset||0),r.enableVertexAttribArray(e)}}setAttributes(t){this.attributes=t;for(const e of t)this.setAttribute(e)}updateAttributes(){this.setAttributes(this.attributes)}static createCostumShader(t,e,r,i,n=0){let a={[s.SPRITE]:w,[s.GRAPHIC]:x}[i],h={[s.SPRITE]:R,[s.GRAPHIC]:y}[i];const o={[s.SPRITE]:S,[s.GRAPHIC]:A}[i];return a=a.replace("void main(void) {",r+"\nvoid main(void) {"),h=h.replace("void main(void) {",e+"\nvoid main(void) {"),a=a.replace("// fragment","fragment(color);"),h=h.replace(/\/\/ vertex s[\s\S]*?\/\/ vertex e/,"vec2 position = aPosition;\n vertex(position, vRegion);\n gl_Position = uProjectionMatrix * vec4(position, 0.0, 1.0);"),new v(t,h,a,o,n)}}class M{constructor(t){this.usedTextures=[],this.shaders=new Map,this.isCostumShader=!1,this.freeTextureUnitNum=0,this.rapid=t,this.gl=t.gl,this.webglArrayBuffer=new o(t.gl,a.Float32,t.gl.ARRAY_BUFFER,t.gl.STREAM_DRAW),this.maxTextureUnits=t.maxTextureUnits}getTextureUnitList(){return Array.from({length:this.maxTextureUnits},((t,e)=>e))}addVertex(t,e,...r){const[i,s]=this.rapid.matrixStack.apply(t,e);this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(s)}useTexture(t){const e=this.usedTextures.indexOf(t);return-1==e?(this.usedTextures.push(t),this.freeTextureUnitNum=this.maxTextureUnits-this.usedTextures.length,[this.usedTextures.length-1,!0]):[e,!1]}enterRegion(t){this.currentShader=t??this.getShader("default"),this.currentShader.use(),this.initializeForNextRender(),this.webglArrayBuffer.bindBuffer(),this.currentShader.updateAttributes(),this.updateProjection(),this.isCostumShader=Boolean(t)}updateProjection(){this.gl.uniformMatrix4fv(this.currentShader.uniformLoc.uProjectionMatrix,!1,this.rapid.projection)}isUnifromChanged(t){return!!t&&(this.costumUnifrom!=t||!!t?.isDirty)}setCurrentUniform(t){t.clearDirty(),this.costumUnifrom=t}exitRegion(){}initDefaultShader(t,e,r){this.setShader("default",t,e,r)}setShader(t,e,r,i){this.webglArrayBuffer.bindBuffer(),this.shaders.set(t,new v(this.rapid,e,r,i)),"default"===t&&(this.defaultShader=this.shaders.get(t))}getShader(t){return this.shaders.get(t)}render(){this.executeRender(),this.initializeForNextRender()}executeRender(){const t=this.gl;for(let e=0;e<this.usedTextures.length;e++)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,this.usedTextures[e]);this.webglArrayBuffer.bufferData()}initializeForNextRender(){this.webglArrayBuffer.clear(),this.usedTextures.length=0,this.isCostumShader=!1,this.freeTextureUnitNum=this.maxTextureUnits}hasPendingContent(){return!1}isShaderChanged(t){return(t||this.defaultShader)!=this.currentShader}}class U extends M{constructor(t){super(t),this.vertex=0,this.offset=d.ZERO,this.drawType=t.gl.TRIANGLE_FAN,this.setShader("default",y,x,A)}startRender(t,e,r,i){i&&this.currentShader?.setUniforms(i,this),this.offset=new d(t,e),this.vertex=0,this.webglArrayBuffer.clear(),r&&r.base&&(this.texture=this.useTexture(r.base.texture)[0])}addVertex(t,e,r,i,s){this.webglArrayBuffer.resize(3),super.addVertex(t+this.offset.x,e+this.offset.y),this.webglArrayBuffer.pushUint32(s),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(i),this.vertex+=1}executeRender(){super.executeRender();const t=this.gl;t.uniform1i(this.currentShader.uniformLoc.uUseTexture,void 0===this.texture?0:1),this.texture&&t.uniform1i(this.currentShader.uniformLoc.uTexture,this.texture),t.drawArrays(this.drawType,0,this.vertex),this.drawType=this.rapid.gl.TRIANGLE_FAN,this.vertex=0,this.texture=void 0}}const F=Math.floor(16384);class _ extends l{constructor(t,e){super(t,6,4,e)}addObject(t){super.addObject(),this.pushUint16(t),this.pushUint16(t+1),this.pushUint16(t+2),this.pushUint16(t),this.pushUint16(t+3),this.pushUint16(t+2)}}class N extends M{constructor(t){const e=t.gl;super(t),this.batchSprite=0,this.spriteTextureUnits=[],this.spriteTextureUnitIndexOffset=0,this.setShader("default",R,w,S),this.indexBuffer=new _(e,F)}addVertex(t,e,r,i,s,n){super.addVertex(t,e),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(s),this.webglArrayBuffer.pushUint32(n)}renderSprite(t,e,r,i,s,n,a,h,o,u,l,c,d,f=0){(1+f>this.freeTextureUnitNum||this.batchSprite>=F||this.isUnifromChanged(l)||this.rapid.projectionDirty)&&(this.render(),l&&this.isUnifromChanged(l)&&(this.currentShader.setUniforms(l,this),this.setCurrentUniform(l)),this.rapid.projectionDirty&&this.updateProjection()),this.batchSprite++,this.webglArrayBuffer.resize(20);const[p,g]=this.useTexture(t);g&&(this.spriteTextureUnits.push(p),this.spriteTextureUnitIndexOffset=this.spriteTextureUnits[0]);const m=p-this.spriteTextureUnitIndexOffset,x=c?n:i,y=c?i:n,T=d?a:s,E=d?s:a,b=h,w=h+e,R=o,S=o+r;this.addVertex(b,R,x,T,m,u),this.addVertex(w,R,y,T,m,u),this.addVertex(w,S,y,E,m,u),this.addVertex(b,S,x,E,m,u)}executeRender(){if(super.executeRender(),this.batchSprite<=0)return;const t=this.gl;this.spriteTextureUnits.length>0&&this.gl.uniform1iv(this.currentShader.uniformLoc.uTextures,this.spriteTextureUnits),t.drawElements(t.TRIANGLES,6*this.batchSprite,t.UNSIGNED_SHORT,0)}enterRegion(t){super.enterRegion(t),this.indexBuffer.bindBuffer()}initializeForNextRender(){super.initializeForNextRender(),this.batchSprite=0,this.spriteTextureUnits.length=0}hasPendingContent(){return this.batchSprite>0}}class C{constructor(t,e){this.cache=new Map,this.render=t,this.antialias=e}async textureFromUrl(t,r=this.antialias,i=e.CLAMP){let s=this.cache.get(t);if(!s){const e=await this.loadImage(t);s=I.fromImageSource(this.render,e,r,i),this.cache.set(t,s)}return new P(s)}textureFromFrameBufferObject(t){return new P(t)}async textureFromSource(t,r=this.antialias,i=e.CLAMP){let s=this.cache.get(t);return s||(s=I.fromImageSource(this.render,t,r,i),this.cache.set(t,s)),new P(s)}async loadImage(t){return new Promise((e=>{const r=new Image;r.onload=()=>{e(r)},r.src=t}))}createText(t){return new L(this.render,t)}destroy(t){t instanceof P?(t.base?.destroy(this.render.gl),this.removeCache(t)):(t.destroy(this.render.gl),this.removeCache(t))}createFrameBufferObject(t,e,r=this.antialias){return new D(this.render,t,e,r)}removeCache(t){const e=t instanceof P?t.base?.texture:t.texture;e&&this.cache.forEach(((t,r)=>{t===e&&this.cache.delete(r)}))}}class I{constructor(t,r,i,s=e.CLAMP){this.texture=t,this.width=r,this.height=i,this.wrapMode=s}static fromImageSource(t,r,i=!1,s=e.CLAMP){return new I(E(t.gl,r,i,!1,!1,s),r.width,r.height)}destroy(t){t.deleteTexture(this.texture)}}class P{constructor(t){this.scale=1,this.setBaseTextur(t)}setBaseTextur(t){t&&(this.base=t,this.setClipRegion(0,0,t.width,t.height))}setClipRegion(t,e,r,i){if(this.base)return this.clipX=t/this.base.width,this.clipY=e/this.base.height,this.clipW=this.clipX+r/this.base.width,this.clipH=this.clipY+i/this.base.height,this.width=r*this.scale,this.height=i*this.scale,this}static fromImageSource(t,e,r=!1){return new P(I.fromImageSource(t,e,r))}static fromUrl(t,e){return t.textures.textureFromUrl(e)}createSpritesHeet(t,e){if(!this.base)return[];const r=[],i=Math.floor(this.base.width/t),s=Math.floor(this.base.height/e);for(let n=0;n<s;n++)for(let s=0;s<i;s++){const i=this.clone();i.setClipRegion(s*t,n*e,t,e),r.push(i)}return r}clone(){return new P(this.base)}}const B=2;class L extends P{constructor(t,e){super(),this.scale=.5,this.rapid=t,this.options=e,this.text=e.text||" ",this.updateTextImage()}updateTextImage(){const t=this.createTextCanvas();this.setBaseTextur(I.fromImageSource(this.rapid,t,!0))}createTextCanvas(){const t=document.createElement("canvas"),e=t.getContext("2d");if(!e)throw new Error("Failed to get canvas context");e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";const r=this.text.split("\n");let i=0,s=0;for(const t of r){const r=e.measureText(t);i=Math.max(i,r.width),s+=this.options.fontSize||16}t.width=2*i,t.height=2*s,e.scale(2,2),e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";let n=0;for(const t of r)e.fillText(t,0,n),n+=this.options.fontSize||16;return t}setText(t){this.text!=t&&(this.text=t,this.updateTextImage())}}class D extends I{constructor(t,e,r,i=!1){const s=t.gl,n=E(s,{width:e,height:r},i,!0,!1),a=s.createFramebuffer();if(!a)throw s.deleteTexture(n),new Error("Failed to create WebGL framebuffer");s.bindFramebuffer(s.FRAMEBUFFER,a),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,n,0);const h=s.createRenderbuffer();if(!h)throw s.deleteFramebuffer(a),s.deleteTexture(n),new Error("Failed to create depth-stencil renderbuffer");s.bindRenderbuffer(s.RENDERBUFFER,h),s.renderbufferStorage(s.RENDERBUFFER,s.STENCIL_INDEX8,e,r),s.framebufferRenderbuffer(s.FRAMEBUFFER,s.STENCIL_ATTACHMENT,s.RENDERBUFFER,h),super(n,e,r),this.gl=s,this.framebuffer=a,s.bindTexture(s.TEXTURE_2D,null),s.bindFramebuffer(s.FRAMEBUFFER,null)}bind(){const t=this.gl;t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,this.framebuffer),t.clearColor(.5,.2,.5,.5),t.clear(t.COLOR_BUFFER_BIT)}unbind(){this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}resize(t,e){this.width=t,this.height=e,this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,t,e,0,this.gl.RGBA,this.gl.UNSIGNED_BYTE,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}destroy(t){t.deleteFramebuffer(this.framebuffer),super.destroy(t)}}const O=new Set;class k{constructor(t,e){this.textures=new Map,this.width=t,this.height=e}setTile(t,e){e instanceof P&&(e={texture:e}),this.textures.set(t,e)}getTile(t){return this.textures.get(t)}}class G{constructor(t){this.rapid=t}getYSortRow(t,e,r){if(!t)return[];const i=[];for(const r of t){const t=Math.floor(r.ySort/e);i[t]||(i[t]=[]),i[t].push(r)}return i}getOffset(t){let e=(t.errorX??2)+1,r=(t.errorY??2)+1;if("number"==typeof t.error){const i=(t.error??2)+1;e=i,r=i}else t.error&&(e=t.error.x+1,r=t.error.y+1);return{errorX:e,errorY:r}}getTileData(t,e){const r=e.shape??i.SQUARE,s=t.width,n=r===i.ISOMETRIC?t.height/2:t.height,a=this.rapid.matrixStack,h=a.globalToLocal(d.ZERO),o=a.getGlobalScale(),{errorX:u,errorY:l}=this.getOffset(e),c=Math.ceil(this.rapid.width/s/o.x)+2*u,f=Math.ceil(this.rapid.height/n/o.y)+2*l,p=new d(h.x<0?Math.ceil(h.x/s):Math.floor(h.x/s),h.y<0?Math.ceil(h.y/n):Math.floor(h.y/n));p.x-=u,p.y-=l;let g=new d(0-h.x%s-u*s,0-h.y%n-l*n);return g=g.add(h),{startTile:p,offset:g,viewportWidth:c,viewportHeight:f,height:n,width:s,shape:r}}renderYSortRow(t,e){for(const r of e)r.render?r.render():r.renderSprite&&t.renderSprite(r.renderSprite)}renderLayer(t,e){this.rapid.matrixStack.applyTransform(e);const r=e.tileSet,{startTile:s,offset:n,viewportWidth:a,viewportHeight:h,shape:o,width:u,height:l}=this.getTileData(r,e),c=this.getYSortRow(e.ySortCallback,l,h),d=e.ySortCallback&&e.ySortCallback.length>0;var f;0!==this.rapid.matrixStack.getGlobalRotation()&&(f="TileMapRender: tilemap is not supported rotation",O.has(f)||(O.add(f),console.warn(f)),this.rapid.matrixStack.setGlobalRotation(0));for(let f=0;f<h;f++){const h=f+s.y,p=c[h]??[];if(h<0||h>=t.length)this.renderYSortRow(this.rapid,p);else{for(let c=0;c<a;c++){const a=c+s.x;if(a<0||a>=t[h].length)continue;const d=t[h][a],g=r.getTile(d);if(!g)continue;let m=c*u+n.x,x=f*l+n.y,y=f*l+n.y+(g.ySortOffset??0);h%2!=0&&o===i.ISOMETRIC&&(m+=u/2);const T=e.eachTile&&e.eachTile(d,a,h)||{};p.push({ySort:y,renderSprite:{...g,x:m+(g.x||0),y:x+(g.y||0),...T}})}d&&p.sort(((t,e)=>t.ySort-e.ySort)),this.renderYSortRow(this.rapid,p)}}this.rapid.matrixStack.applyTransform(e)}localToMap(t,e){const r=e.tileSet;if(e.shape===i.ISOMETRIC){let e=0,i=0;const s=r.height/2,n=r.width/2;let a=Math.floor(t.y/s);const h=a%2==0;let o=Math.floor(t.x/n);const u=o%2==0,l=t.x%n/n,c=t.y%s/s,f=c<l,p=c<1-l;return h||(a-=1),f&&!u&&h?a-=1:f||!u||h?p&&u&&h?(o-=2,a-=1):p||u||h||(a+=1):(a+=1,o-=2),e=o,i=a,e=Math.floor(o/2),new d(e,i)}return new d(Math.floor(t.x/r.width),Math.floor(t.y/r.height))}mapToLocal(t,e){const r=e.tileSet;if(e.shape===i.ISOMETRIC){let e=new d(t.x*r.width,t.y*r.height/2);return t.y%2!=0&&(e.x+=r.width/2),e}return new d(t.x*r.width,t.y*r.height)}}class z{constructor(t){this.projectionDirty=!0,this.matrixStack=new u,this.tileMap=new G(this),this.light=new p(this),this.devicePixelRatio=window.devicePixelRatio||1,this.defaultColor=new c(255,255,255,255),this.regions=new Map,this.currentMaskType=[],this.currentTransform=[],this.currentFBO=[];const e=(t=>{const e={stencil:!0},r=t.getContext("webgl2",e)||t.getContext("webgl",e);if(!r)throw new Error("Unable to initialize WebGL. Your browser may not support it.");return r})(t.canvas);this.gl=e,this.canvas=t.canvas,this.textures=new C(this,t.antialias??!1),this.maxTextureUnits=e.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.width=t.width||this.canvas.width,this.height=t.width||this.canvas.height,this.backgroundColor=t.backgroundColor||new c(255,255,255,255),this.registerBuildInRegion(),this.initWebgl(e),this.projectionDirty=!1}renderTileMapLayer(t,e){this.tileMap.renderLayer(t,e instanceof k?{tileSet:e}:e)}initWebgl(t){this.resize(this.width,this.height),t.enable(t.BLEND),t.disable(t.DEPTH_TEST),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.enable(t.STENCIL_TEST),t.enable(t.SCISSOR_TEST)}clearTextureUnit(){for(let t=0;t<this.maxTextureUnits;t++)this.gl.activeTexture(this.gl.TEXTURE0+t),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}registerBuildInRegion(){this.registerRegion("sprite",N),this.registerRegion("graphic",U)}registerRegion(t,e){this.regions.set(t,new e(this))}quitCurrentRegion(){this.currentRegion&&this.currentRegion.hasPendingContent()&&(this.currentRegion.render(),this.currentRegion.exitRegion())}setRegion(t,e){if(t!=this.currentRegionName||this.currentRegion&&this.currentRegion.isShaderChanged(e)){const r=this.regions.get(t);this.quitCurrentRegion(),this.currentRegion=r,this.currentRegionName=t,r.enterRegion(e)}}save(){this.matrixStack.pushMat()}restore(){this.matrixStack.popMat()}withTransform(t){this.save(),t(),this.restore()}startRender(t=!0){this.clear(),t&&this.matrixStack.clear(),this.matrixStack.pushIdentity(),this.currentRegion=void 0,this.currentRegionName=void 0}endRender(){this.currentRegion?.render(),this.projectionDirty=!1}render(t){this.startRender(),t(),this.endRender()}renderSprite(t){const e=t.texture;if(!e||!e.base)return;const{offsetX:r,offsetY:i}=this.startDraw(t,e.width,e.height);this.setRegion("sprite",t.shader),this.currentRegion.renderSprite(e.base.texture,e.width,e.height,e.clipX,e.clipY,e.clipW,e.clipH,r,i,(t.color||this.defaultColor).uint32,t.uniforms,t.flipX,t.flipY),this.afterDraw()}renderTexture(t){t.base&&this.renderSprite({texture:t})}renderLine(t){const e=t.closed?[...t.points,t.points[0]]:t.points,{vertices:r,uv:i}=m({...t,points:e});this.renderGraphic({...t,drawType:this.gl.TRIANGLES,points:r,uv:i})}renderGraphic(t){this.startGraphicDraw(t),t.points.forEach(((e,r)=>{const i=Array.isArray(t.color)?t.color[r]:t.color,s=t.uv?.[r];this.addGraphicVertex(e.x,e.y,s,i)})),this.endGraphicDraw()}startGraphicDraw(t){const{offsetX:e,offsetY:r}=this.startDraw(t);this.setRegion("graphic",t.shader);const i=this.currentRegion;i.startRender(e,r,t.texture,t.uniforms),t.drawType&&(i.drawType=t.drawType)}addGraphicVertex(t,e,r,i){this.currentRegion.addVertex(t,e,r?.x,r?.y,(i||this.defaultColor).uint32)}endGraphicDraw(){this.currentRegion.render(),this.afterDraw()}startDraw(t,e=0,r=0){return this.currentTransform.push(t),this.matrixStack.applyTransform(t,e,r)}afterDraw(){this.currentTransform.length>0&&this.matrixStack.applyTransformAfter(this.currentTransform.pop())}renderRect(t){const{width:e,height:r}=t,i=[new d(0,0),new d(e,0),new d(e,r),new d(0,r)];this.renderGraphic({...t,points:i,drawType:this.gl.TRIANGLE_FAN})}renderCircle(t){const e=t.segments||32,r=t.radius,i=t.color||this.defaultColor,s=[];for(let t=0;t<=e;t++){const i=t/e*Math.PI*2,n=Math.cos(i)*r,a=Math.sin(i)*r;s.push(new d(n,a))}this.renderGraphic({...t,points:s,color:i,drawType:this.gl.TRIANGLE_FAN})}resize(t,e){const r=t*this.devicePixelRatio,i=e*this.devicePixelRatio;this.canvas.width=r,this.canvas.height=i,this.resizeWebglSize(t,e),this.canvas.style.width=t+"px",this.canvas.style.height=e+"px",this.width=t,this.height=e}resizeWebglSize(t,e,r){const i=t*(r||this.devicePixelRatio),s=e*(r||this.devicePixelRatio);this.gl.viewport(0,0,i,s),this.updateProjection(0,t,e,0),this.gl.scissor(0,0,i,s)}updateProjection(t,e,r,i){this.projection=this.createOrthMatrix(t,e,r,i),this.projectionDirty=!0}clear(t){const e=this.gl,r=t||this.backgroundColor;e.clearColor(r.r/255,r.g/255,r.b/255,r.a/255),e.clear(e.COLOR_BUFFER_BIT),this.clearMask()}createOrthMatrix(t,e,r,i){return new Float32Array([2/(e-t),0,0,0,0,2/(i-r),0,0,0,0,-1,0,-(e+t)/(e-t),-(i+r)/(i-r),0,1])}drawMask(t=r.Include,e){this.startDrawMask(t),e(),this.endDrawMask()}startDrawMask(t=r.Include){const e=this.gl;this.currentMaskType.push(t),this.setMaskType(t,!0),e.stencilOp(e.KEEP,e.KEEP,e.REPLACE),e.colorMask(!1,!1,!1,!1)}endDrawMask(){const t=this.gl;this.quitCurrentRegion(),t.stencilOp(t.KEEP,t.KEEP,t.KEEP),t.colorMask(!0,!0,!0,!0),this.setMaskType(this.currentMaskType.pop()??r.Include,!1)}setMaskType(t,e=!1){const i=this.gl;if(this.quitCurrentRegion(),e)this.clearMask(),i.stencilFunc(i.ALWAYS,1,255);else switch(t){case r.Include:i.stencilFunc(i.EQUAL,1,255);break;case r.Exclude:i.stencilFunc(i.NOTEQUAL,1,255)}}clearMask(){const t=this.gl;this.quitCurrentRegion(),t.clearStencil(0),t.clear(t.STENCIL_BUFFER_BIT),t.stencilFunc(t.ALWAYS,1,255)}createCostumShader(t,e,r,i=0){return v.createCostumShader(this,t,e,r,i)}startFBO(t){this.quitCurrentRegion(),t.bind(),this.clearTextureUnit(),this.resizeWebglSize(t.width,t.height,1),this.updateProjection(0,t.width,0,t.height),this.save(),this.matrixStack.identity(),this.currentFBO.push(t)}endFBO(){if(this.currentFBO.length>0){const t=this.currentFBO.pop();this.quitCurrentRegion(),t.unbind(),this.clearTextureUnit(),this.resizeWebglSize(this.width,this.height),this.updateProjection(0,this.width,this.height,0),this.restore()}}drawToFBO(t,e){this.startFBO(t),e(),this.endFBO()}setBlendMode(t){switch(t){case n.Additive:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE);break;case n.Subtractive:this.gl.blendFunc(this.gl.ZERO,this.gl.ONE_MINUS_SRC_COLOR);break;case n.Mix:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE_MINUS_SRC_ALPHA)}}drawLightShadowMask(t){this.startDrawMask(t.type||r.Exclude);this.light.createLightShadowMaskPolygon(t.occlusion,t.lightSource,t.baseProjectionLength).forEach((t=>{this.renderGraphic({points:t,color:c.Black})})),this.endDrawMask()}}class X{constructor(t){this.isDirty=!1,this.data=t}setUniform(t,e){this.data[t]!=e&&(this.isDirty=!0),this.data[t]=e}clearDirty(){this.isDirty=!1}getUnifromNames(){return Object.keys(this.data)}bind(t,e,r,i){if(!r)return;const s=this.data[e];if("number"==typeof s)t.uniform1f(r,s);else if(Array.isArray(s))switch(s.length){case 1:Number.isInteger(s[0])?t.uniform1i(r,s[0]):t.uniform1f(r,s[0]);break;case 2:Number.isInteger(s[0])?t.uniform2iv(r,s):t.uniform2fv(r,s);break;case 3:Number.isInteger(s[0])?t.uniform3iv(r,s):t.uniform3fv(r,s);break;case 4:Number.isInteger(s[0])?t.uniform4iv(r,s):t.uniform4fv(r,s);break;case 9:t.uniformMatrix3fv(r,!1,s);break;case 16:t.uniformMatrix4fv(r,!1,s);break;default:console.error(`Unsupported uniform array length for ${e}:`,s.length)}else if("boolean"==typeof s)t.uniform1i(r,s?1:0);else if(s.base?.texture){const e=i.useTexture(s.base.texture)[0];t.uniform1i(r,e)}else console.error(`Unsupported uniform type for ${e}:`,typeof s)}}export{a as ArrayType,I as BaseTexture,n as BlendMode,c as Color,h as DynamicArrayBuffer,D as FrameBufferObject,v as GLShader,t as LineTextureMode,r as MaskType,f as MathUtils,u as MatrixStack,z as Rapid,B as SCALEFACTOR,s as ShaderType,L as Text,P as Texture,C as TextureCache,e as TextureWrapMode,G as TileMapRender,k as TileSet,i as TilemapShape,X as Uniform,d as Vec2,o as WebglBufferArray,l as WebglElementBufferArray,A as graphicAttributes,S as spriteAttributes};
|
|
1
|
+
var t,e,i,r,s,n,a;!function(t){t.STRETCH="stretch",t.REPEAT="repeat"}(t||(t={})),function(t){t.REPEAT="repeat",t.CLAMP="clamp",t.MIRROR="mirror"}(e||(e={})),function(t){t.Include="normal",t.Exclude="inverse"}(i||(i={})),function(t){t.SQUARE="square",t.ISOMETRIC="isometric"}(r||(r={})),function(t){t.SPRITE="sprite",t.GRAPHIC="graphic"}(s||(s={})),function(t){t.Additive="additive",t.Subtractive="subtractive",t.Mix="mix"}(n||(n={})),function(t){t.POINT="point",t.CIRCLE="circle",t.RECT="rect"}(a||(a={}));var h;!function(t){t[t.Float32=0]="Float32",t[t.Uint32=1]="Uint32",t[t.Uint16=2]="Uint16"}(h||(h={}));class o{constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(t){switch(t){case h.Float32:return Float32Array;case h.Uint32:return Uint32Array;case h.Uint16:return Uint16Array}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case h.Float32:this.typedArray=this.float32;break;case h.Uint32:this.typedArray=this.uint32;break;case h.Uint16:this.typedArray=this.uint16}}clear(){this.usedElemNum=0}resize(t=0){if((t+=this.usedElemNum)>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum<<=1;this.setMaxSize(this.maxElemNum)}}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.float32[this.usedElemNum++]=t}pushUint16(t){this.uint16[this.usedElemNum++]=t}pop(t){this.usedElemNum-=t}getArray(t=0,e){return null==e?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.typedArray.length}}class u extends o{constructor(t,e,i=t.ARRAY_BUFFER,r=t.STATIC_DRAW){super(e),this.dirty=!0,this.webglBufferSize=0,this.gl=t,this.buffer=t.createBuffer(),this.type=i,this.usage=r}pushFloat32(t){super.pushFloat32(t),this.dirty=!0}pushUint32(t){super.pushUint32(t),this.dirty=!0}pushUint16(t){super.pushUint16(t),this.dirty=!0}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize?(t.bufferData(this.type,this.getArray(),this.usage),this.webglBufferSize=this.maxElemNum):t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class l extends o{constructor(){super(h.Float32)}pushMat(){const t=this.usedElemNum-6,e=this.typedArray;this.resize(6),this.pushFloat32(e[t+0]),this.pushFloat32(e[t+1]),this.pushFloat32(e[t+2]),this.pushFloat32(e[t+3]),this.pushFloat32(e[t+4]),this.pushFloat32(e[t+5])}popMat(){this.pop(6)}pushIdentity(){this.resize(6),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0)}translate(t,e){if("number"!=typeof t)return this.translate(t.x,t.y);const i=this.usedElemNum-6,r=this.typedArray;r[i+4]=r[i+0]*t+r[i+2]*e+r[i+4],r[i+5]=r[i+1]*t+r[i+3]*e+r[i+5]}rotate(t){const e=this.usedElemNum-6,i=this.typedArray,r=Math.cos(t),s=Math.sin(t),n=i[e+0],a=i[e+1],h=i[e+2],o=i[e+3];i[e+0]=n*r-a*s,i[e+1]=n*s+a*r,i[e+2]=h*r-o*s,i[e+3]=h*s+o*r}scale(t,e){if("number"!=typeof t)return this.scale(t.x,t.y);e||(e=t);const i=this.usedElemNum-6,r=this.typedArray;r[i+0]=r[i+0]*t,r[i+1]=r[i+1]*t,r[i+2]=r[i+2]*e,r[i+3]=r[i+3]*e}apply(t,e){if("number"!=typeof t)return new p(...this.apply(t.x,t.y));const i=this.usedElemNum-6,r=this.typedArray;return[r[i+0]*t+r[i+2]*e+r[i+4],r[i+1]*t+r[i+3]*e+r[i+5]]}getInverse(){const t=this.usedElemNum-6,e=this.typedArray,i=e[t+0],r=e[t+1],s=e[t+2],n=e[t+3],a=e[t+4],h=e[t+5],o=i*n-r*s;return new Float32Array([n/o,-r/o,-s/o,i/o,(s*h-n*a)/o,(r*a-i*h)/o])}getTransform(){const t=this.usedElemNum-6,e=this.typedArray;return new Float32Array([e[t+0],e[t+1],e[t+2],e[t+3],e[t+4],e[t+5]])}setTransform(t){const e=this.usedElemNum-6,i=this.typedArray;i[e+0]=t[0],i[e+1]=t[1],i[e+2]=t[2],i[e+3]=t[3],i[e+4]=t[4],i[e+5]=t[5]}getGlobalPosition(){const t=this.usedElemNum-6,e=this.typedArray;return new p(e[t+4],e[t+5])}setGlobalPosition(t,e){if("number"!=typeof t)return void this.setGlobalPosition(t.x,t.y);const i=this.usedElemNum-6,r=this.typedArray;r[i+4]=t,r[i+5]=e}getGlobalRotation(){const t=this.usedElemNum-6,e=this.typedArray;return Math.atan2(e[t+1],e[t+0])}setGlobalRotation(t){const e=this.usedElemNum-6,i=this.typedArray,r=this.getGlobalScale(),s=Math.cos(t),n=Math.sin(t);i[e+0]=s*r.x,i[e+1]=n*r.x,i[e+2]=-n*r.y,i[e+3]=s*r.y}getGlobalScale(){const t=this.usedElemNum-6,e=this.typedArray,i=Math.sqrt(e[t+0]*e[t+0]+e[t+1]*e[t+1]),r=Math.sqrt(e[t+2]*e[t+2]+e[t+3]*e[t+3]);return new p(i,r)}setGlobalScale(t,e){if("number"!=typeof t)return void this.setGlobalScale(t.x,t.y);const i=this.getGlobalRotation(),r=Math.cos(i),s=Math.sin(i),n=this.usedElemNum-6,a=this.typedArray;a[n+0]=r*t,a[n+1]=s*t,a[n+2]=-s*e,a[n+3]=r*e}globalToLocal(t){const e=this.getInverse();return new p(e[0]*t.x+e[2]*t.y+e[4],e[1]*t.x+e[3]*t.y+e[5])}localToGlobal(t){return this.apply(t)}toCSSTransform(){const t=this.usedElemNum-6,e=this.typedArray;return`matrix(${e[t+0]}, ${e[t+1]}, ${e[t+2]}, ${e[t+3]}, ${e[t+4]}, ${e[t+5]})`}identity(){const t=this.usedElemNum-6,e=this.typedArray;e[t+0]=1,e[t+1]=0,e[t+2]=0,e[t+3]=1,e[t+4]=0,e[t+5]=0}applyTransform(t,e=0,i=0){(t.saveTransform??1)&&this.pushMat(),t.afterSave&&t.afterSave();const r=t.x||0,s=t.y||0;(r||s)&&this.translate(r,s),t.position&&this.translate(t.position),t.rotation&&this.rotate(t.rotation),t.scale&&this.scale(t.scale);let n=t.offsetX||0,a=t.offsetY||0;t.offset&&(n+=t.offset.x,a+=t.offset.y);const h=t.origin;return h&&("number"==typeof h?(n-=h*e,a-=h*i):(n-=h.x*e,a-=h.y*i)),{offsetX:n,offsetY:a}}applyTransformAfter(t){t.beforRestore&&t.beforRestore(),(t.restoreTransform??1)&&this.popMat()}}class c extends u{constructor(t,e,i,r){super(t,h.Uint16,t.ELEMENT_ARRAY_BUFFER,t.STATIC_DRAW),this.setMaxSize(e*r);for(let t=0;t<r;t++)this.addObject(t*i);this.bindBuffer(),this.bufferData()}addObject(t){}}class d{constructor(t,e,i,r=255){this._r=t,this._g=e,this._b=i,this._a=r,this.updateUint()}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0}setRGBA(t,e,i,r){this.r=t,this.g=e,this.b=i,this.a=r,this.updateUint()}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new d(this._r,this._g,this._b,this._a)}equal(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),i=parseInt(t.slice(2,4),16),r=parseInt(t.slice(4,6),16);let s=255;return t.length>=8&&(s=parseInt(t.slice(6,8),16)),new d(e,i,r,s)}add(t){return new d(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new d(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)}divide(t){return t instanceof d?new d(this.r/t.r,this.g/t.g,this.b/t.b,this.a/t.a):new d(this.r/t,this.g/t,this.b/t,this.a/t)}multiply(t){return t instanceof d?new d(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a):new d(this.r*t,this.g*t,this.b*t,this.a*t)}clamp(){this.r=Math.max(0,Math.min(255,this.r)),this.g=Math.max(0,Math.min(255,this.g)),this.b=Math.max(0,Math.min(255,this.b)),this.a=Math.max(0,Math.min(255,this.a))}}d.Red=new d(255,0,0,255),d.Green=new d(0,255,0,255),d.Blue=new d(0,0,255,255),d.Yellow=new d(255,255,0,255),d.Purple=new d(128,0,128,255),d.Orange=new d(255,165,0,255),d.Pink=new d(255,192,203,255),d.Gray=new d(128,128,128,255),d.Brown=new d(139,69,19,255),d.Cyan=new d(0,255,255,255),d.Magenta=new d(255,0,255,255),d.Lime=new d(192,255,0,255),d.White=new d(255,255,255,255),d.Black=new d(0,0,0,255),d.TRANSPARENT=new d(0,0,0,0);class p{constructor(t,e){this.x=void 0!==t?t:0,this.y=void 0!==e?e:0}add(t){return new p(this.x+t.x,this.y+t.y)}subtract(t){return new p(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof p?new p(this.x*t.x,this.y*t.y):new p(this.x*t,this.y*t)}divide(t){return t instanceof p?new p(this.x/t.x,this.y/t.y):new p(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,i=this.y-t.y;return Math.sqrt(e*e+i*i)}clone(){return new p(this.x,this.y)}copy(t){this.x=t.x,this.y=t.y}equal(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}angle(){return Math.atan2(this.y,this.x)}middle(t){return new p((this.x+t.x)/2,(this.y+t.y)/2)}abs(){return new p(Math.abs(this.x),Math.abs(this.y))}floor(){return new p(Math.floor(this.x),Math.floor(this.y))}ceil(){return new p(Math.ceil(this.x),Math.ceil(this.y))}snap(t){return new p(Math.round(this.x/t)*t,Math.round(this.y/t)*t)}stringify(){return`Vec2(${this.x}, ${this.y})`}static FromArray(t){return t.map((t=>new p(t[0],t[1])))}static fromAngle(t){return new p(Math.cos(t),Math.sin(t))}angleBetween(t){const e=this.dot(t),i=this.length()*t.length(),r=Math.max(-1,Math.min(1,e/i));return Math.acos(r)}}p.ZERO=new p(0,0),p.ONE=new p(1,1),p.UP=new p(0,1),p.DOWN=new p(0,-1),p.LEFT=new p(-1,0),p.RIGHT=new p(1,0);class f{static deg2rad(t){return t*(Math.PI/180)}static rad2deg(t){return t/(Math.PI/180)}static normalizeDegrees(t){return(t%360+360)%360}}class m{static float(t,e){return Math.random()*(e-t)+t}static int(t,e){return Math.floor(Math.random()*(e-t+1))+t}static angle(){return Math.random()*Math.PI*2}static vector(t,e,i,r){return new p(m.float(t,e),m.float(i,r))}static direction(t){const e=m.angle();return new p(Math.cos(e)*t,Math.sin(e)*t)}static randomColor(t,e){return new d(m.float(t.r,e.r),m.float(t.g,e.g),m.float(t.b,e.b),m.float(t.a,e.a))}static pick(t){return t[m.int(0,t.length-1)]}static pickWeight(t){if(!t||0===t.length)return null;let e=0;for(const i of t)e+=i[1];const i=Math.random()*e;let r=0;for(const e of t)if(r+=e[1],i<=r)return e[0];return t[t.length-1][0]}static scalarOrRange(t,e){if(void 0===t)return e;if(Array.isArray(t)){if("number"==typeof t[0])return m.float(t[0],t[1]);if(t[0]instanceof p)return m.vector(t[0].x,t[1].x,t[0].y,t[1].y);if(t[0]instanceof d)return m.randomColor(t[0],t[1])}return"number"==typeof t?t:t.clone()}}class g{constructor(t){this.render=t}createLightShadowMaskPolygon(t,e,i){const r=[];t.forEach((t=>{for(let e=0;e<t.length;e++){const i=t[e],s=t[(e+1)%t.length];r.push([i,s])}})),i=i||Math.sqrt(Math.pow(this.render.width,2)+Math.pow(this.render.height,2));const s=[];return r.forEach((([t,r])=>{const n=new p(t.x-e.x,t.y-e.y),a=new p(r.x-e.x,r.y-e.y),h=r.subtract(t).perpendicular(),o=Math.abs(h.dot(n))/(h.length()*n.length())+.01,u=Math.abs(h.dot(a))/(h.length()*a.length())+.01,l=i/o,c=i/u,d=new p(n.x,n.y).normalize(),f=new p(a.x,a.y).normalize(),m=new p(t.x+d.x*l,t.y+d.y*l),g=new p(r.x+f.x*c,r.y+f.y*c);s.push([t,r,g,m])})),s}}const x=(t,e,i,r)=>{const s=[],n=r?Math.atan2(e.y,e.x):Math.atan2(-e.y,-e.x),a=Math.PI;for(let e=0;e<10;e++){const r=n+e/10*a,h=n+(e+1)/10*a,o=Math.cos(r)*i,u=Math.sin(r)*i,l=Math.cos(h)*i,c=Math.sin(h)*i;s.push(t),s.push(t.add(new p(o,u))),s.push(t.add(new p(l,c)))}return s},y=e=>{const i=e.points;if(i.length<2)return{vertices:[],uv:[]};const{normals:r,length:s}=((t,e=!1)=>{const i=[];if(t.length<2||e&&t.length<3)return{normals:i,length:0};const r=t.length;let s=0;if(e)for(let e=0;e<r;e++){const i=t[e],n=t[(e+1)%r];s+=i.distanceTo(n)}else for(let e=0;e<r-1;e++)s+=t[e].distanceTo(t[e+1]);const n=(t,e,i)=>{const r=e.subtract(t).normalize(),s=e.subtract(i).normalize(),n=s.dot(r);if(n<-.999)return{normal:r.perpendicular(),miters:1};{let t=s.add(r).normalize();r.cross(s)<0&&(t=t.multiply(-1));let e=1/Math.sqrt((1-n)/2);return{normal:t,miters:Math.min(e,4)}}};if(e){for(let e=0;e<r-1;e++){const s=0===e?t[r-2]:t[e-1],a=t[e],h=t[e+1];i.push(n(s,a,h))}i.push(i[0])}else for(let e=0;e<r;e++)if(0===e){const e=t[1].subtract(t[0]).normalize();i.push({normal:e.perpendicular(),miters:1})}else if(e===r-1){const r=t[e].subtract(t[e-1]).normalize();i.push({normal:r.perpendicular(),miters:1})}else i.push(n(t[e-1],t[e],t[e+1]));return{normals:i,length:s}})(i,e.closed),n=(e.width||1)/2,a=[],h=[],o=e.roundCap||!1,u=e.textureMode||t.STRETCH;let l=0;const c=e.texture?.width||1;for(let e=0;e<i.length-1;e++){const o=i[e],d=r[e].normal,f=r[e].miters,m=o.add(d.multiply(f*n)),g=o.subtract(d.multiply(f*n)),x=i[e+1],y=r[e+1].normal,T=r[e+1].miters,b=x.add(y.multiply(T*n)),E=x.subtract(y.multiply(T*n)),R=o.distanceTo(x);let w=0,S=0;u===t.STRETCH?(w=l/s,S=(l+R)/s):(w=l/c,S=w+R/c);const A=new p(w,0),v=new p(w,1),M=new p(S,0),U=new p(S,1);a.push(m),h.push(A),a.push(g),h.push(v),a.push(b),h.push(M),a.push(b),h.push(M),a.push(E),h.push(U),a.push(g),h.push(v),l+=R}if(o&&!e.closed){const t=i[0],e=r[0].normal,s=x(t,e,n,!0);a.push(...s);const h=i[i.length-1],o=r[i.length-1].normal,u=x(h,o,n,!1);a.push(...u)}return{vertices:a,uv:h}};var T="precision mediump float;\r\nvarying vec2 vRegion;\r\nvarying vec4 vColor;\r\n\r\nuniform sampler2D uTexture;\r\nuniform int uUseTexture;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n if(uUseTexture > 0){\r\n color = texture2D(uTexture, vRegion) * vColor;\r\n }else{\r\n color = vColor;\r\n }\r\n // fragment\r\n gl_FragColor = color;\r\n}\r\n",b="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec4 aColor;\r\nattribute vec2 aRegion;\r\n\r\nvarying vec4 vColor;\r\nuniform mat4 uProjectionMatrix;\r\nuniform vec4 uColor;\r\nvarying vec2 vRegion;\r\n\r\nvoid main(void) {\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n vColor = aColor;\r\n vRegion = aRegion;\r\n}\r\n";const E=(t,e,i)=>{const r=t.createShader(i);if(!r)throw new Error("Unable to create webgl shader");t.shaderSource(r,e),t.compileShader(r);if(!t.getShaderParameter(r,t.COMPILE_STATUS)){const i=t.getShaderInfoLog(r);throw console.error("Shader compilation failed:",i),new Error("Unable to compile shader: "+i+e)}return r};function R(t,e,i,r=!1,s=!1,n="clamp"){const a=t.createTexture();if(!a)throw new Error("unable to create texture");let h;switch(t.bindTexture(t.TEXTURE_2D,a),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,i?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,i?t.LINEAR:t.NEAREST),n){case"repeat":h=t.REPEAT;break;case"mirror":h=t.MIRRORED_REPEAT;break;default:h=t.CLAMP_TO_EDGE}return t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,h),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,h),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,s),r?t.texImage2D(t.TEXTURE_2D,0,t.RGBA,e.width,e.height,0,t.RGBA,t.UNSIGNED_BYTE,null):t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),a}const w=5126;var S="precision mediump float;\r\nuniform sampler2D uTextures[%TEXTURE_NUM%];\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n %GET_COLOR%\r\n // fragment\r\n gl_FragColor = color * vColor;\r\n}",A="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec2 aRegion;\r\nattribute float aTextureId;\r\nattribute vec4 aColor;\r\n\r\nuniform mat4 uProjectionMatrix;\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vRegion = aRegion;\r\n vTextureId = aTextureId;\r\n vColor = aColor;\r\n\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n}";const v=[{name:"aPosition",size:2,type:w,stride:24},{name:"aRegion",size:2,type:w,stride:24,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:w,stride:24,offset:4*Float32Array.BYTES_PER_ELEMENT},{name:"aColor",size:4,type:5121,stride:24,offset:5*Float32Array.BYTES_PER_ELEMENT,normalized:!0}],M=[{name:"aPosition",size:2,type:w,stride:20},{name:"aColor",size:4,type:5121,stride:20,offset:2*Float32Array.BYTES_PER_ELEMENT,normalized:!0},{name:"aRegion",size:2,type:w,stride:20,offset:3*Float32Array.BYTES_PER_ELEMENT}];class U{constructor(t,e,i,r,s=0){this.attributeLoc={},this.uniformLoc={},this.textureUnitNum=0,this.attributes=[];const n=function(t,e){if(t.includes("%TEXTURE_NUM%")&&(t=t.replace("%TEXTURE_NUM%",e.toString())),t.includes("%GET_COLOR%")){let i="";for(let t=0;t<e;t++)i+=0==t?`if(vTextureId == ${t}.0)`:t==e-1?"else":`else if(vTextureId == ${t}.0)`,i+=`{color = texture2D(uTextures[${t}], vRegion);}`;t=t.replace("%GET_COLOR%",i)}return t}(i,t.maxTextureUnits-s);this.program=((t,e,i)=>{var r=t.createProgram(),s=E(t,e,35633),n=E(t,i,35632);if(!r)throw new Error("Unable to create program shader");if(t.attachShader(r,s),t.attachShader(r,n),t.linkProgram(r),!t.getProgramParameter(r,t.LINK_STATUS)){const e=t.getProgramInfoLog(r);throw new Error("Unable to link shader program: "+e)}return r})(t.gl,e,n),this.gl=t.gl,this.textureUnitNum=s,this.parseShader(e),this.parseShader(n),r&&this.setAttributes(r)}setUniforms(t,e){const i=this.gl;for(const r of t.getUnifromNames()){const s=this.getUniform(r);t.bind(i,r,s,e)}}getUniform(t){return this.uniformLoc[t]}use(){this.gl.useProgram(this.program)}parseShader(t){const e=this.gl,i=t.match(/attribute\s+\w+\s+(\w+)/g);if(i)for(const t of i){const i=t.split(" ")[2],r=e.getAttribLocation(this.program,i);-1!=r&&(this.attributeLoc[i]=r)}const r=t.match(/uniform\s+\w+\s+(\w+)/g);if(r)for(const t of r){const i=t.split(" ")[2];this.uniformLoc[i]=e.getUniformLocation(this.program,i)}}setAttribute(t){const e=this.attributeLoc[t.name];if(void 0!==e){const i=this.gl;i.vertexAttribPointer(e,t.size,t.type,t.normalized||!1,t.stride,t.offset||0),i.enableVertexAttribArray(e)}}setAttributes(t){this.attributes=t;for(const e of t)this.setAttribute(e)}updateAttributes(){this.setAttributes(this.attributes)}static createCostumShader(t,e,i,r,n=0){let a={[s.SPRITE]:S,[s.GRAPHIC]:T}[r],h={[s.SPRITE]:A,[s.GRAPHIC]:b}[r];const o={[s.SPRITE]:v,[s.GRAPHIC]:M}[r];return a=a.replace("void main(void) {",i+"\nvoid main(void) {"),h=h.replace("void main(void) {",e+"\nvoid main(void) {"),a=a.replace("// fragment","fragment(color);"),h=h.replace(/\/\/ vertex s[\s\S]*?\/\/ vertex e/,"vec2 position = aPosition;\n vertex(position, vRegion);\n gl_Position = uProjectionMatrix * vec4(position, 0.0, 1.0);"),new U(t,h,a,o,n)}}class C{constructor(t){this.usedTextures=[],this.shaders=new Map,this.isCostumShader=!1,this.freeTextureUnitNum=0,this.rapid=t,this.gl=t.gl,this.webglArrayBuffer=new u(t.gl,h.Float32,t.gl.ARRAY_BUFFER,t.gl.STREAM_DRAW),this.maxTextureUnits=t.maxTextureUnits}getTextureUnitList(){return Array.from({length:this.maxTextureUnits},((t,e)=>e))}addVertex(t,e,...i){const[r,s]=this.rapid.matrixStack.apply(t,e);this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(s)}useTexture(t){const e=this.usedTextures.indexOf(t);return-1==e?(this.usedTextures.push(t),this.freeTextureUnitNum=this.maxTextureUnits-this.usedTextures.length,[this.usedTextures.length-1,!0]):[e,!1]}enterRegion(t){this.currentShader=t??this.getShader("default"),this.currentShader.use(),this.initializeForNextRender(),this.webglArrayBuffer.bindBuffer(),this.currentShader.updateAttributes(),this.updateProjection(),this.isCostumShader=Boolean(t)}updateProjection(){this.gl.uniformMatrix4fv(this.currentShader.uniformLoc.uProjectionMatrix,!1,this.rapid.projection)}isUnifromChanged(t){return!!t&&(this.costumUnifrom!=t||!!t?.isDirty)}setCurrentUniform(t){t.clearDirty(),this.costumUnifrom=t}exitRegion(){}initDefaultShader(t,e,i){this.setShader("default",t,e,i)}setShader(t,e,i,r){this.webglArrayBuffer.bindBuffer(),this.shaders.set(t,new U(this.rapid,e,i,r)),"default"===t&&(this.defaultShader=this.shaders.get(t))}getShader(t){return this.shaders.get(t)}render(){this.executeRender(),this.initializeForNextRender()}executeRender(){const t=this.gl;for(let e=0;e<this.usedTextures.length;e++)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,this.usedTextures[e]);this.webglArrayBuffer.bufferData()}initializeForNextRender(){this.webglArrayBuffer.clear(),this.usedTextures.length=0,this.isCostumShader=!1,this.freeTextureUnitNum=this.maxTextureUnits}hasPendingContent(){return!1}isShaderChanged(t){return(t||this.defaultShader)!=this.currentShader}}class F extends C{constructor(t){super(t),this.vertex=0,this.offset=p.ZERO,this.drawType=t.gl.TRIANGLE_FAN,this.setShader("default",b,T,M)}startRender(t,e,i,r){r&&this.currentShader?.setUniforms(r,this),this.offset=new p(t,e),this.vertex=0,this.webglArrayBuffer.clear(),i&&i.base&&(this.texture=this.useTexture(i.base.texture)[0])}addVertex(t,e,i,r,s){this.webglArrayBuffer.resize(3),super.addVertex(t+this.offset.x,e+this.offset.y),this.webglArrayBuffer.pushUint32(s),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(r),this.vertex+=1}executeRender(){super.executeRender();const t=this.gl;t.uniform1i(this.currentShader.uniformLoc.uUseTexture,void 0===this.texture?0:1),this.texture&&t.uniform1i(this.currentShader.uniformLoc.uTexture,this.texture),t.drawArrays(this.drawType,0,this.vertex),this.drawType=this.rapid.gl.TRIANGLE_FAN,this.vertex=0,this.texture=void 0}}const _=Math.floor(16384);class N extends c{constructor(t,e){super(t,6,4,e)}addObject(t){super.addObject(),this.pushUint16(t),this.pushUint16(t+1),this.pushUint16(t+2),this.pushUint16(t),this.pushUint16(t+3),this.pushUint16(t+2)}}class P extends C{constructor(t){const e=t.gl;super(t),this.batchSprite=0,this.spriteTextureUnits=[],this.spriteTextureUnitIndexOffset=0,this.setShader("default",A,S,v),this.indexBuffer=new N(e,_)}addVertex(t,e,i,r,s,n){super.addVertex(t,e),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(s),this.webglArrayBuffer.pushUint32(n)}renderSprite(t,e,i,r,s,n,a,h,o,u,l,c,d,p=0){(1+p>this.freeTextureUnitNum||this.batchSprite>=_||this.isUnifromChanged(l)||this.rapid.projectionDirty)&&(this.render(),l&&this.isUnifromChanged(l)&&(this.currentShader.setUniforms(l,this),this.setCurrentUniform(l)),this.rapid.projectionDirty&&this.updateProjection()),this.batchSprite++,this.webglArrayBuffer.resize(20);const[f,m]=this.useTexture(t);m&&(this.spriteTextureUnits.push(f),this.spriteTextureUnitIndexOffset=this.spriteTextureUnits[0]);const g=f-this.spriteTextureUnitIndexOffset,x=c?n:r,y=c?r:n,T=d?a:s,b=d?s:a,E=h,R=h+e,w=o,S=o+i;this.addVertex(E,w,x,T,g,u),this.addVertex(R,w,y,T,g,u),this.addVertex(R,S,y,b,g,u),this.addVertex(E,S,x,b,g,u)}executeRender(){if(super.executeRender(),this.batchSprite<=0)return;const t=this.gl;this.spriteTextureUnits.length>0&&this.gl.uniform1iv(this.currentShader.uniformLoc.uTextures,this.spriteTextureUnits),t.drawElements(t.TRIANGLES,6*this.batchSprite,t.UNSIGNED_SHORT,0)}enterRegion(t){super.enterRegion(t),this.indexBuffer.bindBuffer()}initializeForNextRender(){super.initializeForNextRender(),this.batchSprite=0,this.spriteTextureUnits.length=0}hasPendingContent(){return this.batchSprite>0}}class I{constructor(t,e){this.cache=new Map,this.render=t,this.antialias=e}async textureFromUrl(t,i=this.antialias,r=e.CLAMP){let s=this.cache.get(t);if(!s){const e=await this.loadImage(t);s=B.fromImageSource(this.render,e,i,r),this.cache.set(t,s)}return new D(s)}textureFromFrameBufferObject(t){return new D(t)}async textureFromSource(t,i=this.antialias,r=e.CLAMP){let s=this.cache.get(t);return s||(s=B.fromImageSource(this.render,t,i,r),this.cache.set(t,s)),new D(s)}async loadImage(t){return new Promise((e=>{const i=new Image;i.onload=()=>{e(i)},i.src=t}))}createText(t){return new O(this.render,t)}destroy(t){t instanceof D?(t.base?.destroy(this.render.gl),this.removeCache(t)):(t.destroy(this.render.gl),this.removeCache(t))}createFrameBufferObject(t,e,i=this.antialias){return new k(this.render,t,e,i)}removeCache(t){const e=t instanceof D?t.base?.texture:t.texture;e&&this.cache.forEach(((t,i)=>{t===e&&this.cache.delete(i)}))}}class B{constructor(t,i,r,s=e.CLAMP){this.texture=t,this.width=i,this.height=r,this.wrapMode=s}static fromImageSource(t,i,r=!1,s=e.CLAMP){return new B(R(t.gl,i,r,!1,!1,s),i.width,i.height)}destroy(t){t.deleteTexture(this.texture)}}class D{constructor(t){this.scale=1,this.setBaseTextur(t)}setBaseTextur(t){t&&(this.base=t,this.setClipRegion(0,0,t.width,t.height))}setClipRegion(t,e,i,r){if(this.base)return this.clipX=t/this.base.width,this.clipY=e/this.base.height,this.clipW=this.clipX+i/this.base.width,this.clipH=this.clipY+r/this.base.height,this.width=i*this.scale,this.height=r*this.scale,this}static fromImageSource(t,e,i=!1){return new D(B.fromImageSource(t,e,i))}static fromUrl(t,e){return t.textures.textureFromUrl(e)}createSpritesHeet(t,e){if(!this.base)return[];const i=[],r=Math.floor(this.base.width/t),s=Math.floor(this.base.height/e);for(let n=0;n<s;n++)for(let s=0;s<r;s++){const r=this.clone();r.setClipRegion(s*t,n*e,t,e),i.push(r)}return i}clone(){return new D(this.base)}}const L=2;class O extends D{constructor(t,e){super(),this.scale=.5,this.rapid=t,this.options=e,this.text=e.text||" ",this.updateTextImage()}updateTextImage(){const t=this.createTextCanvas();this.setBaseTextur(B.fromImageSource(this.rapid,t,!0))}createTextCanvas(){const t=document.createElement("canvas"),e=t.getContext("2d");if(!e)throw new Error("Failed to get canvas context");e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";const i=this.text.split("\n");let r=0,s=0;for(const t of i){const i=e.measureText(t);r=Math.max(r,i.width),s+=this.options.fontSize||16}t.width=2*r,t.height=2*s,e.scale(2,2),e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";let n=0;for(const t of i)e.fillText(t,0,n),n+=this.options.fontSize||16;return t}setText(t){this.text!=t&&(this.text=t,this.updateTextImage())}}class k extends B{constructor(t,e,i,r=!1){const s=t.gl,n=R(s,{width:e,height:i},r,!0,!1),a=s.createFramebuffer();if(!a)throw s.deleteTexture(n),new Error("Failed to create WebGL framebuffer");s.bindFramebuffer(s.FRAMEBUFFER,a),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,n,0);const h=s.createRenderbuffer();if(!h)throw s.deleteFramebuffer(a),s.deleteTexture(n),new Error("Failed to create depth-stencil renderbuffer");s.bindRenderbuffer(s.RENDERBUFFER,h),s.renderbufferStorage(s.RENDERBUFFER,s.STENCIL_INDEX8,e,i),s.framebufferRenderbuffer(s.FRAMEBUFFER,s.STENCIL_ATTACHMENT,s.RENDERBUFFER,h),super(n,e,i),this.gl=s,this.framebuffer=a,s.bindTexture(s.TEXTURE_2D,null),s.bindFramebuffer(s.FRAMEBUFFER,null)}bind(){const t=this.gl;t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,this.framebuffer),t.clearColor(.5,.2,.5,.5),t.clear(t.COLOR_BUFFER_BIT)}unbind(){this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}resize(t,e){this.width=t,this.height=e,this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,t,e,0,this.gl.RGBA,this.gl.UNSIGNED_BYTE,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}destroy(t){t.deleteFramebuffer(this.framebuffer),super.destroy(t)}}const G=new Set;class z{constructor(t,e){this.textures=new Map,this.width=t,this.height=e}setTile(t,e){e instanceof D&&(e={texture:e}),this.textures.set(t,e)}getTile(t){return this.textures.get(t)}}class X{constructor(t){this.rapid=t}getYSortRow(t,e,i){if(!t)return[];const r=[];for(const i of t){const t=Math.floor(i.ySort/e);r[t]||(r[t]=[]),r[t].push(i)}return r}getOffset(t){let e=(t.errorX??2)+1,i=(t.errorY??2)+1;if("number"==typeof t.error){const r=(t.error??2)+1;e=r,i=r}else t.error&&(e=t.error.x+1,i=t.error.y+1);return{errorX:e,errorY:i}}getTileData(t,e){const i=e.shape??r.SQUARE,s=t.width,n=i===r.ISOMETRIC?t.height/2:t.height,a=this.rapid.matrixStack,h=a.globalToLocal(p.ZERO),o=a.getGlobalScale(),{errorX:u,errorY:l}=this.getOffset(e),c=Math.ceil(this.rapid.width/s/o.x)+2*u,d=Math.ceil(this.rapid.height/n/o.y)+2*l,f=new p(h.x<0?Math.ceil(h.x/s):Math.floor(h.x/s),h.y<0?Math.ceil(h.y/n):Math.floor(h.y/n));f.x-=u,f.y-=l;let m=new p(0-h.x%s-u*s,0-h.y%n-l*n);return m=m.add(h),{startTile:f,offset:m,viewportWidth:c,viewportHeight:d,height:n,width:s,shape:i}}renderYSortRow(t,e){for(const i of e)i.render?i.render():i.renderSprite&&t.renderSprite(i.renderSprite)}renderLayer(t,e){this.rapid.matrixStack.applyTransform(e);const i=e.tileSet,{startTile:s,offset:n,viewportWidth:a,viewportHeight:h,shape:o,width:u,height:l}=this.getTileData(i,e),c=this.getYSortRow(e.ySortCallback,l,h),d=e.ySortCallback&&e.ySortCallback.length>0;var p;0!==this.rapid.matrixStack.getGlobalRotation()&&(p="TileMapRender: tilemap is not supported rotation",G.has(p)||(G.add(p),console.warn(p)),this.rapid.matrixStack.setGlobalRotation(0));for(let p=0;p<h;p++){const h=p+s.y,f=c[h]??[];if(h<0||h>=t.length)this.renderYSortRow(this.rapid,f);else{for(let c=0;c<a;c++){const a=c+s.x;if(a<0||a>=t[h].length)continue;const d=t[h][a],m=i.getTile(d);if(!m)continue;let g=c*u+n.x,x=p*l+n.y,y=p*l+n.y+(m.ySortOffset??0);h%2!=0&&o===r.ISOMETRIC&&(g+=u/2);const T=e.eachTile&&e.eachTile(d,a,h)||{};f.push({ySort:y,renderSprite:{...m,x:g+(m.x||0),y:x+(m.y||0),...T}})}d&&f.sort(((t,e)=>t.ySort-e.ySort)),this.renderYSortRow(this.rapid,f)}}this.rapid.matrixStack.applyTransform(e)}localToMap(t,e){const i=e.tileSet;if(e.shape===r.ISOMETRIC){let e=0,r=0;const s=i.height/2,n=i.width/2;let a=Math.floor(t.y/s);const h=a%2==0;let o=Math.floor(t.x/n);const u=o%2==0,l=t.x%n/n,c=t.y%s/s,d=c<l,f=c<1-l;return h||(a-=1),d&&!u&&h?a-=1:d||!u||h?f&&u&&h?(o-=2,a-=1):f||u||h||(a+=1):(a+=1,o-=2),e=o,r=a,e=Math.floor(o/2),new p(e,r)}return new p(Math.floor(t.x/i.width),Math.floor(t.y/i.height))}mapToLocal(t,e){const i=e.tileSet;if(e.shape===r.ISOMETRIC){let e=new p(t.x*i.width,t.y*i.height/2);return t.y%2!=0&&(e.x+=i.width/2),e}return new p(t.x*i.width,t.y*i.height)}}const j=!0;class Y{constructor(t,e){this.life=0,this.datas={},this.rapid=t,this.options=e,e.texture instanceof D?this.texture=e.texture:e.texture instanceof Array&&e.texture[0]instanceof Array?this.texture=m.pickWeight(e.texture):e.texture instanceof Array&&(this.texture=m.pick(e.texture)),this.maxLife=m.scalarOrRange(e.life,1),this.datas={speed:this.processAttribute(e.animation.speed,0),rotation:this.processAttribute(e.animation.rotation,0),scale:this.processAttribute(e.animation.scale,1),color:this.processAttribute(e.animation.color,d.White),velocity:this.processAttribute(e.animation.velocity,p.ZERO),acceleration:this.processAttribute(e.animation.acceleration,p.ZERO)},this.position=p.ZERO,this.initializePosition()}processAttribute(t,e){if(!t)return{value:e};if("object"==typeof(i=t)&&null!==i&&Object.getPrototypeOf(i)===Object.prototype){const i=m.scalarOrRange(t.start,e),r=m.scalarOrRange(t.end||i,e);return{delta:t.delta??this.getDelta(i,r,this.maxLife),value:i,damping:t.damping}}return this.processAttribute({start:t},e);var i}updateDamping(t){for(const e of Object.values(this.datas))if(e.damping){const i=e.value,r=Math.pow(e.damping,t);e.value="number"==typeof i?i*r:i.multiply(r)}}updateDelta(t){const e=this.datas;for(const e of Object.values(this.datas))if(e.delta){const i=e.value;"number"==typeof i?e.value+=t*e.delta:e.value=i.add(e.delta.multiply(t))}e.color.value.clamp();const i=p.fromAngle(e.rotation.value).multiply(e.speed.value*t);this.position=this.position.add(i).add(e.velocity.value.multiply(t)).add(e.acceleration.value.multiply(t))}getDelta(t,e,i){return"number"==typeof t&&"number"==typeof e?(e-t)/i:t instanceof p&&e instanceof p||t instanceof d&&e instanceof d?e.subtract(t).divide(i):t}update(t){return this.life+=t,!(this.life>=this.maxLife)&&(this.updateDamping(t),this.updateDelta(t),!0)}render(){this.rapid.renderSprite({...this.options,position:this.position,scale:this.datas.scale.value,rotation:this.datas.rotation.value,color:this.datas.color.value,texture:this.texture})}initializePosition(){switch(this.options.emitShape){case a.POINT:this.position=p.ZERO;break;case a.CIRCLE:const t=Math.random()*Math.PI*2,e=(this.options.emitRadius||0)*Math.sqrt(Math.random());this.position=new p(Math.cos(t)*e,Math.sin(t)*e);break;case a.RECT:this.position=new p((Math.random()-.5)*(this.options.emitRect?.width||0),(Math.random()-.5)*(this.options.emitRect?.height||0))}!this.options.localSpace&&this.options.position&&(this.position=this.position.add(this.options.position))}}class W{constructor(t,e){this.particles=[],this.emitting=!1,this.emitTimer=0,this.emitRate=10,this.emitTime=0,this.emitTimeCounter=0,this.localSpace=j,this.position=p.ZERO,this.rapid=t,this.options=e,this.emitRate=void 0!==e.emitRate?e.emitRate:10,this.emitTime=void 0!==e.emitTime?e.emitTime:0,this.localSpace=void 0!==e.localSpace?e.localSpace:j,this.position=e.position||p.ZERO}getTransform(){return this.options}setEmitRate(t){this.emitRate=t}setEmitTime(t){this.emitTime=t}start(){this.emitting=!0,this.emitTimeCounter=0}stop(){this.emitting=!1}clear(){this.particles=[],this.emitTimeCounter=0}emit(t){const e=Math.min(t,(this.options.maxParticles||1/0)-this.particles.length);for(let t=0;t<e;t++){const t={...this.options},e=new Y(this.rapid,t);this.particles.unshift(e)}}update(t){if(this.emitting&&this.emitRate>0)if(this.emitTime>0){if(this.emitTimeCounter+=t,this.emitTimeCounter>=this.emitTime){const t=Math.floor(this.emitTimeCounter/this.emitTime);this.emit(this.emitRate*t),this.emitTimeCounter-=t*this.emitTime}}else{this.emitTimer+=t;const e=this.emitRate*t,i=Math.floor(e);i>0&&(this.emit(i),this.emitTimer-=i/this.emitRate);this.emitTimer*this.emitRate>=1&&(this.emit(1),this.emitTimer-=1/this.emitRate)}for(let e=this.particles.length-1;e>=0;e--)this.particles[e].update(t)||this.particles.splice(e,1)}render(){for(const t of this.particles)t.render()}getParticleCount(){return this.particles.length}isActive(){return this.emitting||this.particles.length>0}oneShot(){this.emit(this.emitRate)}}class H{constructor(t){this.projectionDirty=!0,this.matrixStack=new l,this.tileMap=new X(this),this.light=new g(this),this.devicePixelRatio=window.devicePixelRatio||1,this.defaultColor=new d(255,255,255,255),this.regions=new Map,this.currentMaskType=[],this.currentTransform=[],this.currentFBO=[],this.lastTime=0;const e=(t=>{const e={stencil:!0},i=t.getContext("webgl2",e)||t.getContext("webgl",e);if(!i)throw new Error("Unable to initialize WebGL. Your browser may not support it.");return i})(t.canvas);this.gl=e,this.canvas=t.canvas,this.textures=new I(this,t.antialias??!1),this.maxTextureUnits=e.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.width=t.width||this.canvas.width,this.height=t.width||this.canvas.height,this.backgroundColor=t.backgroundColor||new d(255,255,255,255),this.registerBuildInRegion(),this.initWebgl(e),this.projectionDirty=!1}renderTileMapLayer(t,e){this.tileMap.renderLayer(t,e instanceof z?{tileSet:e}:e)}initWebgl(t){this.resize(this.width,this.height),t.enable(t.BLEND),t.disable(t.DEPTH_TEST),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.enable(t.STENCIL_TEST),t.enable(t.SCISSOR_TEST)}clearTextureUnit(){for(let t=0;t<this.maxTextureUnits;t++)this.gl.activeTexture(this.gl.TEXTURE0+t),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}registerBuildInRegion(){this.registerRegion("sprite",P),this.registerRegion("graphic",F)}registerRegion(t,e){this.regions.set(t,new e(this))}quitCurrentRegion(){this.currentRegion&&this.currentRegion.hasPendingContent()&&(this.currentRegion.render(),this.currentRegion.exitRegion())}setRegion(t,e){if(t!=this.currentRegionName||this.currentRegion&&this.currentRegion.isShaderChanged(e)){const i=this.regions.get(t);this.quitCurrentRegion(),this.currentRegion=i,this.currentRegionName=t,i.enterRegion(e)}}save(){this.matrixStack.pushMat()}restore(){this.matrixStack.popMat()}withTransform(t){this.save(),t(),this.restore()}startRender(t=!0){this.clear(),t&&this.matrixStack.clear(),this.matrixStack.pushIdentity(),this.currentRegion=void 0,this.currentRegionName=void 0;const e=performance.now(),i=this.lastTime?(e-this.lastTime)/1e3:0;return this.lastTime=e,i}endRender(){this.currentRegion?.render(),this.projectionDirty=!1}render(t){t(this.startRender()),this.endRender()}renderCamera(t){this.matrixStack.applyTransform(t),this.matrixStack.setTransform(this.matrixStack.getInverse())}renderSprite(t){const e=t.texture;if(!e||!e.base)return;const{offsetX:i,offsetY:r}=this.startDraw(t,e.width,e.height);this.setRegion("sprite",t.shader),this.currentRegion.renderSprite(e.base.texture,e.width,e.height,e.clipX,e.clipY,e.clipW,e.clipH,i,r,(t.color||this.defaultColor).uint32,t.uniforms,t.flipX,t.flipY),this.afterDraw()}renderParticles(t){t.localSpace?(this.startDraw(t.getTransform()),t.render(),this.afterDraw()):t.render()}renderTexture(t){t.base&&this.renderSprite({texture:t})}renderLine(t){const e=t.closed?[...t.points,t.points[0]]:t.points,{vertices:i,uv:r}=y({...t,points:e});this.renderGraphic({...t,drawType:this.gl.TRIANGLES,points:i,uv:r})}renderGraphic(t){this.startGraphicDraw(t),t.points.forEach(((e,i)=>{const r=Array.isArray(t.color)?t.color[i]:t.color,s=t.uv?.[i];this.addGraphicVertex(e.x,e.y,s,r)})),this.endGraphicDraw()}startGraphicDraw(t){const{offsetX:e,offsetY:i}=this.startDraw(t);this.setRegion("graphic",t.shader);const r=this.currentRegion;r.startRender(e,i,t.texture,t.uniforms),t.drawType&&(r.drawType=t.drawType)}addGraphicVertex(t,e,i,r){this.currentRegion.addVertex(t,e,i?.x,i?.y,(r||this.defaultColor).uint32)}endGraphicDraw(){this.currentRegion.render(),this.afterDraw()}startDraw(t,e=0,i=0){return this.currentTransform.push(t),this.matrixStack.applyTransform(t,e,i)}afterDraw(){this.currentTransform.length>0&&this.matrixStack.applyTransformAfter(this.currentTransform.pop())}renderRect(t){const{width:e,height:i}=t,r=[new p(0,0),new p(e,0),new p(e,i),new p(0,i)];this.renderGraphic({...t,points:r,drawType:this.gl.TRIANGLE_FAN})}renderCircle(t){const e=t.segments||32,i=t.radius,r=t.color||this.defaultColor,s=[];for(let t=0;t<=e;t++){const r=t/e*Math.PI*2,n=Math.cos(r)*i,a=Math.sin(r)*i;s.push(new p(n,a))}this.renderGraphic({...t,points:s,color:r,drawType:this.gl.TRIANGLE_FAN})}resize(t,e){const i=t*this.devicePixelRatio,r=e*this.devicePixelRatio;this.canvas.width=i,this.canvas.height=r,this.resizeWebglSize(t,e),this.canvas.style.width=t+"px",this.canvas.style.height=e+"px",this.width=t,this.height=e}resizeWebglSize(t,e,i){const r=t*(i||this.devicePixelRatio),s=e*(i||this.devicePixelRatio);this.gl.viewport(0,0,r,s),this.updateProjection(0,t,e,0),this.gl.scissor(0,0,r,s)}updateProjection(t,e,i,r){this.projection=this.createOrthMatrix(t,e,i,r),this.projectionDirty=!0}clear(t){const e=this.gl,i=t||this.backgroundColor;e.clearColor(i.r/255,i.g/255,i.b/255,i.a/255),e.clear(e.COLOR_BUFFER_BIT),this.clearMask()}createOrthMatrix(t,e,i,r){return new Float32Array([2/(e-t),0,0,0,0,2/(r-i),0,0,0,0,-1,0,-(e+t)/(e-t),-(r+i)/(r-i),0,1])}drawMask(t=i.Include,e){this.startDrawMask(t),e(),this.endDrawMask()}startDrawMask(t=i.Include){const e=this.gl;this.currentMaskType.push(t),this.setMaskType(t,!0),e.stencilOp(e.KEEP,e.KEEP,e.REPLACE),e.colorMask(!1,!1,!1,!1)}endDrawMask(){const t=this.gl;this.quitCurrentRegion(),t.stencilOp(t.KEEP,t.KEEP,t.KEEP),t.colorMask(!0,!0,!0,!0),this.setMaskType(this.currentMaskType.pop()??i.Include,!1)}setMaskType(t,e=!1){const r=this.gl;if(this.quitCurrentRegion(),e)this.clearMask(),r.stencilFunc(r.ALWAYS,1,255);else switch(t){case i.Include:r.stencilFunc(r.EQUAL,1,255);break;case i.Exclude:r.stencilFunc(r.NOTEQUAL,1,255)}}clearMask(){const t=this.gl;this.quitCurrentRegion(),t.clearStencil(0),t.clear(t.STENCIL_BUFFER_BIT),t.stencilFunc(t.ALWAYS,1,255)}createCostumShader(t,e,i,r=0){return U.createCostumShader(this,t,e,i,r)}startFBO(t){this.quitCurrentRegion(),t.bind(),this.clearTextureUnit(),this.resizeWebglSize(t.width,t.height,1),this.updateProjection(0,t.width,0,t.height),this.save(),this.matrixStack.identity(),this.currentFBO.push(t)}endFBO(){if(this.currentFBO.length>0){const t=this.currentFBO.pop();this.quitCurrentRegion(),t.unbind(),this.clearTextureUnit(),this.resizeWebglSize(this.width,this.height),this.updateProjection(0,this.width,this.height,0),this.restore()}}drawToFBO(t,e){this.startFBO(t),e(),this.endFBO()}setBlendMode(t){switch(t){case n.Additive:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE);break;case n.Subtractive:this.gl.blendFunc(this.gl.ZERO,this.gl.ONE_MINUS_SRC_COLOR);break;case n.Mix:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE_MINUS_SRC_ALPHA)}}drawLightShadowMask(t){this.startDrawMask(t.type||i.Exclude);this.light.createLightShadowMaskPolygon(t.occlusion,t.lightSource,t.baseProjectionLength).forEach((t=>{this.renderGraphic({points:t,color:d.Black})})),this.endDrawMask()}createParticleEmitter(t){return new W(this,t)}}class q{constructor(t){this.isDirty=!1,this.data=t}setUniform(t,e){this.data[t]!=e&&(this.isDirty=!0),this.data[t]=e}clearDirty(){this.isDirty=!1}getUnifromNames(){return Object.keys(this.data)}bind(t,e,i,r){if(!i)return;const s=this.data[e];if("number"==typeof s)t.uniform1f(i,s);else if(Array.isArray(s))switch(s.length){case 1:Number.isInteger(s[0])?t.uniform1i(i,s[0]):t.uniform1f(i,s[0]);break;case 2:Number.isInteger(s[0])?t.uniform2iv(i,s):t.uniform2fv(i,s);break;case 3:Number.isInteger(s[0])?t.uniform3iv(i,s):t.uniform3fv(i,s);break;case 4:Number.isInteger(s[0])?t.uniform4iv(i,s):t.uniform4fv(i,s);break;case 9:t.uniformMatrix3fv(i,!1,s);break;case 16:t.uniformMatrix4fv(i,!1,s);break;default:console.error(`Unsupported uniform array length for ${e}:`,s.length)}else if("boolean"==typeof s)t.uniform1i(i,s?1:0);else if(s.base?.texture){const e=r.useTexture(s.base.texture)[0];t.uniform1i(i,e)}else console.error(`Unsupported uniform type for ${e}:`,typeof s)}}export{h as ArrayType,B as BaseTexture,n as BlendMode,d as Color,o as DynamicArrayBuffer,k as FrameBufferObject,U as GLShader,t as LineTextureMode,i as MaskType,f as MathUtils,l as MatrixStack,W as ParticleEmitter,a as ParticleShape,m as Random,H as Rapid,L as SCALEFACTOR,s as ShaderType,O as Text,D as Texture,I as TextureCache,e as TextureWrapMode,X as TileMapRender,z as TileSet,r as TilemapShape,q as Uniform,p as Vec2,u as WebglBufferArray,c as WebglElementBufferArray,M as graphicAttributes,v as spriteAttributes};
|
package/dist/rapid.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t,e,r,s,i,n;exports.LineTextureMode=void 0,(t=exports.LineTextureMode||(exports.LineTextureMode={})).STRETCH="stretch",t.REPEAT="repeat",exports.TextureWrapMode=void 0,(e=exports.TextureWrapMode||(exports.TextureWrapMode={})).REPEAT="repeat",e.CLAMP="clamp",e.MIRROR="mirror",exports.MaskType=void 0,(r=exports.MaskType||(exports.MaskType={})).Include="normal",r.Exclude="inverse",exports.TilemapShape=void 0,(s=exports.TilemapShape||(exports.TilemapShape={})).SQUARE="square",s.ISOMETRIC="isometric",exports.ShaderType=void 0,(i=exports.ShaderType||(exports.ShaderType={})).SPRITE="sprite",i.GRAPHIC="graphic",exports.BlendMode=void 0,(n=exports.BlendMode||(exports.BlendMode={})).Additive="additive",n.Subtractive="subtractive",n.Mix="mix";var a;exports.ArrayType=void 0,(a=exports.ArrayType||(exports.ArrayType={}))[a.Float32=0]="Float32",a[a.Uint32=1]="Uint32",a[a.Uint16=2]="Uint16";class o{constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(t){switch(t){case exports.ArrayType.Float32:return Float32Array;case exports.ArrayType.Uint32:return Uint32Array;case exports.ArrayType.Uint16:return Uint16Array}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case exports.ArrayType.Float32:this.typedArray=this.float32;break;case exports.ArrayType.Uint32:this.typedArray=this.uint32;break;case exports.ArrayType.Uint16:this.typedArray=this.uint16}}clear(){this.usedElemNum=0}resize(t=0){if((t+=this.usedElemNum)>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum<<=1;this.setMaxSize(this.maxElemNum)}}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.float32[this.usedElemNum++]=t}pushUint16(t){this.uint16[this.usedElemNum++]=t}pop(t){this.usedElemNum-=t}getArray(t=0,e){return null==e?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.typedArray.length}}class h extends o{constructor(t,e,r=t.ARRAY_BUFFER,s=t.STATIC_DRAW){super(e),this.dirty=!0,this.webglBufferSize=0,this.gl=t,this.buffer=t.createBuffer(),this.type=r,this.usage=s}pushFloat32(t){super.pushFloat32(t),this.dirty=!0}pushUint32(t){super.pushUint32(t),this.dirty=!0}pushUint16(t){super.pushUint16(t),this.dirty=!0}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize?(t.bufferData(this.type,this.getArray(),this.usage),this.webglBufferSize=this.maxElemNum):t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class u extends o{constructor(){super(exports.ArrayType.Float32)}pushMat(){const t=this.usedElemNum-6,e=this.typedArray;this.resize(6),this.pushFloat32(e[t+0]),this.pushFloat32(e[t+1]),this.pushFloat32(e[t+2]),this.pushFloat32(e[t+3]),this.pushFloat32(e[t+4]),this.pushFloat32(e[t+5])}popMat(){this.pop(6)}pushIdentity(){this.resize(6),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0)}translate(t,e){if("number"!=typeof t)return this.translate(t.x,t.y);const r=this.usedElemNum-6,s=this.typedArray;s[r+4]=s[r+0]*t+s[r+2]*e+s[r+4],s[r+5]=s[r+1]*t+s[r+3]*e+s[r+5]}rotate(t){const e=this.usedElemNum-6,r=this.typedArray,s=Math.cos(t),i=Math.sin(t),n=r[e+0],a=r[e+1],o=r[e+2],h=r[e+3];r[e+0]=n*s-a*i,r[e+1]=n*i+a*s,r[e+2]=o*s-h*i,r[e+3]=o*i+h*s}scale(t,e){if("number"!=typeof t)return this.scale(t.x,t.y);e||(e=t);const r=this.usedElemNum-6,s=this.typedArray;s[r+0]=s[r+0]*t,s[r+1]=s[r+1]*t,s[r+2]=s[r+2]*e,s[r+3]=s[r+3]*e}apply(t,e){if("number"!=typeof t)return new d(...this.apply(t.x,t.y));const r=this.usedElemNum-6,s=this.typedArray;return[s[r+0]*t+s[r+2]*e+s[r+4],s[r+1]*t+s[r+3]*e+s[r+5]]}getInverse(){const t=this.usedElemNum-6,e=this.typedArray,r=e[t+0],s=e[t+1],i=e[t+2],n=e[t+3],a=e[t+4],o=e[t+5],h=r*n-s*i;return new Float32Array([n/h,-s/h,-i/h,r/h,(i*o-n*a)/h,(s*a-r*o)/h])}getTransform(){const t=this.usedElemNum-6,e=this.typedArray;return new Float32Array([e[t+0],e[t+1],e[t+2],e[t+3],e[t+4],e[t+5]])}setTransform(t){const e=this.usedElemNum-6,r=this.typedArray;r[e+0]=t[0],r[e+1]=t[1],r[e+2]=t[2],r[e+3]=t[3],r[e+4]=t[4],r[e+5]=t[5]}getGlobalPosition(){const t=this.usedElemNum-6,e=this.typedArray;return new d(e[t+4],e[t+5])}setGlobalPosition(t,e){if("number"!=typeof t)return void this.setGlobalPosition(t.x,t.y);const r=this.usedElemNum-6,s=this.typedArray;s[r+4]=t,s[r+5]=e}getGlobalRotation(){const t=this.usedElemNum-6,e=this.typedArray;return Math.atan2(e[t+1],e[t+0])}setGlobalRotation(t){const e=this.usedElemNum-6,r=this.typedArray,s=this.getGlobalScale(),i=Math.cos(t),n=Math.sin(t);r[e+0]=i*s.x,r[e+1]=n*s.x,r[e+2]=-n*s.y,r[e+3]=i*s.y}getGlobalScale(){const t=this.usedElemNum-6,e=this.typedArray,r=Math.sqrt(e[t+0]*e[t+0]+e[t+1]*e[t+1]),s=Math.sqrt(e[t+2]*e[t+2]+e[t+3]*e[t+3]);return new d(r,s)}setGlobalScale(t,e){if("number"!=typeof t)return void this.setGlobalScale(t.x,t.y);const r=this.getGlobalRotation(),s=Math.cos(r),i=Math.sin(r),n=this.usedElemNum-6,a=this.typedArray;a[n+0]=s*t,a[n+1]=i*t,a[n+2]=-i*e,a[n+3]=s*e}globalToLocal(t){const e=this.getInverse();return new d(e[0]*t.x+e[2]*t.y+e[4],e[1]*t.x+e[3]*t.y+e[5])}localToGlobal(t){return this.apply(t)}toCSSTransform(){const t=this.usedElemNum-6,e=this.typedArray;return`matrix(${e[t+0]}, ${e[t+1]}, ${e[t+2]}, ${e[t+3]}, ${e[t+4]}, ${e[t+5]})`}identity(){const t=this.usedElemNum-6,e=this.typedArray;e[t+0]=1,e[t+1]=0,e[t+2]=0,e[t+3]=1,e[t+4]=0,e[t+5]=0}applyTransform(t,e=0,r=0){(t.saveTransform??1)&&this.pushMat(),t.afterSave&&t.afterSave();const s=t.x||0,i=t.y||0;(s||i)&&this.translate(s,i),t.position&&this.translate(t.position),t.rotation&&this.rotate(t.rotation),t.scale&&this.scale(t.scale);let n=t.offsetX||0,a=t.offsetY||0;t.offset&&(n+=t.offset.x,a+=t.offset.y);const o=t.origin;return o&&("number"==typeof o?(n-=o*e,a-=o*r):(n-=o.x*e,a-=o.y*r)),{offsetX:n,offsetY:a}}applyTransformAfter(t){t.beforRestore&&t.beforRestore(),(t.restoreTransform??1)&&this.popMat()}}class l extends h{constructor(t,e,r,s){super(t,exports.ArrayType.Uint16,t.ELEMENT_ARRAY_BUFFER,t.STATIC_DRAW),this.setMaxSize(e*s);for(let t=0;t<s;t++)this.addObject(t*r);this.bindBuffer(),this.bufferData()}addObject(t){}}class c{constructor(t,e,r,s=255){this._r=t,this._g=e,this._b=r,this._a=s,this.updateUint()}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0}setRGBA(t,e,r,s){this.r=t,this.g=e,this.b=r,this.a=s,this.updateUint()}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new c(this._r,this._g,this._b,this._a)}equal(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),s=parseInt(t.slice(4,6),16);let i=255;return t.length>=8&&(i=parseInt(t.slice(6,8),16)),new c(e,r,s,i)}add(t){return new c(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new c(Math.max(this.r-t.r,0),Math.max(this.g-t.g,0),Math.max(this.b-t.b,0),Math.max(this.a-t.a,0))}}c.Red=new c(255,0,0,255),c.Green=new c(0,255,0,255),c.Blue=new c(0,0,255,255),c.Yellow=new c(255,255,0,255),c.Purple=new c(128,0,128,255),c.Orange=new c(255,165,0,255),c.Pink=new c(255,192,203,255),c.Gray=new c(128,128,128,255),c.Brown=new c(139,69,19,255),c.Cyan=new c(0,255,255,255),c.Magenta=new c(255,0,255,255),c.Lime=new c(192,255,0,255),c.White=new c(255,255,255,255),c.Black=new c(0,0,0,255),c.TRANSPARENT=new c(0,0,0,0);class d{constructor(t,e){this.x=void 0!==t?t:0,this.y=void 0!==e?e:0}add(t){return new d(this.x+t.x,this.y+t.y)}subtract(t){return new d(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof d?new d(this.x*t.x,this.y*t.y):new d(this.x*t,this.y*t)}divide(t){return t instanceof d?new d(this.x/t.x,this.y/t.y):new d(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,r=this.y-t.y;return Math.sqrt(e*e+r*r)}clone(){return new d(this.x,this.y)}copy(t){this.x=t.x,this.y=t.y}equal(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}angle(){return Math.atan2(this.y,this.x)}middle(t){return new d((this.x+t.x)/2,(this.y+t.y)/2)}abs(){return new d(Math.abs(this.x),Math.abs(this.y))}floor(){return new d(Math.floor(this.x),Math.floor(this.y))}ceil(){return new d(Math.ceil(this.x),Math.ceil(this.y))}snap(t){return new d(Math.round(this.x/t)*t,Math.round(this.y/t)*t)}stringify(){return`Vec2(${this.x}, ${this.y})`}static FromArray(t){return t.map((t=>new d(t[0],t[1])))}angleBetween(t){const e=this.dot(t),r=this.length()*t.length(),s=Math.max(-1,Math.min(1,e/r));return Math.acos(s)}}d.ZERO=new d(0,0),d.ONE=new d(1,1),d.UP=new d(0,1),d.DOWN=new d(0,-1),d.LEFT=new d(-1,0),d.RIGHT=new d(1,0);class p{constructor(t){this.render=t}createLightShadowMaskPolygon(t,e,r){const s=[];t.forEach((t=>{for(let e=0;e<t.length;e++){const r=t[e],i=t[(e+1)%t.length];s.push([r,i])}})),r=r||Math.sqrt(Math.pow(this.render.width,2)+Math.pow(this.render.height,2));const i=[];return s.forEach((([t,s])=>{const n=new d(t.x-e.x,t.y-e.y),a=new d(s.x-e.x,s.y-e.y),o=s.subtract(t).perpendicular(),h=Math.abs(o.dot(n))/(o.length()*n.length())+.01,u=Math.abs(o.dot(a))/(o.length()*a.length())+.01,l=r/h,c=r/u,p=new d(n.x,n.y).normalize(),f=new d(a.x,a.y).normalize(),x=new d(t.x+p.x*l,t.y+p.y*l),g=new d(s.x+f.x*c,s.y+f.y*c);i.push([t,s,g,x])})),i}}const f=(t,e,r,s)=>{const i=[],n=s?Math.atan2(e.y,e.x):Math.atan2(-e.y,-e.x),a=Math.PI;for(let e=0;e<10;e++){const s=n+e/10*a,o=n+(e+1)/10*a,h=Math.cos(s)*r,u=Math.sin(s)*r,l=Math.cos(o)*r,c=Math.sin(o)*r;i.push(t),i.push(t.add(new d(h,u))),i.push(t.add(new d(l,c)))}return i},x=t=>{const e=t.points;if(e.length<2)return{vertices:[],uv:[]};const{normals:r,length:s}=((t,e=!1)=>{const r=[];if(t.length<2||e&&t.length<3)return{normals:r,length:0};const s=t.length;let i=0;if(e)for(let e=0;e<s;e++){const r=t[e],n=t[(e+1)%s];i+=r.distanceTo(n)}else for(let e=0;e<s-1;e++)i+=t[e].distanceTo(t[e+1]);const n=(t,e,r)=>{const s=e.subtract(t).normalize(),i=e.subtract(r).normalize(),n=i.dot(s);if(n<-.999)return{normal:s.perpendicular(),miters:1};{let t=i.add(s).normalize();s.cross(i)<0&&(t=t.multiply(-1));let e=1/Math.sqrt((1-n)/2);return{normal:t,miters:Math.min(e,4)}}};if(e){for(let e=0;e<s-1;e++){const i=0===e?t[s-2]:t[e-1],a=t[e],o=t[e+1];r.push(n(i,a,o))}r.push(r[0])}else for(let e=0;e<s;e++)if(0===e){const e=t[1].subtract(t[0]).normalize();r.push({normal:e.perpendicular(),miters:1})}else if(e===s-1){const s=t[e].subtract(t[e-1]).normalize();r.push({normal:s.perpendicular(),miters:1})}else r.push(n(t[e-1],t[e],t[e+1]));return{normals:r,length:i}})(e,t.closed),i=(t.width||1)/2,n=[],a=[],o=t.roundCap||!1,h=t.textureMode||exports.LineTextureMode.STRETCH;let u=0;const l=t.texture?.width||1;for(let t=0;t<e.length-1;t++){const o=e[t],c=r[t].normal,p=r[t].miters,f=o.add(c.multiply(p*i)),x=o.subtract(c.multiply(p*i)),g=e[t+1],m=r[t+1].normal,y=r[t+1].miters,T=g.add(m.multiply(y*i)),E=g.subtract(m.multiply(y*i)),b=o.distanceTo(g);let w=0,R=0;h===exports.LineTextureMode.STRETCH?(w=u/s,R=(u+b)/s):(w=u/l,R=w+b/l);const S=new d(w,0),A=new d(w,1),M=new d(R,0),v=new d(R,1);n.push(f),a.push(S),n.push(x),a.push(A),n.push(T),a.push(M),n.push(T),a.push(M),n.push(E),a.push(v),n.push(x),a.push(A),u+=b}if(o&&!t.closed){const t=e[0],s=r[0].normal,a=f(t,s,i,!0);n.push(...a);const o=e[e.length-1],h=r[e.length-1].normal,u=f(o,h,i,!1);n.push(...u)}return{vertices:n,uv:a}};var g="precision mediump float;\r\nvarying vec2 vRegion;\r\nvarying vec4 vColor;\r\n\r\nuniform sampler2D uTexture;\r\nuniform int uUseTexture;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n if(uUseTexture > 0){\r\n color = texture2D(uTexture, vRegion) * vColor;\r\n }else{\r\n color = vColor;\r\n }\r\n // fragment\r\n gl_FragColor = color;\r\n}\r\n",m="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec4 aColor;\r\nattribute vec2 aRegion;\r\n\r\nvarying vec4 vColor;\r\nuniform mat4 uProjectionMatrix;\r\nuniform vec4 uColor;\r\nvarying vec2 vRegion;\r\n\r\nvoid main(void) {\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n vColor = aColor;\r\n vRegion = aRegion;\r\n}\r\n";const y=(t,e,r)=>{const s=t.createShader(r);if(!s)throw new Error("Unable to create webgl shader");t.shaderSource(s,e),t.compileShader(s);if(!t.getShaderParameter(s,t.COMPILE_STATUS)){const r=t.getShaderInfoLog(s);throw console.error("Shader compilation failed:",r),new Error("Unable to compile shader: "+r+e)}return s};function T(t,e,r,s=!1,i=!1,n="clamp"){const a=t.createTexture();if(!a)throw new Error("unable to create texture");let o;switch(t.bindTexture(t.TEXTURE_2D,a),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,r?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,r?t.LINEAR:t.NEAREST),n){case"repeat":o=t.REPEAT;break;case"mirror":o=t.MIRRORED_REPEAT;break;default:o=t.CLAMP_TO_EDGE}return t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,o),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,o),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,i),s?t.texImage2D(t.TEXTURE_2D,0,t.RGBA,e.width,e.height,0,t.RGBA,t.UNSIGNED_BYTE,null):t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),a}const E=5126;var b="precision mediump float;\r\nuniform sampler2D uTextures[%TEXTURE_NUM%];\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n %GET_COLOR%\r\n // fragment\r\n gl_FragColor = color * vColor;\r\n}",w="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec2 aRegion;\r\nattribute float aTextureId;\r\nattribute vec4 aColor;\r\n\r\nuniform mat4 uProjectionMatrix;\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vRegion = aRegion;\r\n vTextureId = aTextureId;\r\n vColor = aColor;\r\n\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n}";const R=[{name:"aPosition",size:2,type:E,stride:24},{name:"aRegion",size:2,type:E,stride:24,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:E,stride:24,offset:4*Float32Array.BYTES_PER_ELEMENT},{name:"aColor",size:4,type:5121,stride:24,offset:5*Float32Array.BYTES_PER_ELEMENT,normalized:!0}],S=[{name:"aPosition",size:2,type:E,stride:20},{name:"aColor",size:4,type:5121,stride:20,offset:2*Float32Array.BYTES_PER_ELEMENT,normalized:!0},{name:"aRegion",size:2,type:E,stride:20,offset:3*Float32Array.BYTES_PER_ELEMENT}];class A{constructor(t,e,r,s,i=0){this.attributeLoc={},this.uniformLoc={},this.textureUnitNum=0,this.attributes=[];const n=function(t,e){if(t.includes("%TEXTURE_NUM%")&&(t=t.replace("%TEXTURE_NUM%",e.toString())),t.includes("%GET_COLOR%")){let r="";for(let t=0;t<e;t++)r+=0==t?`if(vTextureId == ${t}.0)`:t==e-1?"else":`else if(vTextureId == ${t}.0)`,r+=`{color = texture2D(uTextures[${t}], vRegion);}`;t=t.replace("%GET_COLOR%",r)}return t}(r,t.maxTextureUnits-i);this.program=((t,e,r)=>{var s=t.createProgram(),i=y(t,e,35633),n=y(t,r,35632);if(!s)throw new Error("Unable to create program shader");if(t.attachShader(s,i),t.attachShader(s,n),t.linkProgram(s),!t.getProgramParameter(s,t.LINK_STATUS)){const e=t.getProgramInfoLog(s);throw new Error("Unable to link shader program: "+e)}return s})(t.gl,e,n),this.gl=t.gl,this.textureUnitNum=i,this.parseShader(e),this.parseShader(n),s&&this.setAttributes(s)}setUniforms(t,e){const r=this.gl;for(const s of t.getUnifromNames()){const i=this.getUniform(s);t.bind(r,s,i,e)}}getUniform(t){return this.uniformLoc[t]}use(){this.gl.useProgram(this.program)}parseShader(t){const e=this.gl,r=t.match(/attribute\s+\w+\s+(\w+)/g);if(r)for(const t of r){const r=t.split(" ")[2],s=e.getAttribLocation(this.program,r);-1!=s&&(this.attributeLoc[r]=s)}const s=t.match(/uniform\s+\w+\s+(\w+)/g);if(s)for(const t of s){const r=t.split(" ")[2];this.uniformLoc[r]=e.getUniformLocation(this.program,r)}}setAttribute(t){const e=this.attributeLoc[t.name];if(void 0!==e){const r=this.gl;r.vertexAttribPointer(e,t.size,t.type,t.normalized||!1,t.stride,t.offset||0),r.enableVertexAttribArray(e)}}setAttributes(t){this.attributes=t;for(const e of t)this.setAttribute(e)}updateAttributes(){this.setAttributes(this.attributes)}static createCostumShader(t,e,r,s,i=0){let n={[exports.ShaderType.SPRITE]:b,[exports.ShaderType.GRAPHIC]:g}[s],a={[exports.ShaderType.SPRITE]:w,[exports.ShaderType.GRAPHIC]:m}[s];const o={[exports.ShaderType.SPRITE]:R,[exports.ShaderType.GRAPHIC]:S}[s];return n=n.replace("void main(void) {",r+"\nvoid main(void) {"),a=a.replace("void main(void) {",e+"\nvoid main(void) {"),n=n.replace("// fragment","fragment(color);"),a=a.replace(/\/\/ vertex s[\s\S]*?\/\/ vertex e/,"vec2 position = aPosition;\n vertex(position, vRegion);\n gl_Position = uProjectionMatrix * vec4(position, 0.0, 1.0);"),new A(t,a,n,o,i)}}class M{constructor(t){this.usedTextures=[],this.shaders=new Map,this.isCostumShader=!1,this.freeTextureUnitNum=0,this.rapid=t,this.gl=t.gl,this.webglArrayBuffer=new h(t.gl,exports.ArrayType.Float32,t.gl.ARRAY_BUFFER,t.gl.STREAM_DRAW),this.maxTextureUnits=t.maxTextureUnits}getTextureUnitList(){return Array.from({length:this.maxTextureUnits},((t,e)=>e))}addVertex(t,e,...r){const[s,i]=this.rapid.matrixStack.apply(t,e);this.webglArrayBuffer.pushFloat32(s),this.webglArrayBuffer.pushFloat32(i)}useTexture(t){const e=this.usedTextures.indexOf(t);return-1==e?(this.usedTextures.push(t),this.freeTextureUnitNum=this.maxTextureUnits-this.usedTextures.length,[this.usedTextures.length-1,!0]):[e,!1]}enterRegion(t){this.currentShader=t??this.getShader("default"),this.currentShader.use(),this.initializeForNextRender(),this.webglArrayBuffer.bindBuffer(),this.currentShader.updateAttributes(),this.updateProjection(),this.isCostumShader=Boolean(t)}updateProjection(){this.gl.uniformMatrix4fv(this.currentShader.uniformLoc.uProjectionMatrix,!1,this.rapid.projection)}isUnifromChanged(t){return!!t&&(this.costumUnifrom!=t||!!t?.isDirty)}setCurrentUniform(t){t.clearDirty(),this.costumUnifrom=t}exitRegion(){}initDefaultShader(t,e,r){this.setShader("default",t,e,r)}setShader(t,e,r,s){this.webglArrayBuffer.bindBuffer(),this.shaders.set(t,new A(this.rapid,e,r,s)),"default"===t&&(this.defaultShader=this.shaders.get(t))}getShader(t){return this.shaders.get(t)}render(){this.executeRender(),this.initializeForNextRender()}executeRender(){const t=this.gl;for(let e=0;e<this.usedTextures.length;e++)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,this.usedTextures[e]);this.webglArrayBuffer.bufferData()}initializeForNextRender(){this.webglArrayBuffer.clear(),this.usedTextures.length=0,this.isCostumShader=!1,this.freeTextureUnitNum=this.maxTextureUnits}hasPendingContent(){return!1}isShaderChanged(t){return(t||this.defaultShader)!=this.currentShader}}class v extends M{constructor(t){super(t),this.vertex=0,this.offset=d.ZERO,this.drawType=t.gl.TRIANGLE_FAN,this.setShader("default",m,g,S)}startRender(t,e,r,s){s&&this.currentShader?.setUniforms(s,this),this.offset=new d(t,e),this.vertex=0,this.webglArrayBuffer.clear(),r&&r.base&&(this.texture=this.useTexture(r.base.texture)[0])}addVertex(t,e,r,s,i){this.webglArrayBuffer.resize(3),super.addVertex(t+this.offset.x,e+this.offset.y),this.webglArrayBuffer.pushUint32(i),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(s),this.vertex+=1}executeRender(){super.executeRender();const t=this.gl;t.uniform1i(this.currentShader.uniformLoc.uUseTexture,void 0===this.texture?0:1),this.texture&&t.uniform1i(this.currentShader.uniformLoc.uTexture,this.texture),t.drawArrays(this.drawType,0,this.vertex),this.drawType=this.rapid.gl.TRIANGLE_FAN,this.vertex=0,this.texture=void 0}}const U=Math.floor(16384);class F extends l{constructor(t,e){super(t,6,4,e)}addObject(t){super.addObject(),this.pushUint16(t),this.pushUint16(t+1),this.pushUint16(t+2),this.pushUint16(t),this.pushUint16(t+3),this.pushUint16(t+2)}}class _ extends M{constructor(t){const e=t.gl;super(t),this.batchSprite=0,this.spriteTextureUnits=[],this.spriteTextureUnitIndexOffset=0,this.setShader("default",w,b,R),this.indexBuffer=new F(e,U)}addVertex(t,e,r,s,i,n){super.addVertex(t,e),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(s),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushUint32(n)}renderSprite(t,e,r,s,i,n,a,o,h,u,l,c,d,p=0){(1+p>this.freeTextureUnitNum||this.batchSprite>=U||this.isUnifromChanged(l)||this.rapid.projectionDirty)&&(this.render(),l&&this.isUnifromChanged(l)&&(this.currentShader.setUniforms(l,this),this.setCurrentUniform(l)),this.rapid.projectionDirty&&this.updateProjection()),this.batchSprite++,this.webglArrayBuffer.resize(20);const[f,x]=this.useTexture(t);x&&(this.spriteTextureUnits.push(f),this.spriteTextureUnitIndexOffset=this.spriteTextureUnits[0]);const g=f-this.spriteTextureUnitIndexOffset,m=c?n:s,y=c?s:n,T=d?a:i,E=d?i:a,b=o,w=o+e,R=h,S=h+r;this.addVertex(b,R,m,T,g,u),this.addVertex(w,R,y,T,g,u),this.addVertex(w,S,y,E,g,u),this.addVertex(b,S,m,E,g,u)}executeRender(){if(super.executeRender(),this.batchSprite<=0)return;const t=this.gl;this.spriteTextureUnits.length>0&&this.gl.uniform1iv(this.currentShader.uniformLoc.uTextures,this.spriteTextureUnits),t.drawElements(t.TRIANGLES,6*this.batchSprite,t.UNSIGNED_SHORT,0)}enterRegion(t){super.enterRegion(t),this.indexBuffer.bindBuffer()}initializeForNextRender(){super.initializeForNextRender(),this.batchSprite=0,this.spriteTextureUnits.length=0}hasPendingContent(){return this.batchSprite>0}}class C{constructor(t,e){this.cache=new Map,this.render=t,this.antialias=e}async textureFromUrl(t,e=this.antialias,r=exports.TextureWrapMode.CLAMP){let s=this.cache.get(t);if(!s){const i=await this.loadImage(t);s=N.fromImageSource(this.render,i,e,r),this.cache.set(t,s)}return new B(s)}textureFromFrameBufferObject(t){return new B(t)}async textureFromSource(t,e=this.antialias,r=exports.TextureWrapMode.CLAMP){let s=this.cache.get(t);return s||(s=N.fromImageSource(this.render,t,e,r),this.cache.set(t,s)),new B(s)}async loadImage(t){return new Promise((e=>{const r=new Image;r.onload=()=>{e(r)},r.src=t}))}createText(t){return new I(this.render,t)}destroy(t){t instanceof B?(t.base?.destroy(this.render.gl),this.removeCache(t)):(t.destroy(this.render.gl),this.removeCache(t))}createFrameBufferObject(t,e,r=this.antialias){return new P(this.render,t,e,r)}removeCache(t){const e=t instanceof B?t.base?.texture:t.texture;e&&this.cache.forEach(((t,r)=>{t===e&&this.cache.delete(r)}))}}class N{constructor(t,e,r,s=exports.TextureWrapMode.CLAMP){this.texture=t,this.width=e,this.height=r,this.wrapMode=s}static fromImageSource(t,e,r=!1,s=exports.TextureWrapMode.CLAMP){return new N(T(t.gl,e,r,!1,!1,s),e.width,e.height)}destroy(t){t.deleteTexture(this.texture)}}class B{constructor(t){this.scale=1,this.setBaseTextur(t)}setBaseTextur(t){t&&(this.base=t,this.setClipRegion(0,0,t.width,t.height))}setClipRegion(t,e,r,s){if(this.base)return this.clipX=t/this.base.width,this.clipY=e/this.base.height,this.clipW=this.clipX+r/this.base.width,this.clipH=this.clipY+s/this.base.height,this.width=r*this.scale,this.height=s*this.scale,this}static fromImageSource(t,e,r=!1){return new B(N.fromImageSource(t,e,r))}static fromUrl(t,e){return t.textures.textureFromUrl(e)}createSpritesHeet(t,e){if(!this.base)return[];const r=[],s=Math.floor(this.base.width/t),i=Math.floor(this.base.height/e);for(let n=0;n<i;n++)for(let i=0;i<s;i++){const s=this.clone();s.setClipRegion(i*t,n*e,t,e),r.push(s)}return r}clone(){return new B(this.base)}}class I extends B{constructor(t,e){super(),this.scale=.5,this.rapid=t,this.options=e,this.text=e.text||" ",this.updateTextImage()}updateTextImage(){const t=this.createTextCanvas();this.setBaseTextur(N.fromImageSource(this.rapid,t,!0))}createTextCanvas(){const t=document.createElement("canvas"),e=t.getContext("2d");if(!e)throw new Error("Failed to get canvas context");e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";const r=this.text.split("\n");let s=0,i=0;for(const t of r){const r=e.measureText(t);s=Math.max(s,r.width),i+=this.options.fontSize||16}t.width=2*s,t.height=2*i,e.scale(2,2),e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";let n=0;for(const t of r)e.fillText(t,0,n),n+=this.options.fontSize||16;return t}setText(t){this.text!=t&&(this.text=t,this.updateTextImage())}}class P extends N{constructor(t,e,r,s=!1){const i=t.gl,n=T(i,{width:e,height:r},s,!0,!1),a=i.createFramebuffer();if(!a)throw i.deleteTexture(n),new Error("Failed to create WebGL framebuffer");i.bindFramebuffer(i.FRAMEBUFFER,a),i.framebufferTexture2D(i.FRAMEBUFFER,i.COLOR_ATTACHMENT0,i.TEXTURE_2D,n,0);const o=i.createRenderbuffer();if(!o)throw i.deleteFramebuffer(a),i.deleteTexture(n),new Error("Failed to create depth-stencil renderbuffer");i.bindRenderbuffer(i.RENDERBUFFER,o),i.renderbufferStorage(i.RENDERBUFFER,i.STENCIL_INDEX8,e,r),i.framebufferRenderbuffer(i.FRAMEBUFFER,i.STENCIL_ATTACHMENT,i.RENDERBUFFER,o),super(n,e,r),this.gl=i,this.framebuffer=a,i.bindTexture(i.TEXTURE_2D,null),i.bindFramebuffer(i.FRAMEBUFFER,null)}bind(){const t=this.gl;t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,this.framebuffer),t.clearColor(.5,.2,.5,.5),t.clear(t.COLOR_BUFFER_BIT)}unbind(){this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}resize(t,e){this.width=t,this.height=e,this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,t,e,0,this.gl.RGBA,this.gl.UNSIGNED_BYTE,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}destroy(t){t.deleteFramebuffer(this.framebuffer),super.destroy(t)}}const L=new Set;class D{constructor(t,e){this.textures=new Map,this.width=t,this.height=e}setTile(t,e){e instanceof B&&(e={texture:e}),this.textures.set(t,e)}getTile(t){return this.textures.get(t)}}class k{constructor(t){this.rapid=t}getYSortRow(t,e,r){if(!t)return[];const s=[];for(const r of t){const t=Math.floor(r.ySort/e);s[t]||(s[t]=[]),s[t].push(r)}return s}getOffset(t){let e=(t.errorX??2)+1,r=(t.errorY??2)+1;if("number"==typeof t.error){const s=(t.error??2)+1;e=s,r=s}else t.error&&(e=t.error.x+1,r=t.error.y+1);return{errorX:e,errorY:r}}getTileData(t,e){const r=e.shape??exports.TilemapShape.SQUARE,s=t.width,i=r===exports.TilemapShape.ISOMETRIC?t.height/2:t.height,n=this.rapid.matrixStack,a=n.globalToLocal(d.ZERO),o=n.getGlobalScale(),{errorX:h,errorY:u}=this.getOffset(e),l=Math.ceil(this.rapid.width/s/o.x)+2*h,c=Math.ceil(this.rapid.height/i/o.y)+2*u,p=new d(a.x<0?Math.ceil(a.x/s):Math.floor(a.x/s),a.y<0?Math.ceil(a.y/i):Math.floor(a.y/i));p.x-=h,p.y-=u;let f=new d(0-a.x%s-h*s,0-a.y%i-u*i);return f=f.add(a),{startTile:p,offset:f,viewportWidth:l,viewportHeight:c,height:i,width:s,shape:r}}renderYSortRow(t,e){for(const r of e)r.render?r.render():r.renderSprite&&t.renderSprite(r.renderSprite)}renderLayer(t,e){this.rapid.matrixStack.applyTransform(e);const r=e.tileSet,{startTile:s,offset:i,viewportWidth:n,viewportHeight:a,shape:o,width:h,height:u}=this.getTileData(r,e),l=this.getYSortRow(e.ySortCallback,u,a),c=e.ySortCallback&&e.ySortCallback.length>0;var d;0!==this.rapid.matrixStack.getGlobalRotation()&&(d="TileMapRender: tilemap is not supported rotation",L.has(d)||(L.add(d),console.warn(d)),this.rapid.matrixStack.setGlobalRotation(0));for(let d=0;d<a;d++){const a=d+s.y,p=l[a]??[];if(a<0||a>=t.length)this.renderYSortRow(this.rapid,p);else{for(let l=0;l<n;l++){const n=l+s.x;if(n<0||n>=t[a].length)continue;const c=t[a][n],f=r.getTile(c);if(!f)continue;let x=l*h+i.x,g=d*u+i.y,m=d*u+i.y+(f.ySortOffset??0);a%2!=0&&o===exports.TilemapShape.ISOMETRIC&&(x+=h/2);const y=e.eachTile&&e.eachTile(c,n,a)||{};p.push({ySort:m,renderSprite:{...f,x:x+(f.x||0),y:g+(f.y||0),...y}})}c&&p.sort(((t,e)=>t.ySort-e.ySort)),this.renderYSortRow(this.rapid,p)}}this.rapid.matrixStack.applyTransform(e)}localToMap(t,e){const r=e.tileSet;if(e.shape===exports.TilemapShape.ISOMETRIC){let e=0,s=0;const i=r.height/2,n=r.width/2;let a=Math.floor(t.y/i);const o=a%2==0;let h=Math.floor(t.x/n);const u=h%2==0,l=t.x%n/n,c=t.y%i/i,p=c<l,f=c<1-l;return o||(a-=1),p&&!u&&o?a-=1:p||!u||o?f&&u&&o?(h-=2,a-=1):f||u||o||(a+=1):(a+=1,h-=2),e=h,s=a,e=Math.floor(h/2),new d(e,s)}return new d(Math.floor(t.x/r.width),Math.floor(t.y/r.height))}mapToLocal(t,e){const r=e.tileSet;if(e.shape===exports.TilemapShape.ISOMETRIC){let e=new d(t.x*r.width,t.y*r.height/2);return t.y%2!=0&&(e.x+=r.width/2),e}return new d(t.x*r.width,t.y*r.height)}}exports.BaseTexture=N,exports.Color=c,exports.DynamicArrayBuffer=o,exports.FrameBufferObject=P,exports.GLShader=A,exports.MathUtils=class{static deg2rad(t){return t*(Math.PI/180)}static rad2deg(t){return t/(Math.PI/180)}static normalizeDegrees(t){return(t%360+360)%360}},exports.MatrixStack=u,exports.Rapid=class{constructor(t){this.projectionDirty=!0,this.matrixStack=new u,this.tileMap=new k(this),this.light=new p(this),this.devicePixelRatio=window.devicePixelRatio||1,this.defaultColor=new c(255,255,255,255),this.regions=new Map,this.currentMaskType=[],this.currentTransform=[],this.currentFBO=[];const e=(t=>{const e={stencil:!0},r=t.getContext("webgl2",e)||t.getContext("webgl",e);if(!r)throw new Error("Unable to initialize WebGL. Your browser may not support it.");return r})(t.canvas);this.gl=e,this.canvas=t.canvas,this.textures=new C(this,t.antialias??!1),this.maxTextureUnits=e.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.width=t.width||this.canvas.width,this.height=t.width||this.canvas.height,this.backgroundColor=t.backgroundColor||new c(255,255,255,255),this.registerBuildInRegion(),this.initWebgl(e),this.projectionDirty=!1}renderTileMapLayer(t,e){this.tileMap.renderLayer(t,e instanceof D?{tileSet:e}:e)}initWebgl(t){this.resize(this.width,this.height),t.enable(t.BLEND),t.disable(t.DEPTH_TEST),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.enable(t.STENCIL_TEST),t.enable(t.SCISSOR_TEST)}clearTextureUnit(){for(let t=0;t<this.maxTextureUnits;t++)this.gl.activeTexture(this.gl.TEXTURE0+t),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}registerBuildInRegion(){this.registerRegion("sprite",_),this.registerRegion("graphic",v)}registerRegion(t,e){this.regions.set(t,new e(this))}quitCurrentRegion(){this.currentRegion&&this.currentRegion.hasPendingContent()&&(this.currentRegion.render(),this.currentRegion.exitRegion())}setRegion(t,e){if(t!=this.currentRegionName||this.currentRegion&&this.currentRegion.isShaderChanged(e)){const r=this.regions.get(t);this.quitCurrentRegion(),this.currentRegion=r,this.currentRegionName=t,r.enterRegion(e)}}save(){this.matrixStack.pushMat()}restore(){this.matrixStack.popMat()}withTransform(t){this.save(),t(),this.restore()}startRender(t=!0){this.clear(),t&&this.matrixStack.clear(),this.matrixStack.pushIdentity(),this.currentRegion=void 0,this.currentRegionName=void 0}endRender(){this.currentRegion?.render(),this.projectionDirty=!1}render(t){this.startRender(),t(),this.endRender()}renderSprite(t){const e=t.texture;if(!e||!e.base)return;const{offsetX:r,offsetY:s}=this.startDraw(t,e.width,e.height);this.setRegion("sprite",t.shader),this.currentRegion.renderSprite(e.base.texture,e.width,e.height,e.clipX,e.clipY,e.clipW,e.clipH,r,s,(t.color||this.defaultColor).uint32,t.uniforms,t.flipX,t.flipY),this.afterDraw()}renderTexture(t){t.base&&this.renderSprite({texture:t})}renderLine(t){const e=t.closed?[...t.points,t.points[0]]:t.points,{vertices:r,uv:s}=x({...t,points:e});this.renderGraphic({...t,drawType:this.gl.TRIANGLES,points:r,uv:s})}renderGraphic(t){this.startGraphicDraw(t),t.points.forEach(((e,r)=>{const s=Array.isArray(t.color)?t.color[r]:t.color,i=t.uv?.[r];this.addGraphicVertex(e.x,e.y,i,s)})),this.endGraphicDraw()}startGraphicDraw(t){const{offsetX:e,offsetY:r}=this.startDraw(t);this.setRegion("graphic",t.shader);const s=this.currentRegion;s.startRender(e,r,t.texture,t.uniforms),t.drawType&&(s.drawType=t.drawType)}addGraphicVertex(t,e,r,s){this.currentRegion.addVertex(t,e,r?.x,r?.y,(s||this.defaultColor).uint32)}endGraphicDraw(){this.currentRegion.render(),this.afterDraw()}startDraw(t,e=0,r=0){return this.currentTransform.push(t),this.matrixStack.applyTransform(t,e,r)}afterDraw(){this.currentTransform.length>0&&this.matrixStack.applyTransformAfter(this.currentTransform.pop())}renderRect(t){const{width:e,height:r}=t,s=[new d(0,0),new d(e,0),new d(e,r),new d(0,r)];this.renderGraphic({...t,points:s,drawType:this.gl.TRIANGLE_FAN})}renderCircle(t){const e=t.segments||32,r=t.radius,s=t.color||this.defaultColor,i=[];for(let t=0;t<=e;t++){const s=t/e*Math.PI*2,n=Math.cos(s)*r,a=Math.sin(s)*r;i.push(new d(n,a))}this.renderGraphic({...t,points:i,color:s,drawType:this.gl.TRIANGLE_FAN})}resize(t,e){const r=t*this.devicePixelRatio,s=e*this.devicePixelRatio;this.canvas.width=r,this.canvas.height=s,this.resizeWebglSize(t,e),this.canvas.style.width=t+"px",this.canvas.style.height=e+"px",this.width=t,this.height=e}resizeWebglSize(t,e,r){const s=t*(r||this.devicePixelRatio),i=e*(r||this.devicePixelRatio);this.gl.viewport(0,0,s,i),this.updateProjection(0,t,e,0),this.gl.scissor(0,0,s,i)}updateProjection(t,e,r,s){this.projection=this.createOrthMatrix(t,e,r,s),this.projectionDirty=!0}clear(t){const e=this.gl,r=t||this.backgroundColor;e.clearColor(r.r/255,r.g/255,r.b/255,r.a/255),e.clear(e.COLOR_BUFFER_BIT),this.clearMask()}createOrthMatrix(t,e,r,s){return new Float32Array([2/(e-t),0,0,0,0,2/(s-r),0,0,0,0,-1,0,-(e+t)/(e-t),-(s+r)/(s-r),0,1])}drawMask(t=exports.MaskType.Include,e){this.startDrawMask(t),e(),this.endDrawMask()}startDrawMask(t=exports.MaskType.Include){const e=this.gl;this.currentMaskType.push(t),this.setMaskType(t,!0),e.stencilOp(e.KEEP,e.KEEP,e.REPLACE),e.colorMask(!1,!1,!1,!1)}endDrawMask(){const t=this.gl;this.quitCurrentRegion(),t.stencilOp(t.KEEP,t.KEEP,t.KEEP),t.colorMask(!0,!0,!0,!0),this.setMaskType(this.currentMaskType.pop()??exports.MaskType.Include,!1)}setMaskType(t,e=!1){const r=this.gl;if(this.quitCurrentRegion(),e)this.clearMask(),r.stencilFunc(r.ALWAYS,1,255);else switch(t){case exports.MaskType.Include:r.stencilFunc(r.EQUAL,1,255);break;case exports.MaskType.Exclude:r.stencilFunc(r.NOTEQUAL,1,255)}}clearMask(){const t=this.gl;this.quitCurrentRegion(),t.clearStencil(0),t.clear(t.STENCIL_BUFFER_BIT),t.stencilFunc(t.ALWAYS,1,255)}createCostumShader(t,e,r,s=0){return A.createCostumShader(this,t,e,r,s)}startFBO(t){this.quitCurrentRegion(),t.bind(),this.clearTextureUnit(),this.resizeWebglSize(t.width,t.height,1),this.updateProjection(0,t.width,0,t.height),this.save(),this.matrixStack.identity(),this.currentFBO.push(t)}endFBO(){if(this.currentFBO.length>0){const t=this.currentFBO.pop();this.quitCurrentRegion(),t.unbind(),this.clearTextureUnit(),this.resizeWebglSize(this.width,this.height),this.updateProjection(0,this.width,this.height,0),this.restore()}}drawToFBO(t,e){this.startFBO(t),e(),this.endFBO()}setBlendMode(t){switch(t){case exports.BlendMode.Additive:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE);break;case exports.BlendMode.Subtractive:this.gl.blendFunc(this.gl.ZERO,this.gl.ONE_MINUS_SRC_COLOR);break;case exports.BlendMode.Mix:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE_MINUS_SRC_ALPHA)}}drawLightShadowMask(t){this.startDrawMask(t.type||exports.MaskType.Exclude);this.light.createLightShadowMaskPolygon(t.occlusion,t.lightSource,t.baseProjectionLength).forEach((t=>{this.renderGraphic({points:t,color:c.Black})})),this.endDrawMask()}},exports.SCALEFACTOR=2,exports.Text=I,exports.Texture=B,exports.TextureCache=C,exports.TileMapRender=k,exports.TileSet=D,exports.Uniform=class{constructor(t){this.isDirty=!1,this.data=t}setUniform(t,e){this.data[t]!=e&&(this.isDirty=!0),this.data[t]=e}clearDirty(){this.isDirty=!1}getUnifromNames(){return Object.keys(this.data)}bind(t,e,r,s){if(!r)return;const i=this.data[e];if("number"==typeof i)t.uniform1f(r,i);else if(Array.isArray(i))switch(i.length){case 1:Number.isInteger(i[0])?t.uniform1i(r,i[0]):t.uniform1f(r,i[0]);break;case 2:Number.isInteger(i[0])?t.uniform2iv(r,i):t.uniform2fv(r,i);break;case 3:Number.isInteger(i[0])?t.uniform3iv(r,i):t.uniform3fv(r,i);break;case 4:Number.isInteger(i[0])?t.uniform4iv(r,i):t.uniform4fv(r,i);break;case 9:t.uniformMatrix3fv(r,!1,i);break;case 16:t.uniformMatrix4fv(r,!1,i);break;default:console.error(`Unsupported uniform array length for ${e}:`,i.length)}else if("boolean"==typeof i)t.uniform1i(r,i?1:0);else if(i.base?.texture){const e=s.useTexture(i.base.texture)[0];t.uniform1i(r,e)}else console.error(`Unsupported uniform type for ${e}:`,typeof i)}},exports.Vec2=d,exports.WebglBufferArray=h,exports.WebglElementBufferArray=l,exports.graphicAttributes=S,exports.spriteAttributes=R;
|
|
1
|
+
"use strict";var t,e,r,i,s,a,n;exports.LineTextureMode=void 0,(t=exports.LineTextureMode||(exports.LineTextureMode={})).STRETCH="stretch",t.REPEAT="repeat",exports.TextureWrapMode=void 0,(e=exports.TextureWrapMode||(exports.TextureWrapMode={})).REPEAT="repeat",e.CLAMP="clamp",e.MIRROR="mirror",exports.MaskType=void 0,(r=exports.MaskType||(exports.MaskType={})).Include="normal",r.Exclude="inverse",exports.TilemapShape=void 0,(i=exports.TilemapShape||(exports.TilemapShape={})).SQUARE="square",i.ISOMETRIC="isometric",exports.ShaderType=void 0,(s=exports.ShaderType||(exports.ShaderType={})).SPRITE="sprite",s.GRAPHIC="graphic",exports.BlendMode=void 0,(a=exports.BlendMode||(exports.BlendMode={})).Additive="additive",a.Subtractive="subtractive",a.Mix="mix",exports.ParticleShape=void 0,(n=exports.ParticleShape||(exports.ParticleShape={})).POINT="point",n.CIRCLE="circle",n.RECT="rect";var o;exports.ArrayType=void 0,(o=exports.ArrayType||(exports.ArrayType={}))[o.Float32=0]="Float32",o[o.Uint32=1]="Uint32",o[o.Uint16=2]="Uint16";class h{constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(t){switch(t){case exports.ArrayType.Float32:return Float32Array;case exports.ArrayType.Uint32:return Uint32Array;case exports.ArrayType.Uint16:return Uint16Array}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case exports.ArrayType.Float32:this.typedArray=this.float32;break;case exports.ArrayType.Uint32:this.typedArray=this.uint32;break;case exports.ArrayType.Uint16:this.typedArray=this.uint16}}clear(){this.usedElemNum=0}resize(t=0){if((t+=this.usedElemNum)>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum<<=1;this.setMaxSize(this.maxElemNum)}}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.float32[this.usedElemNum++]=t}pushUint16(t){this.uint16[this.usedElemNum++]=t}pop(t){this.usedElemNum-=t}getArray(t=0,e){return null==e?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.typedArray.length}}class u extends h{constructor(t,e,r=t.ARRAY_BUFFER,i=t.STATIC_DRAW){super(e),this.dirty=!0,this.webglBufferSize=0,this.gl=t,this.buffer=t.createBuffer(),this.type=r,this.usage=i}pushFloat32(t){super.pushFloat32(t),this.dirty=!0}pushUint32(t){super.pushUint32(t),this.dirty=!0}pushUint16(t){super.pushUint16(t),this.dirty=!0}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize?(t.bufferData(this.type,this.getArray(),this.usage),this.webglBufferSize=this.maxElemNum):t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class l extends h{constructor(){super(exports.ArrayType.Float32)}pushMat(){const t=this.usedElemNum-6,e=this.typedArray;this.resize(6),this.pushFloat32(e[t+0]),this.pushFloat32(e[t+1]),this.pushFloat32(e[t+2]),this.pushFloat32(e[t+3]),this.pushFloat32(e[t+4]),this.pushFloat32(e[t+5])}popMat(){this.pop(6)}pushIdentity(){this.resize(6),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0),this.pushFloat32(1),this.pushFloat32(0),this.pushFloat32(0)}translate(t,e){if("number"!=typeof t)return this.translate(t.x,t.y);const r=this.usedElemNum-6,i=this.typedArray;i[r+4]=i[r+0]*t+i[r+2]*e+i[r+4],i[r+5]=i[r+1]*t+i[r+3]*e+i[r+5]}rotate(t){const e=this.usedElemNum-6,r=this.typedArray,i=Math.cos(t),s=Math.sin(t),a=r[e+0],n=r[e+1],o=r[e+2],h=r[e+3];r[e+0]=a*i-n*s,r[e+1]=a*s+n*i,r[e+2]=o*i-h*s,r[e+3]=o*s+h*i}scale(t,e){if("number"!=typeof t)return this.scale(t.x,t.y);e||(e=t);const r=this.usedElemNum-6,i=this.typedArray;i[r+0]=i[r+0]*t,i[r+1]=i[r+1]*t,i[r+2]=i[r+2]*e,i[r+3]=i[r+3]*e}apply(t,e){if("number"!=typeof t)return new d(...this.apply(t.x,t.y));const r=this.usedElemNum-6,i=this.typedArray;return[i[r+0]*t+i[r+2]*e+i[r+4],i[r+1]*t+i[r+3]*e+i[r+5]]}getInverse(){const t=this.usedElemNum-6,e=this.typedArray,r=e[t+0],i=e[t+1],s=e[t+2],a=e[t+3],n=e[t+4],o=e[t+5],h=r*a-i*s;return new Float32Array([a/h,-i/h,-s/h,r/h,(s*o-a*n)/h,(i*n-r*o)/h])}getTransform(){const t=this.usedElemNum-6,e=this.typedArray;return new Float32Array([e[t+0],e[t+1],e[t+2],e[t+3],e[t+4],e[t+5]])}setTransform(t){const e=this.usedElemNum-6,r=this.typedArray;r[e+0]=t[0],r[e+1]=t[1],r[e+2]=t[2],r[e+3]=t[3],r[e+4]=t[4],r[e+5]=t[5]}getGlobalPosition(){const t=this.usedElemNum-6,e=this.typedArray;return new d(e[t+4],e[t+5])}setGlobalPosition(t,e){if("number"!=typeof t)return void this.setGlobalPosition(t.x,t.y);const r=this.usedElemNum-6,i=this.typedArray;i[r+4]=t,i[r+5]=e}getGlobalRotation(){const t=this.usedElemNum-6,e=this.typedArray;return Math.atan2(e[t+1],e[t+0])}setGlobalRotation(t){const e=this.usedElemNum-6,r=this.typedArray,i=this.getGlobalScale(),s=Math.cos(t),a=Math.sin(t);r[e+0]=s*i.x,r[e+1]=a*i.x,r[e+2]=-a*i.y,r[e+3]=s*i.y}getGlobalScale(){const t=this.usedElemNum-6,e=this.typedArray,r=Math.sqrt(e[t+0]*e[t+0]+e[t+1]*e[t+1]),i=Math.sqrt(e[t+2]*e[t+2]+e[t+3]*e[t+3]);return new d(r,i)}setGlobalScale(t,e){if("number"!=typeof t)return void this.setGlobalScale(t.x,t.y);const r=this.getGlobalRotation(),i=Math.cos(r),s=Math.sin(r),a=this.usedElemNum-6,n=this.typedArray;n[a+0]=i*t,n[a+1]=s*t,n[a+2]=-s*e,n[a+3]=i*e}globalToLocal(t){const e=this.getInverse();return new d(e[0]*t.x+e[2]*t.y+e[4],e[1]*t.x+e[3]*t.y+e[5])}localToGlobal(t){return this.apply(t)}toCSSTransform(){const t=this.usedElemNum-6,e=this.typedArray;return`matrix(${e[t+0]}, ${e[t+1]}, ${e[t+2]}, ${e[t+3]}, ${e[t+4]}, ${e[t+5]})`}identity(){const t=this.usedElemNum-6,e=this.typedArray;e[t+0]=1,e[t+1]=0,e[t+2]=0,e[t+3]=1,e[t+4]=0,e[t+5]=0}applyTransform(t,e=0,r=0){(t.saveTransform??1)&&this.pushMat(),t.afterSave&&t.afterSave();const i=t.x||0,s=t.y||0;(i||s)&&this.translate(i,s),t.position&&this.translate(t.position),t.rotation&&this.rotate(t.rotation),t.scale&&this.scale(t.scale);let a=t.offsetX||0,n=t.offsetY||0;t.offset&&(a+=t.offset.x,n+=t.offset.y);const o=t.origin;return o&&("number"==typeof o?(a-=o*e,n-=o*r):(a-=o.x*e,n-=o.y*r)),{offsetX:a,offsetY:n}}applyTransformAfter(t){t.beforRestore&&t.beforRestore(),(t.restoreTransform??1)&&this.popMat()}}class c extends u{constructor(t,e,r,i){super(t,exports.ArrayType.Uint16,t.ELEMENT_ARRAY_BUFFER,t.STATIC_DRAW),this.setMaxSize(e*i);for(let t=0;t<i;t++)this.addObject(t*r);this.bindBuffer(),this.bufferData()}addObject(t){}}class p{constructor(t,e,r,i=255){this._r=t,this._g=e,this._b=r,this._a=i,this.updateUint()}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0}setRGBA(t,e,r,i){this.r=t,this.g=e,this.b=r,this.a=i,this.updateUint()}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new p(this._r,this._g,this._b,this._a)}equal(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),i=parseInt(t.slice(4,6),16);let s=255;return t.length>=8&&(s=parseInt(t.slice(6,8),16)),new p(e,r,i,s)}add(t){return new p(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new p(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)}divide(t){return t instanceof p?new p(this.r/t.r,this.g/t.g,this.b/t.b,this.a/t.a):new p(this.r/t,this.g/t,this.b/t,this.a/t)}multiply(t){return t instanceof p?new p(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a):new p(this.r*t,this.g*t,this.b*t,this.a*t)}clamp(){this.r=Math.max(0,Math.min(255,this.r)),this.g=Math.max(0,Math.min(255,this.g)),this.b=Math.max(0,Math.min(255,this.b)),this.a=Math.max(0,Math.min(255,this.a))}}p.Red=new p(255,0,0,255),p.Green=new p(0,255,0,255),p.Blue=new p(0,0,255,255),p.Yellow=new p(255,255,0,255),p.Purple=new p(128,0,128,255),p.Orange=new p(255,165,0,255),p.Pink=new p(255,192,203,255),p.Gray=new p(128,128,128,255),p.Brown=new p(139,69,19,255),p.Cyan=new p(0,255,255,255),p.Magenta=new p(255,0,255,255),p.Lime=new p(192,255,0,255),p.White=new p(255,255,255,255),p.Black=new p(0,0,0,255),p.TRANSPARENT=new p(0,0,0,0);class d{constructor(t,e){this.x=void 0!==t?t:0,this.y=void 0!==e?e:0}add(t){return new d(this.x+t.x,this.y+t.y)}subtract(t){return new d(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof d?new d(this.x*t.x,this.y*t.y):new d(this.x*t,this.y*t)}divide(t){return t instanceof d?new d(this.x/t.x,this.y/t.y):new d(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,r=this.y-t.y;return Math.sqrt(e*e+r*r)}clone(){return new d(this.x,this.y)}copy(t){this.x=t.x,this.y=t.y}equal(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}angle(){return Math.atan2(this.y,this.x)}middle(t){return new d((this.x+t.x)/2,(this.y+t.y)/2)}abs(){return new d(Math.abs(this.x),Math.abs(this.y))}floor(){return new d(Math.floor(this.x),Math.floor(this.y))}ceil(){return new d(Math.ceil(this.x),Math.ceil(this.y))}snap(t){return new d(Math.round(this.x/t)*t,Math.round(this.y/t)*t)}stringify(){return`Vec2(${this.x}, ${this.y})`}static FromArray(t){return t.map((t=>new d(t[0],t[1])))}static fromAngle(t){return new d(Math.cos(t),Math.sin(t))}angleBetween(t){const e=this.dot(t),r=this.length()*t.length(),i=Math.max(-1,Math.min(1,e/r));return Math.acos(i)}}d.ZERO=new d(0,0),d.ONE=new d(1,1),d.UP=new d(0,1),d.DOWN=new d(0,-1),d.LEFT=new d(-1,0),d.RIGHT=new d(1,0);class f{static float(t,e){return Math.random()*(e-t)+t}static int(t,e){return Math.floor(Math.random()*(e-t+1))+t}static angle(){return Math.random()*Math.PI*2}static vector(t,e,r,i){return new d(f.float(t,e),f.float(r,i))}static direction(t){const e=f.angle();return new d(Math.cos(e)*t,Math.sin(e)*t)}static randomColor(t,e){return new p(f.float(t.r,e.r),f.float(t.g,e.g),f.float(t.b,e.b),f.float(t.a,e.a))}static pick(t){return t[f.int(0,t.length-1)]}static pickWeight(t){if(!t||0===t.length)return null;let e=0;for(const r of t)e+=r[1];const r=Math.random()*e;let i=0;for(const e of t)if(i+=e[1],r<=i)return e[0];return t[t.length-1][0]}static scalarOrRange(t,e){if(void 0===t)return e;if(Array.isArray(t)){if("number"==typeof t[0])return f.float(t[0],t[1]);if(t[0]instanceof d)return f.vector(t[0].x,t[1].x,t[0].y,t[1].y);if(t[0]instanceof p)return f.randomColor(t[0],t[1])}return"number"==typeof t?t:t.clone()}}class m{constructor(t){this.render=t}createLightShadowMaskPolygon(t,e,r){const i=[];t.forEach((t=>{for(let e=0;e<t.length;e++){const r=t[e],s=t[(e+1)%t.length];i.push([r,s])}})),r=r||Math.sqrt(Math.pow(this.render.width,2)+Math.pow(this.render.height,2));const s=[];return i.forEach((([t,i])=>{const a=new d(t.x-e.x,t.y-e.y),n=new d(i.x-e.x,i.y-e.y),o=i.subtract(t).perpendicular(),h=Math.abs(o.dot(a))/(o.length()*a.length())+.01,u=Math.abs(o.dot(n))/(o.length()*n.length())+.01,l=r/h,c=r/u,p=new d(a.x,a.y).normalize(),f=new d(n.x,n.y).normalize(),m=new d(t.x+p.x*l,t.y+p.y*l),x=new d(i.x+f.x*c,i.y+f.y*c);s.push([t,i,x,m])})),s}}const x=(t,e,r,i)=>{const s=[],a=i?Math.atan2(e.y,e.x):Math.atan2(-e.y,-e.x),n=Math.PI;for(let e=0;e<10;e++){const i=a+e/10*n,o=a+(e+1)/10*n,h=Math.cos(i)*r,u=Math.sin(i)*r,l=Math.cos(o)*r,c=Math.sin(o)*r;s.push(t),s.push(t.add(new d(h,u))),s.push(t.add(new d(l,c)))}return s},g=t=>{const e=t.points;if(e.length<2)return{vertices:[],uv:[]};const{normals:r,length:i}=((t,e=!1)=>{const r=[];if(t.length<2||e&&t.length<3)return{normals:r,length:0};const i=t.length;let s=0;if(e)for(let e=0;e<i;e++){const r=t[e],a=t[(e+1)%i];s+=r.distanceTo(a)}else for(let e=0;e<i-1;e++)s+=t[e].distanceTo(t[e+1]);const a=(t,e,r)=>{const i=e.subtract(t).normalize(),s=e.subtract(r).normalize(),a=s.dot(i);if(a<-.999)return{normal:i.perpendicular(),miters:1};{let t=s.add(i).normalize();i.cross(s)<0&&(t=t.multiply(-1));let e=1/Math.sqrt((1-a)/2);return{normal:t,miters:Math.min(e,4)}}};if(e){for(let e=0;e<i-1;e++){const s=0===e?t[i-2]:t[e-1],n=t[e],o=t[e+1];r.push(a(s,n,o))}r.push(r[0])}else for(let e=0;e<i;e++)if(0===e){const e=t[1].subtract(t[0]).normalize();r.push({normal:e.perpendicular(),miters:1})}else if(e===i-1){const i=t[e].subtract(t[e-1]).normalize();r.push({normal:i.perpendicular(),miters:1})}else r.push(a(t[e-1],t[e],t[e+1]));return{normals:r,length:s}})(e,t.closed),s=(t.width||1)/2,a=[],n=[],o=t.roundCap||!1,h=t.textureMode||exports.LineTextureMode.STRETCH;let u=0;const l=t.texture?.width||1;for(let t=0;t<e.length-1;t++){const o=e[t],c=r[t].normal,p=r[t].miters,f=o.add(c.multiply(p*s)),m=o.subtract(c.multiply(p*s)),x=e[t+1],g=r[t+1].normal,y=r[t+1].miters,T=x.add(g.multiply(y*s)),b=x.subtract(g.multiply(y*s)),E=o.distanceTo(x);let R=0,w=0;h===exports.LineTextureMode.STRETCH?(R=u/i,w=(u+E)/i):(R=u/l,w=R+E/l);const S=new d(R,0),M=new d(R,1),A=new d(w,0),v=new d(w,1);a.push(f),n.push(S),a.push(m),n.push(M),a.push(T),n.push(A),a.push(T),n.push(A),a.push(b),n.push(v),a.push(m),n.push(M),u+=E}if(o&&!t.closed){const t=e[0],i=r[0].normal,n=x(t,i,s,!0);a.push(...n);const o=e[e.length-1],h=r[e.length-1].normal,u=x(o,h,s,!1);a.push(...u)}return{vertices:a,uv:n}};var y="precision mediump float;\r\nvarying vec2 vRegion;\r\nvarying vec4 vColor;\r\n\r\nuniform sampler2D uTexture;\r\nuniform int uUseTexture;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n if(uUseTexture > 0){\r\n color = texture2D(uTexture, vRegion) * vColor;\r\n }else{\r\n color = vColor;\r\n }\r\n // fragment\r\n gl_FragColor = color;\r\n}\r\n",T="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec4 aColor;\r\nattribute vec2 aRegion;\r\n\r\nvarying vec4 vColor;\r\nuniform mat4 uProjectionMatrix;\r\nuniform vec4 uColor;\r\nvarying vec2 vRegion;\r\n\r\nvoid main(void) {\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n vColor = aColor;\r\n vRegion = aRegion;\r\n}\r\n";const b=(t,e,r)=>{const i=t.createShader(r);if(!i)throw new Error("Unable to create webgl shader");t.shaderSource(i,e),t.compileShader(i);if(!t.getShaderParameter(i,t.COMPILE_STATUS)){const r=t.getShaderInfoLog(i);throw console.error("Shader compilation failed:",r),new Error("Unable to compile shader: "+r+e)}return i};function E(t,e,r,i=!1,s=!1,a="clamp"){const n=t.createTexture();if(!n)throw new Error("unable to create texture");let o;switch(t.bindTexture(t.TEXTURE_2D,n),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,r?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,r?t.LINEAR:t.NEAREST),a){case"repeat":o=t.REPEAT;break;case"mirror":o=t.MIRRORED_REPEAT;break;default:o=t.CLAMP_TO_EDGE}return t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,o),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,o),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,s),i?t.texImage2D(t.TEXTURE_2D,0,t.RGBA,e.width,e.height,0,t.RGBA,t.UNSIGNED_BYTE,null):t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),n}const R=5126;var w="precision mediump float;\r\nuniform sampler2D uTextures[%TEXTURE_NUM%];\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n %GET_COLOR%\r\n // fragment\r\n gl_FragColor = color * vColor;\r\n}",S="precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec2 aRegion;\r\nattribute float aTextureId;\r\nattribute vec4 aColor;\r\n\r\nuniform mat4 uProjectionMatrix;\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vRegion = aRegion;\r\n vTextureId = aTextureId;\r\n vColor = aColor;\r\n\r\n // vertex s\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n // vertex e\r\n}";const M=[{name:"aPosition",size:2,type:R,stride:24},{name:"aRegion",size:2,type:R,stride:24,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:R,stride:24,offset:4*Float32Array.BYTES_PER_ELEMENT},{name:"aColor",size:4,type:5121,stride:24,offset:5*Float32Array.BYTES_PER_ELEMENT,normalized:!0}],A=[{name:"aPosition",size:2,type:R,stride:20},{name:"aColor",size:4,type:5121,stride:20,offset:2*Float32Array.BYTES_PER_ELEMENT,normalized:!0},{name:"aRegion",size:2,type:R,stride:20,offset:3*Float32Array.BYTES_PER_ELEMENT}];class v{constructor(t,e,r,i,s=0){this.attributeLoc={},this.uniformLoc={},this.textureUnitNum=0,this.attributes=[];const a=function(t,e){if(t.includes("%TEXTURE_NUM%")&&(t=t.replace("%TEXTURE_NUM%",e.toString())),t.includes("%GET_COLOR%")){let r="";for(let t=0;t<e;t++)r+=0==t?`if(vTextureId == ${t}.0)`:t==e-1?"else":`else if(vTextureId == ${t}.0)`,r+=`{color = texture2D(uTextures[${t}], vRegion);}`;t=t.replace("%GET_COLOR%",r)}return t}(r,t.maxTextureUnits-s);this.program=((t,e,r)=>{var i=t.createProgram(),s=b(t,e,35633),a=b(t,r,35632);if(!i)throw new Error("Unable to create program shader");if(t.attachShader(i,s),t.attachShader(i,a),t.linkProgram(i),!t.getProgramParameter(i,t.LINK_STATUS)){const e=t.getProgramInfoLog(i);throw new Error("Unable to link shader program: "+e)}return i})(t.gl,e,a),this.gl=t.gl,this.textureUnitNum=s,this.parseShader(e),this.parseShader(a),i&&this.setAttributes(i)}setUniforms(t,e){const r=this.gl;for(const i of t.getUnifromNames()){const s=this.getUniform(i);t.bind(r,i,s,e)}}getUniform(t){return this.uniformLoc[t]}use(){this.gl.useProgram(this.program)}parseShader(t){const e=this.gl,r=t.match(/attribute\s+\w+\s+(\w+)/g);if(r)for(const t of r){const r=t.split(" ")[2],i=e.getAttribLocation(this.program,r);-1!=i&&(this.attributeLoc[r]=i)}const i=t.match(/uniform\s+\w+\s+(\w+)/g);if(i)for(const t of i){const r=t.split(" ")[2];this.uniformLoc[r]=e.getUniformLocation(this.program,r)}}setAttribute(t){const e=this.attributeLoc[t.name];if(void 0!==e){const r=this.gl;r.vertexAttribPointer(e,t.size,t.type,t.normalized||!1,t.stride,t.offset||0),r.enableVertexAttribArray(e)}}setAttributes(t){this.attributes=t;for(const e of t)this.setAttribute(e)}updateAttributes(){this.setAttributes(this.attributes)}static createCostumShader(t,e,r,i,s=0){let a={[exports.ShaderType.SPRITE]:w,[exports.ShaderType.GRAPHIC]:y}[i],n={[exports.ShaderType.SPRITE]:S,[exports.ShaderType.GRAPHIC]:T}[i];const o={[exports.ShaderType.SPRITE]:M,[exports.ShaderType.GRAPHIC]:A}[i];return a=a.replace("void main(void) {",r+"\nvoid main(void) {"),n=n.replace("void main(void) {",e+"\nvoid main(void) {"),a=a.replace("// fragment","fragment(color);"),n=n.replace(/\/\/ vertex s[\s\S]*?\/\/ vertex e/,"vec2 position = aPosition;\n vertex(position, vRegion);\n gl_Position = uProjectionMatrix * vec4(position, 0.0, 1.0);"),new v(t,n,a,o,s)}}class U{constructor(t){this.usedTextures=[],this.shaders=new Map,this.isCostumShader=!1,this.freeTextureUnitNum=0,this.rapid=t,this.gl=t.gl,this.webglArrayBuffer=new u(t.gl,exports.ArrayType.Float32,t.gl.ARRAY_BUFFER,t.gl.STREAM_DRAW),this.maxTextureUnits=t.maxTextureUnits}getTextureUnitList(){return Array.from({length:this.maxTextureUnits},((t,e)=>e))}addVertex(t,e,...r){const[i,s]=this.rapid.matrixStack.apply(t,e);this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(s)}useTexture(t){const e=this.usedTextures.indexOf(t);return-1==e?(this.usedTextures.push(t),this.freeTextureUnitNum=this.maxTextureUnits-this.usedTextures.length,[this.usedTextures.length-1,!0]):[e,!1]}enterRegion(t){this.currentShader=t??this.getShader("default"),this.currentShader.use(),this.initializeForNextRender(),this.webglArrayBuffer.bindBuffer(),this.currentShader.updateAttributes(),this.updateProjection(),this.isCostumShader=Boolean(t)}updateProjection(){this.gl.uniformMatrix4fv(this.currentShader.uniformLoc.uProjectionMatrix,!1,this.rapid.projection)}isUnifromChanged(t){return!!t&&(this.costumUnifrom!=t||!!t?.isDirty)}setCurrentUniform(t){t.clearDirty(),this.costumUnifrom=t}exitRegion(){}initDefaultShader(t,e,r){this.setShader("default",t,e,r)}setShader(t,e,r,i){this.webglArrayBuffer.bindBuffer(),this.shaders.set(t,new v(this.rapid,e,r,i)),"default"===t&&(this.defaultShader=this.shaders.get(t))}getShader(t){return this.shaders.get(t)}render(){this.executeRender(),this.initializeForNextRender()}executeRender(){const t=this.gl;for(let e=0;e<this.usedTextures.length;e++)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,this.usedTextures[e]);this.webglArrayBuffer.bufferData()}initializeForNextRender(){this.webglArrayBuffer.clear(),this.usedTextures.length=0,this.isCostumShader=!1,this.freeTextureUnitNum=this.maxTextureUnits}hasPendingContent(){return!1}isShaderChanged(t){return(t||this.defaultShader)!=this.currentShader}}class C extends U{constructor(t){super(t),this.vertex=0,this.offset=d.ZERO,this.drawType=t.gl.TRIANGLE_FAN,this.setShader("default",T,y,A)}startRender(t,e,r,i){i&&this.currentShader?.setUniforms(i,this),this.offset=new d(t,e),this.vertex=0,this.webglArrayBuffer.clear(),r&&r.base&&(this.texture=this.useTexture(r.base.texture)[0])}addVertex(t,e,r,i,s){this.webglArrayBuffer.resize(3),super.addVertex(t+this.offset.x,e+this.offset.y),this.webglArrayBuffer.pushUint32(s),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(i),this.vertex+=1}executeRender(){super.executeRender();const t=this.gl;t.uniform1i(this.currentShader.uniformLoc.uUseTexture,void 0===this.texture?0:1),this.texture&&t.uniform1i(this.currentShader.uniformLoc.uTexture,this.texture),t.drawArrays(this.drawType,0,this.vertex),this.drawType=this.rapid.gl.TRIANGLE_FAN,this.vertex=0,this.texture=void 0}}const F=Math.floor(16384);class P extends c{constructor(t,e){super(t,6,4,e)}addObject(t){super.addObject(),this.pushUint16(t),this.pushUint16(t+1),this.pushUint16(t+2),this.pushUint16(t),this.pushUint16(t+3),this.pushUint16(t+2)}}class _ extends U{constructor(t){const e=t.gl;super(t),this.batchSprite=0,this.spriteTextureUnits=[],this.spriteTextureUnitIndexOffset=0,this.setShader("default",S,w,M),this.indexBuffer=new P(e,F)}addVertex(t,e,r,i,s,a){super.addVertex(t,e),this.webglArrayBuffer.pushFloat32(r),this.webglArrayBuffer.pushFloat32(i),this.webglArrayBuffer.pushFloat32(s),this.webglArrayBuffer.pushUint32(a)}renderSprite(t,e,r,i,s,a,n,o,h,u,l,c,p,d=0){(1+d>this.freeTextureUnitNum||this.batchSprite>=F||this.isUnifromChanged(l)||this.rapid.projectionDirty)&&(this.render(),l&&this.isUnifromChanged(l)&&(this.currentShader.setUniforms(l,this),this.setCurrentUniform(l)),this.rapid.projectionDirty&&this.updateProjection()),this.batchSprite++,this.webglArrayBuffer.resize(20);const[f,m]=this.useTexture(t);m&&(this.spriteTextureUnits.push(f),this.spriteTextureUnitIndexOffset=this.spriteTextureUnits[0]);const x=f-this.spriteTextureUnitIndexOffset,g=c?a:i,y=c?i:a,T=p?n:s,b=p?s:n,E=o,R=o+e,w=h,S=h+r;this.addVertex(E,w,g,T,x,u),this.addVertex(R,w,y,T,x,u),this.addVertex(R,S,y,b,x,u),this.addVertex(E,S,g,b,x,u)}executeRender(){if(super.executeRender(),this.batchSprite<=0)return;const t=this.gl;this.spriteTextureUnits.length>0&&this.gl.uniform1iv(this.currentShader.uniformLoc.uTextures,this.spriteTextureUnits),t.drawElements(t.TRIANGLES,6*this.batchSprite,t.UNSIGNED_SHORT,0)}enterRegion(t){super.enterRegion(t),this.indexBuffer.bindBuffer()}initializeForNextRender(){super.initializeForNextRender(),this.batchSprite=0,this.spriteTextureUnits.length=0}hasPendingContent(){return this.batchSprite>0}}class N{constructor(t,e){this.cache=new Map,this.render=t,this.antialias=e}async textureFromUrl(t,e=this.antialias,r=exports.TextureWrapMode.CLAMP){let i=this.cache.get(t);if(!i){const s=await this.loadImage(t);i=I.fromImageSource(this.render,s,e,r),this.cache.set(t,i)}return new B(i)}textureFromFrameBufferObject(t){return new B(t)}async textureFromSource(t,e=this.antialias,r=exports.TextureWrapMode.CLAMP){let i=this.cache.get(t);return i||(i=I.fromImageSource(this.render,t,e,r),this.cache.set(t,i)),new B(i)}async loadImage(t){return new Promise((e=>{const r=new Image;r.onload=()=>{e(r)},r.src=t}))}createText(t){return new L(this.render,t)}destroy(t){t instanceof B?(t.base?.destroy(this.render.gl),this.removeCache(t)):(t.destroy(this.render.gl),this.removeCache(t))}createFrameBufferObject(t,e,r=this.antialias){return new D(this.render,t,e,r)}removeCache(t){const e=t instanceof B?t.base?.texture:t.texture;e&&this.cache.forEach(((t,r)=>{t===e&&this.cache.delete(r)}))}}class I{constructor(t,e,r,i=exports.TextureWrapMode.CLAMP){this.texture=t,this.width=e,this.height=r,this.wrapMode=i}static fromImageSource(t,e,r=!1,i=exports.TextureWrapMode.CLAMP){return new I(E(t.gl,e,r,!1,!1,i),e.width,e.height)}destroy(t){t.deleteTexture(this.texture)}}class B{constructor(t){this.scale=1,this.setBaseTextur(t)}setBaseTextur(t){t&&(this.base=t,this.setClipRegion(0,0,t.width,t.height))}setClipRegion(t,e,r,i){if(this.base)return this.clipX=t/this.base.width,this.clipY=e/this.base.height,this.clipW=this.clipX+r/this.base.width,this.clipH=this.clipY+i/this.base.height,this.width=r*this.scale,this.height=i*this.scale,this}static fromImageSource(t,e,r=!1){return new B(I.fromImageSource(t,e,r))}static fromUrl(t,e){return t.textures.textureFromUrl(e)}createSpritesHeet(t,e){if(!this.base)return[];const r=[],i=Math.floor(this.base.width/t),s=Math.floor(this.base.height/e);for(let a=0;a<s;a++)for(let s=0;s<i;s++){const i=this.clone();i.setClipRegion(s*t,a*e,t,e),r.push(i)}return r}clone(){return new B(this.base)}}class L extends B{constructor(t,e){super(),this.scale=.5,this.rapid=t,this.options=e,this.text=e.text||" ",this.updateTextImage()}updateTextImage(){const t=this.createTextCanvas();this.setBaseTextur(I.fromImageSource(this.rapid,t,!0))}createTextCanvas(){const t=document.createElement("canvas"),e=t.getContext("2d");if(!e)throw new Error("Failed to get canvas context");e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";const r=this.text.split("\n");let i=0,s=0;for(const t of r){const r=e.measureText(t);i=Math.max(i,r.width),s+=this.options.fontSize||16}t.width=2*i,t.height=2*s,e.scale(2,2),e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";let a=0;for(const t of r)e.fillText(t,0,a),a+=this.options.fontSize||16;return t}setText(t){this.text!=t&&(this.text=t,this.updateTextImage())}}class D extends I{constructor(t,e,r,i=!1){const s=t.gl,a=E(s,{width:e,height:r},i,!0,!1),n=s.createFramebuffer();if(!n)throw s.deleteTexture(a),new Error("Failed to create WebGL framebuffer");s.bindFramebuffer(s.FRAMEBUFFER,n),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,a,0);const o=s.createRenderbuffer();if(!o)throw s.deleteFramebuffer(n),s.deleteTexture(a),new Error("Failed to create depth-stencil renderbuffer");s.bindRenderbuffer(s.RENDERBUFFER,o),s.renderbufferStorage(s.RENDERBUFFER,s.STENCIL_INDEX8,e,r),s.framebufferRenderbuffer(s.FRAMEBUFFER,s.STENCIL_ATTACHMENT,s.RENDERBUFFER,o),super(a,e,r),this.gl=s,this.framebuffer=n,s.bindTexture(s.TEXTURE_2D,null),s.bindFramebuffer(s.FRAMEBUFFER,null)}bind(){const t=this.gl;t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,this.framebuffer),t.clearColor(.5,.2,.5,.5),t.clear(t.COLOR_BUFFER_BIT)}unbind(){this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}resize(t,e){this.width=t,this.height=e,this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,t,e,0,this.gl.RGBA,this.gl.UNSIGNED_BYTE,null),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}destroy(t){t.deleteFramebuffer(this.framebuffer),super.destroy(t)}}const O=new Set;class k{constructor(t,e){this.textures=new Map,this.width=t,this.height=e}setTile(t,e){e instanceof B&&(e={texture:e}),this.textures.set(t,e)}getTile(t){return this.textures.get(t)}}class G{constructor(t){this.rapid=t}getYSortRow(t,e,r){if(!t)return[];const i=[];for(const r of t){const t=Math.floor(r.ySort/e);i[t]||(i[t]=[]),i[t].push(r)}return i}getOffset(t){let e=(t.errorX??2)+1,r=(t.errorY??2)+1;if("number"==typeof t.error){const i=(t.error??2)+1;e=i,r=i}else t.error&&(e=t.error.x+1,r=t.error.y+1);return{errorX:e,errorY:r}}getTileData(t,e){const r=e.shape??exports.TilemapShape.SQUARE,i=t.width,s=r===exports.TilemapShape.ISOMETRIC?t.height/2:t.height,a=this.rapid.matrixStack,n=a.globalToLocal(d.ZERO),o=a.getGlobalScale(),{errorX:h,errorY:u}=this.getOffset(e),l=Math.ceil(this.rapid.width/i/o.x)+2*h,c=Math.ceil(this.rapid.height/s/o.y)+2*u,p=new d(n.x<0?Math.ceil(n.x/i):Math.floor(n.x/i),n.y<0?Math.ceil(n.y/s):Math.floor(n.y/s));p.x-=h,p.y-=u;let f=new d(0-n.x%i-h*i,0-n.y%s-u*s);return f=f.add(n),{startTile:p,offset:f,viewportWidth:l,viewportHeight:c,height:s,width:i,shape:r}}renderYSortRow(t,e){for(const r of e)r.render?r.render():r.renderSprite&&t.renderSprite(r.renderSprite)}renderLayer(t,e){this.rapid.matrixStack.applyTransform(e);const r=e.tileSet,{startTile:i,offset:s,viewportWidth:a,viewportHeight:n,shape:o,width:h,height:u}=this.getTileData(r,e),l=this.getYSortRow(e.ySortCallback,u,n),c=e.ySortCallback&&e.ySortCallback.length>0;var p;0!==this.rapid.matrixStack.getGlobalRotation()&&(p="TileMapRender: tilemap is not supported rotation",O.has(p)||(O.add(p),console.warn(p)),this.rapid.matrixStack.setGlobalRotation(0));for(let p=0;p<n;p++){const n=p+i.y,d=l[n]??[];if(n<0||n>=t.length)this.renderYSortRow(this.rapid,d);else{for(let l=0;l<a;l++){const a=l+i.x;if(a<0||a>=t[n].length)continue;const c=t[n][a],f=r.getTile(c);if(!f)continue;let m=l*h+s.x,x=p*u+s.y,g=p*u+s.y+(f.ySortOffset??0);n%2!=0&&o===exports.TilemapShape.ISOMETRIC&&(m+=h/2);const y=e.eachTile&&e.eachTile(c,a,n)||{};d.push({ySort:g,renderSprite:{...f,x:m+(f.x||0),y:x+(f.y||0),...y}})}c&&d.sort(((t,e)=>t.ySort-e.ySort)),this.renderYSortRow(this.rapid,d)}}this.rapid.matrixStack.applyTransform(e)}localToMap(t,e){const r=e.tileSet;if(e.shape===exports.TilemapShape.ISOMETRIC){let e=0,i=0;const s=r.height/2,a=r.width/2;let n=Math.floor(t.y/s);const o=n%2==0;let h=Math.floor(t.x/a);const u=h%2==0,l=t.x%a/a,c=t.y%s/s,p=c<l,f=c<1-l;return o||(n-=1),p&&!u&&o?n-=1:p||!u||o?f&&u&&o?(h-=2,n-=1):f||u||o||(n+=1):(n+=1,h-=2),e=h,i=n,e=Math.floor(h/2),new d(e,i)}return new d(Math.floor(t.x/r.width),Math.floor(t.y/r.height))}mapToLocal(t,e){const r=e.tileSet;if(e.shape===exports.TilemapShape.ISOMETRIC){let e=new d(t.x*r.width,t.y*r.height/2);return t.y%2!=0&&(e.x+=r.width/2),e}return new d(t.x*r.width,t.y*r.height)}}const z=!0;class X{constructor(t,e){this.life=0,this.datas={},this.rapid=t,this.options=e,e.texture instanceof B?this.texture=e.texture:e.texture instanceof Array&&e.texture[0]instanceof Array?this.texture=f.pickWeight(e.texture):e.texture instanceof Array&&(this.texture=f.pick(e.texture)),this.maxLife=f.scalarOrRange(e.life,1),this.datas={speed:this.processAttribute(e.animation.speed,0),rotation:this.processAttribute(e.animation.rotation,0),scale:this.processAttribute(e.animation.scale,1),color:this.processAttribute(e.animation.color,p.White),velocity:this.processAttribute(e.animation.velocity,d.ZERO),acceleration:this.processAttribute(e.animation.acceleration,d.ZERO)},this.position=d.ZERO,this.initializePosition()}processAttribute(t,e){if(!t)return{value:e};if("object"==typeof(r=t)&&null!==r&&Object.getPrototypeOf(r)===Object.prototype){const r=f.scalarOrRange(t.start,e),i=f.scalarOrRange(t.end||r,e);return{delta:t.delta??this.getDelta(r,i,this.maxLife),value:r,damping:t.damping}}return this.processAttribute({start:t},e);var r}updateDamping(t){for(const e of Object.values(this.datas))if(e.damping){const r=e.value,i=Math.pow(e.damping,t);e.value="number"==typeof r?r*i:r.multiply(i)}}updateDelta(t){const e=this.datas;for(const e of Object.values(this.datas))if(e.delta){const r=e.value;"number"==typeof r?e.value+=t*e.delta:e.value=r.add(e.delta.multiply(t))}e.color.value.clamp();const r=d.fromAngle(e.rotation.value).multiply(e.speed.value*t);this.position=this.position.add(r).add(e.velocity.value.multiply(t)).add(e.acceleration.value.multiply(t))}getDelta(t,e,r){return"number"==typeof t&&"number"==typeof e?(e-t)/r:t instanceof d&&e instanceof d||t instanceof p&&e instanceof p?e.subtract(t).divide(r):t}update(t){return this.life+=t,!(this.life>=this.maxLife)&&(this.updateDamping(t),this.updateDelta(t),!0)}render(){this.rapid.renderSprite({...this.options,position:this.position,scale:this.datas.scale.value,rotation:this.datas.rotation.value,color:this.datas.color.value,texture:this.texture})}initializePosition(){switch(this.options.emitShape){case exports.ParticleShape.POINT:this.position=d.ZERO;break;case exports.ParticleShape.CIRCLE:const t=Math.random()*Math.PI*2,e=(this.options.emitRadius||0)*Math.sqrt(Math.random());this.position=new d(Math.cos(t)*e,Math.sin(t)*e);break;case exports.ParticleShape.RECT:this.position=new d((Math.random()-.5)*(this.options.emitRect?.width||0),(Math.random()-.5)*(this.options.emitRect?.height||0))}!this.options.localSpace&&this.options.position&&(this.position=this.position.add(this.options.position))}}class j{constructor(t,e){this.particles=[],this.emitting=!1,this.emitTimer=0,this.emitRate=10,this.emitTime=0,this.emitTimeCounter=0,this.localSpace=z,this.position=d.ZERO,this.rapid=t,this.options=e,this.emitRate=void 0!==e.emitRate?e.emitRate:10,this.emitTime=void 0!==e.emitTime?e.emitTime:0,this.localSpace=void 0!==e.localSpace?e.localSpace:z,this.position=e.position||d.ZERO}getTransform(){return this.options}setEmitRate(t){this.emitRate=t}setEmitTime(t){this.emitTime=t}start(){this.emitting=!0,this.emitTimeCounter=0}stop(){this.emitting=!1}clear(){this.particles=[],this.emitTimeCounter=0}emit(t){const e=Math.min(t,(this.options.maxParticles||1/0)-this.particles.length);for(let t=0;t<e;t++){const t={...this.options},e=new X(this.rapid,t);this.particles.unshift(e)}}update(t){if(this.emitting&&this.emitRate>0)if(this.emitTime>0){if(this.emitTimeCounter+=t,this.emitTimeCounter>=this.emitTime){const t=Math.floor(this.emitTimeCounter/this.emitTime);this.emit(this.emitRate*t),this.emitTimeCounter-=t*this.emitTime}}else{this.emitTimer+=t;const e=this.emitRate*t,r=Math.floor(e);r>0&&(this.emit(r),this.emitTimer-=r/this.emitRate);this.emitTimer*this.emitRate>=1&&(this.emit(1),this.emitTimer-=1/this.emitRate)}for(let e=this.particles.length-1;e>=0;e--)this.particles[e].update(t)||this.particles.splice(e,1)}render(){for(const t of this.particles)t.render()}getParticleCount(){return this.particles.length}isActive(){return this.emitting||this.particles.length>0}oneShot(){this.emit(this.emitRate)}}exports.BaseTexture=I,exports.Color=p,exports.DynamicArrayBuffer=h,exports.FrameBufferObject=D,exports.GLShader=v,exports.MathUtils=class{static deg2rad(t){return t*(Math.PI/180)}static rad2deg(t){return t/(Math.PI/180)}static normalizeDegrees(t){return(t%360+360)%360}},exports.MatrixStack=l,exports.ParticleEmitter=j,exports.Random=f,exports.Rapid=class{constructor(t){this.projectionDirty=!0,this.matrixStack=new l,this.tileMap=new G(this),this.light=new m(this),this.devicePixelRatio=window.devicePixelRatio||1,this.defaultColor=new p(255,255,255,255),this.regions=new Map,this.currentMaskType=[],this.currentTransform=[],this.currentFBO=[],this.lastTime=0;const e=(t=>{const e={stencil:!0},r=t.getContext("webgl2",e)||t.getContext("webgl",e);if(!r)throw new Error("Unable to initialize WebGL. Your browser may not support it.");return r})(t.canvas);this.gl=e,this.canvas=t.canvas,this.textures=new N(this,t.antialias??!1),this.maxTextureUnits=e.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.width=t.width||this.canvas.width,this.height=t.width||this.canvas.height,this.backgroundColor=t.backgroundColor||new p(255,255,255,255),this.registerBuildInRegion(),this.initWebgl(e),this.projectionDirty=!1}renderTileMapLayer(t,e){this.tileMap.renderLayer(t,e instanceof k?{tileSet:e}:e)}initWebgl(t){this.resize(this.width,this.height),t.enable(t.BLEND),t.disable(t.DEPTH_TEST),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.enable(t.STENCIL_TEST),t.enable(t.SCISSOR_TEST)}clearTextureUnit(){for(let t=0;t<this.maxTextureUnits;t++)this.gl.activeTexture(this.gl.TEXTURE0+t),this.gl.bindTexture(this.gl.TEXTURE_2D,null)}registerBuildInRegion(){this.registerRegion("sprite",_),this.registerRegion("graphic",C)}registerRegion(t,e){this.regions.set(t,new e(this))}quitCurrentRegion(){this.currentRegion&&this.currentRegion.hasPendingContent()&&(this.currentRegion.render(),this.currentRegion.exitRegion())}setRegion(t,e){if(t!=this.currentRegionName||this.currentRegion&&this.currentRegion.isShaderChanged(e)){const r=this.regions.get(t);this.quitCurrentRegion(),this.currentRegion=r,this.currentRegionName=t,r.enterRegion(e)}}save(){this.matrixStack.pushMat()}restore(){this.matrixStack.popMat()}withTransform(t){this.save(),t(),this.restore()}startRender(t=!0){this.clear(),t&&this.matrixStack.clear(),this.matrixStack.pushIdentity(),this.currentRegion=void 0,this.currentRegionName=void 0;const e=performance.now(),r=this.lastTime?(e-this.lastTime)/1e3:0;return this.lastTime=e,r}endRender(){this.currentRegion?.render(),this.projectionDirty=!1}render(t){t(this.startRender()),this.endRender()}renderCamera(t){this.matrixStack.applyTransform(t),this.matrixStack.setTransform(this.matrixStack.getInverse())}renderSprite(t){const e=t.texture;if(!e||!e.base)return;const{offsetX:r,offsetY:i}=this.startDraw(t,e.width,e.height);this.setRegion("sprite",t.shader),this.currentRegion.renderSprite(e.base.texture,e.width,e.height,e.clipX,e.clipY,e.clipW,e.clipH,r,i,(t.color||this.defaultColor).uint32,t.uniforms,t.flipX,t.flipY),this.afterDraw()}renderParticles(t){t.localSpace?(this.startDraw(t.getTransform()),t.render(),this.afterDraw()):t.render()}renderTexture(t){t.base&&this.renderSprite({texture:t})}renderLine(t){const e=t.closed?[...t.points,t.points[0]]:t.points,{vertices:r,uv:i}=g({...t,points:e});this.renderGraphic({...t,drawType:this.gl.TRIANGLES,points:r,uv:i})}renderGraphic(t){this.startGraphicDraw(t),t.points.forEach(((e,r)=>{const i=Array.isArray(t.color)?t.color[r]:t.color,s=t.uv?.[r];this.addGraphicVertex(e.x,e.y,s,i)})),this.endGraphicDraw()}startGraphicDraw(t){const{offsetX:e,offsetY:r}=this.startDraw(t);this.setRegion("graphic",t.shader);const i=this.currentRegion;i.startRender(e,r,t.texture,t.uniforms),t.drawType&&(i.drawType=t.drawType)}addGraphicVertex(t,e,r,i){this.currentRegion.addVertex(t,e,r?.x,r?.y,(i||this.defaultColor).uint32)}endGraphicDraw(){this.currentRegion.render(),this.afterDraw()}startDraw(t,e=0,r=0){return this.currentTransform.push(t),this.matrixStack.applyTransform(t,e,r)}afterDraw(){this.currentTransform.length>0&&this.matrixStack.applyTransformAfter(this.currentTransform.pop())}renderRect(t){const{width:e,height:r}=t,i=[new d(0,0),new d(e,0),new d(e,r),new d(0,r)];this.renderGraphic({...t,points:i,drawType:this.gl.TRIANGLE_FAN})}renderCircle(t){const e=t.segments||32,r=t.radius,i=t.color||this.defaultColor,s=[];for(let t=0;t<=e;t++){const i=t/e*Math.PI*2,a=Math.cos(i)*r,n=Math.sin(i)*r;s.push(new d(a,n))}this.renderGraphic({...t,points:s,color:i,drawType:this.gl.TRIANGLE_FAN})}resize(t,e){const r=t*this.devicePixelRatio,i=e*this.devicePixelRatio;this.canvas.width=r,this.canvas.height=i,this.resizeWebglSize(t,e),this.canvas.style.width=t+"px",this.canvas.style.height=e+"px",this.width=t,this.height=e}resizeWebglSize(t,e,r){const i=t*(r||this.devicePixelRatio),s=e*(r||this.devicePixelRatio);this.gl.viewport(0,0,i,s),this.updateProjection(0,t,e,0),this.gl.scissor(0,0,i,s)}updateProjection(t,e,r,i){this.projection=this.createOrthMatrix(t,e,r,i),this.projectionDirty=!0}clear(t){const e=this.gl,r=t||this.backgroundColor;e.clearColor(r.r/255,r.g/255,r.b/255,r.a/255),e.clear(e.COLOR_BUFFER_BIT),this.clearMask()}createOrthMatrix(t,e,r,i){return new Float32Array([2/(e-t),0,0,0,0,2/(i-r),0,0,0,0,-1,0,-(e+t)/(e-t),-(i+r)/(i-r),0,1])}drawMask(t=exports.MaskType.Include,e){this.startDrawMask(t),e(),this.endDrawMask()}startDrawMask(t=exports.MaskType.Include){const e=this.gl;this.currentMaskType.push(t),this.setMaskType(t,!0),e.stencilOp(e.KEEP,e.KEEP,e.REPLACE),e.colorMask(!1,!1,!1,!1)}endDrawMask(){const t=this.gl;this.quitCurrentRegion(),t.stencilOp(t.KEEP,t.KEEP,t.KEEP),t.colorMask(!0,!0,!0,!0),this.setMaskType(this.currentMaskType.pop()??exports.MaskType.Include,!1)}setMaskType(t,e=!1){const r=this.gl;if(this.quitCurrentRegion(),e)this.clearMask(),r.stencilFunc(r.ALWAYS,1,255);else switch(t){case exports.MaskType.Include:r.stencilFunc(r.EQUAL,1,255);break;case exports.MaskType.Exclude:r.stencilFunc(r.NOTEQUAL,1,255)}}clearMask(){const t=this.gl;this.quitCurrentRegion(),t.clearStencil(0),t.clear(t.STENCIL_BUFFER_BIT),t.stencilFunc(t.ALWAYS,1,255)}createCostumShader(t,e,r,i=0){return v.createCostumShader(this,t,e,r,i)}startFBO(t){this.quitCurrentRegion(),t.bind(),this.clearTextureUnit(),this.resizeWebglSize(t.width,t.height,1),this.updateProjection(0,t.width,0,t.height),this.save(),this.matrixStack.identity(),this.currentFBO.push(t)}endFBO(){if(this.currentFBO.length>0){const t=this.currentFBO.pop();this.quitCurrentRegion(),t.unbind(),this.clearTextureUnit(),this.resizeWebglSize(this.width,this.height),this.updateProjection(0,this.width,this.height,0),this.restore()}}drawToFBO(t,e){this.startFBO(t),e(),this.endFBO()}setBlendMode(t){switch(t){case exports.BlendMode.Additive:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE);break;case exports.BlendMode.Subtractive:this.gl.blendFunc(this.gl.ZERO,this.gl.ONE_MINUS_SRC_COLOR);break;case exports.BlendMode.Mix:this.gl.blendFunc(this.gl.SRC_ALPHA,this.gl.ONE_MINUS_SRC_ALPHA)}}drawLightShadowMask(t){this.startDrawMask(t.type||exports.MaskType.Exclude);this.light.createLightShadowMaskPolygon(t.occlusion,t.lightSource,t.baseProjectionLength).forEach((t=>{this.renderGraphic({points:t,color:p.Black})})),this.endDrawMask()}createParticleEmitter(t){return new j(this,t)}},exports.SCALEFACTOR=2,exports.Text=L,exports.Texture=B,exports.TextureCache=N,exports.TileMapRender=G,exports.TileSet=k,exports.Uniform=class{constructor(t){this.isDirty=!1,this.data=t}setUniform(t,e){this.data[t]!=e&&(this.isDirty=!0),this.data[t]=e}clearDirty(){this.isDirty=!1}getUnifromNames(){return Object.keys(this.data)}bind(t,e,r,i){if(!r)return;const s=this.data[e];if("number"==typeof s)t.uniform1f(r,s);else if(Array.isArray(s))switch(s.length){case 1:Number.isInteger(s[0])?t.uniform1i(r,s[0]):t.uniform1f(r,s[0]);break;case 2:Number.isInteger(s[0])?t.uniform2iv(r,s):t.uniform2fv(r,s);break;case 3:Number.isInteger(s[0])?t.uniform3iv(r,s):t.uniform3fv(r,s);break;case 4:Number.isInteger(s[0])?t.uniform4iv(r,s):t.uniform4fv(r,s);break;case 9:t.uniformMatrix3fv(r,!1,s);break;case 16:t.uniformMatrix4fv(r,!1,s);break;default:console.error(`Unsupported uniform array length for ${e}:`,s.length)}else if("boolean"==typeof s)t.uniform1i(r,s?1:0);else if(s.base?.texture){const e=i.useTexture(s.base.texture)[0];t.uniform1i(r,e)}else console.error(`Unsupported uniform type for ${e}:`,typeof s)}},exports.Vec2=d,exports.WebglBufferArray=u,exports.WebglElementBufferArray=c,exports.graphicAttributes=A,exports.spriteAttributes=M;
|
package/dist/render.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { ICircleRenderOptions, IGraphicRenderOptions, ILayerRenderOptions, IRapidOptions, IRectRenderOptions, IRenderLineOptions, ISpriteRenderOptions, ShaderType as ShaderType, MaskType, WebGLContext, BlendMode, ILightRenderOptions } from "./interface";
|
|
1
|
+
import { ICircleRenderOptions, IGraphicRenderOptions, ILayerRenderOptions, IRapidOptions, IRectRenderOptions, IRenderLineOptions, ISpriteRenderOptions, ShaderType as ShaderType, MaskType, WebGLContext, BlendMode, ILightRenderOptions, IParticleOptions, ICameraOptions } from "./interface";
|
|
2
2
|
import { LightManager } from "./light";
|
|
3
3
|
import { Color, MatrixStack, Vec2 } from "./math";
|
|
4
4
|
import RenderRegion from "./regions/region";
|
|
5
5
|
import { FrameBufferObject, Texture, TextureCache } from "./texture";
|
|
6
6
|
import { TileMapRender, TileSet } from "./tilemap";
|
|
7
7
|
import GLShader from "./webgl/glshader";
|
|
8
|
+
import { ParticleEmitter } from "./particle";
|
|
8
9
|
/**
|
|
9
10
|
* The `Rapid` class provides a WebGL-based rendering engine.
|
|
10
11
|
*/
|
|
@@ -29,6 +30,7 @@ declare class Rapid {
|
|
|
29
30
|
private currentMaskType;
|
|
30
31
|
private currentTransform;
|
|
31
32
|
private currentFBO;
|
|
33
|
+
private lastTime;
|
|
32
34
|
/**
|
|
33
35
|
* Constructs a new `Rapid` instance with the given options.
|
|
34
36
|
* @param options - Options for initializing the `Rapid` instance.
|
|
@@ -84,7 +86,7 @@ declare class Rapid {
|
|
|
84
86
|
* Starts the rendering process, resetting the matrix stack and clearing the current region.
|
|
85
87
|
* @param clear - Whether to clear the matrix stack. Defaults to true.
|
|
86
88
|
*/
|
|
87
|
-
startRender(clear?: boolean):
|
|
89
|
+
startRender(clear?: boolean): number;
|
|
88
90
|
/**
|
|
89
91
|
* Ends the rendering process by rendering the current region.
|
|
90
92
|
*/
|
|
@@ -93,13 +95,15 @@ declare class Rapid {
|
|
|
93
95
|
* Render
|
|
94
96
|
* @param cb - The function to render.
|
|
95
97
|
*/
|
|
96
|
-
render(cb: () => void): void;
|
|
98
|
+
render(cb: (dt: number) => void): void;
|
|
99
|
+
renderCamera(options: ICameraOptions): void;
|
|
97
100
|
/**
|
|
98
101
|
* Renders a sprite with the specified options.
|
|
99
102
|
*
|
|
100
103
|
* @param options - The rendering options for the sprite, including texture, position, color, and shader.
|
|
101
104
|
*/
|
|
102
105
|
renderSprite(options: ISpriteRenderOptions): void;
|
|
106
|
+
renderParticles(particleEmitter: ParticleEmitter): void;
|
|
103
107
|
/**
|
|
104
108
|
* Renders a texture directly without additional options.
|
|
105
109
|
* This is a convenience method that calls renderSprite with just the texture.
|
|
@@ -239,5 +243,6 @@ declare class Rapid {
|
|
|
239
243
|
* @param lightSource - The light source position
|
|
240
244
|
*/
|
|
241
245
|
drawLightShadowMask(options: ILightRenderOptions): void;
|
|
246
|
+
createParticleEmitter(options: IParticleOptions): ParticleEmitter;
|
|
242
247
|
}
|
|
243
248
|
export default Rapid;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const
|
|
1
|
+
export declare const isPlainObject: (obj: any) => boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rapid-render",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.13",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
"rollup-plugin-terser": "^7.0.2",
|
|
29
29
|
"rollup-plugin-typescript2": "^0.36.0",
|
|
30
30
|
"tslib": "^2.6.2",
|
|
31
|
-
"typedoc": "^0.26.6",
|
|
32
31
|
"typedoc-theme-category-nav": "^0.0.3",
|
|
33
32
|
"typescript": "^5.3.3"
|
|
34
33
|
},
|
|
35
34
|
"dependencies": {
|
|
36
|
-
"concurrently": "^9.1.2"
|
|
35
|
+
"concurrently": "^9.1.2",
|
|
36
|
+
"typedoc": "^0.28.3"
|
|
37
37
|
}
|
|
38
38
|
}
|