forked from SuperSodaSea/Omniscent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Omniscent.js
1044 lines (946 loc) · 47.5 KB
/
Omniscent.js
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
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*/
'use strict';
class Omniscent {
constructor(canvas) {
this.canvas = canvas;
this.renderer = new THREE.WebGLRenderer({canvas: canvas, antialias: false});
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.autoClear = false;
this.backgroundScene = new THREE.Scene();
this.backgroundMaterial = new THREE.MeshBasicMaterial({
depthTest: false,
side: THREE.DoubleSide,
});
this.backgroundGeometry = new THREE.PlaneBufferGeometry(1, 1);
this.backgroundMesh = new THREE.Mesh(this.backgroundGeometry, this.backgroundMaterial);
this.backgroundScene.add(this.backgroundMesh);
this.backgroundCamera = new THREE.OrthographicCamera(-0.5, 0.5, -0.5, 0.5, 0.0, 1.0);
this.scene = new THREE.Scene();
this.sceneMaterial = new THREE.MeshBasicMaterial({
depthTest: false,
alphaTest: 0.5,
side: THREE.BackSide,
vertexColors: true,
});
this.sceneMesh = new THREE.Mesh(undefined, this.sceneMaterial);
this.scene.add(this.sceneMesh);
this.camera = new THREE.Camera();
this.camera.projectionMatrix.set(
2 / 320 * 1.2, 0, 0, 0,
0, 2 / (179 - 21 + 1), 0, 0,
0, 0, 0, -1 / 512,
0, 0, -1 / 160, 13 / 160,
);
this.camera.scale.y = -1;
window.addEventListener('resize', () => this.onResize(), false);
this.onResize();
this.FONT = new Uint8Array(Base64.toUint8Array('AAAAAAAAAAB+gaWBvZmBfn7/2//D5/9+Nn9/fz4cCAAIHD5/PhwIABw+HH9/Phw+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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3AADANywHwDcgAAAHHAxwNzAMADcXAAA3IAAANxAMADcwzAA3EAAAMxDMyj/zIAAADwAHAGFLAA3UYUMADdVrIADVbEMt0AVjLdAFYy3QBWcADwVnAAu1YysAtGcrsAVnGwsFZxAhFWcSIRVjEgEVZzC7BWc7sAVjALC0YwALtW/6H4+JihpvCnszcQNlCEhYWE8PCyp6amp/MmcCeAREV3dqawYWFSp3cwdoCiAUFBAfUw9LOk+KXQR0bwpoCncKcQpo7wUI+y+KOAjvBQj7cQtvCApqfwh/Bwhjbw8JA3gyzw8PAwLYIwtbTypjCng4Om8Find3bw8PDw+PgAAQBm709NTklTQ0VOVChDKSAxOTk3IFNBTkNUSU9OAAClwwAAAAAAAIBCAAAAAAAAgL8AAAAAAAAAAAAAAAAAAIC/AACAPwAAAAAAAAAA');
this.MEMORY = new Uint8Array(0x44000);
// 0x0104-0x0119
this.background = new Uint8Array(this.MEMORY.buffer, 0x10000, 320 * 200);
this.frameBuffer = new Uint8Array(this.MEMORY.buffer, 0x20000, 320 * 200);
this.textures = new Array(0x14);
for(let i = 0; i < 0x14; ++i)
this.textures[i] = new Uint8Array(this.MEMORY.buffer, 0x30000 + 0x1000 * i, 0x1000);
this.reset();
}
reset() {
// Use WebGL to render model instead of original software rasterizer
this.hardwareRenderer = false;
this.MEMORY.fill(0x00);
for(let i = 0; i < 0x12AE; ++i)
this.MEMORY[i + 0x0100] = this.DATA[i];
// Uint32 [0x0DDB]
this.randomA = 0x08088405;
// Uint32 [0x0DDF]
this.randomB = 0x000010FB;
this.cameraPosition = new Float32Array([-330.0, 0.0, 64.0]);
this.cameraMatrix0 = new Float32Array([0.0, -1.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0]);
this.cameraMatrix1 = new Float32Array([0.0, -1.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0]);
this.backgroundStarDrawn = false;
this.rendererCounter = 0;
this.cameraDataCounter = 0;
this.cameraDataPointer = 0;
this.cameraA = 0;
// Int16 [0x1360]
this.cameraDir = 1;
this.cameraData = new Int16Array(4);
// Int16 [0x0DD5]
this.cameraSpeed = 1792;
this.doorOffset = this.toInt16(this.MEMORY[0x1362] | (this.MEMORY[0x1363] << 8));
this.running = false;
this.stopping = false;
this.generateMIDI();
this.generateModel();
this.generateText();
this.generatePalette();
this.generateTexture();
}
// 0x014C-0x016A
generateMIDI() {
// 0x0DE3-0x0F27: MIDI Data
this.midiData = [];
let p = 0x0DE3;
while(this.MEMORY[p] - 1 >= 0) {
let channelData = {};
channelData.channel = this.MEMORY[p++] - 1;
channelData.instrument = this.MEMORY[p++];
channelData.data = [];
p = this.generateMIDILoop(p, channelData);
channelData.data = new Uint8Array(channelData.data);
this.midiData.push(channelData);
}
}
generateText() {
// 0x0173-0x018B
let data = new Uint8Array(320 * 200);
this.drawText(data, 6, 1, 'OMNISCENT', 0x1C);
this.drawText(data, 1, 12, '(C) 1997 SANCTION', 0x1C);
// 0x0360-0x0376
for(let i = 0; i < 0x7D00; ++i)
this.background[i * 2] = this.background[i * 2 + 1] = data[0x0640 + i];
}
// 0x018C-0x01BC
generatePalette() {
// 0x0F28-0x0F68: Palette data
// 0x1408-0x1707: Generated palette data
this.palette = new Uint8Array(3 * 0x100);
let a = 0x0000, b = 0x0001;
for(let i = 0; i < 3; ++i) {
let p = 0x0F28, q = 0x00;
a = this.MEMORY[p];
// Don't clear DL?
b &= 0x00FF;
let entryCount = this.MEMORY[p++];
for(let j = 0; j < entryCount; ++j) {
a = (a & 0x00FF) | (this.MEMORY[p + i + 1] << 8);
a = ((a - b) / this.MEMORY[p]) | 0;
for(let k = 0; k < this.MEMORY[p]; ++k, ++q) {
b += a;
this.palette[q * 3 + i] = b >> 8;
}
p += 4;
}
}
// Original palette is 18bpp, convert it to 24bpp.
for(let i = 0; i < 3 * 0x100; ++i)
this.palette[i] = (this.palette[i] << 2) | (this.palette[i] >> 4);
return this.palette;
}
// 0x01BD-0x02FE
generateTexture() {
// 0x0F69-0x1004: Texture pattern data
// 0x1005-0x1020: Texture background data
// 0x41ED-0x4246: Texture 0x01 data
// 0x01BD-0x01C7
// Generate texture 0x11
this.generateCircleNoise(0x11, 0x05, 0x0320);
// 0x01C8-0x01D1
// Generate texture 0x10
this.generateCircleNoise(0x10, 0x0F, 0x0070);
// 0x01D2-0x01F4
// Generate texture 0x12
this.textures[0x12].fill(0x14);
for(let i = 0; i < 0xFC0; ++i) {
let a = this.random(0x4);
a = (a + this.textures[0x12][i] + this.textures[0x12][i + 1]) & 0xFF;
a = (a - 1) >> 1;
this.textures[0x12][i + 0x40] = a;
}
// 0x01F5-0x020D
// Generate texture 0x12
this.textures[0x13].fill(0x03);
for(let i = 0; i < 0x1000; ++i) {
let p = this.random(0x1000);
++this.textures[0x13][p];
}
// 0x020E-0x022C
// Generate texture 0x01 data
this.texture01Data = new Array(0x1E);
for(let i = 0; i < 0x1E; ++i) {
let t = new Array(2);
t[0] = this.random(0x100);
t[1] = this.random(0xEC0 + 0x80) + 0x40;
this.texture01Data[i] = t;
}
// 0x022D-0x0259
// Generate texture 0x00
for(let i = 0; i < 0x1000; ++i) {
let x = i;
if(x & 0x08)
x ^= 0x00FF;
x &= 0x0F;
let y = i >> 6;
if(y & 0x08)
y ^= 0x00FF;
y &= 0x0F;
this.textures[0x00][i] = 0xE0 + Math.min(x, y);
}
// 0x025A-0x0280
// Generate texture 0x02-0x0F background
for(let i = 0x02, p = 0x1005; i <= 0x0F; ++i, p += 2) {
let src = this.textures[this.MEMORY[p] >> 1], dst = this.textures[i];
let offset = this.MEMORY[p + 1];
for(let j = 0; j < 0x1000; ++j)
dst[j] = src[j] + offset;
}
// 0x0281-0x02D7
// Generate texture 0x03, 0x05, 0x0C, 0x0E, 0x0F pattern
for(let i = 0, p = 0x0F69; i < 0x1A; ++i, p += 6) {
let dst = this.textures[this.MEMORY[p] & 0x0F];
let mode = this.MEMORY[p] >> 4;
let x0 = this.MEMORY[p + 1], y0 = this.MEMORY[p + 2];
let x1 = this.MEMORY[p + 3], y1 = this.MEMORY[p + 4];
let offset = this.MEMORY[p + 5];
for(let y = y0; y <= y1; ++y)
for(let x = x0; x <= x1; ++x) {
let index = (y << 6) | x;
let a;
switch(mode) {
case 1: {
a = 0;
break;
}
case 2: {
a = dst[index];
break;
}
default: { // 3
a = (x + y) & 0x04;
if(a !== 0) a = 0x98;
break;
}
}
a += offset;
dst[index] = a;
}
}
// 0x02D8-0x02FE
// Generate texture 0x0B pattern
for(let i = 0; i < 0x14; ++i) {
for(let j = 0; j < ((0x14 - i) << 3); ++j) {
let p = 0xFFF - 0x40 * i - this.random(0x40);
this.textures[0x0B][p] = 0x5E - i;
}
}
// 0x4274-0xC273: Light map
this.lightMap = new Uint8Array(0x80 * 0x100);
// 0x02FF-0x032D
let p = 0;
for(let i = 0x00; i < 0x80; ++i) {
for(let j = 0x00; j < 0xC0; ++j)
this.lightMap[p++] = (j & 0xE0) + (((j & 0x1F) * i * 2) >> 8);
for(let j = 0xC0; j < 0x100; ++j)
this.lightMap[p++] = j;
}
}
// 0x041D-0x0523
onRender() {
let offset = this.doorOffset;
if(offset < 0) offset = 0;
else if(!this.backgroundStarDrawn) {
// 0x0428
// Draw background star
this.backgroundStarDrawn = true;
for(let i = 0; i < 100; ++i) {
let p = this.random(0xC1C0) + 0x1900;
this.drawStar(this.background, p, 0x013B, 0x0002, (p >> 8) & 0x07);
}
}
// 0x0457
// Update texture 0x0D
if(offset > 0x18) offset = 0x18;
offset <<= 6;
for(let i = 0; i < 0x800; ++i)
this.textures[0x0D][i] = this.textures[0x0F][offset + i];
for(let i = 0; i < 0x800; ++i)
this.textures[0x0D][0x800 + i] = this.textures[0x0F][0x800 - offset + i];
for(let i = 0; i < offset * 2; ++i)
this.textures[0x0D][0x800 - offset + i] = 0x00;
// 0x0486
for(let i = 0; i < 320 * 200; ++i)
this.frameBuffer[i] = this.background[i];
// 0x0493
this.transformModel();
// 0x04D3
this.sortList = new Int16Array((this.quadCount + 1) * 2);
for(let i = 0; i < this.quadCount; ++i) {
let z = 0;
for(let j = 0; j < 4; ++j) {
let index = this.quads[i * 5 + j];
z -= this.transformedVertexs[index * 3 + 2];
}
this.sortList[i * 2] = z;
this.sortList[i * 2 + 1] = i;
}
this.quickSort(0, this.quadCount - 1); // -1?
// 0x0504
if(!this.hardwareRenderer)
for(let i = 0; i < this.quadCount; ++i)
this.drawQuad(this.sortList[i * 2 + 1]);
let size = new THREE.Vector2();
this.renderer.getSize(size);
let width = size.x, height = size.y;
this.renderer.clear();
this.backgroundMaterial.map
= new THREE.DataTexture(this.convertFrameBuffer(this.frameBuffer),
320, 200, THREE.RGBFormat);
this.renderer.setViewport(0, 0, width, height);
this.renderer.render(this.backgroundScene, this.backgroundCamera);
this.backgroundMaterial.map.dispose();
if(this.hardwareRenderer) {
this.sceneMesh.geometry = this.convertModel();
this.sceneMaterial.map
= new THREE.DataTexture(this.convertTexture(), 4 * 64, 4 * 64, THREE.RGBAFormat);
this.renderer.setViewport(0, (20 / 200) * height,
width, ((179 - 21 + 1) / 200) * height);
this.renderer.render(this.scene, this.camera);
this.sceneMesh.geometry.dispose();
this.sceneMaterial.map.dispose();
}
}
// 0x0493-0x04D2
transformModel() {
this.transformedVertexs = new Float32Array(this.vertexCount * 3);
for(let i = 0; i < this.vertexCount; ++i)
for(let j = 0; j < 3; ++j)
this.transformedVertexs[i * 3 + j] =
(this.vertexs[i * 4] - this.cameraPosition[0]) * this.cameraMatrix1[j * 3]
+ (this.vertexs[i * 4 + 1] - this.cameraPosition[1]) * this.cameraMatrix1[j * 3 + 1]
+ (this.vertexs[i * 4 + 2] - this.cameraPosition[2]) * this.cameraMatrix1[j * 3 + 2];
}
// 0x0563-0x0601
onTimer() {
// 0x0568
for(let i = 0; i < this.midiData.length; ++i) {
const channel = this.midiData[i].channel;
const data = this.midiData[i].data;
if(this.midiIndexs[i][0] < data.length) {
if(this.midiIndexs[i][1] === 0) {
this.midiIndexs[i][0] += 2;
if(this.midiIndexs[i][0] > 0)
this.midiOutput.send([0x80 | channel, data[this.midiIndexs[i][0] - 2], 0x7F]);
if(this.midiIndexs[i][0] >= data.length) continue;
this.midiIndexs[i][1] = data[this.midiIndexs[i][0] + 1] * 29;
this.midiOutput.send([0x90 | channel, data[this.midiIndexs[i][0]], 0x7F]);
}
--this.midiIndexs[i][1];
}
}
this.rendererCounter = (this.rendererCounter + 1) & 0x7;
if(this.rendererCounter === 0) {
// 0x05B3
// Update palette 0xC0-0xDF
let t = [this.palette[0xC0 * 3], this.palette[0xC0 * 3 + 1]];
for(let i = 0; i < 0x5D; ++i)
this.palette[0xC0 * 3 + i] = this.palette[0xC1 * 3 + i];
this.palette[0xDF * 3] = t[0];
this.palette[0xDF * 3 + 1] = t[1];
// 0x05C2
// Generate texture 0x01
this.textures[0x01].fill(0x00);
for(let i = 0; i < 0x1E; ++i) {
this.texture01Data[i][0] = (this.texture01Data[i][0] - 1) & 0xFF;
let a = this.texture01Data[i][0] & 0x3F;
if(a < 0x1F)
a = ~a & 0x1F;
a >>= 1;
let b = this.texture01Data[i][1];
this.drawStar(this.textures[0x01], b, 0x003B, 0xE000, a);
}
++this.doorOffset;
}
// 0x0396
if(--this.cameraDataCounter < 0) {
let a = this.MEMORY[0x12E3 + this.cameraDataPointer];
if(a === 0) { this.stop(); return; }
this.cameraDataCounter = (a & 0xF8) << 1;
a &= 0x07;
this.cameraA = a;
if(a == 0x1)
this.cameraDir = -this.cameraDir;
++this.cameraDataPointer;
}
if(this.cameraA == 0x1)
this.cameraData[3] += this.cameraDir;
for(let i = 0; i < 3; ++i) {
if(this.cameraA == 0x02 + i * 2)
++this.cameraData[i];
if(this.cameraA == 0x03 + i * 2)
--this.cameraData[i];
}
this.updateCameraMatrix(
this.cameraData[2] << 4, this.cameraData[3] >> 2, this.cameraData[2] >> 2, this.cameraData[1] >> 2);
for(let i = 0; i < 3; ++i)
this.cameraPosition[i] += this.cameraMatrix0[6 + i] * this.cameraData[0] / this.cameraSpeed;
}
// 0x0611-0x062F
drawStar(data, index, stride, color, value) {
// 0x1021-0x1039: Star pattern data
let pattern = new Uint8Array(this.MEMORY.buffer, 0x1021, 5 * 5);
for(let i = 0, p = 0; i < 5; ++i, index += stride)
for(let j = 0; j < 5; ++j, ++p, ++index) {
let x = pattern[p];
if(x > value)
data[index] = ((x - value) + (color >> 8)) << (color & 0xFF);
}
}
// 0x063B-0x0659
generateMIDILoop(p, channelData) {
while(this.MEMORY[p] !== 0x00) {
if(this.MEMORY[p] >= 0x80) {
let count = 0x100 - this.MEMORY[p++];
let q = p;
for(let i = 0; i < count; ++i) {
p = q;
p = this.generateMIDILoop(p, channelData);
}
} else
channelData.data.push(this.MEMORY[p++], this.MEMORY[p++]);
}
++p;
return p;
}
// 0x065A-0x698
quickSort(l, r) {
if(l >= r) return;
let ll = l, rr = r;
let x = this.sortList[ll * 2];
do {
while(this.sortList[ll * 2] > x) ++ll;
while(this.sortList[rr * 2] < x) --rr;
if(ll > rr) break;
let t0 = this.sortList[ll * 2];
this.sortList[ll * 2] = this.sortList[rr * 2];
this.sortList[rr * 2] = t0;
let t1 = this.sortList[ll * 2 + 1];
this.sortList[ll * 2 + 1] = this.sortList[rr * 2 + 1];
this.sortList[rr * 2 + 1] = t1;
++ll;
--rr;
} while(ll <= rr);
this.quickSort(l, rr);
this.quickSort(ll, r);
}
// 0x0699-0x06B8
random(x) {
this.randomB = (Math.imul(this.randomB, this.randomA) + 1) | 0;
return ((((this.randomB >> 16) & 0xFFFF) * x) >> 16) & 0xFFFF;
}
// 0x06E8-0x074D
generateCircleNoise(id, r, n) {
let texture = this.textures[id];
texture.fill(0x00);
let table = new Array(r * 2);
for(let i = r; i > -r; --i)
table[r - i] = Math.round(Math.sqrt(r * r - i * i)) | 0;
for(let i = 0; i < n; ++i) {
let p = this.random(0x1000);
for(let j = 0; j < r * 2; ++j) {
let a = table[j];
for(let k = 0; k < a * 2; ++k)
++texture[(p - a + k) & 0x0FFF];
p += 0x40;
}
}
}
// 0x074E-0x09BE
drawPolygon(textureIndex, clippedVertexCount, clippedDataI) {
// 0x0752
let minX = 32767, maxX = -32767;
let minY = 32767, maxY = -32767;
let indexL, indexR;
for(let i = 0; i < clippedVertexCount; ++i) {
let x = clippedDataI[4][i] >> 16;
if(minX > x) minX = x;
if(maxX < x) maxX = x;
let y = this.toInt16(clippedDataI[3][i]);
if(minY > y) minY = y, indexL = indexR = i;
if(maxY < y) maxY = y;
// console.log(x, y);
}
// 0x0793
if(maxX < 0 || minY > 320 || maxY < 21 || minY > 179) return;
// console.log(minX, minY, maxX, maxY);
if(maxY > 179) maxY = 179;
if(minY === maxY) return;
let scanlineY = minY;
// [BP - 0x3C]
let t0 = new Int32Array(10);
// [BP - 0x78]
let t1 = new Int32Array(10);
// [BP - 0x50]
let t2 = new Int32Array(5);
// [BP - 0x14]
let t3 = new Int32Array(5);
// 0x09BF-0x0A08
function interpolate(index0, index1, offset) {
let y = this.toInt16(clippedDataI[3][index1]);
let dy = y - scanlineY;
for(let i = 0; i < 5; ++i) {
let x0 = clippedDataI[4 + i][index0];
t0[offset + i] = x0;
let x1 = clippedDataI[4 + i][index1];
let dx = x1 - x0;
if(dy !== 0) {
dx = (dx / dy) | 0;
t1[offset + i] = dx;
}
}
}
interpolate = interpolate.bind(this);
// 0x07D5
let texture = this.textures[textureIndex];
while(scanlineY <= maxY) {
// 0x07E8
while(scanlineY <= maxY) { // while(true)?
let y = this.toInt16(clippedDataI[3][indexL]);
if(scanlineY != y) break;
let index0 = indexL;
if(--indexL < 0) indexL = clippedVertexCount - 1;
interpolate(index0, indexL, 0);
}
// 0x0816
while(scanlineY <= maxY) { // while(true)?
let y = this.toInt16(clippedDataI[3][indexR]);
if(scanlineY != y) break;
let index0 = indexR;
if(++indexR == clippedVertexCount) indexR = 0;
interpolate(index0, indexR, 5);
}
// 0x0846
if(scanlineY >= 21) {
let l = t0[0] >> 16, r = t0[5] >> 16;
let dx = r - l;
if(dx !== 0 && r > 0 && l <= 320) {
// 0x0878
let dl = -l;
if(dl < 0) dl = 0;
else l = 0;
// 0x0885
for(let i = 1; i < 5; ++i) {
let a0 = t0[i], a1 = t0[5 + i];
let da = a1 - a0;
if(dx !== 0)
t2[i] = da / dx;
t3[i] = t2[i] * dl + a0;
}
// 0x08AF
let dd = this.toInt16(t2[4]);
if(r > 320) r = 320;
let p = scanlineY * 320 + l;
let u0 = this.toInt16(t3[1] * 256 / t3[3]); // [BP - 0x8C]
let v0 = this.toInt16(t3[2] * 256 / t3[3]); // [BP - 0x8E]
// 0x08EC
while(true) {
let d = r - l;
if(d <= 0) break;
if(d > 16) d = 16;
t3[1] += t2[1] * d;
t3[2] += t2[2] * d;
t3[3] += t2[3] * d;
let u1 = this.toInt16(t3[1] * 256 / t3[3]);
let v1 = this.toInt16(t3[2] * 256 / t3[3]);
let du = this.toInt16((u1 - u0) / d);
let dv = this.toInt16((v1 - v0) / d);
for(let i = 0; i < d; ++i) {
let q = ((v0 & 0xFF00) >> 2) | (u0 >> 8);
let value = texture[q];
if(value)
this.frameBuffer[p] = this.lightMap[value + (t3[4] & 0xFF00)];
t3[4] += dd;
u0 = this.toInt16(u0 + du);
v0 = this.toInt16(v0 + dv);
++p;
}
u0 = u1;
v0 = v1;
l += 16;
}
}
}
// 0x09A4
for(let i = 0; i < 10; ++i)
t0[i] += t1[i];
++scanlineY;
}
}
// 0x0A09-0x0B5F
drawQuad(quadIndex) {
// [BP - 0xBE, 0xBA, 0xB6, (0xB2), (0xAE), 0xAA, 0xA6, (0xA2), 0x9E]
let data = new Array(9), dataI = new Array(9);
// [BP - 0x172, 0x16E, 0x16A, (0x166), (0x162), 0x15E, 0x15A, (0x156), 0x152]
let clippedData = new Array(9), clippedDataI = new Array(9);
for(let i = 0; i < 9; ++i) {
data[i] = new Float32Array(4);
clippedData[i] = new Float32Array(5);
}
for(let i = 0; i < 9; ++i) {
dataI[i] = new Int32Array(data[i].buffer);
clippedDataI[i] = new Int32Array(clippedData[i].buffer);
}
// 0x0A0D
for(let i = 0; i < 4; ++i) {
let vertexIndex = this.quads[quadIndex * 5 + i];
data[0][i] = this.transformedVertexs[vertexIndex * 3];
data[1][i] = this.transformedVertexs[vertexIndex * 3 + 1];
data[2][i] = this.transformedVertexs[vertexIndex * 3 + 2];
let z = Math.round(data[2][i]) + 512;
if(z < 0) return;
if(z > 511) z = 511;
dataI[8][i] = ((z << 7) * this.vertexs[vertexIndex * 4 + 3]) >> 16;
dataI[6][i] = this.MEMORY[0x1043 + i] << 16; // 63, 0, 0, 63
dataI[5][i] = this.MEMORY[0x103F + i] << 16; // 0, 0, 63, 63
}
// 0x0A80
let textureIndex = this.quads[quadIndex * 5 + 4];
// 0x0A85
let clippedVertexCount = 0;
// 0x0A8A
let neg = data[2][3] < 0;
// 0x0B60-0x0BAC
function clip(i1) {
let i0 = i1 - 1;
if(i0 < 0) i0 = 3;
let z = data[2][i1] / (data[2][i1] - data[2][i0]);
for(let i of [0, 1, 2, 5, 6, 8])
clippedData[i][clippedVertexCount]
= (data[i][i0] - data[i][i1]) * z + data[i][i1];
++clippedVertexCount;
}
clip = clip.bind(this);
// 0x0A9B
for(let i = 0; i < 4; ++i) {
let z = data[2][i];
if(z < 0) {
if(!neg) clip(i), neg = !neg;
for(let j = 0; j < 9; ++j)
clippedDataI[j][clippedVertexCount] = dataI[j][i];
++clippedVertexCount;
} else {
if(neg) clip(i), neg = !neg;
}
}
// 0x0AED
if(clippedVertexCount < 2) return; // 3?
// 0x0AF7
for(let i = 0; i < clippedVertexCount; ++i) {
let z = (13 - clippedData[2][i]) / 160;
clippedDataI[3][i] = Math.round(clippedData[1][i] / z + 100);
clippedDataI[4][i] = Math.round(clippedData[0][i] * 1.2 / z + 160) * 65536;
clippedDataI[5][i] = Math.round(clippedDataI[5][i] / z);
clippedDataI[6][i] = Math.round(clippedDataI[6][i] / z);
clippedDataI[7][i] = Math.round(65536 / z);
}
this.drawPolygon(textureIndex, clippedVertexCount, clippedDataI);
}
// 0x0BAD-0x0BCF
rotateVector2(vector, angle) {
angle *= Math.PI / 0x10000;
let c = Math.cos(angle), s = Math.sin(angle);
return [c * vector[0] - s * vector[1], s * vector[0] + c * vector[1]];
}
// 0x0BD0-0x0BFB
rotateVector3(vector, angle) {
let result = [vector[0], vector[1], vector[2]];
let a = this.rotateVector2([result[1], result[2]], angle[2]);
result[1] = a[0], result[2] = a[1];
let b = this.rotateVector2([result[2], result[0]], angle[1]);
result[2] = b[0], result[0] = b[1];
let c = this.rotateVector2([result[0], result[1]], angle[0]);
result[0] = c[0], result[1] = c[1];
return result;
}
// 0x0BFC-0x0C7F
updateCameraMatrix(a, b, c, d) {
let cameraMatrix2 = new Float32Array(9);
for(let i = 0; i < 3; ++i)
for(let j = 0; j < 3; ++j)
cameraMatrix2[i * 3 + j] = i == j;
for(let i = 0; i < 3; ++i) {
let v = this.rotateVector3([
cameraMatrix2[i * 3],
cameraMatrix2[i * 3 + 1],
cameraMatrix2[i * 3 + 2],
], [b, c, d]);
cameraMatrix2[i * 3] = v[0];
cameraMatrix2[i * 3 + 1] = v[1];
cameraMatrix2[i * 3 + 2] = v[2];
for(let j = 0; j < 3; ++j)
this.cameraMatrix0[i * 3 + j] = this.cameraMatrix1[i * 3 + j]
= cameraMatrix2[i * 3] * this.cameraMatrix0[j]
+ cameraMatrix2[i * 3 + 1] * this.cameraMatrix0[3 + j]
+ cameraMatrix2[i * 3 + 2] * this.cameraMatrix0[6 + j];
}
for(let i = 0; i < 3; ++i) {
let v = this.rotateVector2([this.cameraMatrix1[i], this.cameraMatrix1[3 + i]], a);
this.cameraMatrix1[i] = v[0];
this.cameraMatrix1[3 + i] = v[1];
}
}
// 0x0C80-0x0DCA
generateModel() {
// 0x1047-0x105E: Index data
// 0x105F-0x108B: Vertex position data
// 0x1768: Vertex count (362)
// 0x176A: Quad count (367)
// 0x176C-0x22BB: Vertex data
// 0x23EC-0x3241: Quad data
this.vertexCount = 0;
this.quadCount = 0;
this.vertexs = new Int16Array(362 * 4);
this.quads = new Int16Array(367 * 5);
let p = 0x108C;
let int8Table = new Int8Array(this.MEMORY.buffer, 0x105F, 3 * 15);
let int16Table = new Int16Array(4 * 15);
let float32Table = new Float32Array(3 * 15);
for(let i = 0; i < 4; ++i) {
// 0x0C8B-0x0CAE
for(let j = 0; j < 15; ++j) {
for(let k = 0; k < 3; ++k) {
let x = int8Table[j * 3 + k];
int16Table[j * 4 + k] = x;
float32Table[j * 3 + k] = x;
}
int16Table[j * 4 + 3] = 0x7F00;
}
// 0x0CAF-0x0DC0
while(true) {
let a = this.MEMORY[p++];
if(a === 0xFF) break;
let face = a & 0x0F;
let light = ((a << 8) & 0x7000) | 0x0F00;
for(let j = 0; j < 6; ++j) {
let textureIndex;
if(j & 0x01)
textureIndex = this.MEMORY[p++] & 0x0F;
else
textureIndex = this.MEMORY[p] >> 4;
--textureIndex;
if(textureIndex < 0) continue;
this.quads[this.quadCount * 5 + 4] = textureIndex;
for(let k = 0; k < 4; ++k) {
let index = this.MEMORY[0x1047 + j * 4 + k] >> 3;
let vertexIndex = 0;
for(; vertexIndex < this.vertexCount; ++vertexIndex)
if(int16Table[index * 4] === this.vertexs[vertexIndex * 4]
&& int16Table[index * 4 + 1] === this.vertexs[vertexIndex * 4 + 1]
&& int16Table[index * 4 + 2] === this.vertexs[vertexIndex * 4 + 2]) break;
if(vertexIndex === this.vertexCount) {
this.vertexs[vertexIndex * 4] = int16Table[index * 4];
this.vertexs[vertexIndex * 4 + 1] = int16Table[index * 4 + 1];
this.vertexs[vertexIndex * 4 + 2] = int16Table[index * 4 + 2];
this.vertexs[vertexIndex * 4 + 3] = int16Table[index * 4 + 3];
++this.vertexCount;
}
this.quads[this.quadCount * 5 + k] = vertexIndex;
}
++this.quadCount;
}
// 0x0D36
for(let j = 0; j < 3; ++j)
float32Table[14 * 3 + j] += float32Table[(8 + face) * 3 + j];
// 0x0D4C
if(a & 0x80) {
let angle = (this.MEMORY[p] << 8) & 0xF000;
if(angle >= 0x8000) angle -= 0x10000;
let angles = [
this.MEMORY[p] & 0x04 ? angle : 0,
this.MEMORY[p] & 0x02 ? angle : 0,
this.MEMORY[p] & 0x01 ? angle : 0,
];
for(let j = 0; j < 14; ++j) {
let v = this.rotateVector3([
float32Table[j * 3],
float32Table[j * 3 + 1],
float32Table[j * 3 + 2]
], angles);
float32Table[j * 3] = v[0];
float32Table[j * 3 + 1] = v[1];
float32Table[j * 3 + 2] = v[2];
}
++p;
}
// 0x0D77
for(let j = 0; j < 4; ++j) {
let index0 = this.MEMORY[0x1047 + face * 4 + j] >> 3;
let index1 = this.MEMORY[0x1047 + ((face * 4 + j) ^ 0x7)] >> 3;
for(let k = 0; k < 4; ++k)
int16Table[index1 * 4 + k] = int16Table[index0 * 4 + k];
for(let k = 0; k < 3; ++k)
int16Table[index0 * 4 + k] = Math.round(
float32Table[index0 * 3 + k] + float32Table[14 * 3 + k]);
int16Table[index0 * 4 + 3] = light;
}
}
}
}
async start() {
if(this.running) return;
this.running = true;
const unitTime = 1000 / 350;
this.midiOutput = await JZZ.synth.Tiny();
this.midiIndexs = new Array(this.midiData.length);
for(let i = 0; i < this.midiData.length; ++i)
this.midiIndexs[i] = [-2, 0];
for(const channelData of this.midiData)
this.midiOutput.send([0xC0 + channelData.channel, channelData.instrument]);
function getTime() { return (new Date()).getTime(); }
function wait() { return new Promise((resolve) => setTimeout(resolve, 10)); }
console.log('Start.');
let lastFrame = 0;
let startTime = getTime();
loop:
while(true) {
let currentFrame = Math.floor((getTime() - startTime) / unitTime);
while(lastFrame < currentFrame) {
this.onTimer();
if(this.stopping) break loop;
++lastFrame;
}
await wait();
}
// 0x052C
for(let i = 1; i <= 15; ++i)
this.midiOutput.send([0xB0 + i, 0x7B, 0x00]);
console.log('End.');
this.running = false;
this.stopping = false;
}
stop() {
if(!this.running) return;
this.stopping = true;
}
onResize() {
const WIDTH = 320, HEIGHT = 200;
let scale = Math.max(Math.floor(Math.min(window.innerWidth / WIDTH, window.innerHeight / HEIGHT)
* window.devicePixelRatio * 0.8), 1) / window.devicePixelRatio;
this.renderer.setSize(WIDTH * scale, HEIGHT * scale);
}
toInt16(x) {
x &= 0xFFFF;
if(x >= 0x8000) x -= 0x10000;
return x;
}
drawChar(data, x, y, c, color) {
let index = c.charCodeAt(0);
for(let i = 0; i < 8; ++i)
for(let j = 0; j < 8; ++j)
if((this.FONT[index * 8 + i] >> j) & 1)
data[(y * 8 + i) * 320 + (x * 8 + j)] = color;
}
drawText(data, x, y, s, color) {
for(let i = 0; i < s.length; ++i)
this.drawChar(data, x + i, y, s.charAt(i), color);
}
convertModel() {
let positionData = [];
let colorData = [];
let textureCoordData = [];
let indexData = [];
let textureUnitX = 1 / 4, textureUnitY = 1 / 4;
let indexOffset = 0;
for(let i = 0; i < this.quadCount; ++i) {
let quadIndex = this.sortList[i * 2 + 1];
let textureIndex = this.quads[quadIndex * 5 + 4];
let clipped = false;
for(let j = 0; j < 4; ++j) {
let vertexIndex = this.quads[quadIndex * 5 + j];
if(this.transformedVertexs[vertexIndex * 3 + 2] + 512 < 0) {
clipped = true;
break;
}
}
if(clipped) continue;
for(let j = 0; j < 4; ++j) {
let vertexIndex = this.quads[quadIndex * 5 + j];
for(let k = 0; k < 3; ++k)
positionData.push(this.transformedVertexs[vertexIndex * 3 + k]);
let color = textureIndex <= 2 ? 0xFF : (
(this.vertexs[vertexIndex * 4 + 3] / 0x7F00)
* (Math.min(511, this.transformedVertexs[vertexIndex * 3 + 2] + 512) / 511)
* 0xFF) | 0;
colorData.push(color, color, color);
}
let textureX = (textureIndex & 0x3) * textureUnitX;
let textureY = (textureIndex >> 2) * textureUnitY;
textureCoordData.push(
textureX, textureY + textureUnitY * (63 / 64),
textureX, textureY,
textureX + textureUnitX * (63 / 64), textureY,
textureX + textureUnitX * (63 / 64), textureY + textureUnitY * (63 / 64),
);
indexData.push(indexOffset, indexOffset + 1, indexOffset + 2,
indexOffset, indexOffset + 2, indexOffset + 3);
indexOffset += 4;
}
let geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.Float32BufferAttribute(positionData, 3));
geometry.setAttribute('color', new THREE.Uint8BufferAttribute(colorData, 3, true));
geometry.setAttribute('uv', new THREE.Float32BufferAttribute(textureCoordData, 2));
geometry.setIndex(indexData);
return geometry;
}
convertFrameBuffer(data) {
let result = new Uint8Array(3 * 320 * 200);
for(let i = 0; i < 320 * 200; ++i) {
let value = data[i];
result[i * 3] = this.palette[value * 3];
result[i * 3 + 1] = this.palette[value * 3 + 1];
result[i * 3 + 2] = this.palette[value * 3 + 2];
}
return result;
}
convertTexture() {
const WIDTH = 4 * 64, HEIGHT = 4 * 64;
let result = new Uint8Array(4 * WIDTH * HEIGHT);