-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathGLTilemap.ts
756 lines (612 loc) · 22.9 KB
/
GLTilemap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
// @if DEBUG
import { ASSERT } from './debug';
// @endif
import { ILayer } from './tiled/layers';
import { ITilemap } from './tiled/Tilemap';
import { assertNever } from './utils/assertNever';
import { GLProgram } from './utils/GLProgram';
import { hasOwnKey } from './utils/hasOwnKey';
import { parseColorStr } from './utils/parseColorStr';
import { ELayerType } from './ELayerType';
import { GLTileset } from './GLTileset';
// import { GLObjectgroup } from './GLObjectgroup';
import { GLTilelayer } from './GLTilelayer';
import { GLImagelayer } from './GLImagelayer';
import { IAssetCache } from './IAssetCache';
import backgroundVS from './shaders/background.vert';
import backgroundFS from './shaders/background.frag';
import tilelayerVS from './shaders/tilelayer.vert';
import tilelayerFS from './shaders/tilelayer.frag';
import imagelayerVS from './shaders/imagelayer.vert';
import imagelayerFS from './shaders/imagelayer.frag';
export type TGLLayer = GLTilelayer | GLImagelayer /*| GLObjectgroup*/;
export interface IBlendMode
{
/**
* A 2 or 4 element array specifying which blend function to use.
*
* Default: [gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA]
*/
func: number[];
/** What blend equation to use. Default: gl.FUNC_ADD */
equation: number;
}
export interface ITilemapOptions
{
/** The WebGL context to use to render. */
gl?: WebGLRenderingContext;
/** A cache of preloaded assets. Keyed by URL as it appears in the tilemap data. */
assetCache?: IAssetCache;
/** What blend function/equation should we draw with? */
blendMode?: IBlendMode;
/** Should we render the background color of the map? Default: true */
renderBackgroundColor?: boolean;
/** Should we automatically create each imagelayer? Default: true */
createAllImagelayers?: boolean;
/** Should we automatically create each tilelayer? Default: true */
createAllTilelayers?: boolean;
/** Should we automatically create each objectgroup? Default: false */
// createAllObjectgroups?: boolean;
}
interface IShaderCache
{
background: GLProgram;
tilelayer: GLProgram;
imagelayer: GLProgram;
}
export class GLTilemap
{
private static _attribIndices = {
aPosition: 0,
aTexture: 1,
};
gl: WebGLRenderingContext | null = null;
shaders: IShaderCache | null = null;
renderBackgroundColor: boolean;
blendMode: IBlendMode;
readonly assetCache: IAssetCache | undefined = undefined;
private _layers: TGLLayer[] = [];
private _tilesets: GLTileset[] = [];
private _viewportSize = new Float32Array(2);
private _scaledViewportSize = new Float32Array(2);
private _inverseLayerTileSize = new Float32Array(2);
private _quadVerts = new Float32Array([
//x y u v
-1, -1, 0, 1,
1, -1, 1, 1,
1, 1, 1, 0,
-1, -1, 0, 1,
1, 1, 1, 0,
-1, 1, 0, 0,
]);
private _quadVertBuffer: WebGLBuffer | null = null;
private _firstTilelayerUniformUpload = true;
private _tileScale = 1;
private _totalTilesetImages = 0;
private _backgroundColor: Float32Array;
private _tilesetIndices: Int32Array;
private _tilesetTileSizeBuffer: Float32Array;
private _tilesetTileOffsetBuffer: Float32Array;
private _inverseTilesetTextureSizeBuffer: Float32Array;
constructor(public readonly desc: ITilemap, options: ITilemapOptions = {})
{
// @if DEBUG
ASSERT(desc.version >= 1.2, `Unsupported JSON format version ${desc.version}, please update your JSON to v1.2`);
// @endif
if (options.assetCache)
this.assetCache = options.assetCache;
this.renderBackgroundColor = typeof options.renderBackgroundColor === 'boolean' ? options.renderBackgroundColor : true;
this.blendMode = {
func: options.blendMode && options.blendMode.func || [WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA],
equation: options.blendMode && options.blendMode.equation || WebGLRenderingContext.FUNC_ADD,
};
this._inverseLayerTileSize[0] = 1 / desc.tilewidth;
this._inverseLayerTileSize[1] = 1 / desc.tileheight;
for (let i = 0; i < desc.tilesets.length; ++i)
{
const tileset = new GLTileset(desc.tilesets[i], this.assetCache);
this._totalTilesetImages += tileset.images.length;
this._tilesets.push(tileset);
}
this._createInitialLayers(desc.layers, options);
// parse the background color
this._backgroundColor = new Float32Array(4);
if (desc.backgroundcolor)
parseColorStr(desc.backgroundcolor, this._backgroundColor);
// setup the different buffers
this._tilesetIndices = new Int32Array(this._totalTilesetImages);
this._tilesetTileSizeBuffer = new Float32Array(this._totalTilesetImages * 2);
this._tilesetTileOffsetBuffer = new Float32Array(this._totalTilesetImages * 2);
this._inverseTilesetTextureSizeBuffer = new Float32Array(this._totalTilesetImages * 2);
this._buildBufferData();
if (options.gl)
{
this.glInitialize(options.gl);
}
}
get layers(): ReadonlyArray<TGLLayer>
{
return this._layers;
}
get tilesets(): ReadonlyArray<GLTileset>
{
return this._tilesets;
}
get viewportWidth(): number
{
return this._viewportSize[0];
}
get viewportHeight(): number
{
return this._viewportSize[1];
}
get scaledViewportWidth(): number
{
return this._scaledViewportSize[0];
}
get scaledViewportHeight(): number
{
return this._scaledViewportSize[1];
}
set repeatTiles(v: boolean)
{
for (let i = 0; i < this._layers.length; ++i)
{
const layer = this._layers[i];
if (layer.type === ELayerType.Tilelayer)
{
layer.repeatTiles = false;
}
}
}
get tileScale(): number
{
return this._tileScale;
}
set tileScale(scale: number)
{
if (this._tileScale != scale)
{
this._tileScale = scale;
this._updateViewportSize();
}
}
resizeViewport(width: number, height: number): void
{
if (this._viewportSize[0] != width || this._viewportSize[1] != height)
{
this._viewportSize[0] = width;
this._viewportSize[1] = height;
this._updateViewportSize();
}
}
glInitialize(gl: WebGLRenderingContext): void
{
this.glTerminate();
this.gl = gl;
this._firstTilelayerUniformUpload = true;
// initialize layers
for (let i = 0; i < this._layers.length; ++i)
{
this._layers[i].glInitialize(gl);
}
// initialize tilesets
for (let i = 0; i < this._tilesets.length; ++i)
{
this._tilesets[i].glInitialize(gl);
}
// create buffers
this._quadVertBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this._quadVertBuffer);
gl.bufferData(gl.ARRAY_BUFFER, this._quadVerts, gl.STATIC_DRAW);
// create shaders
this._createShaders();
// update viewport uniforms
this._updateViewportSize();
}
glTerminate(): void
{
if (!this.gl)
return;
const gl = this.gl;
// destroy layers
for (let i = 0; i < this._layers.length; ++i)
{
this._layers[i].glTerminate();
}
// destroy tilesets
for (let i = 0; i < this._tilesets.length; ++i)
{
this._tilesets[i].glTerminate();
}
// destroy buffers
if (this._quadVertBuffer)
{
gl.deleteBuffer(this._quadVertBuffer);
this._quadVertBuffer = null;
}
// destroy shaders
for (const k in this.shaders)
{
if (!hasOwnKey(this.shaders, k))
continue;
const shader = this.shaders[k];
gl.deleteProgram(shader.program);
}
this.shaders = null;
this.gl = null;
}
/**
* Updates each layer's animations by the given delta time.
*
* @param dt Delta time in milliseconds to perform an update for.
*/
update(dt: number): void
{
for (let i = 0; i < this.layers.length; ++i)
{
const layer = this._layers[i];
if (layer.type === ELayerType.Tilelayer)
layer.update(dt);
}
}
/**
* Draws the tilemap.
*
* @param x The x offset at which to draw the map
* @param y The y offset at which to draw the map
*/
draw(x: number = 0, y: number = 0): void
{
if (!this.gl || !this.shaders)
return;
var gl = this.gl;
gl.enable(gl.BLEND);
gl.blendEquation(this.blendMode.equation);
if (this.blendMode.func.length > 2)
{
gl.blendFuncSeparate(
this.blendMode.func[0],
this.blendMode.func[1],
this.blendMode.func[2],
this.blendMode.func[3]);
}
else
{
gl.blendFunc(this.blendMode.func[0], this.blendMode.func[1]);
}
// Enable attributes, these are the same for all shaders.
gl.bindBuffer(gl.ARRAY_BUFFER, this._quadVertBuffer);
gl.enableVertexAttribArray(GLTilemap._attribIndices.aPosition);
gl.enableVertexAttribArray(GLTilemap._attribIndices.aTexture);
gl.vertexAttribPointer(GLTilemap._attribIndices.aPosition, 2, gl.FLOAT, false, 16, 0);
gl.vertexAttribPointer(GLTilemap._attribIndices.aTexture, 2, gl.FLOAT, false, 16, 8);
// Draw background
if (this.renderBackgroundColor && this._backgroundColor[3] > 0)
{
const bgShader = this.shaders.background;
// @if DEBUG
ASSERT(!!(bgShader.uniforms.uColor), 'Invalid uniforms for background shader.');
// @endif
gl.useProgram(bgShader.program);
gl.uniform4fv(bgShader.uniforms.uColor!, this._backgroundColor);
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
// Bind tileset textures
let imgIndex = 0;
for (let i = 0; i < this._tilesets.length; ++i)
{
const tileset = this._tilesets[i];
for (let t = 0; t < tileset.textures.length; ++t)
{
this.gl.activeTexture(gl.TEXTURE1 + imgIndex);
this.gl.bindTexture(this.gl.TEXTURE_2D, tileset.textures[t]);
imgIndex++;
}
}
// Draw each layer of the map
gl.activeTexture(gl.TEXTURE0);
let lastShader = ELayerType.UNKNOWN;
let activeShader: GLProgram | null = null;
for (let i = 0; i < this._layers.length; ++i)
{
const layer = this._layers[i];
const offsetx = layer.desc.offsetx || 0;
const offsety = layer.desc.offsety || 0;
if (!layer.desc.visible)
continue;
if (lastShader != layer.type)
{
activeShader = this._bindShader(layer);
lastShader = layer.type;
}
if (!activeShader)
continue;
// @if DEBUG
ASSERT(!!(activeShader.uniforms.uOffset), 'Invalid uniforms for layer shader.');
// @endif
switch (layer.type)
{
case ELayerType.Tilelayer:
layer.uploadUniforms(activeShader);
gl.uniform2f(
activeShader.uniforms.uOffset!,
-offsetx + (x * layer.scrollScaleX),
-offsety + (y * layer.scrollScaleY)
);
break;
case ELayerType.Imagelayer:
layer.uploadUniforms(activeShader);
gl.uniform2f(
activeShader.uniforms.uOffset!,
offsetx + (-x * layer.scrollScaleX),
-offsety + (y * layer.scrollScaleY)
);
break;
// case ELayerType.Objectgroup:
// break;
default:
assertNever(layer);
}
// if (layer.type !== ELayerType.Objectgroup)
{
gl.bindTexture(gl.TEXTURE_2D, layer.texture);
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
}
}
findLayerDesc(...name: string[]): ILayer | null
{
return this._doFindLayerDesc(this.desc.layers, name, 0);
}
createLayer(...name: string[]): boolean
{
if (name.length === 0)
return false;
const layerDesc = this._doFindLayerDesc(this.desc.layers, name, 0);
if (!layerDesc)
return false;
this.createLayerFromDesc(layerDesc);
return true;
}
destroyLayer(...name: string[]): boolean
{
if (name.length === 0)
return false;
const layerDesc = this._doFindLayerDesc(this.desc.layers, name, 0);
if (!layerDesc)
return false;
return this.destroyLayerFromDesc(layerDesc);
}
createLayerFromDesc(layer: ILayer): void
{
let newLayer: TGLLayer | null = null;
switch (layer.type)
{
case 'tilelayer':
newLayer = new GLTilelayer(layer, this.tilesets);
break;
case 'objectgroup':
// newLayer = new GLObjectgroup(layer);
break;
case 'imagelayer':
newLayer = new GLImagelayer(layer, this.assetCache);
break;
case 'group':
for (let i = 0; i < layer.layers.length; ++i)
{
this.createLayerFromDesc(layer.layers[i]);
}
break;
default:
return assertNever(layer);
}
if (newLayer)
{
this._layers.push(newLayer);
if (this.gl)
newLayer.glInitialize(this.gl);
}
}
destroyLayerFromDesc(layerDesc: ILayer): boolean
{
for (let i = 0; i < this._layers.length; ++i)
{
const layer = this._layers[i];
if (layer.desc === layerDesc)
{
layer.glTerminate();
this._layers.splice(1, 1);
return true;
}
}
return false;
}
private _doFindLayerDesc(layers: ILayer[], names: string[], nameIndex: number): ILayer | null
{
for (let i = 0; i < layers.length; ++i)
{
const layer = layers[i];
if (layer.name === names[nameIndex])
{
if (layer.type === 'group')
{
// more names, so try something in this group
if (names.length < nameIndex + 1)
{
return this._doFindLayerDesc(layer.layers, names, ++nameIndex);
}
// No more names, return the group.
else
{
return layer;
}
}
else
{
return layer;
}
}
}
return null;
}
private _bindShader(layer: TGLLayer): GLProgram
{
// @if DEBUG
ASSERT(!!(this.gl && this.shaders), 'Cannot call `_bindShader` before `glInitialize`.');
// @endif
const gl = this.gl!;
switch (layer.type)
{
case ELayerType.Tilelayer:
{
const tileShader = this.shaders!.tilelayer;
gl.useProgram(tileShader.program);
// these are static, and only need to be uploaded once.
if (this._firstTilelayerUniformUpload)
{
this._firstTilelayerUniformUpload = false;
// @if DEBUG
ASSERT(!!(tileShader.uniforms.uLayer
&& tileShader.uniforms.uInverseLayerTileSize
&& tileShader.uniforms.uTilesetTileSize
&& tileShader.uniforms.uTilesetTileOffset
&& tileShader.uniforms.uInverseTilesetTextureSize),
'Invalid uniforms for tile layer shader.');
// @endif
gl.uniform1i(tileShader.uniforms.uLayer!, 0);
gl.uniform2fv(tileShader.uniforms.uInverseLayerTileSize!, this._inverseLayerTileSize);
for (let index = 0; index < this._tilesetIndices.length; index++) {
gl.uniform1i(
gl.getUniformLocation(
tileShader.program,
`uTilesets${index}`,
),
this._tilesetIndices[index],
);
}
gl.uniform2fv(tileShader.uniforms.uTilesetTileSize!, this._tilesetTileSizeBuffer);
gl.uniform2fv(tileShader.uniforms.uTilesetTileOffset!, this._tilesetTileOffsetBuffer);
gl.uniform2fv(tileShader.uniforms.uInverseTilesetTextureSize!, this._inverseTilesetTextureSizeBuffer);
}
return tileShader;
}
case ELayerType.Imagelayer:
{
const imageShader = this.shaders!.imagelayer;
gl.useProgram(imageShader.program);
return imageShader;
}
// case ELayerType.Objectgroup:
// return ;
default:
return assertNever(layer);
}
}
private _updateViewportSize(): void
{
if (!this.gl || !this.shaders)
return;
this._scaledViewportSize[0] = this._viewportSize[0] / this._tileScale;
this._scaledViewportSize[1] = this._viewportSize[1] / this._tileScale;
const gl = this.gl!;
const tileShader = this.shaders!.tilelayer;
// @if DEBUG
ASSERT(!!(tileShader.uniforms.uViewportSize
&& tileShader.uniforms.uInverseTileScale),
'Invalid uniforms for tile layer shader.');
// @endif
gl.useProgram(tileShader.program);
gl.uniform2fv(tileShader.uniforms.uViewportSize!, this._scaledViewportSize);
gl.uniform1f(tileShader.uniforms.uInverseTileScale!, 1.0 / this._tileScale);
const imageShader = this.shaders!.imagelayer;
// @if DEBUG
ASSERT(!!(imageShader.uniforms.uViewportSize
&& imageShader.uniforms.uInverseTileScale),
'Invalid uniforms for image shader.');
// @endif
gl.useProgram(imageShader.program);
gl.uniform2fv(imageShader.uniforms.uViewportSize!, this._scaledViewportSize);
gl.uniform1f(imageShader.uniforms.uInverseTileScale!, 1.0 / this._tileScale);
}
private _buildBufferData(): void
{
// Index buffer
for (let i = 0; i < this._tilesetIndices.length; ++i)
this._tilesetIndices[i] = i + 1;
// tileset size buffers
let imgIndex = 0;
for (let i = 0; i < this._tilesets.length; ++i)
{
const tileset = this._tilesets[i];
for (let s = 0; s < tileset.images.length; ++s)
{
this._tilesetTileSizeBuffer[(imgIndex * 2)] = tileset.desc.tilewidth;
this._tilesetTileSizeBuffer[(imgIndex * 2) + 1] = tileset.desc.tileheight;
this._tilesetTileOffsetBuffer[(imgIndex * 2)] = tileset.desc.spacing;
this._tilesetTileOffsetBuffer[(imgIndex * 2) + 1] = tileset.desc.margin;
const imgDesc = tileset.desc.tiles && tileset.desc.tiles[s];
const imgWidth = imgDesc && imgDesc.imagewidth ? imgDesc.imagewidth : tileset.desc.imagewidth;
const imgHeight = imgDesc && imgDesc.imageheight ? imgDesc.imageheight : tileset.desc.imageheight;
this._inverseTilesetTextureSizeBuffer[(imgIndex * 2)] = 1 / imgWidth;
this._inverseTilesetTextureSizeBuffer[(imgIndex * 2) + 1] = 1 / imgHeight;
imgIndex++;
}
}
}
private _createInitialLayers(layers: ILayer[], options: ITilemapOptions): void
{
const createTilelayers = typeof options.createAllTilelayers === 'boolean' ? options.createAllTilelayers : true;
const createImagelayers = typeof options.createAllImagelayers === 'boolean' ? options.createAllImagelayers : true;
// const createObjectgroups = typeof options.createAllObjectgroups === 'boolean' ? options.createAllObjectgroups : false;
// We don't create anything, early out.
if (!createTilelayers && !createImagelayers /*&& !createObjectgroups*/)
return;
for (let i = 0; i < layers.length; ++i)
{
const layer = layers[i];
if ((layer.type === 'tilelayer' && createTilelayers)
// || (layer.type === 'objectgroup' && createObjectgroups)
|| (layer.type === 'imagelayer' && createImagelayers))
{
this.createLayerFromDesc(layer);
}
else if (layer.type === 'group')
{
this._createInitialLayers(layer.layers, options);
}
}
}
private _createShaders(): void
{
// @if DEBUG
ASSERT(!!(this.gl), 'Cannot call `_createShaders` before `glInitialize`.');
// @endif
const tilelayerFragShader = tilelayerFS
.replace('#pragma define(NUM_TILESETS)', `#define NUM_TILESETS ${this._tilesets.length}`)
.replace('#pragma define(NUM_TILESET_IMAGES)', `#define NUM_TILESET_IMAGES ${this._totalTilesetImages}`)
.replace(
'#pragma declare_tileset_uniforms',
new Array(this._totalTilesetImages)
.fill(0).map((_,i) => i)
.map((i) => `uniform sampler2D uTilesets${i};`)
.join('\n'),
)
.replace(
'#pragma get_texture_cases',
new Array(this._totalTilesetImages)
.fill(0).map((_,i) => i)
.map((i) =>
`if(index == ${i}){
return texture2D(uTilesets${i}, coord * uInverseTilesetTextureSize[${i}]);
}`)
.join('\n'),
);
const gl = this.gl!;
this.shaders = {
background: new GLProgram(gl, backgroundVS, backgroundFS, GLTilemap._attribIndices),
tilelayer: new GLProgram(gl, tilelayerVS, tilelayerFragShader, GLTilemap._attribIndices),
imagelayer: new GLProgram(gl, imagelayerVS, imagelayerFS, GLTilemap._attribIndices),
};
}
}