-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1329 lines (1172 loc) · 42.1 KB
/
index.html
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
<head>
</head>
<body>
<!--
<script src="source/Type.js"></script>
<script src="source/Attribute.js"></script>
<script src="source/Map.js"></script>
<script src="source/Path.js"></script>
<script src="source/Quest.js"></script>
<script src="source/Action.js"></script>
<script src="source/Object.js"></script>
<script src="source/Attribute.js"></script>
<script src="source/Value.js"></script>
<script src="source/Coordinates.js"></script>
<script src="source/Location.js"></script>
<script src="source/Time.js"></script>
<script src="source/Reference.js"></script>
-->
<script src="source/GL.js"></script>
<!--
<script src="data/Properties.js"></script>
<script src="data/Quests.js"></script>
<script src="data/Resources.js"></script>
<script src="datasta/Actions.js"></script>
<script src="data/Creatures.js"></script>
<script src="data/Items.js"></script>
-->
<script src="lib/Seed.js"></script>
<script src="lib/Noise.js"></script>
<script src="lib/gl-matrix/common.js"></script>
<script src="lib/gl-matrix/mat4.js"></script>
<script src="lib/gl-matrix/vec3.js"></script>
<script>
// stats.js - http://github.com/mrdoob/stats.js
var Stats=function(){var l=Date.now(),m=l,g=0,n=Infinity,o=0,h=0,p=Infinity,q=0,r=0,s=0,f=document.createElement("div");f.id="stats";f.addEventListener("mousedown",function(b){b.preventDefault();t(++s%2)},!1);f.style.cssText="width:80px;opacity:0.9;cursor:pointer";var a=document.createElement("div");a.id="fps";a.style.cssText="padding:0 0 3px 3px;text-align:left;background-color:#002";f.appendChild(a);var i=document.createElement("div");i.id="fpsText";i.style.cssText="color:#0ff;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px";
i.innerHTML="FPS";a.appendChild(i);var c=document.createElement("div");c.id="fpsGraph";c.style.cssText="position:relative;width:74px;height:30px;background-color:#0ff";for(a.appendChild(c);74>c.children.length;){var j=document.createElement("span");j.style.cssText="width:1px;height:30px;float:left;background-color:#113";c.appendChild(j)}var d=document.createElement("div");d.id="ms";d.style.cssText="padding:0 0 3px 3px;text-align:left;background-color:#020;display:none";f.appendChild(d);var k=document.createElement("div");
k.id="msText";k.style.cssText="color:#0f0;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px";k.innerHTML="MS";d.appendChild(k);var e=document.createElement("div");e.id="msGraph";e.style.cssText="position:relative;width:74px;height:30px;background-color:#0f0";for(d.appendChild(e);74>e.children.length;)j=document.createElement("span"),j.style.cssText="width:1px;height:30px;float:left;background-color:#131",e.appendChild(j);var t=function(b){s=b;switch(s){case 0:a.style.display=
"block";d.style.display="none";break;case 1:a.style.display="none",d.style.display="block"}};return{REVISION:11,domElement:f,setMode:t,begin:function(){l=Date.now()},end:function(){var b=Date.now();g=b-l;n=Math.min(n,g);o=Math.max(o,g);k.textContent=g+" MS ("+n+"-"+o+")";var a=Math.min(30,30-30*(g/200));e.appendChild(e.firstChild).style.height=a+"px";r++;b>m+1E3&&(h=Math.round(1E3*r/(b-m)),p=Math.min(p,h),q=Math.max(q,h),i.textContent=h+" FPS ("+p+"-"+q+")",a=Math.min(30,30-30*(h/100)),c.appendChild(c.firstChild).style.height=
a+"px",m=b,r=0);return b},update:function(){l=this.end()}}};
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
// Align top-left
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
document.body.appendChild( stats.domElement );
</script>
<style>
body, html {
padding: 0;
margin: 0;
overflow: hidden;
}
</style>
<script id="2d-vertex-shader" type="x-shader/x-vertex">
attribute vec2 position; // -1..1
//attribute vec2 texture; // 0..1
varying vec2 tile;
void main() {
tile = position;
vec2 pos = position * 2.0 - 1.0;
gl_Position = vec4(pos, 0, 1);
}
</script>
<script id="2d-isometric-vertex-shader" type="x-shader/x-vertex">
precision mediump float;
attribute vec2 position; // -1..1
attribute vec2 texture; // 0..1
uniform vec2 viewport;
uniform vec2 scroll;
uniform mat4 matrix;
uniform mat4 camera;
uniform float tileSize;
uniform float mapSize;
varying vec2 pixel;
varying vec2 tile;
void main() {
tile = texture;
vec2 pos = position;
pos.y = - pos.y - 0.5;
pixel = ((pos + 1.0) / 2.0 * mapSize * tileSize * 2.0);
gl_Position = vec4(position, 0, 1) * matrix * camera;
gl_Position.xy += scroll / viewport * 2.0;
}
</script>
<script id="2d-computer-shader" type="x-shader/x-fragment">
// precomputes sprite data
precision mediump float;
uniform sampler2D terrain;
uniform sampler2D subterrain;
uniform sampler2D objects;
uniform sampler2D subobjects;
uniform float mapSize;
varying vec2 tile;
vec2 getPosition(vec2 shift) {
return (floor(mod(tile, 0.5) * 2.0 * mapSize) + shift) / mapSize;
}
vec4 get(sampler2D texture, vec2 shift) {
return floor(texture2D(texture, getPosition(shift)) * 255.0);
}
float encodeTransition(float x, float a, float b, float c, float d, float e, float f, float g, float h) {
return (128.0 * float(x > a)) +
(64.0 * float(x > b)) +
(32.0 * float(x > c)) +
(16.0 * float(x > d)) +
(8.0 * float(x > e)) +
(4.0 * float(x > f)) +
(2.0 * float(x > g)) +
(1.0 * float(x > h));
}
float encodeReverseTransition(float x, float a, float b, float c, float d, float e, float f, float g, float h) {
float result = 0.0;
if (x < a || a > 0.0)
result += 128.0;
if (x < b || b > 0.0)
result += 64.0;
if (x < c || c > 0.0)
result += 32.0;
if (x < d || b > 0.0)
result += 16.0;
if (x < e)
result += 8.0;
if (x < f)
result += 4.0;
if (x < g)
result += 2.0;
if (x < h)
result += 1.0;
return result;
}
vec4 getLocation(float alpha, vec2 shift) {
vec4 sub = get(subterrain, shift);
vec4 location = get(terrain, shift);
if (sub.r > 0.0 && location.a != alpha)
location.rgb = sub.rgb;
return location;
}
vec4 getOcclusion() {
vec4 result = vec4(0,0,0,0);
int subobject = 0;
// RGB channels encode up to 24 sprite occlusions
// (originating at different tiles) over given tile
foreach (
r 128.0 4 4 100.0,
r 64.0 3 4 100.0,
r 32.0 4 3 100.0,
r 16.0 2 4 200.0,
r 8.0 3 3 100.0,
r 4.0 4 2 200.0,
r 2.0 1 4 200.0,
r 1.0 2 3 100.0,
g 128.0 3 2 100.0,
g 64.0 4 1 200.0,
g 32.0 0 4 200.0,
g 16.0 1 3 200.0,
g 8.0 2 2 100.0,
g 4.0 3 1 200.0,
g 2.0 4 0 200.0,
g 1.0 0 3 200.0,
b 128.0 1 2 100.0,
b 64.0 2 1 100.0,
b 32.0 3 0 200.0,
b 16.0 0 2 200.0,
b 8.0 1 1 0.0,
b 4.0 2 0 200.0,
b 2.0 0 1 0.0,
b 1.0 1 0 0.0
as $channel, $value, $x, $y, $size)
if (get(objects, vec2($x, $y)).r > $size) {
result.$channel += $value;
if (get(subobjects, vec2($x, $y)).r > $size)
subobject = 1;
}
endforeach
vec4 x = get(terrain, vec2(0, 0));
vec4 a = getLocation(x.a, vec2(-1, 1));
vec4 b = getLocation(x.a, vec2(-1, 0));
vec4 c = getLocation(x.a, vec2(-1, -1));
vec4 d = getLocation(x.a, vec2(0, -1));
vec4 e = getLocation(x.a, vec2(1, -1));
vec4 f = getLocation(x.a, vec2(1, 0));
vec4 g = getLocation(x.a, vec2(1, 1));
vec4 h = getLocation(x.a, vec2(0, 1));
result.a = encodeReverseTransition(x.a, a.a, b.a, c.a, d.a, e.a, f.a, g.a, h.a);
/*
if (get(subterrain, vec2(0, 0)).r > 0.0)
result.a += 128.0;
if (subobject == 1)
result.a += 64.0;
*/
return result;
}
vec4 getTransition() {
vec4 x = get(terrain, vec2(0, 0));
vec4 a = getLocation(x.a, vec2(-1, 1));
vec4 b = getLocation(x.a, vec2(-1, 0));
vec4 c = getLocation(x.a, vec2(-1, -1));
vec4 d = getLocation(x.a, vec2(0, -1));
vec4 e = getLocation(x.a, vec2(1, -1));
vec4 f = getLocation(x.a, vec2(1, 0));
vec4 g = getLocation(x.a, vec2(1, 1));
vec4 h = getLocation(x.a, vec2(0, 1));
return vec4(
encodeTransition(x.r, a.r, b.r, c.r, d.r, e.r, f.r, g.r, h.r),
encodeTransition(x.g, a.g, b.g, c.g, d.g, e.g, f.g, g.g, h.g),
encodeTransition(x.b, a.b, b.b, c.b, d.b, e.b, f.b, g.b, h.b),
encodeTransition(x.a, a.a, b.a, c.a, d.a, e.a, f.a, g.a, h.a)
);
}
void main() {
if (tile.y < 0.5) {
if (tile.x < 0.5) {
gl_FragColor = getOcclusion() / 255.0;
} else {
gl_FragColor = getTransition() / 255.0;
}
} else {
if (tile.x < 0.5) {
gl_FragColor = vec4(1,0,0,1);
} else {
gl_FragColor = vec4(0,1,0,1);
}
}
}
</script>
<script id="2d-fragment-shader" type="x-shader/x-fragment">
precision mediump float;
float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
/*
# Isometric map renderer shader.
Renders terrain and object sprites (up to 3x5 tiles) and multilevel
landscape in one pass. Takes specially crafted images as input,
where each pixel represents a tile, and each color channel contains
encoded value.
## Input textures
Map texture. Value in each channel varies depending on neighbour
tiles values. There's autogenerated transitions texture below
that contains id of a mask that should be applied to the sprite.
If a tile has z-level value different from baseline, the tile is
shifted on z-axis, so the texture only specifies the map surface.
Subterrain map is a flat projection onto z-elevated map. It only
specifies the tiles that are visible and are not surface tiles.
R: Terrain texture
G: Overlay texture
B: Water sprite
A: Z-level
*/
uniform sampler2D terrain;
uniform sampler2D subterrain;
uniform sampler2D objects;
uniform sampler2D subobjects;
uniform sampler2D overlay;
// generated textures (via separate shaders)
uniform sampler2D data;
// previous frame
uniform sampler2D frame;
// terrain textures and masks
uniform sampler2D spritesLandscape;
uniform sampler2D spritesTerrain;
uniform sampler2D spritesTransitions;
// object spritesheets
uniform sampler2D sprites64x64;
uniform sampler2D sprites64x160;
uniform sampler2D sprites192x160;
uniform vec2 viewport;
uniform vec2 textureSize;
uniform float tileSize;
uniform float mapSize;
uniform int iteration;
uniform vec2 scroll;
uniform vec2 scrolled;
varying vec2 pixel;
varying vec2 tile;
vec2 getPixelPosition(vec2 tile) {
vec2 cell = floor(tile * mapSize);
return vec2(
(cell.x - cell.y) * (tileSize)
+ (mapSize * tileSize ) - tileSize,
(cell.x + cell.y) * (tileSize / 2.0)
);
}
vec2 getCoordinates() {
return (tile * mapSize - floor(tile * mapSize));
}
vec2 getPosition(vec2 shift) {
return (floor(tile * mapSize) + shift) / mapSize;
}
vec4 get(sampler2D texture, vec2 shift) {
return floor(texture2D(texture, getPosition(shift)) * 255.0);
}
vec4 get(sampler2D texture, vec2 shift, int quadrant) {
vec2 position = getPosition(shift) / 2.0;
if (quadrant == 1) {
position += vec2(0.5, 0);
} else if (quadrant == 2) {
position += vec2(0, 0.5);
} else if (quadrant == 3) {
position += vec2(0.5, 0.5);
}
return floor(texture2D(texture, position) * 255.0);
}
float getVariation(vec2 position, float seed) {
return rand(floor(position * mapSize * seed) / 2.0);
}
float getRandom(vec2 position, float range, float seed) {
return floor(getVariation(position, seed) * range);
}
vec4 getColor(sampler2D texture, vec2 locals, vec2 position, vec2 grid, vec2 shift, vec2 translate) {
vec2 offset = position * grid;
vec2 start = getPixelPosition(tile + shift / mapSize);
vec2 diff = ((pixel - locals) - start);
vec2 point = floor(locals + diff + translate) + 0.5;
point.x += (grid.x - tileSize * 2.0) / 2.0;
point.y += (grid.y - tileSize);
if (point.x > 0.0 && point.x < grid.x && point.y > 0.0 && point.y < grid.y)
return texture2D(texture, (point + offset) / textureSize);
else
return vec4(0, 0, 0, 0);
}
float getMask(float value, bool diagonals) {
// unpack bitmask
foreach(
a 128.0,
b 64.0,
c 32.0,
d 16.0,
e 8.0,
f 4.0,
g 2.0,
h 1.0
as $flag, $value)
bool $flag = (value >= $value);
value -= min(float($flag) * 256.0, $value);
endforeach
vec2 coordinates = (tile * mapSize - floor(tile * mapSize));
return
max(19.0 * float(b && d && f && h),
max(16.0 * float(a && !h && !b && coordinates.x < 0.5 && coordinates.y > 0.5),
max(15.0 * float(c && !d && !b && coordinates.x < 0.5 && coordinates.y < 0.5),
max(14.0 * float(e && !d && !f && coordinates.x > 0.5 && coordinates.y < 0.5),
max(13.0 * float(g && !h && !f && coordinates.x > 0.5 && coordinates.y > 0.5),
max(12.0 * float(h && b && d),
max(11.0 * float(b && d && f),
max(10.0 * float(d && f && h),
max(9.0 * float(f && h && b),
max(8.0 * float(h && b),
max(7.0 * float(b && d),
max(6.0 * float(d && f),
max(5.0 * float(f && h),
max(4.0 * float(b && coordinates.x < 0.5),
max(3.0 * float(d && coordinates.y < 0.5),
max(2.0 * float(f && coordinates.x > 0.5),
(1.0 * float(h && coordinates.y > 0.5)))))))))))))))))
);
}
// get color of a mask pixel
// composes sprites combinations (corners + sides)
vec4 getTransitionedColor(vec2 locals, vec2 position, vec2 shift, vec2 translate) {
vec2 coordinates = (tile * mapSize - floor(tile * mapSize));
float x = position.x;
if (x == 0.0)
return vec4(0,0,0,1);
float sprite = 0.0;
position.x = x - 1.0;
vec2 grid = vec2(tileSize * 2.0, tileSize);
return getColor(spritesTransitions, locals, position, grid, vec2(0, 0), translate);
}
// dither neighbhour terrain textures
vec4 getBestCorner(vec2 pos, float variation, vec2 first, vec2 second, vec2 third, bool a, bool b, bool c) {
float results = 0.0;
if (a)
results++;
if (b) {
results++;
if (!a)
first = second;
}
if (c) {
results++;
if (!a && !b)
first = third;
else if (!a || !b)
second = third;
}
vec2 result = vec2(0,0);
if (results > 0.0) {
float choice = floor(results * (variation));
if (choice == 2.0)
result = third;
else if (choice == 1.0)
result = second;
else
result = first;
return get(terrain, result);
} else {
return vec4(0,0,0,0);
}
}
vec4 getCorner(vec2 locals, vec2 shift, vec2 translate, float value) {
vec2 coordinates = getCoordinates();
vec2 now = floor(tile * mapSize + shift);
float variation = getVariation(floor(now + coordinates * tileSize), 1.0);
float halfSize = tileSize / 2.0;
float thirdSize = tileSize / 1.5;
float sixthSize = tileSize / 3.0;
vec2 pos = now / mapSize;
// unpack transition bitmask
foreach(
a 128.0,
b 64.0,
c 32.0,
d 16.0,
e 8.0,
f 4.0,
g 2.0,
h 1.0
as $flag, $value)
bool $flag = (value >= $value);
value -= min(float($flag) * 256.0, $value);
endforeach
if (coordinates.x < 1.0 / 3.0 && coordinates.y < 1.0 / 3.0) {
vec4 corner = getBestCorner(pos, variation, vec2(-1, -1), vec2(-1, 0), vec2(0, -1), c, b, d);
if (corner.r > 0.0)
return corner;
}
if (coordinates.x > 2.0 / 3.0 && coordinates.y < 1.0 / 3.0) {
vec4 corner = getBestCorner(pos, variation, vec2(1, -1), vec2(0, -1), vec2(1,0), e, d, f);
if (corner.r > 0.0)
return corner;
}
if (coordinates.x > 2.0 / 3.0 && coordinates.y > 2.0 / 3.0) {
vec4 corner = getBestCorner(pos, variation, vec2(1, 1), vec2(1, 0), vec2(0, 1), g, f, h);
if (corner.r > 0.0)
return corner;
}
if (coordinates.x < 1.0 / 3.0 && coordinates.y > 2.0 / 3.0) {
vec4 corner = getBestCorner(pos, variation, vec2(-1, 1), vec2(-1, 0), vec2(0, 1), a, b, h);
if (corner.r > 0.0)
return corner;
}
if (coordinates.x < 1.0 / 2.0 && coordinates.y < 1.0 / 2.0) {
vec4 corner = getBestCorner(pos, variation, vec2(-1, 0), vec2(-1, 0), vec2(-1, -1), b, d, c);
if (corner.r > 0.0)
return corner;
}
if (coordinates.x > 1.0 / 2.0 && coordinates.y < 1.0 / 2.0){
vec4 corner = getBestCorner(pos, variation, vec2(0, -1), vec2(1, 0), vec2(1, -1), d, f, e);
if (corner.r > 0.0)
return corner;
}
if (coordinates.x > 1.0 / 2.0 && coordinates.y > 1.0 / 2.0){
vec4 corner = getBestCorner(pos, variation, vec2(1, 0), vec2(0, 1), vec2(1, 1), f, h, g);
if (corner.r > 0.0)
return corner;
}
if (coordinates.x < 1.0 / 2.0 && coordinates.y > 1.0 / 2.0) {
vec4 corner = getBestCorner(pos, variation, vec2(0, 1), vec2(-1, 0), vec2(-1, 1), h, b, a);
if (corner.r > 0.0)
return corner;
}
return vec4(0,0,0,0);
}
vec4 getTerrainColor(vec2 locals, vec2 shift, vec4 location, vec4 transition) {
vec2 translate = vec2(0,0);
vec2 grid = vec2(tileSize * 2.0, tileSize);
vec2 here = vec2(0, 0);
float variation = getRandom(getPosition(shift), 4.0, 1.0);
vec2 terrain = vec2(variation, location.r - 1.0);
if (transition.r > 0.0) {
vec2 mask = vec2(getMask(transition.r, true), 1);
vec4 masked = getTransitionedColor(locals, mask, here, translate);
if (masked.a == 0.0) {
vec4 corner = getCorner(locals, shift, translate, transition.r);
if (corner.r > 0.0)
terrain.y = corner.r - 1.0;
}
}
return getColor(spritesTerrain, locals, terrain, grid, vec2(0, 0), translate);
}
vec4 getOverlayColor(vec2 locals, vec2 shift, vec4 location, vec4 transition) {
vec2 translate = vec2(0,0);
if (transition.b > 0.0) {
vec2 mask = vec2(getMask(transition.b, true), 0);
vec4 masked = getTransitionedColor(locals, mask, vec2(0, 0), translate);
if (!(masked.r == 0.0 && masked.g == 0.0 && masked.b == 0.0 && masked.a == 1.0))
return masked;
}
float variation = getRandom(shift, 4.0, 1.0);
vec2 terrain = vec2(variation, 4.0);
vec2 grid = vec2(tileSize * 2.0, tileSize);
return getColor(spritesTerrain, locals, terrain, grid, vec2(0, 0), translate);
}
vec4 getObjectColor(vec4 object, vec2 locals, vec2 shift) {
vec2 translate = vec2(0, 0);
object.xy -= 1.0;
if (object.x >= 200.0) {
vec2 grid192x160 = vec2(tileSize * 6.0, tileSize * 5.0);
object.x -= 200.0;
return getColor(sprites192x160, locals, object.xy, grid192x160, shift, translate);
} else if (object.x >= 100.0) {
object.x -= 100.0;
vec2 grid64x160 = vec2(tileSize * 2.0, tileSize * 5.0);
return getColor(sprites64x160, locals, object.xy, grid64x160, shift, translate);
} else {
vec2 grid64x64 = vec2(tileSize * 2.0, tileSize * 2.0);
return getColor(sprites64x64, locals, object.xy, grid64x64, shift, translate);
}
return vec4(0, 0, 0, 0);
}
vec4 blend(vec4 overlay, vec4 color) {
overlay.rgb *= overlay.a;
color.rgb *= color.a;
vec3 blended = overlay.rgb + ((1.0-overlay.a)*color.rgb);
float alpha = color.a + (1.0-color.a)*overlay.a;
return vec4(blended, alpha);
}
vec4 blend(vec4 overlay, vec4 color, float a, float b) {
return a > b ? blend(color, overlay) : blend(overlay, color);
}
float getZIndex(float index, vec2 shift) {
float height = floor(index);
vec2 pos = floor(tile * mapSize) + shift + height;
return pos.y * mapSize + pos.x + height / 255.0;
}
vec4 render() {
float z = 0.0;
vec2 locals = pixel - getPixelPosition(tile);
vec4 color = vec4(0, 0, 0, 0);
vec2 shift = vec2(0, 0);
vec4 occluded = get(data, shift, 0);
vec2 grid = vec2(tileSize * 2.0, tileSize * 2.0);
vec2 translate = vec2(0,0);
int subterrained = 0;/*
if (occluded.a >= 128.0) {
occluded.a -= 128.0;
subterrained = 1;
}*/
int subobjected = 0;
/*
if (occluded.a >= 64.0) {
occluded.a -= 64.0;
subobjected = 1;
}*/
vec4 transition = get(data, shift, 1);
vec4 location = get(terrain, shift);
float value = occluded.a;
// check if a pixel is occluded by an object sprite
// in one of 24 possible tiles
foreach (
r 128.0 4 4 100.0,
r 64.0 3 4 100.0,
r 32.0 4 3 100.0,
r 16.0 2 4 200.0,
r 8.0 3 3 100.0,
r 4.0 4 2 200.0,
r 2.0 1 4 200.0,
r 1.0 2 3 100.0,
g 128.0 3 2 100.0,
g 64.0 4 1 200.0,
g 32.0 0 4 200.0,
g 16.0 1 3 200.0,
g 8.0 2 2 100.0,
g 4.0 3 1 200.0,
g 2.0 4 0 200.0,
g 1.0 0 3 200.0,
b 128.0 1 2 100.0,
b 64.0 2 1 100.0,
b 32.0 3 0 200.0,
b 16.0 0 2 200.0,
b 8.0 1 1 0.0,
b 4.0 2 0 200.0,
b 2.0 0 1 0.0,
b 1.0 1 0 0.0
as $channel, $value, $x, $y, $size)
if (occluded.$channel >= $value) {
occluded.$channel -= $value;
vec2 subshift = vec2($x, $y);
vec4 object = get(objects, subshift);
float index = getZIndex(object.a, subshift);
if (color.a < 1.0 || index > z) {
vec4 result = getObjectColor(object, locals, subshift);
if (color.a < 1.0) {
vec4 subobject = get(subobjects, subshift);
}
if (result.a > 0.0) {
color = blend(color, result, index, z);
z = index;
}
}
}
endforeach
// unpack bitmask with landscape transitions
foreach(
a 128.0,
b 64.0,
c 32.0,
d 16.0,
e 8.0,
f 4.0,
g 2.0,
h 1.0
as $flag, $value)
bool $flag = (value >= $value);
value -= min(float($flag) * 256.0, $value);
endforeach
// check if pixel is occluded by surrounding subterrain
foreach (
g 1 1,
h 0 1,
f 1 0,
b -1 0,
d 0 -1,
c -1 -1
as $flag, $x, $y)
if ($flag) {
vec2 subshift = vec2($x, $y);
vec4 origin = get(terrain, subshift);
float index = getZIndex(origin.a, subshift);
float rest = mod(origin.a * 255.0, 2.0);
if (color.a < 1.0 || index > z) {
//if (rest == 0.0) {
vec2 position = vec2(getRandom(getPosition(shift + subshift), 4.0, 1.0), 0);
vec4 result = getColor(spritesLandscape, locals, position, grid, subshift + 1.0, translate);
if (result.a > 0.0) {
color = blend(color, result, index, z);
z = index;
}
//} else {
// vec4 subtransition = get(data, subshift + 1.0, 1);
// float mask = getMask(subtransition.a, false);
// vec2 position = vec2(mask, 1);
// vec4 result = getColor(spritesLandscape, locals, position, grid, subshift +// 1.0, translate);
// if (result.a > 0.0) {
// color = blend(color, result, index, z);
// z = index;
// }
//}
}
}
endforeach
float index = getZIndex(location.a, shift);
if (color.a < 1.0 || index > z) {
float random = getRandom(getPosition(shift), 4.0, 1.0);
vec4 transitioned = vec4(0,0,0,0);
vec4 object = get(objects, shift);
// pick color of an object in this tile
if (object.r > 0.0) {
float objectIndex = getZIndex(object.a, shift);
if (color.a < 1.0 || objectIndex > z) {
vec4 obj = getObjectColor(object, locals, shift);
if (obj.a > 0.0) {
color = blend(color, obj, objectIndex, z);
if (objectIndex > z)
z = objectIndex;
}
}
}
// pick ground color (if not occluded by object)
if (color.a < 1.0 || index > z) {
// apply landscape transition mask
if (transition.a != 0.0) {
float mask = getMask(transition.a, true);
vec2 pos = vec2(mask, 5.0 + (random > 1.0 ? 1.0 : 0.0));
transitioned = getTransitionedColor(locals, pos, vec2(0, 0), vec2(0,0));
}
// pick terrain color with mask applied
if (transitioned.a > 0.0 || transition.a == 0.0) {
if (location.b > 0.0)
transitioned = getOverlayColor(locals, shift, location, transition);
else
transitioned = vec4(0,0,0,0);
if (transitioned.a < 1.0) {
transitioned = blend(transitioned, getTerrainColor(locals, shift, location, transition));
}
}
// pick landscape color
vec2 position = vec2(random, 0);
if (transitioned.a < 1.0) {
transitioned = getColor(spritesLandscape, locals, position, grid, shift + 1.0, translate);
}
if (transitioned.a > 0.0)
color = blend(color, transitioned, index, z);
}
}
// pick subterrain color
if (color.a < 1.0) {
vec4 l = get(subterrain, shift);
vec4 t = vec4(0,0,0,0);
if (l.b > 0.0)
color = blend(color, getOverlayColor(locals, shift, l, t), location.a, z);
if (color.a < 1.0)
color = blend(color, getTerrainColor(locals, shift, l, t), location.a, z);
}
return color;
}
void main() {
vec2 diff = (scroll - scrolled);
vec2 offset = diff / viewport;
if ((gl_FragCoord.x < diff.x || gl_FragCoord.x > viewport.x + diff.x)
|| (gl_FragCoord.y < diff.y || gl_FragCoord.y > viewport.y + diff.y)
|| (iteration == 0)) {
gl_FragColor = render();
} else {
gl_FragColor = texture2D(frame, (gl_FragCoord.xy - diff) / viewport);
}
}
</script>
<script id="2d-upscaler-shader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D frame;
uniform float depth;
uniform vec2 viewport;
varying vec2 tile;
void main() {
gl_FragColor = texture2D(frame, tile);
}
</script>
<canvas id="canvas" width="512" height="512"></canvas>
<script>
var seed = parseFloat(location.search.split('seed=')[1]) || Math.random();
function setup() {
var options = {
zoom: 2,
speed: parseFloat(location.search.split('speed=')[1]) || 2,
mapSize: parseFloat(location.search.split('size=')[1]) || 256,
range: parseFloat(location.search.split('range=')[1]) || 600,
tileSize: parseFloat(location.search.split('tilesize=')[1]) || 32,
textureSize: 1280,
flat: location.search.indexOf('flat') > -1,
zooming: location.search.indexOf('zooming') > -1,
objects: parseFloat(location.search.split('objects=')[1]) || 100,
maxheight: parseFloat(location.search.split('maxheight=')[1]) || 4,
seed: seed,
surface: location.search.indexOf('surface') > -1,
limit: parseFloat(location.search.split('limit=')[1]) || Infinity,
profile: parseFloat(location.search.split('profile=')[1]),
depth: parseFloat(location.search.split('depth=')[1]) || window.devicePixelRatio || 1
};
Math.seedrandom(seed)
var random2 = Math.random;
Math.seedrandom(seed)
// unroll some pseudo loops
var unroll = function(source) {
return source.replace(/foreach\s*\(\s*([^]*?)\s*as\s*([^]*?)\s*\)\s*([^]*?)endforeach/gm, function(s, table, keys, body) {
keys = keys.split(/\s*,\s*/);
return table.split(/\s*,\s*/g).map(function(values) {
var iteration = body;
values.split(/\s+/g).forEach(function(value, i) {
while (iteration.indexOf(keys[i]) > -1)
iteration = iteration.replace(keys[i], value);
})
return iteration;
}).join("\n")
})
}
var canvas = document.getElementById('canvas');
canvas.addEventListener("webglcontextlost", function(event) {
event.preventDefault();
}, false);
var gl = GL(canvas);
var vertex = GL.Script(gl, '2d-vertex-shader')
var isoVertex = GL.Script(gl, '2d-isometric-vertex-shader')
var computer = GL.Program(gl, [vertex, GL.Script(gl, '2d-computer-shader',
unroll)])
var program = GL.Program(gl, [isoVertex, GL.Script(gl, '2d-fragment-shader', unroll)])
var upscaler = GL.Program(gl, [vertex, GL.Script(gl, '2d-upscaler-shader')])
var isometry = new Float32Array([
//x y u v
0,-0.5, 1, 1,
-1, 0, 0, 1,
1, 0, 1, 0,
-1, 0, 0, 1,
0, 0.5, 0, 0,
1, 0, 1, 0,
]);
var rectangle = new Float32Array([
-1, -1,
1, -1,
-1, 1,
1, -1,
1, 1,
-1, 1,
]);
var Textures = {
'spritesTerrain': 'examples/sprites/terrain.png',
'spritesLandscape': 'examples/sprites/landscape.png',
'spritesTransitions': 'examples/sprites/transitions.png',
'sprites64x64': 'examples/sprites/sprites64x64.png',
'sprites64x160': 'examples/sprites/sprites64x160.png',
'sprites192x160': 'examples/sprites/sprites192x160.png'
};
var textures = {};
var loading = [];
for (var name in Textures) {
!function(name) {
loading.push(name);
load(Textures[name], function() {
var texture = textures[name] = {
image: this,
location: gl.getUniformLocation(program, name)
}
loading.pop();
if (!loading.length)
start()
})
}(name)
}
var start = function() {
render()
if (options.profile) {
console.profile('render ' + options.profile + ' frames');
for (var i = 0; i < options.profile; i++) {
render()
//gl.finish()
}
console.profileEnd('render ' + options.profile + ' frames');
}
}
var generateMap = function() {
var terrain = getImageData('terrain');
var subterrain = getImageData('subterrain');
var objects = getImageData('objects')
for (var i = 0, j = Math.pow(options.mapSize, 2); i < j; i++) {
var values = [
// R: terrain
Math.floor(Math.random() * 3) + 1,
// G: overlay
0, //Math.floor(Math.random() * 4) + 1;
// B: water
matchRegion(terrain, i, options.mapSize, water) && 1,
// A: elevation
0
]
var regioned = matchRegion(terrain, i, options.mapSize, mountians);
var x = i % options.mapSize;
var y = Math.floor(i / options.mapSize);
var height = Math.floor((Noise(x / 15, y / 15) / 2 + 1) * options.maxheight);
for (var k = options.surface ? height : 0; k <= height; k++) {
var pos = (i - k * options.mapSize - k) * 4;
if (!subterrain.data[pos] && terrain.data[pos]) {
subterrain.data[pos + 0] = terrain.data[pos + 0]
subterrain.data[pos + 1] = terrain.data[pos + 1]
subterrain.data[pos + 2] = terrain.data[pos + 2]
subterrain.data[pos + 3] = terrain.data[pos + 3]
}
terrain.data[pos + 0] = values[0]
terrain.data[pos + 1] = values[1]
terrain.data[pos + 2] = values[2]
terrain.data[pos + 3] = values[3] + k;
if (k == height && Math.random() > 0.85) {
generateObject(objects, pos, values[3] + k);
}
}
}
return {
terrain: terrain,
subterrain: subterrain,
objects: objects
};
}
var water = [
[12, 10, 8, 5],
//[21, 10, 0, 0],
[15, 17, 0, 3],
[14, 20, 0, 0],
[12, 21, 1, 0],
[14, 18, 5, 0],
[18, 17, 0, 3],
[17, 21, 0, 0],
];
var mountians = [
[8, 14, 1, 1],
[10, 20, 0, 1],
[11, 19, 0, 1],
[12, 21, 0, 0]
]
var scroll = 0;
var lastScroll = 0;
var matchRegion = function(data, i, width, regions) {
var x = i % width
var y = Math.floor(i / width);
for (var pool, j = 0; pool = regions[j++];) {
if (x >= pool[0] && y >= pool[1] && x <= pool[0] + pool[2] && y <= pool[1] + pool[3]) {
return pool;
break;
}
}
}
var generateOverlay = function() {
var data = getImageData('overlay');
return data;
}
var generateObject = function(data, i, height, size, x, y) {
if (size == null)