-
Notifications
You must be signed in to change notification settings - Fork 0
/
map2stl.js
868 lines (747 loc) · 22.7 KB
/
map2stl.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
"use strict";
let sectorInfo=[];//static sect_t *sec;
let map2stl_output;
function new_sect_t() { // typedef struct { float z[2]; point2d grad[2]; wall_t *wall; long n; } sect_t;
return {
z: [0, 0],
grad: [
{x:0, y:0, slope: 0},
{x:0, y:0, slope: 0}
],
wall: [],
wallcount:0
};
}
function remove_duplicates(arr) {
return arr.filter((c, index) => {
return arr.indexOf(c) === index;
});
}
/*
* sect2trap()
* Slices sectors into trapezoids which can then be split into triangles.
* Ken Silverman wrote the original C code just for exporting triangles.
* This is not how Build does it, but it is one way to extract geometry.
*/
function sect2trap(wal) { // static long sect2trap (wall_t *wal, long n, zoid_t **retzoids, long *retnzoids)
let sector_y = [], trapx0 = [], trapx1 = [];
let pwal = [];
let zoids = []; // Empty array
if (wal.length < 3) return(0);
// malloc here because this is traversed backwards
//sector_y.length = wal.length;
for (let i=0;i<wal.length;i++) {
sector_y.push(0);
trapx0.push(0);
trapx1.push(0);
pwal.push({});
}
// Copy values from wall[i].y
for(let i=wal.length-1;i>=0;i--) sector_y[i] = wal[i].y;
// Remove duplicates
sector_y = remove_duplicates(sector_y);
// Then sort from low to high
sector_y.sort(function(a , b) {
if(a > b) return 1;
if(a < b) return -1;
return 0;
});
let j = 0;
for(let s=0;s<sector_y.length-1;s++) {
let sy0 = sector_y[s];
let sy1 = sector_y[s+1];
let ntrap = 0;
for(let i=0;i<wal.length;i++) {
// First wall
let x0 = wal[i].x;
let y0 = wal[i].y;
j = wal[i].n+i; // next wall + i
// Second wall
let x1 = wal[j].x;
let y1 = wal[j].y;
if (y0 > y1) {
// Swap x0,y0 with x1,y1
let f = x0;
x0 = x1;
x1 = f;
f = y0;
y0 = y1;
y1 = f;
}
if ((y0 >= sy1) || (y1 <= sy0)) continue;
if (y0 < sy0) x0 = (sy0-wal[i].y)*(wal[j].x-wal[i].x)/(wal[j].y-wal[i].y) + wal[i].x;
if (y1 > sy1) x1 = (sy1-wal[i].y)*(wal[j].x-wal[i].x)/(wal[j].y-wal[i].y) + wal[i].x;
trapx0[ntrap] = x0;
trapx1[ntrap] = x1;
pwal[ntrap] = wal[i];
ntrap++;
}
for(let g=(ntrap>>1);g;g>>=1) {
for(let i=0;i<ntrap-g;i++) {
for(j=i;j>=0;j-=g) {
if (trapx0[j]+trapx1[j] <= trapx0[j+g]+trapx1[j+g]) break;
let f = trapx0[j]; trapx0[j] = trapx0[j+g]; trapx0[j+g] = f;
f = trapx1[j]; trapx1[j] = trapx1[j+g]; trapx1[j+g] = f;
let k = pwal[j]; pwal[j] = pwal[j+g]; pwal[j+g] = k;
}
}
}
for(let i=0;i<ntrap;i=j+1) {
j = i+1;
if ((trapx0[i+1] <= trapx0[i]) && (trapx1[i+1] <= trapx1[i])) continue;
while ((j+2 < ntrap) && (trapx0[j+1] <= trapx0[j]) && (trapx1[j+1] <= trapx1[j])) j += 2;
// Add to the zoids that we're returning
zoids.push({
x: [trapx0[i], trapx0[j], trapx1[j], trapx1[i]],
y: [sy0, sy1]
});
}
}
return zoids;
}
function getslopez(sector, floorOrCeil, x, y) { // static float getslopez (sect_t *s, long i, float x, float y)
let firstWall = sector.wall[0];
let grad = sector.grad[floorOrCeil];
let z = sector.z[floorOrCeil];
return ((firstWall.x-x)*grad.x + (firstWall.y-y)*grad.y + z);
}
/*
function getPortalWalls(sectorNum, wallInd) { // static long getwalls (long s, long w, vertlist_t *ver, long maxverts)
let sectorWalls = sectorInfo[sectorNum].wall;
let nextSector = sectorWalls[wallInd].neighborSector;
let verts = [];
// -1 means there are no neighboring sectors
if (nextSector != -1) {
verts.push({
s: sectorWalls[wallInd].neighborSector,
w: sectorWalls[wallInd].neighborWall
});
}
return verts;
}
*/
function swap_vals(v1, v2) {
let tv = v1;
v1 = v2;
v2 = tv;
}
function copy_vec3(vec3) {
return {
x: vec3.x,
y: vec3.y,
z: vec3.z
};
}
// Gets point where two slopes intersect. Also maybe should be called lerp_vec3()
function intersect_vec3(v1, v0, lerp) { // lerp = value in between 0 and 1
return {
x: (v1.x-v0.x)*lerp + v0.x,
y: (v1.y-v0.y)*lerp + v0.y,
z: (v1.z-v0.z)*lerp + v0.z
};
}
// Looks like this clips along the Z axis in order to creates walls. Removed .n values since they don't do anything.
function wallclip(pol) { // static long wallclip (kgln_t *pol, kgln_t *npol)
let npol = [];
// Height difference is used to determine where the wall clips.
// No difference, no wall. Negative difference, no wall (because it's in the world).
let dz0 = pol[3].z-pol[0].z; // This wall
let dz1 = pol[2].z-pol[1].z; // Next wall
if (dz0 > 0.0) { //do not include null case for rendering
if (dz1 > 0.0) { //do not include null case for rendering
npol.push(copy_vec3(pol[0])); //npol[0] = pol[0];
npol.push(copy_vec3(pol[1])); //npol[1] = pol[1];
npol.push(copy_vec3(pol[2])); //npol[2] = pol[2];
npol.push(copy_vec3(pol[3])); //npol[3] = pol[3];
return npol;
}
else {
let lerp = dz0/(dz0-dz1);
npol.push(copy_vec3(pol[0])); //npol[0] = pol[0];
npol.push(intersect_vec3(pol[1], pol[0], lerp));
npol.push(copy_vec3(pol[3])); //npol[2] = pol[3];
return npol;
}
}
else if (dz1 > 0.0) { //do not include null case for rendering
let lerp = dz0/(dz0-dz1);
npol.push(intersect_vec3(pol[1], pol[0], lerp));
npol.push(copy_vec3(pol[1])); //npol[1] = pol[1];
npol.push(copy_vec3(pol[2])); //npol[2] = pol[2];
return npol;
}
// Clip. Do not include
return npol;
}
function normal_from_tri(tri) { // tri = array[3] of {x,y,z}
var result = {x:0, y:0, z:0};
result.x = (tri[1].y-tri[0].y)*(tri[2].z-tri[0].z) - (tri[1].z-tri[0].z)*(tri[2].y-tri[0].y);
result.y = (tri[1].z-tri[0].z)*(tri[2].x-tri[0].x) - (tri[1].x-tri[0].x)*(tri[2].z-tri[0].z);
result.z = (tri[1].x-tri[0].x)*(tri[2].y-tri[0].y) - (tri[1].y-tri[0].y)*(tri[2].x-tri[0].x);
let f = result.x*result.x + result.y*result.y + result.z*result.z;
if (f > 0) f = -1/Math.sqrt(f);
result.x *= f;
result.y *= f;
result.z *= f;
return result;
}
function triangulate(contours) {
var tessy = new libtess.GluTesselator();
tessy.gluTessCallback(libtess.gluEnum.GLU_TESS_VERTEX_DATA, function (data, polyVertArray) {
// console.log(data[0], data[1]);
polyVertArray[polyVertArray.length] = data[0];
polyVertArray[polyVertArray.length] = data[1];
});
// libtess will take 3d verts and flatten to a plane for tesselation
// since only doing 2d tesselation here, provide z=1 normal to skip
// iterating over verts only to get the same answer.
// comment out to test normal-generation code
tessy.gluTessNormal(0, 0, 1);
var triangleVerts = [];
tessy.gluTessBeginPolygon(triangleVerts);
for (var i = 0; i < contours.length; i++) {
tessy.gluTessBeginContour();
var contour = contours[i];
for (var j = 0; j < contour.length; j += 2) {
var coords = [contour[j], contour[j + 1], 0];
tessy.gluTessVertex(coords, coords);
}
tessy.gluTessEndContour();
}
// finish polygon (and time triangulation process)
tessy.gluTessEndPolygon();
return triangleVerts;
}
/*
* saveasstl()
* This function appears to take the stuff we have loaded from loadmap() and convert it into "Simple Triangle Soup"
* This is where all the action is
*/
function saveasstl() {
const MAXVERTS = 256;
//#define MAXVERTS 256 //WARNING:not dynamic
// pol,npol= typedef struct { float x, y, z; int n; } kgln_t;
let pol = [
{x:0, y:0, z:0},
{x:0, y:0, z:0},
{x:0, y:0, z:0},
{x:0, y:0, z:0}
];
// Output Geometry
let tri = [
{x:0, y:0, z:0},
{x:0, y:0, z:0},
{x:0, y:0, z:0}
];
let normal = {x:0, y:0, z:0};
let f;
// This is our intermediate format before converting to obj or whatever
map2stl_output = [];
// Two for loops nested in this for loop. One for creating ceilings/floors. The second for creating walls.
for(let s=0; s<dukemap.map.numsects; s++) {
let wall = sectorInfo[s].wall;
let firstWall = wall[0]; // Slopes are aligned to wall[0]
// NOTE: This is done slightly differently than in map2stl.c
// We return nzoids here because it's easy. We're also not expecting memory to fail. So we aren't even checking for that anymore.
// Also in this version sect2trap only gets called once for both the floor and the ceiling
let zoids = sect2trap(wall);
// Generate triangles
let sector = dukemap.map.sectors[s];
let flat = [];
let contour = [];
let hole = [];
let endWall = sector.wallnum+sector.wallptr;
for (let w=sector.wallptr;w<endWall;w++) {
let curWall = dukemap.map.walls[w];
let nextWall = dukemap.map.walls[curWall.point2];
flat.push(curWall.x);
flat.push(curWall.y);
flat.push(nextWall.x);
flat.push(nextWall.y);
// Close the loop?
if (curWall.point2 < w) {
contour.push(new Float32Array(flat));
flat = [];
}
}
/*
let poly = earcut(flat, hole, 2);
let deviation = earcut.deviation(flat, hole, 2, poly);
if (deviation > 0) {
console.log(deviation);
}
*/
//let poly = triangulate([new Float32Array(flat)]);
let poly = triangulate(contour);
//draw sector filled - Ceilings and Floors first
//is_floor=0; // CEILING
//is_floor=1; // FLOOR
for(let is_floor=0; is_floor<=1; is_floor++) {
// parallaxing = skybox. SKIP
if (is_floor == 0 && dukemap.map.sectors[s].ceilingstat_.parallaxing == true) continue;
if (is_floor == 1 && dukemap.map.sectors[s].floorstat_.parallaxing == true) continue;
/*
for(let i=0; i<zoids.length; i++) {
let polInd=0;
for(let j=0; j<4; j++) {
pol[polInd].x = zoids[i].x[j];
pol[polInd].y = zoids[i].y[j>>1];
if ((polInd == 0) || (pol[polInd].x != pol[polInd-1].x) || (pol[polInd].y != pol[polInd-1].y)) {
pol[polInd].z = (firstWall.x-pol[polInd].x)*grad.x + (firstWall.y-pol[polInd].y)*grad.y + fz;
polInd++;
}
}
if (polInd < 3) continue;
tri[0].x = pol[0].x;
tri[0].y = pol[0].y;
tri[0].z = pol[0].z;
for(let j=2;j<polInd;j++) {
let k1 = j-is_floor;
tri[1].x = pol[k1].x;
tri[1].y = pol[k1].y;
tri[1].z = pol[k1].z;
let k2 = (j-1)+is_floor;
tri[2].x = pol[k2].x;
tri[2].y = pol[k2].y;
tri[2].z = pol[k2].z;
normal = normal_from_tri(tri);
write_map2stl_output({
type: (is_floor == 1) ? "floor" : "ceil",
normal: normal,
tri: [
tri[2], tri[1], tri[0]
],
sector: s,
originalIndex: s // original index in the .MAP file
});
}
}
*/
//console.log(flat,hole,poly);
for (let i=0;i<poly.length;i+=6) {
if (is_floor == 1) {
tri[0].x = poly[i+0];
tri[0].y = poly[i+1];
tri[1].x = poly[i+2];
tri[1].y = poly[i+3];
tri[2].x = poly[i+4];
tri[2].y = poly[i+5];
}
else {
tri[0].x = poly[i+4];
tri[0].y = poly[i+5];
tri[1].x = poly[i+2];
tri[1].y = poly[i+3];
tri[2].x = poly[i+0];
tri[2].y = poly[i+1];
}
tri[0].z = getslopez(sectorInfo[s], is_floor, tri[0].x, tri[0].y);
tri[1].z = getslopez(sectorInfo[s], is_floor, tri[1].x, tri[1].y);
tri[2].z = getslopez(sectorInfo[s], is_floor, tri[2].x, tri[2].y);
normal = normal_from_tri(tri);
write_map2stl_output({
type: (is_floor == 1) ? "floor" : "ceil",
normal: normal,
tri: [
tri[2], tri[1], tri[0]
],
sector: s,
originalIndex: s // original index in the .MAP file
});
}
}
/*
for(let w=0; w<sectorInfo[s].wallcount; w++) {
let cur_wall = wall[w];
let next_wall = wall[w+cur_wall.n];
let sector = sectorInfo[s];
if (typeof cur_wall.skip !== "undefined") continue;
if (cur_wall.nw == -1) { // -1 = no next wall
let tri1 = [
{
x: cur_wall.x,
y: cur_wall.y,
z: getslopez(sector,0,cur_wall.x,cur_wall.y)//sector.z[0]
},
{
x: cur_wall.x,
y: cur_wall.y,
z: getslopez(sector,1,cur_wall.x,cur_wall.y)//sector.z[1]
},
{
x: next_wall.x,
y: next_wall.y,
z: getslopez(sector,0,next_wall.x,next_wall.y)//sector.z[0]
}
];
let tri2 = [
{
x: cur_wall.x,
y: cur_wall.y,
z: getslopez(sector,1,cur_wall.x,cur_wall.y)//sector.z[1]
},
{
x: next_wall.x,
y: next_wall.y,
z: getslopez(sector,1,next_wall.x,next_wall.y)//sector.z[1]
},
{
x: next_wall.x,
y: next_wall.y,
z: getslopez(sector,0,next_wall.x,next_wall.y)//sector.z[0]
}
];
let normal = normal_from_tri(tri1);
write_map2stl_output({
type: "wall",
normal: normal,
tri: tri1,
wall: wall[w],
sector: s,
originalIndex: wall[w].orig.wallIndex // Original index in the .MAP file
});
write_map2stl_output({
type: "wall",
normal: normal,
tri: tri2,
wall: wall[w],
sector: s,
originalIndex: wall[w].orig.wallIndex // Original index in the .MAP file
});
}
else {
// Find the neighboring sector/wall
let nbr_sector = sectorInfo[cur_wall.neighborSector];
let nbr_wall = nbr_sector.wall[cur_wall.nw];
nbr_sector.wall[cur_wall.nw].skip = true;
if (nbr_sector.z[0] != sector.z[0]) { // ceiling height different
let cw_z = getslopez(sector,0,cur_wall.x,cur_wall.y);
let nw_z = getslopez(sector,0,next_wall.x,next_wall.y);
let tri1 = [
{
x: cur_wall.x,
y: cur_wall.y,
z: getslopez(sector,0,cur_wall.x,cur_wall.y)//sector.z[0]
},
{
x: cur_wall.x,
y: cur_wall.y,
z: getslopez(nbr_sector,0,cur_wall.x,cur_wall.y)//nbr_sector.z[0]
},
{
x: next_wall.x,
y: next_wall.y,
z: getslopez(sector,0,next_wall.x,next_wall.y)//sector.z[0]
}
];
let tri2 = [
{
x: cur_wall.x,
y: cur_wall.y,
z: getslopez(nbr_sector,0,cur_wall.x,cur_wall.y)//nbr_sector.z[0]
},
{
x: next_wall.x,
y: next_wall.y,
z: getslopez(nbr_sector,0,next_wall.x,next_wall.y)//nbr_sector.z[0]
},
{
x: next_wall.x,
y: next_wall.y,
z: getslopez(sector,0,next_wall.x,next_wall.y)//sector.z[0]
}
];
let normal = normal_from_tri(tri1);
write_map2stl_output({
type: "wall",
normal: normal,
tri: tri1,
wall: nbr_wall,
sector: s,
originalIndex: wall[w].orig.wallIndex // Original index in the .MAP file
});
write_map2stl_output({
type: "wall",
normal: normal,
tri: tri2,
wall: nbr_wall,
sector: s,
originalIndex: wall[w].orig.wallIndex // Original index in the .MAP file
});
}
if (nbr_sector.z[1] != sector.z[1]) { // floor height different
let tri1 = [
{
x: cur_wall.x,
y: cur_wall.y,
z: getslopez(nbr_sector,1,cur_wall.x,cur_wall.y)//nbr_sector.z[1]
},
{
x: cur_wall.x,
y: cur_wall.y,
z: getslopez(sector,1,cur_wall.x,cur_wall.y)//sector.z[1]
},
{
x: next_wall.x,
y: next_wall.y,
z: getslopez(nbr_sector,1,next_wall.x,next_wall.y)//nbr_sector.z[1]
}
];
let tri2 = [
{
x: cur_wall.x,
y: cur_wall.y,
z: getslopez(sector,1,cur_wall.x,cur_wall.y)//sector.z[1]
},
{
x: next_wall.x,
y: next_wall.y,
z: getslopez(sector,1,next_wall.x,next_wall.y)//sector.z[1]
},
{
x: next_wall.x,
y: next_wall.y,
z: getslopez(nbr_sector,1,next_wall.x,next_wall.y)//nbr_sector.z[1]
}
];
let normal = normal_from_tri(tri1);
write_map2stl_output({
type: "wall",
normal: normal,
tri: tri1,
wall: wall[w],
sector: s,
originalIndex: wall[w].orig.wallIndex // Original index in the .MAP file
});
write_map2stl_output({
type: "wall",
normal: normal,
tri: tri2,
wall: wall[w],
sector: s,
originalIndex: wall[w].orig.wallIndex // Original index in the .MAP file
});
}
}
}
*/
// Draw Walls
for(let w=0; w<sectorInfo[s].wallcount; w++) {
let nextWall = wall[w].n+w;
//let verts = getPortalWalls(s,w);
let vn = 0;
pol[0].x = wall[w].x; pol[0].y = wall[w].y;
pol[1].x = wall[nextWall].x; pol[1].y = wall[nextWall].y;
pol[2].x = wall[nextWall].x; pol[2].y = wall[nextWall].y;
pol[3].x = wall[w].x; pol[3].y = wall[w].y;
if (wall[w].neighborSector != -1) {
vn = 1;
}
for(let k=0;k<=vn;k++) { //Warning: do not reverse for loop!
let s0;
let s1;
let cf0;
let cf1;
if (wall[w].neighborSector != -1) { // Has neighbor wall
if (k == 0) {
s0 = s; // Cur Sector
s1 = wall[w].neighborSector; // Next sector
cf0 = 0; // Ceiling
cf1 = 0; // Ceiling
// Check for skybox...
let tsec = dukemap.map.sectors[s];
let nsec = dukemap.map.sectors[wall[w].neighborSector];
if (tsec.ceilingstat_.parallaxing && nsec.ceilingstat_.parallaxing) continue;
}
else {
s0 = wall[w].neighborSector; // Next sector
s1 = s; // Cur Sector
cf0 = 1; // Floor
cf1 = 1; // Floor
// Check for skybox...
let tsec = dukemap.map.sectors[s];
let nsec = dukemap.map.sectors[wall[w].neighborSector];
if (tsec.floorstat_.parallaxing && nsec.floorstat_.parallaxing) continue;
}
}
else { // Build wall from top to bottom
s0 = s; // Cur Sector
s1 = s; // Cur Sector
cf0 = 0; // Ceiling
cf1 = 1; // Floor
}
// Z positions (aka height) will determine where the wall clips
pol[0].z = getslopez(sectorInfo[s0],cf0,pol[0].x,pol[0].y);
pol[1].z = getslopez(sectorInfo[s0],cf0,pol[1].x,pol[1].y);
pol[2].z = getslopez(sectorInfo[s1],cf1,pol[2].x,pol[2].y);
pol[3].z = getslopez(sectorInfo[s1],cf1,pol[3].x,pol[3].y);
// Now clip based on Z
let npol = wallclip(pol);
if (npol.length == 0) continue;
// Finalized triangles come from npol starting with #0
tri[0] = copy_vec3(npol[0]);
for(let j=1;j<npol.length-1;j++) {
tri[1] = copy_vec3(npol[j]);
tri[2] = copy_vec3(npol[j+1]);
normal = normal_from_tri(tri);
write_map2stl_output({
j: j,
npol: npol,
type: "wall",
normal: normal,
tri: [
{
x: npol[j+1].x,
y: npol[j+1].y,
z: npol[j+1].z
},
{
x: npol[j].x,
y: npol[j].y,
z: npol[j].z,
},
{
x: npol[0].x,
y: npol[0].y,
z: npol[0].z,
}
],
wall: wall[w],
sector: s,
originalIndex: wall[w].orig.wallIndex // Original index in the .MAP file
});
}
}
}
}
}
/*
* write_map2stl_output()
* Since JavaScript will pass a lot of these things by reference, it is important that we explicitly copy everything
* That's all this function is doing. Copying variables into the map2stl_output variable.
*/
function write_map2stl_output(params) {
var result = {
type: params.type,
normal: new THREE.Vector3(),
tri: [
new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3()
],
wall: params.wall,
originalIndex: params.originalIndex,
j: params.j,
npol: params.npol
};
if (typeof params.sector !== "undefined") {
result.sector = params.sector;
}
result.normal.copy(params.normal);
result.tri[0].copy(params.tri[0]);
result.tri[1].copy(params.tri[1]);
result.tri[2].copy(params.tri[2]);
map2stl_output.push(result);
}
/*
* loadmap()
* In the original version of map2stl, all the loading happened here
* In the JS version, we did most of our loading in our more general purpose dukemap.js
* This function is now designed to take the data extrated from dukemap and format it for
* use with map2stl
*/
function loadmap() {
// Copy relevant sector info and walls to sectorInfo[]
sectorInfo = [];
// By now we've pulled data using dukemap.js
// This converts sectors from the dukemap format to our map2stl format so we can process them in the saveasstl() function
for (let i=0;i<dukemap.map.sectors.length;i++) {
let b7sec = dukemap.map.sectors[i];
// Create new sectorInfo
sectorInfo.push(new_sect_t());
sectorInfo[i].sectorIndex = i;
sectorInfo[i].orig = b7sec;
sectorInfo[i].wallcount = b7sec.wallnum;
if (b7sec.lotag == 2) { // Water
sectorInfo[i].isWater = true;
}
else {
sectorInfo[i].isWater = false;
}
// Floor Z position
sectorInfo[i].z[0] = (b7sec.ceilingz / 16);
sectorInfo[i].z[1] = (b7sec.floorz / 16);
// Convert slopes from 8192 to 90 degrees. Max 32767
if (b7sec.ceilingstat&2) { //&2 = Enable slopes flag
sectorInfo[i].grad[0].slope = b7sec.ceilingheinum*(1/4096); // 4096 = 45 degrees. 0 = flat
}
if (b7sec.floorstat&2) { //&2 = Enable slopes flag
sectorInfo[i].grad[1].slope = b7sec.floorheinum*(1/4096); // 4096 = 45 degrees. 0 = flat
}
}
let wallIndex = 0;
for(let i=0;i<dukemap.map.numsects;i++) {
for(let j=0;j<sectorInfo[i].wallcount;j++,wallIndex++) {
let startpos = dukemap.map.sectors[i].wallptr;
let b7wal = dukemap.map.walls[wallIndex];
b7wal.wallIndex = wallIndex;
sectorInfo[i].wall.push({
x: b7wal.x,
y: b7wal.y,
n: b7wal.point2-wallIndex,
// Added orig so we can extract wall textures and other attributes later...
orig: b7wal
});
}
let fx = sectorInfo[i].wall[1].y-sectorInfo[i].wall[0].y;
let fy = sectorInfo[i].wall[0].x-sectorInfo[i].wall[1].x;
let f = fx*fx + fy*fy;
if (f > 0) {
f = 1/Math.sqrt(f);
}
fx *= f;
fy *= f;
for(let j=0;j<2;j++) {
sectorInfo[i].grad[j].x = fx*sectorInfo[i].grad[j].slope;
sectorInfo[i].grad[j].y = fy*sectorInfo[i].grad[j].slope;
}
}
return true;
}
function checknextwalls() {
let $goto = false;
//Clear all nextsect/nextwalls
for(let s0=0;s0<dukemap.map.numsects;s0++) {
for(let w0=0;w0<sectorInfo[s0].wallcount;w0++) {
sectorInfo[s0].wall[w0].neighborSector = sectorInfo[s0].wall[w0].neighborWall = -1;
}
}
for(let s1=1;s1<dukemap.map.numsects;s1++) {
for(let w1=0;w1<sectorInfo[s1].wallcount;w1++) {
let x0 = sectorInfo[s1].wall[w1].x;
let y0 = sectorInfo[s1].wall[w1].y;
let nextWall = sectorInfo[s1].wall[w1].n+w1;
//console.log(s1, nextWall,sectorInfo[s1].wall[nextWall]);
let x1 = sectorInfo[s1].wall[nextWall].x;
let y1 = sectorInfo[s1].wall[nextWall].y;
// This next step checks for walls that are right next to each other.
// This data is already saved in the map, but for some reason we're scanning through this anyway
$goto = false; // Little hack to simulate goto
for(let s0=0;s0<s1;s0++) {
for(let w0=0;w0<sectorInfo[s0].wallcount;w0++) {
if ((sectorInfo[s0].wall[w0].x == sectorInfo[s1].wall[nextWall].x) && (sectorInfo[s0].wall[w0].y == sectorInfo[s1].wall[nextWall].y)) {
let w0n = sectorInfo[s0].wall[w0].n+w0;
if ((sectorInfo[s0].wall[w0n].x == sectorInfo[s1].wall[w1].x) && (sectorInfo[s0].wall[w0n].y == sectorInfo[s1].wall[w1].y)) {
sectorInfo[s1].wall[w1].neighborSector = s0;
sectorInfo[s1].wall[w1].neighborWall = w0;
sectorInfo[s0].wall[w0].neighborSector = s1;
sectorInfo[s0].wall[w0].neighborWall = w1;
$goto = true;
}
}
if ($goto) { break; }
}
if ($goto) { break; }
}
//cnw_break2:;
}
}
}