-
Notifications
You must be signed in to change notification settings - Fork 1
/
entv.html
649 lines (602 loc) · 35.2 KB
/
entv.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
<html>
<head>
<title>Travellers Tales PSX entity GFX viewer by @Juanmv94</title>
<style>
body { margin: 0; font-family: Arial;}
canvas { width: 100%; height: 100% }
center {position:absolute;display: table; height: 100%; width: 100%; color: white;}
h1 {display: table-cell; vertical-align: middle;}
div {position: fixed; margin: 4px; right:0; text-align:right;}
#buttons {left:0; text-align:left;}
button {width: 140px; height: 26px; margin: 4px; background-color: #FFFFFF77; color: black; border: 2px solid #00000077; border-radius: 10px;}
#anmradios {left:0; bottom:0; text-align:left;}
</style>
</head>
<body>
<center id="progresscontainer"><h1 id="progress">Loading SaveState file</h1></center>
<div id="buttons">
<button id="loadfilebutton" onclick="allfile.click()" disabled>Load .ALL / .MDL</button>
<input id="allfile" type="file" accept=".ALL,.MDL" style="display:none">
<button id="loadanmbutton" onclick="anmfile.click()" disabled>Load .ANM</button>
<input id="anmfile" type="file" accept=".ANM" style="display:none">
</div>
<div>
<input type="radio" id="modecollision" name="mode" value="Collision" onchange="[materialtex,materialtexst,materialtexsta,materialtexsts,materialtexds,materialtexstds,materialtexstads,materialtexstsds].forEach(function(x){x.visible=false;}); blinecolor.visible=true; glinecolor.visible=true; colors.forEach(function(x){x.visible=true;});" disabled><label for="Textured">Collision</label>
<input type="radio" id="modetextured" name="mode" value="Textured" onchange="[materialtex,materialtexst,materialtexsta,materialtexsts,materialtexds,materialtexstds,materialtexstads,materialtexstsds].forEach(function(x){x.visible=true; x.wireframe=false;x.map=texture;x.needsUpdate=true;}); blinecolor.visible=false; glinecolor.visible=false; colors.forEach(function(x){x.visible=false;}); setWireframe(model,false);selectedObj=null;" disabled><label for="Textured">Textured</label>
<input type="radio" id="modeuntextured" name="mode" value="Untextured" onchange="[materialtex,materialtexst,materialtexsta,materialtexsts,materialtexds,materialtexstds,materialtexstads,materialtexstsds].forEach(function(x){x.visible=true; x.wireframe=false;x.map=null;x.needsUpdate=true;}); blinecolor.visible=false; glinecolor.visible=false; colors.forEach(function(x){x.visible=false;}); setWireframe(model,false);selectedObj=null;" disabled><label for="Untextured">Untextured</label>
<input type="radio" id="modewireframe" name="mode" value="Wireframe" onchange="[materialtex,materialtexst,materialtexsta,materialtexsts,materialtexds,materialtexstds,materialtexstads,materialtexstsds].forEach(function(x){x.visible=true; x.wireframe=true;x.map=null;x.needsUpdate=true;}); blinecolor.visible=false; glinecolor.visible=false; colors.forEach(function(x){x.visible=false;}); setWireframe(model,false);selectedObj=null;" disabled><label for="Wireframe">Wireframe</label>
</div>
<div id="anmradios"></div>
<div style="bottom:0; font-family: monospace;">
W,A,S,D: move<br/>
←,↑,→,↓,Q,E: turn<br/>
Left click: toggle mouse look<br/>
Right click: entity select + info
</div>
<script src="three.min.js"></script>
<script src="FlyControls.js"></script>
<script>
var urlvars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
urlvars[key] = value;
});
var games = {
"blsc": {"clut": addtopos(0x800E2518), "thumbsp": 0,"newgfxengine": true, "newanmengine": true, "gfxpointerdist": 44, "colpointerdist": 0x34, "colpolysize":22, "getcolpolydata":getcolpolydatats2, "coliniarea": psxetopos(0xb69ec8)},
"ts2": {"clut": addtopos(0x800CD200), "thumbsp": 4,"newgfxengine": false, "newanmengine": true, "gfxpointerdist": 108, "colpointerdist": 0x34, "colpolysize":22, "getcolpolydata":getcolpolydatats2, "coliniarea": psxetopos(0xb54bb0)},
"ts2demo": {"clut": addtopos(0x800BCCE0), "thumbsp": 4, "newgfxengine": false, "newanmengine": false, "colpointerdist": 0x30, "colpolysize":22, "getcolpolydata":getcolpolydatats2, "coliniarea": psxetopos(0xb43b80)},
"ts2proto": {"clut": addtopos(0x800B58A8), "thumbsp": 4, "newgfxengine": false, "newanmengine": false, "colpointerdist": 0x30, "colpolysize":22, "getcolpolydata":getcolpolydatats2, "coliniarea": psxetopos(0xb3c748)},
"bugs": {"clutpag": addtopos(0x800910B0), "clutpal": addtopos(0x800A7DD8), "thumbsp": 0, "newgfxengine": false, "newanmengine": false, "gfxpointerdist": 84, "colpointerdist": 0x30, "colpolysize":16, "getcolpolydata":getcolpolydatabugs, "coliniarea": psxetopos(0xb334e8)},
"bugsdemo": {"clutpag": addtopos(0x80094740),"clutpal": addtopos(0x800a6be8), "thumbsp": 0, "newgfxengine": false, "newanmengine": false, "colpointerdist": 0x34, "colpolysize":24, "getcolpolydata":getcolpolydatabugs, "coliniarea": psxetopos(0xb322f8)},
"rascal": {"newgfxengine": false, "newanmengine": false, "colpointerdist": 0xC4, "colpolysize":44, "getcolpolydata": getcolpolydatarascal, "getcolmeshfromss":getcolmeshfromssrascal, "coliniarea": psxetopos(0xb4cd6c)},
"wl": {"clut": addtopos(0x800AAAB8), "thumbsp": 0, "newgfxengine": true, "newanmengine": true, "gfxpointerdist": 44},
"tsr": {"clut": addtopos(0x800DA610), "thumbsp": 4, "newgfxengine": true, "newanmengine": true, "gfxpointerdist": 112},
"mrm": {"clut": addtopos(0x800DFF00), "thumbsp": 4, "newgfxengine": false, "newanmengine": true}
};
var mult=urlvars["mult"]||0.02;
function postopsxe(pos) {
return (pos+11056360).toString(16);
}
function psxetopos(psxe) {
return psxe-11056360;
}
function postoadd(add) {
return (add+2147483208).toString(16);
}
function addtopos(add) {
return add-2147483208;
}
var blinecolor=new THREE.LineBasicMaterial({color: 0x000000, linewidth: 0.5});
var glinecolor=new THREE.LineBasicMaterial({color: 0x777777, linewidth: 0.5});
var colors=[
new THREE.MeshPhongMaterial({color:0xccffcc, flatShading: true}), //0= TS2: green toxic, BLSC: toxic, Bugs: normal
new THREE.MeshPhongMaterial({color:0xaa00ff, flatShading: true}), //1= TS2: toxic 2, BLSC: toxic with high jump, Bugs: instant death
new THREE.MeshPhongMaterial({color:0xff00aa, flatShading: true}), //2= TS2: toxic 3, BLSC: toxic with waves, Bugs: level finish 1
new THREE.MeshPhongMaterial({color:0x7777ff, flatShading: true}), //3= TS2: wet, BLSC: ???, Bugs: level finish 2
new THREE.MeshPhongMaterial({color:0x2222aa, flatShading: true}), //4= TS2: dirty wet, BLSC: slippery wet, Bugs: toxic
new THREE.MeshPhongMaterial({color:0xffffaa, flatShading: true}), //5= TS2: slippery 1, BLSC: slippery 1
new THREE.MeshPhongMaterial({color:0xffff77, flatShading: true}), //6= TS2: slippery 2, BLSC: slippery 2
new THREE.MeshPhongMaterial({color:0xffff00, flatShading: true}), //7= BLSC: slippery 3
new THREE.MeshPhongMaterial({color:0xffffff, flatShading: true})]; //def= TS2: normal, BLSC: normal
var texture=null,textureba,materialtex,materialtexst,materialtexsta,materialtexsts,materialtexds,materialtexstds,materialtexstads,materialtexstsds,side=(urlvars["ds"]=="y")?THREE.DoubleSide:THREE.FrontSide,
materialbasic=new THREE.MeshBasicMaterial({ color:0xFFFFFF, side: side, vertexColors: THREE.FaceColors}), materialbasicst=new THREE.MeshBasicMaterial({ color:0xFFFFFF, side: side, vertexColors: THREE.FaceColors, opacity: 0.5, transparent: true, depthWrite: false}),
materialbasicsta=new THREE.MeshBasicMaterial({ color:0xFFFFFF, side: side, vertexColors: THREE.FaceColors, opacity: 1, blending: THREE.AdditiveBlending, transparent: true, depthWrite: false}),
materialbasicsts=new THREE.MeshBasicMaterial({ color:0xFFFFFF, side: side, vertexColors: THREE.FaceColors, opacity: 1, blending: THREE.SubtractiveBlending, transparent: true, depthWrite: false}),
materialbasicds=new THREE.MeshBasicMaterial({ color:0xFFFFFF, side: THREE.DoubleSide, vertexColors: THREE.FaceColors}), materialbasicstds=new THREE.MeshBasicMaterial({ color:0xFFFFFF, side: THREE.DoubleSide, vertexColors: THREE.FaceColors, opacity: 0.5, transparent: true, depthWrite: false}),
materialbasicstads=new THREE.MeshBasicMaterial({ color:0xFFFFFF, side: THREE.DoubleSide, vertexColors: THREE.FaceColors, opacity: 1, blending: THREE.AdditiveBlending, transparent: true, depthWrite: false}),
materialbasicstsds=new THREE.MeshBasicMaterial({ color:0xFFFFFF, side: THREE.DoubleSide, vertexColors: THREE.FaceColors, opacity: 1, blending: THREE.SubtractiveBlending, transparent: true, depthWrite: false});
var scene = new THREE.Scene(), model=null, modelfromss, selectedObj=null;
scene.background = new THREE.Color(0x7777ff);
var camera = new THREE.PerspectiveCamera( 50, window.innerWidth/window.innerHeight, 0.1, 10000 );
scene.add(new THREE.AmbientLight(0xffffff, 1));
var light = new THREE.HemisphereLight(0xffffff, 0x101010, -0.35);
light.position.set(1000, -10000, 5000);
scene.add(light);
var renderer = new THREE.WebGLRenderer({antialias: urlvars["antialias"]=="y"});
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
if (["rascal","bugs","bugsdemo"].indexOf(urlvars["game"])>=0) loadanmbutton.style.display="none";
if (urlvars["file"]) {
var oReq = new XMLHttpRequest();
var progresstring=document.getElementById("progress");
oReq.open("GET", "SSTATES/"+urlvars["file"], true);
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
if ((oReq.readyState != 4) || (oReq.status != 200)) {
progresstring.innerText="Failed to load "+urlvars["file"];
return;
}
arrayBuffer = oReq.response; // Note: not oReq.responseText
if (arrayBuffer) {
array8u = new Uint8Array(arrayBuffer.slice(2));
array16 = new Int16Array(arrayBuffer.slice(2));
array16u = new Uint16Array(arrayBuffer.slice(2));
array32 = new Int32Array(arrayBuffer.slice(2));
array32u = new Uint32Array(arrayBuffer.slice(2));
if (urlvars["ssvram"]!="n") {
vram = new Uint16Array(arrayBuffer.slice(2048*1254+991,2048*1254+991+(2048*256)));
texture = getPalTextures();
texture.flipY=true; texture.flipX=true;
//texture.magFilter=THREE.LinearFilter;
texture.needsUpdate=true;
}
setTimeout(loaded,50); //we show loading 100%
}
};
oReq.onprogress = function(pe) {
if (pe.lengthComputable) progresstring.innerText="Loading SaveState file: "+Math.floor(pe.loaded/pe.total*100)+"%";
}
oReq.send(null);
} else {
loaded();
}
function setWireframe(obj, wireframe) {
if (obj instanceof THREE.Mesh) obj.visible = !wireframe;
else obj.children.forEach(function(child) {setWireframe(child, wireframe);});
}
function onDocumentMouseDown(event) {
if (!model || event.button==0) return;
if (selectedObj || event.button==1) {setWireframe(model,false);selectedObj=null;return;}
var raycaster=new THREE.Raycaster(), mouse=new THREE.Vector2((event.clientX/window.innerWidth)*2-1, -(event.clientY/window.innerHeight)*2+1);
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects(model.children, true).filter(function(x){return (x.object instanceof THREE.Mesh) && ((colors.indexOf(x.object.material)>=0) ^ (!blinecolor.visible));}); //intersected meshes with visible material
if (intersects.length > 0) {
var obj=intersects[0].object.parent.parent.parent //Colision mesh
if (!obj) obj=intersects[0].object; //GFX mesh
setWireframe(model,true);
setWireframe(obj,false);
selectedObj=obj;
if (modelfromss) {
console.log("Selected object at add: 0x"+postoadd(obj.pointer)+", emu: 0x"+postopsxe(obj.pointer));
console.log("Datagroup at add: 0x"+postoadd(obj.dgpointer)+", emu: 0x"+postopsxe(obj.dgpointer));
}
else {
console.log("Selected object at pos: 0x"+obj.pointer.toString(16));
console.log("Datagroup at pos: 0x"+obj.dgpointer.toString(16));
}
//console.log(intersects[0].object.parent.parent.info); //Clicked col polygon information
console.log(obj);
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function colorcurve(c) { //RGB color intensity in PSX is not linear! in the range 0-31 the middle is about 8 (nopsx documentation)
return Math.sin(c/31*Math.PI/2)*255;
}
function getPalTextures() { //Gets the pal4 16color level textures
function getpal(x,y) { //returns palette for each pal4 texture pixel
if (n<games[urlvars["game"]].thumbsp) {
if (y<32 && (x%64)<8) return palspage[Math.floor(y/8)*4+Math.floor(x%64/2)];
if (y<64 && (x%64)<16) return palspage[Math.floor(y/16)*4+Math.floor(x%64/4)];
if (y<128 && (x%64)<32) return palspage[Math.floor(y/32)*4+Math.floor(x%64/8)];
}
return palspage[Math.floor(y/64)*4+Math.floor(x%64/16)];
}
//const pals16=768+193*1024;
textureba = new Uint8Array(8192*256*4);
var palspage,vrampage;
var lut=games[urlvars["game"]].clut, lutpal, lutpag;
if (!lut) { //BL has PAL LUT and VRAM page at two diferent addresses
lutpal=games[urlvars["game"]].clutpal; lutpag=games[urlvars["game"]].clutpag;
}
for (var n=0;n<32;n++) {
vrampage=array16u[(lut||lutpag)>>1]&0x7F;
var pal8=array16u[(lut||lutpag)>>1]&0x80;
if (vrampage>15) {if (lut) lut+=16; else {lutpag+=2; lutpal+=2} continue;}
var palp=array16u[(lutpal||(lut+2))>>1]*16;
if (!pal8) {
palspage=new Array(16);
for (var j=0;j<16;j++) {
var pal=new Array(16);
for (var i=0;i<16;i++) {
var color=vram[palp++];
if (!color) pal[i]=[0,0,0,10];
else pal[i]=[colorcurve(color&31),colorcurve((color>>5)&31),colorcurve((color>>10)&31),255];
}
palspage[j]=pal;
}
for (let y=0;y<256;y++) {
var imarrp=n*1024+8192*4*y;
var vrp=vrampage*64+1024*y;
for (let x=0;x<64;x++) {
var pal=getpal(x,y);
var c1=vram[vrp]&15,c2=(vram[vrp]>>4)&15,c3=(vram[vrp]>>8)&15,c4=(vram[vrp]>>12)&15;
for (let i=0;i<4;i++) textureba[imarrp++]=pal[c1][i];
for (let i=0;i<4;i++) textureba[imarrp++]=pal[c2][i];
for (let i=0;i<4;i++) textureba[imarrp++]=pal[c3][i];
for (let i=0;i<4;i++) textureba[imarrp++]=pal[c4][i];
vrp++;
}
}
} else {
var pal=new Array(256);
for (var i=0;i<256;i++) {
var color=vram[palp++];
if (!color) pal[i]=[0,0,0,10];
else pal[i]=[colorcurve(color&31),colorcurve((color>>5)&31),colorcurve((color>>10)&31),255];
}
for (let y=0;y<256;y++) {
var imarrp=n*1024+8192*4*y;
var vrp=vrampage*64+1024*y;
for (let x=0;x<128;x++) {
var c1=vram[vrp]&255,c2=(vram[vrp]>>8)&255;
for (let i=0;i<4;i++) textureba[imarrp++]=pal[c1][i];
for (let i=0;i<4;i++) textureba[imarrp++]=pal[c2][i];
vrp++;
}
}
}
if (lut) lut+=16; else {lutpag+=2; lutpal+=2}
}
return new THREE.DataTexture( textureba, 8192, 256, THREE.RGBAFormat );
}
function getcolmesh(pos,color) {
var group = new THREE.Group();
getcolpolydatalist(pos>>1).forEach(function(x){group.add(getcolpoly(x,color));});
return group;
}
function getcolpoly(polydata,color) {
var group = new THREE.Group();
group.info=polydata;
var coords1= [polydata.pOrigin, polydata.p2, polydata.p3];
if (polydata.bouncing2==0x7FFF) {
group.add(getcoltriangle(coords1,color,false));
return group;
}
group.add(getcoltriangle(coords1,color,true));
var coords2=[polydata.p4, polydata.p3, polydata.p2];
group.add(getcoltriangle(coords2,color,true));
return group;
}
function getcoltriangle(coords, color, gray) {
var group = new THREE.Group();
var geom = new THREE.BufferGeometry();
if (gray) {
geom.addAttribute('position', new THREE.BufferAttribute(new Float32Array(coords[1].concat(coords[0]).concat(coords[2])),3));
var glgeom=new THREE.BufferGeometry();
glgeom.addAttribute('position', new THREE.BufferAttribute(new Float32Array(coords[2].concat(coords[1])),3));
group.add(new THREE.Line(glgeom, glinecolor));
} else {
geom.addAttribute('position', new THREE.BufferAttribute(new Float32Array(coords[1].concat(coords[0]).concat(coords[2]).concat(coords[1])),3));
}
group.add(new THREE.Line(geom, blinecolor));
group.add(new THREE.Mesh(geom, colors[color]));
return group;
}
function getcolpolydatalist(pos) {
var enabled=array16[pos++];
if (enabled!=1) return [];
var count=array16[pos++];
//console.log("count:"+count);
var unk=[array16[pos],array16[pos+1],array16[pos+2],array16[pos+3]];
pos+=4;
var res=new Array(count);
for (var i=0;i<count;i++) {
res[i]=games[urlvars["game"]].getcolpolydata(pos);
pos+=games[urlvars["game"]].colpolysize;
}
return res.concat(getcolpolydatalist(pos));
}
function getcolpolydatats2(pos) {
return {activearea:[array16[pos+0],array16[pos+1],array16[pos+2],array16[pos+3]], pOrigin:[array16[pos+4],array16[pos+5],array16[pos+6]],p2:[array16[pos+4]+array16[pos+7],array16[pos+5]+array16[pos+8],array16[pos+6]+array16[pos+9]],p3:[array16[pos+4]+array16[pos+10],array16[pos+5]+array16[pos+11],array16[pos+6]+array16[pos+12]],p4:[array16[pos+4]+array16[pos+13],array16[pos+5]+array16[pos+14],array16[pos+6]+array16[pos+15]],inclinacion1:[array16[pos+16],array16[pos+18]], bouncing1:array16[pos+17],inclinacion2:[array16[pos+19],array16[pos+21]], bouncing2:array16[pos+20]};
}
function getcolpolydatabugs(pos) {
return {unk1:[array16[pos+0],array16[pos+1],array16[pos+2]],pOrigin:[array16[pos+3],array16[pos+4],array16[pos+5]],p2:[array16[pos+3]+array16[pos+6],array16[pos+4]+array16[pos+7],array16[pos+5]+array16[pos+8]],p3:[array16[pos+3]+array16[pos+9],array16[pos+4]+array16[pos+10],array16[pos+5]+array16[pos+11]],unk2:[array16[pos+12],array16[pos+13],array16[pos+14],array16[pos+15]],
bouncing2:0x7FFF //bouncing doesn't exist in bugs life, and polygons are single triangles. Set 0x7FFF -> second triangle doesn't exist.
};
}
function getcolpolydatarascal(pos) {
pos32=pos-4>>1; //rescal uses 32bit fields, and skips 4 words of "unk" data from getcolpolydatalist
return {pOrigin:[array32[pos32+0]/32,array32[pos32+1]/32,array32[pos32+2]/32],p2:[array32[pos32+3]/32,array32[pos32+4]/32,array32[pos32+5]/32],p3:[array32[pos32+6]/32,array32[pos32+7]/32,array32[pos32+8]/32],
bouncing2:0x7FFF //bouncing doesn't exist in rascal, and polygons are single triangles. Set 0x7FFF -> second triangle doesn't exist.
};
}
function getgfxmesh(pos) {
//console.log("start processing mesh at pos: "+pos.toString(16)+", psxe: "+postopsxe(pos));
var geom=new THREE.BufferGeometry(),color=[],position=[],uv=[],idx=0,vrampag;
while ((array8u[pos+3]&0xF0)==0x30) {
var currposition=[],currcolor=[],curruv=[];
for (let i=0;i<3;i++) currcolor.push(Math.sin(array8u[pos++]*Math.PI/255));
var flag=array8u[pos++],nv=(flag&8)?4:3;
if (!games[urlvars["game"]].newgfxengine) { //TS2, BugsLife, Rascal
for (let i=0;i<nv;i++) {
currposition.unshift(array16[pos>>1], array16[pos+2>>1], array16[pos+4>>1]); pos+=6;
curruv.unshift(array8u[pos++],array8u[pos++]);
}
for (let i=1;i<nv;i++) {
currcolor.unshift(Math.sin(array8u[pos++]*Math.PI/255), Math.sin(array8u[pos++]*Math.PI/255), Math.sin(array8u[pos++]*Math.PI/255));
pos++;
}
vrampag=array8u[pos-1];
} else { //BLSC, MRM, TSR
for (let i=0;i<nv;i++) {
if (i>0) {
for (let j=0;j<3;j++) currcolor.push(Math.sin(array8u[pos++]*Math.PI/255));
pos++;
}
for (let j=0;j<3;j++) {currposition.push(array16[pos>>1]); pos+=2;}
for (let j=0;j<2;j++) curruv.push(array8u[pos++]);
}
vrampag=array8u[pos-9];
}
switch(urlvars["hack"]) {
//case "blscuvfix": curruv=curruv.map(function(x)x+0x40&0xFF); break;
case "wlnovc": color[color.length-1]=color[color.length-2]=color[color.length-3]=1; currcolor.fill(1);
}
if (nv==4) {
for (let i=0;i<9;i++) position.push(currposition[i]);
for (let i=0;i<3;i++) position.push(currposition[i]);
for (let i=6;i<12;i++) position.push(currposition[i]);
for (let i=0;i<6;i++) uv.push(curruv[i]);
for (let i=0;i<2;i++) uv.push(curruv[i]);
for (let i=4;i<8;i++) uv.push(curruv[i]);
for (let i=0;i<9;i++) color.push(currcolor[i]);
for (let i=0;i<3;i++) color.push(currcolor[i]);
for (let i=6;i<12;i++) color.push(currcolor[i]);
} else {
currposition.forEach(function(x){position.push(x);});
curruv.forEach(function(x){uv.push(x);});
currcolor.forEach(function(x){color.push(x);});
}
var addedvertex=(nv===4)?6:nv;
geom.addGroup(idx,addedvertex,(flag&0x07)|((vrampag>>4)&8)); idx+=addedvertex;
for (let i=0;i<addedvertex;i++) { //fix UVs
uv[uv.length-1-i*2]=1-uv[uv.length-1-i*2]/256;
uv[uv.length-2-i*2]=uv[uv.length-2-i*2]/8192+(vrampag&0x1F)*0.03125;
}
geom.addAttribute('position',new THREE.BufferAttribute(new Float32Array(position), 3));
geom.addAttribute('color',new THREE.BufferAttribute(new Float32Array(color), 3));
geom.addAttribute('uv',new THREE.BufferAttribute(new Float32Array(uv), 2));
}
if (array32u[pos>>2]==2) { //LOD mesh
if (urlvars["lod"]=="y") {
console.log("HID data ignored at pos: "+pos.toString(16)+", psxe: "+postopsxe(pos));
return getgfxmesh(pos+4);
}
else console.log("LOD data ignored at pos: "+pos.toString(16)+", psxe: "+postopsxe(pos));
}
else if (array32u[pos>>2]) console.warn("rubish data processing mesh at vertex no "+idx+" at pos: "+pos.toString(16)+", psxe: "+postopsxe(pos));
//else console.log("end processing mesh at pos: "+pos.toString(16)+", psxe: "+postopsxe(pos));
return new THREE.Mesh( geom, [materialbasic,materialbasicsts,materialbasicsta,materialbasicst,materialtex,materialtexsts,materialtexsta,materialtexst,materialbasicds,materialbasicstsds,materialbasicstads,materialbasicstds,materialtexds,materialtexstsds,materialtexstads,materialtexstds]);
}
function getentfromfile() {
if (model) scene.remove(model);
modelfromss=false;
model=new THREE.Group();
var currpos=4, metadatapos=array32u[0]*2, nmesh=array32u[metadatapos>>2], lastpos;
console.clear();
console.log("Input file has "+nmesh+" data groups");
var gfxgroupsread=0,colgroupsread=0;
metadatapos+=4;
for (let i=0;i<nmesh;i++) {
var pos=(array32u[metadatapos>>2])?currpos:lastpos;
switch (array32[metadatapos+0x10>>2]) {
case 0x01: //gfx mesh data group
gfxgroupsread++;
var mesh=getgfxmesh(pos);
mesh.position.x=array32[metadatapos+4>>2]; mesh.position.y=array32[metadatapos+8>>2]; mesh.position.z=array32[metadatapos+12>>2];
mesh.pointer=metadatapos; mesh.dgpointer=pos; //Diagnostics on-click data
model.add(mesh);
break;
case 0x011F: console.log("gfx mesh joint data group found (ignored)"); break;
case 0x09: console.log("target position data group found (ignored)"); break;
case 0x0101: console.log("infinite wall collision data group found (ignored)"); break;
case 0x0104: console.log("collision footer data group found (ignored)"); break; //Always at the end of collision ALL files. It's 16 bytes length and edit it or remove it has no noticeable effect
case 0x06: //collision data group
case 0x08: //dynamic collision data group
colgroupsread++;
if ((!(array8u[metadatapos+41]&0x04) && urlvars["lod"]=="y") || ((array8u[metadatapos+41]&0x04) && urlvars["lod"]=="n")) break;
var colmesh = getcolmesh(pos, Math.min(array8u[metadatapos+40],8));
colmesh.position.x=array32[metadatapos+4>>2]; colmesh.position.y=array32[metadatapos+8>>2]; colmesh.position.z=array32[metadatapos+12>>2];
colmesh.pointer=metadatapos; colmesh.dgpointer=pos; //Diagnostics on-click data
model.add(colmesh);
break;
default: console.warn("unknown data group (id=0x"+array32u[metadatapos+0x10>>2].toString(16)+") found (ignored) pos: 0x"+metadatapos.toString(16)+" dgpos: 0x"+pos.toString(16)+" size: "+array32u[metadatapos>>2]*2);
}
if (array32u[metadatapos+0x4C>>2]) lastpos=currpos;
currpos+=array32u[metadatapos>>2]*2;
metadatapos+=0x4C;
}
model.scale.x=mult; model.scale.y=mult; model.scale.z=mult;
scene.add(model);
camera.position.x=0; camera.position.y=-4; camera.position.z=-8;
camera.rotation.x=Math.PI; camera.rotation.y=0; camera.rotation.z=0;
console.log(gfxgroupsread+colgroupsread+" mesh groups read");
["modetextured","modeuntextured","modewireframe"].forEach(function(x){document.getElementById(x).disabled=!gfxgroupsread;});
if (!texture) document.getElementById("modetextured").disabled=true;
document.getElementById("modecollision").disabled=!colgroupsread;
document.querySelector("input[name=mode]:not([disabled])").click();
}
function getcolmeshfromss(pos) {
var colmesh = getcolmesh(addtopos(array32u[16+pos>>2]),Math.min(array8u[pos+46],8));
colmesh.position.x=array32[pos>>2]/32; colmesh.position.y=array32[4+pos>>2]/32; colmesh.position.z=array32[8+pos>>2]/32;
colmesh.pointer=pos; colmesh.dgpointer=addtopos(array32u[16+pos>>2]);
return colmesh;
}
function getcolmeshfromssrascal(pos) {
var pos32=pos>>2;
var colmesh = getcolmesh(addtopos(array32u[pos32+4]),0);
colmesh.position.x=array32[pos32+8]; colmesh.position.y=array32[pos32+9]; colmesh.position.z=array32[pos32+10];
colmesh.pointer=pos; colmesh.dgpointer=addtopos(array32u[pos32+4]);
return colmesh;
}
function getcolfromss(pos) {
if (model) scene.remove(model);
modelfromss=true;
model=new THREE.Group();
while (array32u[16+pos>>2]!=0) {
if (!((!(array8u[pos+47]&0x04) && urlvars["lod"]=="y") || ((array8u[pos+47]&0x04) && urlvars["lod"]=="n"))) model.add((games[urlvars["game"]].getcolmeshfromss || getcolmeshfromss)(pos));
pos+=games[urlvars["game"]].colpointerdist;
}
model.scale.x=mult; model.scale.y=mult; model.scale.z=mult;
scene.add(model);
camera.position.x=0; camera.position.y=-10; camera.position.z=-20;
camera.rotation.x=Math.PI; camera.rotation.y=0; camera.rotation.z=0;
["modetextured","modeuntextured","modewireframe"].forEach(function(x){document.getElementById(x).disabled=true;});
document.getElementById("modecollision").disabled=false;
document.getElementById("modecollision").click();
}
function getgfxfromss(mempos,size) { //Do not support object translations!
if (model) scene.remove(model);
modelfromss=true;
model=new THREE.Group();
for (let i=0;i<size;i++) {
var meshdgpos=addtopos(array32u[mempos>>2]), mesh=getgfxmesh(meshdgpos);
mesh.pointer=mempos; mesh.dgpointer=meshdgpos; //Diagnostics on-click data
model.add(mesh);
mempos+=games[urlvars["game"]].gfxpointerdist;
}
model.scale.x=mult; model.scale.y=mult; model.scale.z=mult;
scene.add(model);
camera.position.x=0; camera.position.y=-10; camera.position.z=-20;
camera.rotation.x=Math.PI; camera.rotation.y=0; camera.rotation.z=0;
["modetextured","modeuntextured","modewireframe"].forEach(function(x){document.getElementById(x).disabled=false;});
if (!texture) document.getElementById("modetextured").disabled=true;
document.getElementById("modecollision").disabled=true;
document.querySelector("input[name=mode]:not([disabled])").click();
}
var anminterval=null;
function cancelanm() {
if (anminterval!==null) clearInterval(anminterval); anminterval=null;
if (model) model.children.forEach(c=>{c.position.x=c.position.y=c.position.z=c.rotation.x=c.rotation.y=c.rotation.z=0; c.visible=true;});
}
function selanm() {
cancelanm();
let mempos=this.value>>1, headers;
if (games[urlvars["game"]].newanmengine) {
headers={hfff0: array16u[mempos], h3: array16u[mempos+1], loopframes: array16u[mempos+2], bones1: array16u[mempos+3],
frameoffset: array16u[mempos+4], bones2: array16u[mempos+5], sizeflaghides: array16u[mempos+6], padding: array16u[mempos+7]};
if (headers.hfff0==0xfff2) return alert("You've tried to load an old-engine ANM on "+urlvars["game"]+" which uses new-engine ANM!");
if (headers.hfff0!=0xfff0 || headers.h3!=0x03 || headers.bones1!=headers.bones2) return alert("Corrupted ANM!");
mempos+=8;
} else {
headers={hfff2: array16u[mempos], loopframes: array16u[mempos+1], bones1: array16u[mempos+2],
frameoffset: array16u[mempos+3], bones2: array16u[mempos+4], sizeflaghides: array16u[mempos+5], padding: array16u[mempos+6]};
if (headers.hfff2==0xfff0) return alert("You've tried to load a new-engine ANM on "+urlvars["game"]+" which uses old-engine ANM!");
if (headers.hfff2!=0xfff2 || headers.bones1!=headers.bones2) return alert("Corrupted ANM!");
mempos+=7;
}
if (headers.bones1>model.children.length) return alert("Unsupported ANM for current model");
for (let i=headers.bones1; i<model.children.length; i++) model.children[i].visible=false;
const boneanims=[], frames=Array(headers.loopframes).fill().map(()=>[]);
for (let i=0;i<headers.bones1;i++) boneanims[i]=array16[mempos++]
for (let i=0;i<(headers.sizeflaghides>>1);i++) {
let flaghides=array16u[mempos++];
for (let j=0;j<16;j++) if (flaghides&(1<<j)) model.children[i*16+j].visible=false;
}
for (let i=0;i<headers.bones1;i++) {
if (boneanims[i]<0) { // -3, -2
model.children[i].visible=false;
continue;
}
for (let fno=0; fno<headers.loopframes; fno++) {
let animframe=mempos+boneanims[i]+fno*headers.frameoffset;
if (games[urlvars["game"]].newanmengine) {
let rot = array16u[animframe+3]+(array16u[animframe+4]<<16);
frames[fno][i]={x: array16[animframe]/4, y: array16[animframe+1]/4, z: array16[animframe+2]/4,
rz: (rot&0x3FF)*Math.PI/512, ry: ((rot>>10)&0x3FF)*Math.PI/512, rx: ((rot>>20)&0x3FF)*Math.PI/512};
} else {
frames[fno][i]={x: array16[animframe], y: array16[animframe+1], z: array16[animframe+2],
rx: array16[animframe+3]*Math.PI/2048, ry: array16[animframe+4]*Math.PI/2048, rz: array16[animframe+5]*Math.PI/2048};
}
}
}
let f=0;
anminterval=setInterval(()=>{
model.children.forEach((x,i)=>{if (frames[f][i]) {
x.position.x=frames[f][i].x; x.position.y=frames[f][i].y; x.position.z=frames[f][i].z;
x.rotation.x=frames[f][i].rx; x.rotation.y=frames[f][i].ry; x.rotation.z=frames[f][i].rz;
}});
if (++f==headers.loopframes) f=0;
},50);
}
function getanmfromss(mempos) {
let radio = document.createElement("input");
radio.type="radio"; radio.name="anmsel"; radio.value=""; radio.onchange=cancelanm; radio.style.filter="brightness(0.5)"; radio.checked=true;
anmradios.appendChild(radio);
let count = array16u[(mempos>>1)+1];
for (let i=0;i<count;i++) {
let add = array32u[(mempos>>2)+2+i];
let radio = document.createElement("input");
radio.type="radio"; radio.name="anmsel"; radio.value=addtopos(add); radio.onchange=selanm;
if (!add) radio.disabled=true;
anmradios.appendChild(radio);
}
}
function getanmfromfile() {
anmradios.innerHTML="";
let radio = document.createElement("input");
radio.type="radio"; radio.name="anmsel"; radio.value=""; radio.onchange=cancelanm; radio.style.filter="brightness(0.5)"; radio.checked=true;
anmradios.appendChild(radio);
let count = array16u[1];
for (let i=0;i<count;i++) {
let pos = array32u[2+i];
let radio = document.createElement("input");
radio.type="radio"; radio.name="anmsel"; radio.value=pos; radio.onchange=selanm;
if (!pos) radio.disabled=true;
anmradios.appendChild(radio);
}
}
function loaded() {
document.getElementById("progresscontainer").style.display="none";
window.addEventListener( 'resize', onWindowResize, false );
materialtex=new THREE.MeshBasicMaterial({side: side, map: texture, wireframe: false, alphaTest:0.5, vertexColors: THREE.FaceColors});
materialtexst=new THREE.MeshBasicMaterial({side: side, map: texture, wireframe: false, transparent:true, vertexColors: THREE.FaceColors, opacity: 0.5, depthWrite: false});
materialtexsta=new THREE.MeshBasicMaterial({side: side, map: texture, wireframe: false, transparent:true, vertexColors: THREE.FaceColors, opacity: 1, blending: THREE.AdditiveBlending, depthWrite: false});
materialtexsts=new THREE.MeshBasicMaterial({side: side, map: texture, wireframe: false, transparent:true, vertexColors: THREE.FaceColors, opacity: 1, blending: THREE.SubtractiveBlending, depthWrite: false});
materialtexds=new THREE.MeshBasicMaterial({side: THREE.DoubleSide, map: texture, wireframe: false, alphaTest:0.5, vertexColors: THREE.FaceColors});
materialtexstds=new THREE.MeshBasicMaterial({side: THREE.DoubleSide, map: texture, wireframe: false, transparent:true, vertexColors: THREE.FaceColors, opacity: 0.5, depthWrite: false});
materialtexstads=new THREE.MeshBasicMaterial({side: THREE.DoubleSide, map: texture, wireframe: false, transparent:true, vertexColors: THREE.FaceColors, opacity: 1, blending: THREE.AdditiveBlending, depthWrite: false});
materialtexstsds=new THREE.MeshBasicMaterial({side: THREE.DoubleSide, map: texture, wireframe: false, transparent:true, vertexColors: THREE.FaceColors, opacity: 1, blending: THREE.SubtractiveBlending, depthWrite: false});
if (urlvars["sscol"]=="y") getcolfromss(games[urlvars["game"]].coliniarea);
else if (urlvars["gfxpointer"]) {
getgfxfromss(psxetopos(parseInt(urlvars["gfxpointer"])),parseInt(urlvars["gfxsize"]));
if (urlvars["anmpointer"]) getanmfromss(psxetopos(parseInt(urlvars["anmpointer"])));
loadanmbutton.disabled = false;
}
document.addEventListener('mousedown', onDocumentMouseDown, false);
allfile.addEventListener('change', function() {
var reader = new FileReader();
reader.onload = function() {
arrayBuffer = this.result;
array8u = new Uint8Array(arrayBuffer);
array16 = new Int16Array(arrayBuffer);
array16u = new Uint16Array(arrayBuffer);
array32 = new Int32Array(arrayBuffer);
array32u = new Uint32Array(arrayBuffer);
cancelanm();
anmradios.innerHTML="";
getentfromfile();
loadanmbutton.disabled = false;
}
reader.readAsArrayBuffer(this.files[0]);
}, false);
loadfilebutton.disabled = false;
anmfile.addEventListener('change', function() {
var reader = new FileReader();
reader.onload = function() {
arrayBuffer = this.result;
array16 = new Int16Array(arrayBuffer);
array16u = new Uint16Array(arrayBuffer);
array32u = new Uint32Array(arrayBuffer.slice(0,4*(array16u[1]+2))); //Not mult4 sizes
cancelanm();
getanmfromfile();
}
reader.readAsArrayBuffer(this.files[0]);
}, false);
var controls = new THREE.FlyControls( camera, renderer.domElement );
var t=performance.now();
var animate = function () {
requestAnimationFrame( animate );
var newt=performance.now();
controls.update(newt-t);
t=newt;
renderer.render( scene, camera );
};
animate();
}
</script>
</body>
</html>