-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pine3D.lua
2438 lines (2078 loc) · 67.8 KB
/
Pine3D.lua
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
-- Made by Xella#8655
local libFolder = (...):match("(.-)[^%.]+$")
local betterblittle = require(libFolder .. "betterblittle")
local min = math.min
local max = math.max
local floor = math.floor
local ceil = math.ceil
---Creates a new Buffer for rendering triangles
---@param x integer the new x position of the window
---@param y integer the new y position of the window
---@param w integer the new width of the window
---@param h integer the new height of the window
---@return Buffer
local function newBuffer(x, y, w, h)
---@class Buffer
local buffer = {
width = w * 2,
height = h * 3,
colorValues = {},
depthValues = {},
blitWin = window.create(term.current(), x, y, w, h, false),
backgroundColor = colors.lightBlue
}
---Reposition the Buffer
---@param x integer the new x position of the window
---@param y integer the new y position of the window
---@param w integer the new width of the window
---@param h integer the new height of the window
function buffer:setSize(x, y, w, h)
self.width = w * 2
self.height = h * 3
self.blitWin.reposition(x, y, w, h)
self:clear()
end
---Fully clear the Buffer
function buffer:clear()
self.colorValues = {}
self.depthValues = {}
local colors = self.colorValues
local depths = self.depthValues
local bigNeg = math.huge
local width = self.width
local color = self.backgroundColor
for y = 1, self.height do
colors[y] = {}
depths[y] = {}
local colorsY = colors[y]
local depthsY = depths[y]
for x = 1, width do
colorsY[x] = color
depthsY[x] = bigNeg
end
end
end
function buffer:clearDepth()
local depths = self.depthValues
local bigNeg = -math.huge
local width = self.width
for y = 1, self.height do
local depthsY = depths[y]
for x = 1, width do
depthsY[x] = bigNeg
end
end
end
---Clear the Buffer quickly
function buffer:fastClear()
local bgColor = self.backgroundColor
local colors = self.colorValues
local width = self.width
for y = 1, self.height do
local colorsY = colors[y]
for x = 1, width do
colorsY[x] = bgColor
end
end
end
---Set the color of an individual pixel
---@param x number teletext pixel x coordinate
---@param y number teletext pixel y coordinate
---@param c integer color for the pixel
function buffer:setPixel(x, y, c)
x = floor(x + 0.5)
y = floor(y + 0.5)
if x >= 1 and x <= self.width then
if y >= 1 and y <= self.height then
self.colorValues[y][x] = c
end
end
end
---@alias PaintUtilsImage table
---Render an image to the Buffer at a given offset
---@param x integer teletext pixel x coordinate
---@param y integer teletext pixel y coordinate
---@param image PaintUtilsImage can be loaded with paintutils.loadImage (from .nfp)
function buffer:image(x, y, image)
local width = self.width
local colors = self.colorValues
for yImage, row in pairs(image) do
local drawY = yImage + y
local colorsY = colors[drawY]
if colorsY then
for xImage, value in pairs(row) do
if value and value > 0 then
local drawX = xImage + x
if drawX >= 1 and drawX <= width then
colorsY[drawX] = value
end
end
end
end
end
end
function buffer:drawLineNoInterp(x1, y1, x2, y2, c, depthInverted)
local colors = self.colorValues
local depths = self.depthValues
local frameWidth = self.width
local frameHeight = self.height
local dx = x2 - x1
local dy = y2 - y1
if dx ~= 0 then
for x = max(ceil(min(x1, x2)), 1), min(floor(max(x1, x2)), frameWidth) do
local xRatio = (x - x1) / dx
local y = floor(y1 + xRatio * dy + 0.5)
if y > 0 and y <= frameHeight and depthInverted >= depths[y][x] then
colors[y][x] = c
depths[y][x] = depthInverted
end
end
end
if dy ~= 0 then
for y = max(ceil(min(y1, y2)), 1), min(floor(max(y1, y2)), frameHeight) do
local yRatio = (y - y1) / dy
local x = floor(x1 + yRatio * dx + 0.5)
if x > 0 and x <= frameWidth and depthInverted >= depths[y][x] then
colors[y][x] = c
depths[y][x] = depthInverted
end
end
end
end
function buffer:drawLineInterp(x1, y1, x2, y2, c, z1Inv, z2Inv)
local colors = self.colorValues
local depths = self.depthValues
local frameWidth = self.width
local frameHeight = self.height
x1 = x1 + 0.5
x2 = x2 + 0.5
y1 = y1 - 0.5
y2 = y2 - 0.5
local dx = x2 - x1
local dy = y2 - y1
local slopeZInv = z2Inv - z1Inv
if dx ~= 0 then
for x = max(ceil(min(x1, x2)), 1), min(floor(max(x1, x2)), frameWidth) do
local xRatio = (x - x1) / dx
local y = floor(y1 + xRatio * dy + 0.5)
local zInv = z1Inv + xRatio * slopeZInv
if y > 0 and y <= frameHeight and zInv >= depths[y][x] then
colors[y][x] = c
depths[y][x] = zInv
end
end
end
if dy ~= 0 then
for y = max(ceil(min(y1, y2)), 1), min(floor(max(y1, y2)), frameHeight) do
local yRatio = (y - y1) / dy
local x = floor(x1 + yRatio * dx + 0.5)
local zInv = z1Inv + yRatio * slopeZInv
if x > 0 and x <= frameWidth and zInv >= depths[y][x] then
colors[y][x] = c
depths[y][x] = zInv
end
end
end
end
local defaultOutlineColor = colors.black
function buffer:drawTriangleNoInterp(x1, y1, x2, y2, x3, y3, c, outlineColor, depth)
if x1 < 0 and x2 < 0 and x3 < 0 or y1 < 1 and y2 < 1 and y3 < 1 then return end
local frameWidth = self.width
if x1 > frameWidth and x2 > frameWidth and x3 > frameWidth then return end
local frameHeight = self.height
if y1 > frameHeight and y2 > frameHeight and y3 > frameHeight then return end
if y1 > y2 then
y1, y2 = y2, y1
x1, x2 = x2, x1
end
if y2 > y3 then
y3, y2 = y2, y3
x3, x2 = x2, x3
end
if y1 > y2 then
y1, y2 = y2, y1
x1, x2 = x2, x1
end
local floor, ceil = floor, ceil
local min, max = min, max
local minY = min(max(1, ceil(y1)), frameHeight)
local midY = min(max(0, floor(y2)), frameHeight)
local maxY = min(max(1, floor(y3)), frameHeight)
local colors = self.colorValues
local depths = self.depthValues
local depthInverted = 1 / depth
if y1 ~= y2 and y1 ~= y3 then
local slopeX_YA = (x2 - x1) / (y2 - y1)
local slopeX_YB = (x3 - x1) / (y3 - y1)
for y = minY, midY do
local dy = y - y1
local xA = x1 + dy * slopeX_YA
local xB = x1 + dy * slopeX_YB
if xB < xA then xA, xB = xB, xA end
local xABounded = floor(xA + 0.5)
local xBBounded = floor(xB + 0.5)
if xABounded < 1 then xABounded = 1 end
if xBBounded > frameWidth then xBBounded = frameWidth end
local colorsY = colors[y]
local depthsY = depths[y]
for x = xABounded, xBBounded do
if depthInverted > depthsY[x] then
depthsY[x] = depthInverted
colorsY[x] = c
end
end
end
end
if y3 ~= y2 and y1 ~= y3 then
local slopeX_YA = (x2 - x3) / (y2 - y3)
local slopeX_YB = (x1 - x3) / (y1 - y3)
for y = midY + 1, maxY do
local dy = y - y3
local xA = x3 + dy * slopeX_YA
local xB = x3 + dy * slopeX_YB
if xB < xA then xA, xB = xB, xA end
local xABounded = floor(xA + 0.5)
local xBBounded = floor(xB + 0.5)
if xABounded < 1 then xABounded = 1 end
if xBBounded > frameWidth then xBBounded = frameWidth end
local colorsY = colors[y]
local depthsY = depths[y]
for x = xABounded, xBBounded do
if depthInverted > depthsY[x] then
depthsY[x] = depthInverted
colorsY[x] = c
end
end
end
end
local outlineColor = outlineColor
if outlineColor or self.triangleEdges then
local loadLine = self.drawLineNoInterp
local c = outlineColor or defaultOutlineColor
loadLine(self, x1, y1, x2, y2, c, depthInverted)
loadLine(self, x2, y2, x3, y3, c, depthInverted)
loadLine(self, x3, y3, x1, y1, c, depthInverted)
end
end
function buffer:drawTriangleInterp(x1, y1, x2, y2, x3, y3, c, outlineColor, d, z1, z2, z3)
if x1 < 0 and x2 < 0 and x3 < 0 or y1 < 1 and y2 < 1 and y3 < 1 then return end
local frameWidth = self.width
if x1 > frameWidth and x2 > frameWidth and x3 > frameWidth then return end
local frameHeight = self.height
if y1 > frameHeight and y2 > frameHeight and y3 > frameHeight then return end
if y1 > y2 then
y1, y2 = y2, y1
x1, x2 = x2, x1
z1, z2 = z2, z1
end
if y2 > y3 then
y3, y2 = y2, y3
x3, x2 = x2, x3
z3, z2 = z2, z3
end
if y1 > y2 then
y1, y2 = y2, y1
x1, x2 = x2, x1
z1, z2 = z2, z1
end
local floor, ceil = floor, ceil
local min, max = min, max
local minY = min(max(1, ceil(y1)), frameHeight)
local midY = min(max(0, floor(y2)), frameHeight)
local maxY = min(max(1, floor(y3)), frameHeight)
local colors = self.colorValues
local depths = self.depthValues
local z3Inv = 1 / z3
local z2Inv = 1 / z2
local z1Inv = 1 / z1
if y1 ~= y2 and y1 ~= y3 then
local y2_min_y1 = y2 - y1
local y3_min_y1 = y3 - y1
local slopeX_YA = (x2 - x1) / y2_min_y1
local slopeX_YB = (x3 - x1) / y3_min_y1
local slopeZ_YAInv = z2Inv - z1Inv
local slopeZ_YBInv = z3Inv - z1Inv
for y = minY, midY do
local dy = y - y1
local xA = x1 + dy * slopeX_YA
local xB = x1 + dy * slopeX_YB
local yRatioA = dy / y2_min_y1
local yRatioB = dy / y3_min_y1
local zAInv = z1Inv + yRatioA * slopeZ_YAInv
local zBInv = z1Inv + yRatioB * slopeZ_YBInv
if xB < xA then
xA, xB = xB, xA
zAInv, zBInv = zBInv, zAInv
end
local xABounded = floor(xA + 1.5)
local xBBounded = floor(xB + 0.5)
if xABounded < 1 then xABounded = 1 end
if xBBounded > frameWidth then xBBounded = frameWidth end
xA = floor(xA + 0.5)
xB = floor(xB + 1.5)
local xLength = xB - xA
local colorsY = colors[y]
local depthsY = depths[y]
local slopeZ_XInv = zBInv - zAInv
for x = xABounded, xBBounded do
local xRatio = (x - xA) / xLength
local zInv = zAInv + xRatio * slopeZ_XInv
if zInv > depthsY[x] then
depthsY[x] = zInv
colorsY[x] = c
end
end
end
end
if y3 ~= y2 and y1 ~= y3 then
local y2_min_y3 = y2 - y3
local y1_min_y3 = y1 - y3
local slopeX_YA = (x2 - x3) / y2_min_y3
local slopeX_YB = (x1 - x3) / y1_min_y3
local slopeZ_YAInv = z2Inv - z3Inv
local slopeZ_YBInv = z1Inv - z3Inv
for y = midY + 1, maxY do
local dy = y - y3
local xA = x3 + dy * slopeX_YA
local xB = x3 + dy * slopeX_YB
local yRatioA = dy / y2_min_y3
local yRatioB = dy / y1_min_y3
local zAInv = z3Inv + yRatioA * slopeZ_YAInv
local zBInv = z3Inv + yRatioB * slopeZ_YBInv
if xB < xA then
xA, xB = xB, xA
zAInv, zBInv = zBInv, zAInv
end
local xABounded = floor(xA + 1.5)
local xBBounded = floor(xB + 0.5)
if xABounded < 1 then xABounded = 1 end
if xBBounded > frameWidth then xBBounded = frameWidth end
xA = floor(xA + 0.5)
xB = floor(xB + 1.5)
local xLength = xB - xA
local colorsY = colors[y]
local depthsY = depths[y]
local slopeZ_XInv = zBInv - zAInv
for x = xABounded, xBBounded do
local xRatio = (x - xA) / xLength
local zInv = zAInv + xRatio * slopeZ_XInv
if zInv > depthsY[x] then
depthsY[x] = zInv
colorsY[x] = c
end
end
end
end
local outlineColor = outlineColor
if outlineColor or self.triangleEdges then
local loadLine = self.drawLineInterp
local c = outlineColor or defaultOutlineColor
loadLine(self, x1, y1, x2, y2, c, z1Inv, z2Inv)
loadLine(self, x2, y2, x3, y3, c, z2Inv, z3Inv)
loadLine(self, x3, y3, x1, y1, c, z3Inv, z1Inv)
end
end
function buffer:drawBuffer()
local blitWin = self.blitWin
betterblittle.drawBuffer(self.colorValues, blitWin)
blitWin.setVisible(true)
blitWin.setVisible(false)
end
function buffer:depthInterpolation(enabled)
self.drawTriangle = enabled and self.drawTriangleInterp or self.drawTriangleNoInterp
self:clear()
end
function buffer:useTriangleEdges(enabled)
self.triangleEdges = enabled
end
buffer:depthInterpolation(true)
return buffer
end
---Computes distance to comera for each polygon
---@param polygons Polygon[]
---@param objectX number
---@param objectY number
---@param objectZ number
---@param camera CollapsedCamera
local function computePolyCamDistance(polygons, objectX, objectY, objectZ, camera)
local camX = camera[1]
local camY = camera[2]
local camZ = camera[3]
local rx = objectX and (objectX - camX) or 0
local ry = objectY and (objectY - camY) or 0
local rz = objectZ and (objectZ - camZ) or 0
for i = 1, #polygons do
local polygon = polygons[i]
local avgX = rx + (polygon[1] + polygon[4] + polygon[7]) / 3
local avgY = ry + (polygon[2] + polygon[5] + polygon[8]) / 3
local avgZ = rz + (polygon[3] + polygon[6] + polygon[9]) / 3
polygon[16] = avgX * avgX + avgY * avgY + avgZ * avgZ -- relative distance
end
end
local rad = math.rad
local sin = math.sin
local cos = math.cos
local function rotatePolygonX(x, y, z, rotS, rotC)
local z2 = rotC * z - rotS * y
local y = rotS * z + rotC * y
local z = z2
return x, y, z
end
local function rotatePolygonY(x, y, z, rotS, rotC)
local z2 = rotC * z - rotS * x
local x = rotS * z + rotC * x
local z = z2
return x, y, z
end
local function rotatePolygonZ(x, y, z, rotS, rotC)
local y2 = rotC * y - rotS * x
local x = rotS * y + rotC * x
local y = y2
return x, y
end
---Rotates a given CollapsedModel around three axes
---@param model CollapsedModel
---@param rotX number?
---@param rotY number?
---@param rotZ number?
---@return CollapsedModel
local function rotateCollapsedModel(model, rotX, rotY, rotZ)
local rotXS, rotXC = 0, 1
local rotYS, rotYC = 0, 1
local rotZS, rotZC = 0, 1
if rotX == 0 then rotX = nil end
if rotX then rotXS, rotXC = sin(rotX), cos(rotX) end
if rotY == 0 then rotY = nil end
if rotY then rotYS, rotYC = sin(rotY), cos(rotY) end
if rotZ == 0 then rotZ = nil end
if rotZ then rotZS, rotZC = sin(rotZ), cos(rotZ) end
local rotatedModel = {}
for _, polygon in pairs(model) do
local x1, y1, z1 = polygon[1], polygon[2], polygon[3]
local x2, y2, z2 = polygon[4], polygon[5], polygon[6]
local x3, y3, z3 = polygon[7], polygon[8], polygon[9]
if rotY then
x1, y1, z1 = rotatePolygonY(x1, y1, z1, rotYS, rotYC)
x2, y2, z2 = rotatePolygonY(x2, y2, z2, rotYS, rotYC)
x3, y3, z3 = rotatePolygonY(x3, y3, z3, rotYS, rotYC)
end
if rotZ then
x1, y1 = rotatePolygonZ(x1, y1, z1, rotZS, rotZC)
x2, y2 = rotatePolygonZ(x2, y2, z2, rotZS, rotZC)
x3, y3 = rotatePolygonZ(x3, y3, z3, rotZS, rotZC)
end
if rotX then
x1, y1, z1 = rotatePolygonX(x1, y1, z1, rotXS, rotXC)
x2, y2, z2 = rotatePolygonX(x2, y2, z2, rotXS, rotXC)
x3, y3, z3 = rotatePolygonX(x3, y3, z3, rotXS, rotXC)
end
rotatedModel[#rotatedModel + 1] = {x1, y1, z1, x2, y2, z2, x3, y3, z3}
rotatedModel[#rotatedModel][10] = polygon[10]
rotatedModel[#rotatedModel][11] = polygon[11]
rotatedModel[#rotatedModel][12] = polygon[12]
end
return rotatedModel
end
local transforms = {}
---Model Inverts the direction of all triangles
---@param model Model
---@return Model
function transforms.invertTriangles(model)
if not model or type(model) ~= "table" then
error("transforms.invertTriangles expected arg#1 to be a table (model)")
end
for i = 1, #model do
local triangle = model[i]
model[i] = {
x1 = triangle.x1,
y1 = triangle.y1,
z1 = triangle.z1,
x2 = triangle.x3,
y2 = triangle.y3,
z2 = triangle.z3,
x3 = triangle.x2,
y3 = triangle.y2,
z3 = triangle.z2,
c = triangle.c,
forceRender = triangle.forceRender,
outlineColor = triangle.outlineColor,
}
end
return model
end
---Change the outline colors of polygons in a Model
---@param model Model
---@param col number|table if number, will set the outline color for each Polygon, if table, uses it as a mapping from Polygon color to new outline color
---@return Model
function transforms.setOutline(model, col)
if not model or type(model) ~= "table" then
error("transforms.invertTriangles expected arg#1 to be a table (model)")
end
for i = 1, #model do
local triangle = model[i]
if type(col) == "table" then -- colormap
triangle.outlineColor = col[triangle.c] or triangle.outlineColor
else
triangle.outlineColor = col
end
end
return model
end
---Change the colors of polygons in a Model
---@param model Model
---@param col number|table if number, will set the color for each Polygon, if table, uses it as a mapping from Polygon color to new the new color
---@return Model
function transforms.mapColor(model, col)
if not model or type(model) ~= "table" then
error("transforms.mapColor expected arg#1 to be a table (model)")
end
for i = 1, #model do
local triangle = model[i]
if type(col) == "table" then -- colormap
triangle.c = col[triangle.c] or triangle.c
else
triangle.c = col
end
end
return model
end
---Center the Model such that the origin is in the middle of the bounding box
---@param model Model
function transforms.center(model)
local minX, maxX = math.huge, -math.huge
local minY, maxY = math.huge, -math.huge
local minZ, maxZ = math.huge, -math.huge
for i = 1, #model do
local poly = model[i]
minX, maxX = min(minX, poly.x1), max(maxX, poly.x1)
minX, maxX = min(minX, poly.x2), max(maxX, poly.x2)
minX, maxX = min(minX, poly.x3), max(maxX, poly.x3)
minY, maxY = min(minY, poly.y1), max(maxY, poly.y1)
minY, maxY = min(minY, poly.y2), max(maxY, poly.y2)
minY, maxY = min(minY, poly.y3), max(maxY, poly.y3)
minZ, maxZ = min(minZ, poly.z1), max(maxZ, poly.z1)
minZ, maxZ = min(minZ, poly.z2), max(maxZ, poly.z2)
minZ, maxZ = min(minZ, poly.z3), max(maxZ, poly.z3)
end
local offsetX = -(maxX + minX) * 0.5
local offsetY = -(maxY + minY) * 0.5
local offsetZ = -(maxZ + minZ) * 0.5
for i = 1, #model do
local poly = model[i]
poly.x1 = poly.x1 + offsetX
poly.x2 = poly.x2 + offsetX
poly.x3 = poly.x3 + offsetX
poly.y1 = poly.y1 + offsetY
poly.y2 = poly.y2 + offsetY
poly.y3 = poly.y3 + offsetY
poly.z1 = poly.z1 + offsetZ
poly.z2 = poly.z2 + offsetZ
poly.z3 = poly.z3 + offsetZ
end
return model, offsetX, offsetY, offsetZ
end
---Rescales the model such that the largest value of any coordinate is equal to 1
---@param model Model
function transforms.normalizeScale(model)
local maxVal = -math.huge
for i = 1, #model do
local poly = model[i]
maxVal = max(maxVal, poly.x1)
maxVal = max(maxVal, poly.x2)
maxVal = max(maxVal, poly.x3)
maxVal = max(maxVal, poly.y1)
maxVal = max(maxVal, poly.y2)
maxVal = max(maxVal, poly.y3)
maxVal = max(maxVal, poly.z1)
maxVal = max(maxVal, poly.z2)
maxVal = max(maxVal, poly.z3)
end
for i = 1, #model do
local poly = model[i]
poly.x1 = poly.x1 / maxVal
poly.x2 = poly.x2 / maxVal
poly.x3 = poly.x3 / maxVal
poly.y1 = poly.y1 / maxVal
poly.y2 = poly.y2 / maxVal
poly.y3 = poly.y3 / maxVal
poly.z1 = poly.z1 / maxVal
poly.z2 = poly.z2 / maxVal
poly.z3 = poly.z3 / maxVal
end
return model
end
---Similar to normalizeScale, rescales the model, but only uses the y coordinate to determine how much it is scaled (normalizes height)
---@param model Model
function transforms.normalizeScaleY(model)
local maxVal = -math.huge
for i = 1, #model do
local poly = model[i]
maxVal = max(maxVal, poly.y1)
maxVal = max(maxVal, poly.y2)
maxVal = max(maxVal, poly.y3)
end
for i = 1, #model do
local poly = model[i]
poly.x1 = poly.x1 / maxVal
poly.x2 = poly.x2 / maxVal
poly.x3 = poly.x3 / maxVal
poly.y1 = poly.y1 / maxVal
poly.y2 = poly.y2 / maxVal
poly.y3 = poly.y3 / maxVal
poly.z1 = poly.z1 / maxVal
poly.z2 = poly.z2 / maxVal
poly.z3 = poly.z3 / maxVal
end
return model
end
---Scales the model
---@param model Model
---@param scale number
function transforms.scale(model, scale)
for i = 1, #model do
local poly = model[i]
poly.x1 = poly.x1 * scale
poly.x2 = poly.x2 * scale
poly.x3 = poly.x3 * scale
poly.y1 = poly.y1 * scale
poly.y2 = poly.y2 * scale
poly.y3 = poly.y3 * scale
poly.z1 = poly.z1 * scale
poly.z2 = poly.z2 * scale
poly.z3 = poly.z3 * scale
end
return model
end
---Translates the model
---@param model Model
---@param dx number?
---@param dy number?
---@param dz number?
function transforms.translate(model, dx, dy, dz)
for i = 1, #model do
local poly = model[i]
poly.x1 = poly.x1 + (dx or 0)
poly.x2 = poly.x2 + (dx or 0)
poly.x3 = poly.x3 + (dx or 0)
poly.y1 = poly.y1 + (dy or 0)
poly.y2 = poly.y2 + (dy or 0)
poly.y3 = poly.y3 + (dy or 0)
poly.z1 = poly.z1 + (dz or 0)
poly.z2 = poly.z2 + (dz or 0)
poly.z3 = poly.z3 + (dz or 0)
end
return model
end
---Rotates a given Model around three axes
---@param model Model
---@param rotX number? in radians
---@param rotY number? in radians
---@param rotZ number? in radians
---@return Model
function transforms.rotate(model, rotX, rotY, rotZ)
local rotXS, rotXC = 0, 1
local rotYS, rotYC = 0, 1
local rotZS, rotZC = 0, 1
if rotX == 0 then rotX = nil end
if rotX then rotXS, rotXC = sin(rotX), cos(rotX) end
if rotY == 0 then rotY = nil end
if rotY then rotYS, rotYC = sin(rotY), cos(rotY) end
if rotZ == 0 then rotZ = nil end
if rotZ then rotZS, rotZC = sin(rotZ), cos(rotZ) end
for i = 1, #model do
local polygon = model[i]
local x1, y1, z1 = polygon.x1, polygon.y1, polygon.z1
local x2, y2, z2 = polygon.x2, polygon.y2, polygon.z2
local x3, y3, z3 = polygon.x3, polygon.y3, polygon.z3
if rotY then
x1, y1, z1 = rotatePolygonY(x1, y1, z1, rotYS, rotYC)
x2, y2, z2 = rotatePolygonY(x2, y2, z2, rotYS, rotYC)
x3, y3, z3 = rotatePolygonY(x3, y3, z3, rotYS, rotYC)
end
if rotZ then
x1, y1 = rotatePolygonZ(x1, y1, z1, rotZS, rotZC)
x2, y2 = rotatePolygonZ(x2, y2, z2, rotZS, rotZC)
x3, y3 = rotatePolygonZ(x3, y3, z3, rotZS, rotZC)
end
if rotX then
x1, y1, z1 = rotatePolygonX(x1, y1, z1, rotXS, rotXC)
x2, y2, z2 = rotatePolygonX(x2, y2, z2, rotXS, rotXC)
x3, y3, z3 = rotatePolygonX(x3, y3, z3, rotXS, rotXC)
end
polygon.x1, polygon.y1, polygon.z1 = x1, y1, z1
polygon.x2, polygon.y2, polygon.z2 = x2, y2, z2
polygon.x3, polygon.y3, polygon.z3 = x3, y3, z3
end
return model
end
---Translates the model such that the bottom aligns with y = 0
---@param model Model
function transforms.alignBottom(model)
local minY = math.huge
for i = 1, #model do
local poly = model[i]
minY = min(minY, poly.y1)
minY = min(minY, poly.y2)
minY = min(minY, poly.y3)
end
for i = 1, #model do
local poly = model[i]
poly.y1 = poly.y1 - minY
poly.y2 = poly.y2 - minY
poly.y3 = poly.y3 - minY
end
return model
end
---Triangle decimation (reduce the quality / number of polygons in a model), default mode "ratio"
---@param model Model
---@param quality number
---@param mode? "ratio"|"polys"
---@return Model
function transforms.decimate(model, quality, mode)
-- convert model format
local vertices = {}
local triangles = {}
local function findVertex(x, y, z)
for i = #vertices, 1, -1 do
local vertex = vertices[i]
if vertex[1] == x and vertex[2] == y and vertex[3] == z then
return i
end
end
end
for i = 1, #model do
local poly = model[i]
local i1 = findVertex(poly.x1, poly.y1, poly.z1)
if not i1 then
vertices[#vertices + 1] = {poly.x1, poly.y1, poly.z1}
i1 = #vertices
end
local i2 = findVertex(poly.x2, poly.y2, poly.z2)
if not i2 then
vertices[#vertices + 1] = {poly.x2, poly.y2, poly.z2}
i2 = #vertices
end
local i3 = findVertex(poly.x3, poly.y3, poly.z3)
if not i3 then
vertices[#vertices + 1] = {poly.x3, poly.y3, poly.z3}
i3 = #vertices
end
local triangle = {i1, i2, i3, poly.c, poly.forceRender}
triangles[#triangles + 1] = triangle
end
-- actually decimate
local function getDistance(v1, v2)
local dx = v1[1] - v2[1]
local dy = v1[2] - v2[2]
local dz = v1[3] - v2[3]
return (dx * dx + dy * dy + dz * dz) ^ 0.5
end
---@class EdgeCandidate
---@field length number
---@field vA number vertex index
---@field vB number vertex index
---@type EdgeCandidate[]
local candidateEdges = {}
for i = 1, #triangles do
local triangle = triangles[i]
local edge1Length = getDistance(
vertices[triangle[1]],
vertices[triangle[2]]
)
local edge2Length = getDistance(
vertices[triangle[2]],
vertices[triangle[3]]
)
local edge3Length = getDistance(
vertices[triangle[1]],
vertices[triangle[3]]
)
candidateEdges[#candidateEdges + 1] = {
length = edge1Length,
vA = triangle[1],
vB = triangle[2],
}
candidateEdges[#candidateEdges + 1] = {
length = edge2Length,
vA = triangle[2],
vB = triangle[3],
}
candidateEdges[#candidateEdges + 1] = {
length = edge3Length,
vA = triangle[1],
vB = triangle[3],
}
end
local function collapseEdge(vA, vB)
local vertex1 = vertices[vA]
local vertex2 = vertices[vB]
local vertexNew = { -- TODO: Maybe don't use average position, but offset in a smart way?
(vertex1[1] + vertex2[1]) * 0.5,
(vertex1[2] + vertex2[2]) * 0.5,
(vertex1[3] + vertex2[3]) * 0.5,
}
vertices[#vertices + 1] = vertexNew
local vNew = #vertices
for i = #triangles, 1, -1 do
local tri = triangles[i]
if tri[1] == vA or tri[1] == vB or tri[2] == vA or tri[2] == vB or tri[3] == vA or tri[3] == vB then
-- triangle contains at least one of the vertices being collapsed
if tri[1] == vA or tri[1] == vB then
tri[1] = vNew
end
if tri[2] == vA or tri[2] == vB then
tri[2] = vNew
end
if tri[3] == vA or tri[3] == vB then
tri[3] = vNew
end
if tri[1] == tri[2] or tri[2] == tri[3] or tri[1] == tri[3] then
-- no longer valid triangle
table.remove(triangles, i)
end
end
end
return vNew
end
local maxTriangles = quality
if mode ~= "polys" then
maxTriangles = (#model) * quality
end
while #triangles > maxTriangles do
-- find smallest edge
local smallestLength = math.huge
local smallestIndex
for i = 1, #candidateEdges do
local candidate = candidateEdges[i]
if candidate.length < smallestLength then
smallestLength = candidate.length
smallestIndex = i
end
end
local smallestCandidate = candidateEdges[smallestIndex]
-- collapse smallest edge
local vNew = collapseEdge(smallestCandidate.vA, smallestCandidate.vB)
-- remove now invalid candidate edges