forked from iliasHDZ/GDRenderW
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
1351 lines (1111 loc) · 46.5 KB
/
Copy pathmain.js
File metadata and controls
1351 lines (1111 loc) · 46.5 KB
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as glMatrix from "./matrixgl/index"
import monitor from "./monitor"
import spritesheet from "./assets/spritesheet.png"
import dataJson from "./assets/data.json"
import VERT_SRC from "./assets/shaders/vertex_shader.vsh"
import FRAG_SRC from "./assets/shaders/fragment_shader.fsh"
import bg1 from "./assets/bg/1.png"
import bg2 from "./assets/bg/2.png"
import bg3 from "./assets/bg/3.png"
import bg4 from "./assets/bg/4.png"
import bg5 from "./assets/bg/5.png"
import bg6 from "./assets/bg/6.png"
import bg7 from "./assets/bg/7.png"
import bg8 from "./assets/bg/8.png"
import bg9 from "./assets/bg/9.png"
import bg10 from "./assets/bg/10.png"
import bg11 from "./assets/bg/11.png"
import bg12 from "./assets/bg/12.png"
import bg13 from "./assets/bg/13.png"
import bg14 from "./assets/bg/14.png"
import bg15 from "./assets/bg/15.png"
import bg16 from "./assets/bg/16.png"
import bg17 from "./assets/bg/17.png"
import bg18 from "./assets/bg/18.png"
import bg19 from "./assets/bg/19.png"
import bg20 from "./assets/bg/20.png"
import font1_img from "./assets/font/font1.png"
import font1_json from "./assets/font/font1.json"
import chatfont_img from "./assets/font/chat_font.png"
import chatfont_json from "./assets/font/chat_font.json"
const TEXTURE_INSET = 0.6;
export const util = {
/**
* Creates and compiles the given shader and checks for errors. It then returns the shader.
* @param {WebGLRenderingContext} gl gl context
* @param {number} type shader type, gl.FRAGMENT_SHADER or gl.VERTEX_SHADER
* @param {string} source the shader source code
* @returns gl shader or undefined if unsuccessful
*/
createShader: function(gl, type, source) {
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (success)
return shader;
let message = gl.getShaderInfoLog(shader);
console.error("SHADER ERROR: " + message);
gl.deleteShader(shader);
},
/**
* Takes the 2 given shaders and combines them into a WebGLProgram and also checks for errors
* @param {WebGLRenderingContext} gl gl context
* @param {WebGLShader} vertexShader the vertex shader
* @param {WebGLShader} fragmentShader the fragment shader
* @returns WebGLProgram or undefined if unsuccessful
*/
createProgram: function(gl, vertexShader, fragmentShader) {
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
var success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (success)
return program;
gl.deleteProgram(program);
},
/**
* Takes in given values an puts them in a WebGLBuffer
* @param {WebGLRenderingContext} gl
* @param {number[]} values
* @returns a WebGLBuffer containing the given values
*/
createBuffer: function(gl, values) {
var buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(values), gl.STATIC_DRAW);
return buffer;
},
/**
* Enabling the given WebGLBuffer so that gl.drawArrays is called with it.
* It will give the values contained in the buffer to the GPU to render.
* It also asks for how the buffer is layouted and what components to give.
* @param {WebGLRenderingContext} gl gl context
* @param {WebGLBuffer} buffer buffer to be enabled
* @param {number} attr the attribute index the buffer is going to be put in
* @param {number} size number of components per vertex. usually 2 because of 2d
*/
enableBuffer: function(gl, buffer, attr, size) {
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.vertexAttribPointer(attr, size, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(attr);
},
/**
* Enabling the given WebGLTexture so that gl.drawArrays is called with it.
* It will take the texture and give it as a uniform for the shaders to use.
* @param {WebGLRenderingContext} gl gl context
* @param {WebGLTexture} texture the gl texture to be activated
* @param {WebGLUniformLocation} uniLoc uniform location
*/
enableTexture: function(renderer, texture, uniLoc) {
let gl = renderer.gl;
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture.texture);
gl.uniform1i(uniLoc, 0);
renderer.current_texture = texture;
},
/**
* It takes in the texture cutout out of the spreadsheet and sets the correct uniforms of the cutout.
* The cutout is 0.6 pixels smaller to avoid texture leaking from the nearing textures in the sheet.
* @param {GDRenderer} renderer GDRenderer
* @param {{x : number, y : number, w : number, h : number}} tex the texture cutout
*/
setTexture: function(renderer, tex) {
if (!renderer.current_texture) return;
let tx = renderer.current_texture;
let w = tx.width;
let h = tx.height;
let texM = glMatrix.mat3.create();
glMatrix.mat3.translate(texM, texM, [(tex.x + TEXTURE_INSET) / w, (tex.y + TEXTURE_INSET) / h]);
glMatrix.mat3.scale(texM, texM, [(tex.w - TEXTURE_INSET * 2) / w, (tex.h - TEXTURE_INSET) / h]);
renderer.gl.uniformMatrix3fv(renderer.textM, false, texM);
},
/**
* Sets the texture so that it renders the whole thing.
* @param {GDRenderer} renderer
*/
setFullTexture: function(renderer) {
let texM = glMatrix.mat3.create();
renderer.gl.uniformMatrix3fv(renderer.textM, false, texM);
},
/**
* This sets the model matrix. The model matrix is the transformation of an object.
* The vertex shader takes the model matrix in as 3 component matrix uniform and multiplies it
* with the vertices of the texture rectangle so it applies the transformations.
* @param {GDRenderer} renderer GDRenderer
* @param {number} x the x position
* @param {number} y the y position
* @param {number} w the width scaling
* @param {number} h the height scaling
* @param {number} rot the rotation in angels (forgot if it was clockwise or counter-clockwise)
*/
setModelMatrix: function(renderer, x, y, w, h, rot) {
let model = glMatrix.mat3.create();
glMatrix.mat3.translate(model, model, [x, y]);
if (rot)
glMatrix.mat3.rotate(model, model, (-rot) * Math.PI / 180);
if (!h)
glMatrix.mat3.scale(model, model, [w, w]);
else
glMatrix.mat3.scale(model, model, [w, h]);
renderer.gl.uniformMatrix3fv(renderer.mmUni, false, model);
},
/**
* Sets the view matrix. The view matrix is the transformation of the view (basically the camera).
* For the rest, it basically does the same thing as the model matrix.
* If the x, y, zoom values are not given. It will use the camera in the GDRenderer.
* @param {GDRenderer} renderer GDRenderer
* @param {number} x the x position of the camera
* @param {number} y the y position of the camera
* @param {number} zoom the zoom of the camera (basically scaling the scene)
*/
setCamera: function(renderer, x, y, zoom) {
if (x == undefined) {
x = -renderer.camera.x;
y = renderer.camera.y;
zoom = renderer.camera.zoom;
}
let gl = renderer.gl;
gl.uniform1f(renderer.cxUni, x);
gl.uniform1f(renderer.cyUni, y);
let matrix = glMatrix.mat3.create();
if (zoom)
glMatrix.mat3.scale(matrix, matrix, [zoom, zoom]);
else
glMatrix.mat3.scale(matrix, matrix, [1, 1]);
gl.uniformMatrix3fv(renderer.vmUni, false, matrix);
},
setAdditiveBlending: function(renderer, blending) {
let gl = renderer.gl;
if (blending) gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
else gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
},
color255: function(r = 255, g = 255, b = 255, a = 255) {
return {r, g, b, a};
},
/**
* This sets the projection matrix so that the view looks 2d and normally scaled to the canvas.
* Another example of a projection matrix is a perspective matrix that gives the 3d effect.
* @param {GDRenderer} renderer GDRenderer
*/
setProjection: function(renderer) {
let matrix = glMatrix.mat3.create();
glMatrix.mat3.scale(matrix, matrix, [2/renderer.width, 2/renderer.height]);
renderer.gl.uniformMatrix3fv(renderer.pmUni, false, matrix);
},
/**
* Sets the tint of the object for the next gl.drawArrays call. This is what basically makes objects different colors.
* The given tint is then multiplied with every pixels of the object.
* @param {GDRenderer} renderer GDRenderer
* @param {{r : number, g : number, b : number, a : number}} tint the tint (values from 0 - 1)
*/
setTint: function(renderer, tint) {
let uniform = renderer.gl.getUniformLocation(renderer.gProg, "a_tint")
renderer.gl.uniform4fv(uniform, new Float32Array([tint.r, tint.g, tint.b, tint.opacity || 1]));
},
getSpeedPortal: function(obj) {
if (obj.id == 200)
return 0;
if (obj.id == 201)
return 1;
if (obj.id == 202)
return 2;
if (obj.id == 203)
return 3;
if (obj.id == 1334)
return 4;
return null;
},
getColorTriggerChannel: function (obj) {
if (obj.type != 'trigger' || obj.info != 'color') return null;
if (obj.color) return obj.color;
switch (obj.id) {
case 29: return 1000;
case 30: return 1001;
case 104: return 1002;
case 105: return 1004;
case 221: return 1;
case 717: return 2;
case 718: return 3;
case 743: return 4;
case 744: return 1003;
}
return 1;
},
ups: {
[0]: 258,
[1]: 312,
[2]: 388.8,
[3]: 468,
[4]: 578.1
},
color_names: {
1000: "BG",
1001: "G1",
1002: "LINE",
1003: "3DL",
1004: "OBJ"
},
/**
* This function takes in a level and time in seconds and returns the x position
* where the amount of time passed is the same as the input given you went through each speed portal
* @param {GDExtLevel} level the level in GDExt JSON format. though it also needs the `sps` property.
* @param {number} x the time in seconds
* @returns {number} the returned x position
*/
secToX: function(level, sec) {
let lastX = 0;
let lastSec = 0;
let lastSpd = +(level.info.speed || 0) + 1;
for (let key of level.sps) {
let sp = level.data[key];
if (sec < sp.secx)
break;
lastX = +sp.x;
lastSec = +sp.secx;
lastSpd = this.getSpeedPortal(sp);
}
return lastX + (sec - lastSec) * this.ups[lastSpd];
},
/**
* Takes in a color object with properties `red, green, blue` (a color trigger for example).
* And returns a color object in `r, g, b` format
* @param {{red : number, green : number, blue : number}} col the long color object
* @returns {{r : number, g : number, b : number}} col the long color object
*/
longToShortCol: (col) => {
return {r: parseFloat(col.red), g: parseFloat(col.green), b: parseFloat(col.blue)};
},
pickColor: (o) => {
return { r: o.r / 255, g: o.g / 255, b: o.b / 255 };
},
pickColorFromTrigger: (o) => {
return { r: o.red / 255, g: o.green / 255, b: o.blue / 255, a: o.opacity || 1 };
},
calColorFrom: function (level, pX, pColor, pDuration, nX, nColor) {
let pSec = this.xToSec(level, pX);
let dSec = pSec + pDuration;
let nSec = this.xToSec(level, nX);
let minmax = (n) => Math.min(Math.max(n, 0), 1);
return this.blendColor(pColor, nColor,
minmax( (nSec - pSec) / pDuration ) );
},
/**
* This converts each color component so the values go from `0 - 255` to `0 - 1`
* which is more useful for WebGL.
* @param {{r: number, g: number, b: number}} col color value `0 - 255`
* @returns color value `0 - 1`
*/
toOne: function(col) {
return {r: col.r/255, g: col.g/255, b: col.b/255};
},
/**
* This interpolates the 2 color components depending on the `blend` value
* @param {number} c1 color component 1
* @param {number} c2 color component 2
* @param {number} blend blend amount `0 - 1`
* @returns result blend
*/
blendComp: function(c1, c2, blend) {
return c1 * (1-blend) + c2 * blend;
},
/**
* This interpolates the 2 color values depending on the `blend` value
* @param {{r: number, g: number, b: number}} col1 color value 1
* @param {{r: number, g: number, b: number}} col2 color value 2
* @param {number} blend blend amount `0 - 1`
* @returns result blend
*/
blendColor: function(col1, col2, blend) {
let ret = {r: this.blendComp(col1.r, col2.r, blend), b: this.blendComp(col1.b, col2.b, blend), g: this.blendComp(col1.g, col2.g, blend)};
if (col1.a) ret.a = this.blendComp(col1.a, col2.a, blend);
return ret;
},
/**
* This function loads all the color triggers from a specific color id so that each
* color trigger has a `curCol` which is the color currently at that trigger before
* it gets converted and also lists the indexes of the color triggers so you dont have to
* go through each object to find the color trigger needed. This is purely for optimisation.
* @param {GDExtLevel} level the level in GDExt JSON format
* @param {number} color the color id
* @returns a list of all the indexes of each color trigger of that color id.
*/
loadColors: function(level, color) {
var listCOLs = [];
for (const obj of level.data)
if (obj.type == "trigger" && obj.info == "color" && (obj.color == "" + color || (color == 1 && !obj.color)))
listCOLs.push(obj);
listCOLs.sort((a, b) => a.x - b.x);
var lastCOL = {x: -200000, red: 255, blue: 255, green: 255, duration: 0};
var curCol = {r: 255, g: 255, b: 255};
var baseColor = level.info.colors.filter((f) => {return f.channel == color;});
if (baseColor.length > 0) {
baseColor = baseColor[0];
lastCOL = {x: -200000, red: baseColor.r, blue: baseColor.b, green: baseColor.g, duration: 0};
curCol = {r: baseColor.r, b: baseColor.b, g: baseColor.g};
}
for (const obj of listCOLs) {
var delta = this.xToSec(level, obj.x) - this.xToSec(level, lastCOL.x);
if (delta < lastCOL.duration) {
curCol = this.blendColor(curCol, this.longToShortCol(lastCOL), delta / lastCOL.duration);
} else {
curCol = this.longToShortCol(lastCOL);
}
obj.curCol = curCol;
lastCOL = obj;
}
return listCOLs;
},
guidelineColor: function(color) {
if (color >= 1.0) return {r: 0, g: 1, b: 0, a: 1};
if (color == 0.8 || color == 0) return {r: 1, g: 0.5, b: 0, a: 1};
else if (color == 0.9) return {r: 1, g: 1, b: 0, a: 1};
return {r: 0, b: 0, g: 0, a: 0};
},
zorder: {
'-3': -4,
'-1': -3,
'1' : -2,
'3' : -1,
'5' : 1,
'7' : 2,
'9' : 3
}
}
/**
* This is a texture class that automatically creates a texture.
* @param {GDRenderer} renderer renderer
* @param {string} url url of the texture
*/
function Texture(renderer, url) {
this.width = null;
this.height = null;
this.loaded = false;
renderer.loading.tasks++;
let gl = renderer.gl;
this.texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, this.texture);
this.image = new Image();
var tex = this;
this.image.onload = function() {
tex.width = this.width;
tex.height = this.height;
gl.bindTexture(gl.TEXTURE_2D, tex.texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, tex.image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
renderer.loading.complete();
tex.loaded = true;
}
this.image.src = url;
}
function Font(renderer, json, img) {
this.json = json;
this.img = img;
this.tex = new Texture(renderer, this.img);
this.getChar = function(char) {
for (let c of this.json.chars)
if (c.letter == char) return c;
}
this.getKerning = function(char1, char2) {
for (let k of this.json.kernings)
if (k.first == char1 && k.second == char2) return k.amount;
return 0;
}
}
/**
* This is the object definition class that contains where its textures are located, their size,
* their default color id and their default zlayer and zorder.
* This is loaded from the `data.json` file.
* @param {WebGLRenderingContext} gl gl context
* @param {{}} obj an object from `data.json`
*/
function ObjectDef(gl, obj) {
this.texture_i = null;
this.texture_a = null;
this.texture_b = null;
this.texture_l = null;
this.width = 0;
this.height = 0;
this.maincol = null;
this.seccol = null;
this.zlayer = null; // ex. B2, B1, T2
this.zorder = null;
if (obj.sprite_i) {
this.texture_i = obj.sprite_i;
this.width = Math.max(this.width, obj.sprite_i.w) / 62 * 30;
this.height = Math.max(this.height, obj.sprite_i.h) / 62 * 30;
} if (obj.sprite_a) {
this.texture_a = obj.sprite_a;
this.width = Math.max(this.width, obj.sprite_a.w) / 62 * 30;
this.height = Math.max(this.height, obj.sprite_a.h) / 62 * 30;
} if (obj.sprite_b) {
this.texture_b = obj.sprite_b;
this.width = Math.max(this.width, obj.sprite_b.w) / 62 * 30;
this.height = Math.max(this.height, obj.sprite_b.h) / 62 * 30;
} if (obj.sprite_l) {
this.texture_l = obj.sprite_l;
this.width = Math.max(this.width, obj.sprite_l.w) / 62 * 30;
this.height = Math.max(this.height, obj.sprite_l.h) / 62 * 30;
}
this.maincol = obj.mainCol || 1004;
this.seccol = obj.secCol || 1;
this.zlayer = obj.zlayer;
this.zorder = obj.zorder;
}
/**
* This is a kinda simple camera class.
* @param {number} x x position
* @param {number} y y position
* @param {number} zoom zoom
*/
function Camera(x, y, zoom) {
this.x = x;
this.y = y;
this.zoom = zoom;
/**
* DOES NOT WORK
*/
this.zoomTo = function(zoom, x, y) {
let dx = x - this.x;
let dy = y - this.y;
let dz = this.zoom - zoom;
this.x += dx * dz;
this.y += dy * dz;
this.zoom = zoom;
}
}
/**
* The core of GDRenderW. The GDRenderer class contains (almost) everything for rendering a level.
* @param {WebGLRenderingContext} gl the webgl context to draw on
* @param {Function} loaded_callback callback every time progress changed in loading
*/
export function GDRenderer(gl, loaded_callback = null) {
this.gl = gl;
this.gProg = null;
this.pBuff = null;
this.pAttr = null;
this.tBuff = null;
this.tAttr = null;
this.mmUni = null;
this.pmUni = null;
this.vmUni = null;
this.cxUni = null;
this.cyUni = null;
this.projM = null;
this.viewM = null;
this.spUni = null;
this.mainT = null;
this.textM = null;
this.width = null;
this.height = null;
this.level = {
info: {},
data: {}
};
this.objectDefs = {};
this.camera = null;
this.bgs = {};
this.current_texture = null;
this.loading = {
tasks: 0,
completed: 0,
callback: loaded_callback,
complete: () => {
this.loading.completed++;
if (this.loading.callback)
this.loading.callback({
progress: this.loading.completed / this.loading.tasks,
loaded: this.loading.completed >= this.loading.tasks
});
}
}
/**
* This cache caches simple variables and current color value
* so they don't have to be calculated every time
*/
this.cache = {
colors: {},
parent: null,
camX1: null,
camX2: null,
objCount: 0,
/**
* This clears the cache
*/
clear: function() {
this.colors = {};
this.objCount = 0;
},
/**
* This will get the current color of a color id depending on the camera's x position.
* This also caches the color and returns that cached color if it gets called again.
* @param {GDRenderer} renderer the renderer since the function is too far in the hiërarchy.
* @param {number} color color id
* @returns {{r: number, g: number, b: number}} the color from `0 - 1`
*/
getColor: function(renderer, color) {
monitor.startCategory("Color calculation");
if (renderer.current_options.custom_colors) {
let cst = renderer.current_options.custom_colors;
if (cst[color])
return cst[color];
}
if (!renderer.level.info.colors)
return {r: 1, g: 1, b: 1, opacity: 1, blending: false};
if (color == 1010) // BLACK
return {r: 0, g: 0, b: 0, opacity: 1, blending: false};
if (color == 1011) // WHITE (unverified color id)
return {r: 1, g: 1, b: 1, opacity: 1, blending: false};
/* LIGHTER TO BE ADDED (color id 1012 (unverified color id) ) */
if (this.colors[color])
return this.colors[color];
this.colors[color] = renderer.gdlevel.getColorAt(renderer.camera.x, color);
monitor.endCategory("Color calculation");
return this.colors[color];
}
}
this.gl = gl;
// Creates the gl program using the shader source codes above
this.gProg = util.createProgram(gl,
util.createShader(gl, gl.VERTEX_SHADER, VERT_SRC),
util.createShader(gl, gl.FRAGMENT_SHADER, FRAG_SRC));
// This enables blending so that objects can be seen through transparent objects
gl.enable(gl.BLEND);
util.setAdditiveBlending(this, false);
//gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
// These are the vertices of a rectangle since textures are rectangulair.
// This is used in the rendering process.
const vertices = [
-0.5, 0.5,
0.5, -0.5,
-0.5, -0.5,
-0.5, 0.5,
0.5, 0.5,
0.5, -0.5,
];
// These are the corresponding texture coordinates.
const texCoords = [
0, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1
];
// This creates the 2 buffers for the vertices and texture coordinates so they can be used
// in the shaders.
this.pBuff = util.createBuffer(gl, vertices);
this.tBuff = util.createBuffer(gl, texCoords);
// These are the attribute locations so that each of the 2 buffers above can be linked to
// the attribute variables in the shader code called ´a_position´ and ´a_texcoord´
this.pAttr = gl.getAttribLocation(this.gProg, "a_position");
this.tAttr = gl.getAttribLocation(this.gProg, "a_texcoord");
// These are all the other uniform locations
this.mmUni = gl.getUniformLocation(this.gProg, "model");
this.pmUni = gl.getUniformLocation(this.gProg, "proj");
this.vmUni = gl.getUniformLocation(this.gProg, "view");
this.cxUni = gl.getUniformLocation(this.gProg, "camx");
this.cyUni = gl.getUniformLocation(this.gProg, "camy");
this.spUni = gl.getUniformLocation(this.gProg, "a_sampler");
this.textM = gl.getUniformLocation(this.gProg, "textM");
// This is the camera
this.camera = new Camera(0, 0, 1);
// This is the texture of the spritesheet
this.mainT = new Texture(this, spritesheet);
this.current_options = {};
/**
* This takes the data json and creates all the object definitions and also
* load all the background textures.
*/
this.loadTextures = () => {
for (var i = 0; i < 2000; i++) {
var obj = dataJson[i];
if (obj != undefined)
this.objectDefs[i] = new ObjectDef(this.gl, obj);
}
this.bgs[1] = new Texture(this, bg1); // I know it's not beatiful but shut up.
this.bgs[2] = new Texture(this, bg2);
this.bgs[3] = new Texture(this, bg3);
this.bgs[4] = new Texture(this, bg4);
this.bgs[5] = new Texture(this, bg5);
this.bgs[6] = new Texture(this, bg6);
this.bgs[7] = new Texture(this, bg7);
this.bgs[8] = new Texture(this, bg8);
this.bgs[9] = new Texture(this, bg9);
this.bgs[10] = new Texture(this, bg10);
this.bgs[11] = new Texture(this, bg11);
this.bgs[12] = new Texture(this, bg12);
this.bgs[13] = new Texture(this, bg13);
this.bgs[14] = new Texture(this, bg14);
this.bgs[15] = new Texture(this, bg15);
this.bgs[16] = new Texture(this, bg16);
this.bgs[17] = new Texture(this, bg17);
this.bgs[18] = new Texture(this, bg18);
this.bgs[19] = new Texture(this, bg19);
this.bgs[20] = new Texture(this, bg20);
this.font = new Font(this, font1_json, font1_img);
this.chat_font = new Font(this, chatfont_json, chatfont_img);
}
// Then runs it lol
this.loadTextures();
/**
* This tries to render the background.
* @param {number} bg background id
* @param {{r: number, g: number, b: number}} tint the tint of the background
* @returns true if successful, false if not
*/
this.renderBG = (bg, tint) => {
var tex = this.bgs[bg];
if (!tex.loaded)
return false;
var gl = this.gl;
util.setCamera(this, 0, 0);
var size = Math.max(this.width, this.height);
util.setModelMatrix(this, 0, 0, size * 2);
util.setFullTexture(this);
util.setTint(this, tint);
util.enableTexture(this, tex, this.spUni);
gl.drawArrays(gl.TRIANGLES, 0, 6);
return true;
};
/**
* This draws a rectangle and is used for the grid
* @param {number} x x position (center)
* @param {number} y y position (center)
* @param {number} width width of the rectangle
* @param {number} height height of the rectangle
* @param {{r: number, g: number, b: number, a: number}} tint This is the color of the rectangle. White by default.
* @param {boolean} toCamera whenever the line should stick to the camera (false by default)
*/
this.renderRect = (x, y, width, height, tint = {r: 1, g: 1, b: 1, a: 1}, toCamera = false) => {
let gl = this.gl;
gl.uniform1i(gl.getUniformLocation(this.gProg, "type"), 1);
if (toCamera)
util.setCamera(this);
else
util.setCamera(this, 0, 0);
let zoom = this.camera.zoom;
util.setModelMatrix(this, x, y, width, height);
util.setTint(this, tint);
gl.drawArrays(gl.TRIANGLES, 0, 6);
gl.uniform1i(gl.getUniformLocation(this.gProg, "type"), 0);
}
/**
* This draws a line and is used for the grid
* @param {number} x if vertical is true, this is the x position. Otherwise it is the y position
* @param {boolean} vertical if the line is vertical or not.
* @param {{r: number, g: number, b: number, a: number}} tint this is the color of the line. White by default.
* @param {number} width line width (1 by default)
* @param {boolean} toCamera whenever the line should stick to the camera (false by default)
*/
this.renderLine = (x, vertical, tint = {r: 1, g: 1, b: 1, opacity: 1}, width = 1, toCamera = false) => {
let gl = this.gl;
gl.uniform1i(gl.getUniformLocation(this.gProg, "type"), 1);
if (!toCamera)
util.setCamera(this, 0, 0);
if (!vertical) {
if (!toCamera)
util.setModelMatrix(this, 0, -this.height/2 + x, this.width, width);
else {
util.setCamera(this, 0, this.camera.y, this.camera.zoom);
util.setModelMatrix(this, 0, x, this.width / this.camera.zoom, width / this.camera.zoom);
}
} else {
if (!toCamera)
util.setModelMatrix(this, -this.width/2 + x, 0, width, this.height);
else {
util.setCamera(this, -this.camera.x, 0, this.camera.zoom);
util.setModelMatrix(this, x, 0, width / this.camera.zoom, this.height / this.camera.zoom);
}
}
util.setTint(this, tint);
gl.drawArrays(gl.TRIANGLES, 0, 6);
gl.uniform1i(gl.getUniformLocation(this.gProg, "type"), 0);
};
this.renderText = (text, x, y, font, scale = 0.5, tint = {r: 1, g: 1, b: 1, opacity: 1}, toCamera = true, centered = true) => {
let gl = this.gl;
text = text + '';
if (toCamera)
util.setCamera(this);
else util.setCamera(this, 0, 0, 1);
util.enableTexture(this, font.tex, this.spUni);
y += 30 * 1.1 * scale;
let len = 0;
if (centered)
for (let i = 0; i < text.length; i++) {
let char = font.getChar(text[i]);
let krn = 0;
if (text[i + 1]) krn = font.getKerning(char, font.getChar(text[i + 1]) );
len += char.advance + krn;
}
let adv = 0;
util.setTint(this, tint);
for (let i = 0; i < text.length; i++) {
let c = text[i];
let char = font.getChar(c);
let nxt;
if (text[i + 1])
nxt = font.getChar(text[i + 1]);
let krn = 0;
if (nxt) krn = font.getKerning(char, nxt);
util.setTexture(this, {x: char.x, y: char.y, w: char.width, h: char.height});
util.setModelMatrix(this, x + (adv + char.xoffset + char.width / 2 + krn - len / 2) * scale, y - (char.yoffset + char.height / 2) * scale, char.width * scale, char.height * scale);
gl.drawArrays(gl.TRIANGLES, 0, 6);
adv += char.advance + krn;
}
util.enableTexture(this, this.mainT, this.spUni);
}
/**
* This will try to render a texture of an object
* @param {{x: number, y: number, w: number, h: number}} tex texture cutout
* @param {number} x x position
* @param {number} y y position
* @param {number} rot rotation in angles
* @param {boolean} xflip whenever it should be horizontally flipped
* @param {boolean} yflip whenever it should be vertically flipped
* @param {{r: 1, g: 1, b: 1, a: 1}} tint the tint of the texture (white by default)
* @param {number} scale the scaling of the texture
* @returns
*/
this.renderTexture = (tex, x, y, rot, xflip, yflip, tint = {r: 1, g: 1, b: 1, opacity: 1, blending: false}, scale = 1) => {
if (tex == undefined)
return;
var gl = this.gl;
var sx = tex.w * 0.48387096774 * xflip * scale;
var sy = tex.h * 0.48387096774 * yflip * scale;
util.setModelMatrix(this, x, y, sx, sy, rot, true);
util.setTint(this, tint);
util.setTexture(this, tex);
if (tint.blending) util.setAdditiveBlending(this, true);
if (tint.i) gl.uniform1i(gl.getUniformLocation(this.gProg, "type"), 2);
gl.drawArrays(gl.TRIANGLES, 0, 6);
gl.uniform1i(gl.getUniformLocation(this.gProg, "type"), 0);
util.setAdditiveBlending(this, false);
}
/**
* This will render the given object
* @param {{}} obj object to be rendered
* @param {number} key the key of the object in the object array
* @returns
*/
this.renderObject = (obj, key = -1) => {
if(!obj) return;
var def = this.objectDefs[obj.id];
if (!def && obj.type != "text")
return;
if ((obj.x > this.cache.camX2 || obj.x < this.cache.camX1) && obj.type != "text")
return;
var rot = obj.r;
var xflip = ( obj.flipx ? -1 : 1 ) || 1;
var yflip = ( obj.flipy ? -1 : 1 ) || 1;
var mainc = obj.baseCol || ( def ? def.maincol : null ) || 1004;
var secc = obj.decorCol || ( def ? def.seccol : null ) || 1;
var maincol = this.cache.getColor(this, mainc);
var seccol = this.cache.getColor(this, secc);
this.cache.objCount++;
var def_tint = {r: 1, g: 1, b: 1, a: 1, opacity: 1, blending: false};
var slc = obj.scale || 1;
if (obj._MICHIGUN) maincol = {r: 1, g: 1, b: 1, opacity: 0.5, blending: false};
if (this.current_options.colored_objects) {
let cld = this.current_options.colored_objects;
if (cld[key]) {
maincol = this.cache.getColor(this, cld[key].base || maincol);
seccol = this.cache.getColor(this, cld[key].decor || seccol);
if (cld[key].base) def_tint = maincol;
}
}
monitor.startCategory("Object rendering");
if (obj.type == "text") {
this.renderText(obj.text, +obj.x, +obj.y, this.font, 0.5 * slc, maincol); // TODO: Text Rotation!!!
} else {
if (def.texture_i)
this.renderTexture(def.texture_i, obj.x, obj.y, rot, xflip, yflip, def_tint, slc);
if (def.texture_l)
this.renderTexture(def.texture_l, obj.x, obj.y, rot, xflip, yflip, maincol, slc);
if (def.texture_b)