This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
format_GC.py
1511 lines (1265 loc) · 44.7 KB
/
format_GC.py
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
import bpy
import math
import mathutils
from typing import List, Dict, Tuple
import copy
from . import fileHelper, enums, strippifier, common
from .common import Vector3, ColorARGB, UV, BoundingBox
from .prop.properties import SAMaterialSettings
DO = False
def debug(*string):
global DO
if DO:
print(*string)
# == Geometry parameters ==
class Parameter:
pType: enums.ParameterType
data: int
def __init__(self, pType: enums.ParameterType):
self.pType = pType
self.data = 0
def write(self, fileW: fileHelper.FileWriter):
fileW.wUInt(self.pType.value)
fileW.wUInt(self.data)
@classmethod
def read(cls, fileR: fileHelper.FileReader, address: int):
pType = enums.ParameterType(fileR.rUInt(address))
data = fileR.rUInt(address+4)
if pType == enums.ParameterType.VtxAttrFmt:
param = VtxAttrFmt(enums.VertexAttribute.Null)
elif pType == enums.ParameterType.IndexAttributeFlags:
param = IndexAttributes(enums.IndexAttributeFlags.HasPosition)
elif pType == enums.ParameterType.Lighting:
param = Lighting(1)
elif pType == enums.ParameterType.BlendAlpha:
param = AlphaBlend(enums.AlphaInstruction.SrcAlpha, enums.AlphaInstruction.InverseSrcAlpha, False)
elif pType == enums.ParameterType.AmbientColor:
param = AmbientColor(ColorARGB())
elif pType == enums.ParameterType.Texture:
param = Texture(0, enums.TileMode.null)
elif pType == enums.ParameterType.Unknown_9:
param = unknown_9()
elif pType == enums.ParameterType.TexCoordGen:
param = TexCoordGen(enums.TexGenMtx.Identity, enums.TexGenSrc.TexCoord0, enums.TexGenType.Matrix2x4, enums.TexCoordID.TexCoord0)
else:
print("type not found?")
param.data = data
#print(param.data)
#print(param.pType)
return param
class VtxAttrFmt(Parameter):
"""We dont know what this does but we know that we
need one for each vertex data set"""
def __init__(self,
vtxType: enums.VertexAttribute):
super(VtxAttrFmt, self).__init__(enums.ParameterType.VtxAttrFmt)
self.vtxType = vtxType
if vtxType == enums.VertexAttribute.Position:
self.unknown = 5120
elif vtxType == enums.VertexAttribute.Normal:
self.unknown = 9216
elif vtxType == enums.VertexAttribute.Color0:
self.unknown = 27136
elif vtxType == enums.VertexAttribute.Tex0:
self.unknown = 33544
@property
def unknown(self) -> int:
return self.data & 0xFFFF
@unknown.setter
def unknown(self, val: int):
self.data &= ~0xFFFF
self.data |= min(0xFFFF, val)
@property
def vtxType(self) -> enums.VertexAttribute:
return enums.VertexAttribute((self.data >> 16) & 0xFFFF)
@vtxType.setter
def vtxType(self, val: enums.VertexAttribute):
self.data &= ~0xFFFF0000
self.data |= val.value << 16
class IndexAttributes(Parameter):
"""Which data the polygon corners hold"""
def __init__(self,
idAttr: enums.IndexAttributeFlags):
super(IndexAttributes, self).__init__(enums.ParameterType.IndexAttributeFlags)
self.indexAttributes = idAttr
@property
def indexAttributes(self) -> enums.IndexAttributeFlags:
return enums.IndexAttributeFlags(self.data)
@indexAttributes.setter
def indexAttributes(self, val: enums.IndexAttributeFlags):
self.data = val.value
class Lighting(Parameter):
"""Holds lighting data for the mesh"""
def __init__(self,
shadowStencil: int):
super(Lighting, self).__init__(enums.ParameterType.Lighting)
# everything except shadow stencil are default values
self.lightingFlags = 0x0B11
self.shadowStencil = shadowStencil
self.unknown1 = 0
self.unknown2 = 0
@property
def lightingFlags(self) -> int:
return self.data & 0xFFFF
@lightingFlags.setter
def lightingFlags(self, val: int):
self.data &= (~0xFFFF)
self.data |= min(0xFFFF, val)
@property
def shadowStencil(self) -> int:
return (self.data >> 16) & 0xF
@shadowStencil.setter
def shadowStencil(self, val: int):
self.data &= (~0xF0000)
self.data |= min(0xF, val) << 16
@property
def unknown1(self) -> int:
return (self.data >> 20) & 0xF
@unknown1.setter
def unknown1(self, val: int):
self.data &= (~0xF00000)
self.data |= min(0xF, val) << 20
@property
def unknown2(self) -> int:
return (self.data >> 24) & 0xFF
@unknown2.setter
def unknown2(self, val: int):
self.data &= (~0xFF000000)
self.data |= min(0xFF, val) << 24
class AlphaBlend(Parameter):
"""How the alpha is rendered on top of the opaque geometry"""
def __init__(self,
src: enums.AlphaInstruction,
dst: enums.AlphaInstruction,
active: bool):
super(AlphaBlend, self).__init__(enums.ParameterType.BlendAlpha)
self.src = src
self.dst = dst
self.active = active
@property
def dst(self) -> enums.AlphaInstruction:
return enums.AlphaInstruction((self.data >> 8) & 0x7)
@dst.setter
def dst(self, val: enums.AlphaInstruction):
self.data &= ~0x700
self.data |= val.value << 8
@property
def src(self) -> enums.AlphaInstruction:
return enums.AlphaInstruction((self.data >> 11) & 0x7)
@src.setter
def src(self, val: enums.AlphaInstruction):
self.data &= ~0x3800
self.data |= val.value << 11
@property
def active(self) -> bool:
return (self.data & 0x4000) > 1
@active.setter
def active(self, val: bool):
if val:
self.data |= 0x4000
else:
self.data &= ~0x4000
class AmbientColor(Parameter):
"""Ambient color of the mesh"""
def __init__(self, color: ColorARGB):
super(AmbientColor, self).__init__(enums.ParameterType.AmbientColor)
self.color = color
@property
def color(self) -> ColorARGB:
a = self.data & 0xFF
r = (self.data >> 8) & 0xFF
g = (self.data >> 16) & 0xFF
b = (self.data >> 24) & 0xFF
return ColorARGB((a,r,g,b))
@color.setter
def color(self, val: ColorARGB):
self.data = min(val.a, 0xFF) | min(val.b, 0xFF) << 8 | min(val.g, 0xFF) << 16 | min(val.r, 0xFF) << 24
class Texture(Parameter):
"""Holds texture info"""
def __init__(self, texID: int, tilemode: enums.TileMode):
super(Texture, self).__init__(enums.ParameterType.Texture)
self.texID = texID
self.tilemode = tilemode
@property
def texID(self) -> int:
return self.data & 0xFFFF
@texID.setter
def texID(self, val: int):
self.data &= ~0xFFFF
self.data |= min(0xFFFF, val)
@property
def tilemode(self) -> enums.TileMode:
return enums.TileMode((self.data >> 16) & 0xFFFF)
@tilemode.setter
def tilemode(self, val: enums.TileMode):
self.data &= ~0xFFFF0000
self.data |= val.value << 16
class unknown_9(Parameter):
"""We have absolutely no clue what this is for, but we need it"""
def __init__(self):
super(unknown_9, self).__init__(enums.ParameterType.Unknown_9)
self.unknown1 = 4
self.unknown2 = 0
@property
def unknown1(self) -> int:
return self.data & 0xFFFF
@unknown1.setter
def unknown1(self, val: int):
self.data &= ~0xFFFF
self.data |= min(0xFFFF, val)
@property
def unknown2(self) -> int:
return (self.data >> 16) & 0xFFFF
@unknown2.setter
def unknown2(self, val: int):
self.data &= ~0xFFFF0000
self.data |= min(0xFFFF, val) << 16
class TexCoordGen(Parameter):
"""Determines how the uv data should be used"""
def __init__(self,
mtx: enums.TexGenMtx,
src: enums.TexGenSrc,
typ: enums.TexGenType,
texID: enums.TexCoordID):
super(TexCoordGen, self).__init__(enums.ParameterType.TexCoordGen)
self.mtx = mtx
self.src = src
self.typ = typ
self.texID = texID
@property
def mtx(self) -> enums.TexGenMtx:
return enums.TexGenMtx(self.data & 0xF)
@mtx.setter
def mtx(self, val: enums.TexGenMtx):
self.data &= ~0xF
self.data |= val.value
@property
def src(self) -> enums.TexGenSrc:
return enums.TexGenSrc((self.data >> 4) & 0xFF)
@src.setter
def src(self, val: enums.TexGenSrc):
self.data &= ~0xFF0
self.data |= val.value << 4
@property
def typ(self) -> enums.TexGenType:
return enums.TexGenType((self.data >> 12) & 0xF)
@typ.setter
def typ(self, val: enums.TexGenType):
self.data &= ~0xF000
self.data |= val.value << 12
@property
def texID(self) -> enums.TexCoordID:
return enums.TexCoordID((self.data >> 16) & 0xFF)
@texID.setter
def texID(self, val: enums.TexCoordID):
self.data &= ~0xFF0000
self.data |= val.value << 16
class PolyVert:
"""Indices of a single polygon corner"""
posID = 0
nrmID = 0
vcID = 0
uvID = 0
def __init__(self, posID, nrmID, vcID, uvID):
self.posID = posID
self.nrmID = nrmID
self.vcID = vcID
self.uvID = uvID
def __eq__(self, other):
return self.posID == other.posID and self.nrmID == other.nrmID and self.vcID == other.vcID and self.uvID == other.uvID
def __str__(self):
return "(" + str(self.posID).zfill(3) + ", " + str(self.nrmID).zfill(3) + ", " + str(self.vcID).zfill(3) + ", " + str(self.uvID).zfill(3) + ")"
class Geometry:
"""Holds a single polygon data set"""
params: List[Parameter]
paramPtr: int
polygons: List[List[PolyVert]] # each list in the list is a polygon. lists with 3 items are triangles, more items and its a strip
polygonPtr: int
polygonSize: int
indexAttributes: enums.IndexAttributeFlags
transparent: bool
def __init__(self,
params: List[Parameter],
polygons: List[List[PolyVert]]):
self.params = params
self.polygons = polygons
self.indexAttributes = None
self.transparent = False
for p in params:
if isinstance(p, IndexAttributes):
self.indexAttributes = p.indexAttributes
if isinstance(p, AlphaBlend):
self.transparent = p.active
if self.indexAttributes is None:
print("Index attributes not found")
def writeParams(self, fileW: fileHelper.FileWriter):
"""Writes the parameters of the geometry"""
self.paramPtr = fileW.tell()
for p in self.params:
p.write(fileW)
def writePolygons(self, fileW: fileHelper.FileWriter):
"""Writes the polygon data of the geometry"""
self.polygonPtr = fileW.tell()
fileW.setBigEndian(True)
toWrite = list()
triangleList = list()
for l in self.polygons:
if len(l) == 3:
triangleList.extend(l)
else:
toWrite.append(l)
if len(triangleList) > 0:
toWrite.append(triangleList)
for l in toWrite:
if l is triangleList:
fileW.wByte(enums.PrimitiveType.Triangles.value)
else:
fileW.wByte(enums.PrimitiveType.TriangleStrip.value)
fileW.wUShort(len(l))
for p in l:
if self.indexAttributes & enums.IndexAttributeFlags.Position16BitIndex:
fileW.wUShort(p.posID)
else:
fileW.wByte(p.posID)
if self.indexAttributes & enums.IndexAttributeFlags.HasNormal:
if self.indexAttributes & enums.IndexAttributeFlags.Normal16BitIndex:
fileW.wUShort(p.nrmID)
else:
fileW.wByte(p.nrmID)
if self.indexAttributes & enums.IndexAttributeFlags.HasColor:
if self.indexAttributes & enums.IndexAttributeFlags.Color16BitIndex:
fileW.wUShort(p.vcID)
else:
fileW.wByte(p.vcID)
if self.indexAttributes & enums.IndexAttributeFlags.HasUV:
if self.indexAttributes & enums.IndexAttributeFlags.UV16BitIndex:
fileW.wUShort(p.uvID)
else:
fileW.wByte(p.uvID)
fileW.setBigEndian(False)
self.polygonSize = fileW.tell() - self.polygonPtr
def writeGeom(self, fileW: fileHelper.FileWriter):
"""Writes geometry data (requires params and polygons to be written)"""
fileW.wUInt(self.paramPtr)
fileW.wUInt(len(self.params))
fileW.wUInt(self.polygonPtr)
fileW.wUInt(self.polygonSize)
@classmethod
def read(cls, fileR: fileHelper.FileReader, address: int, paramDict: dict):
paramPtr = fileR.rUInt(address)
paramCount = fileR.rUInt(address + 4)
polyPtr = fileR.rUInt(address + 8)
polySize = fileR.rUInt(address + 12)
if DO:
print(" Param Count:", paramCount)
print(" Poly Size:", polySize, "\n")
for i in range(paramCount):
param = Parameter.read(fileR, paramPtr)
if param.pType == enums.ParameterType.VtxAttrFmt:
paramDict[param.vtxType] = param
else:
paramDict[param.pType] = param
paramPtr += 8
idAttr = enums.IndexAttributeFlags.null
params = list()
for p in paramDict.values():
params.append(copy.deepcopy(p))
if enums.ParameterType.IndexAttributeFlags in paramDict:
idAttr = paramDict[enums.ParameterType.IndexAttributeFlags].indexAttributes
else:
print("no index attributes found")
#reading polygons
tmpAddr = polyPtr
polygons: List[List[PolyVert]] = list()
fileR.setBigEndian(True)
while tmpAddr - polyPtr < polySize:
polyType = fileR.rByte(tmpAddr)
tmpAddr += 1
if polyType == 0:
break
polyType = enums.PrimitiveType(polyType)
vCount = fileR.rUShort(tmpAddr)
tmpAddr += 2
polys = list()
if vCount == 0:
break
for i in range(vCount):
if idAttr & enums.IndexAttributeFlags.Position16BitIndex:
pos = fileR.rUShort(tmpAddr)
tmpAddr += 2
else:
pos = fileR.rByte(tmpAddr)
tmpAddr += 1
nrm = 0
if idAttr & enums.IndexAttributeFlags.HasNormal:
if idAttr & enums.IndexAttributeFlags.Normal16BitIndex:
nrm = fileR.rUShort(tmpAddr)
tmpAddr += 2
else:
nrm = fileR.rByte(tmpAddr)
tmpAddr += 1
col = 0
if idAttr & enums.IndexAttributeFlags.HasColor:
if idAttr & enums.IndexAttributeFlags.Color16BitIndex:
col = fileR.rUShort(tmpAddr)
tmpAddr += 2
else:
col = fileR.rByte(tmpAddr)
tmpAddr += 1
uv = 0
if idAttr & enums.IndexAttributeFlags.HasUV:
if idAttr & enums.IndexAttributeFlags.UV16BitIndex:
uv = fileR.rUShort(tmpAddr)
tmpAddr += 2
else:
uv = fileR.rByte(tmpAddr)
tmpAddr += 1
polys.append(PolyVert(pos, nrm, col, uv))
if polyType == enums.PrimitiveType.Triangles and vCount > 3:
triCount = math.floor(vCount/3)
for i in range(triCount):
polygons.append([polys[i*3], polys[i*3 +1], polys[i*3 +2]])
else:
polygons.append(polys)
fileR.setBigEndian(False)
return Geometry(params, polygons)
class Vertices:
"""One vertex data array"""
vType: enums.VertexAttribute
fracBitCount: int
compCount: enums.ComponentCount
dataType: enums.DataType
dataPtr: int
data: list
def __init__(self,
vType: enums.VertexAttribute,
fracBitCount: int,
compCount: enums.ComponentCount,
dataType: enums.DataType,
data: list):
self.vType = vType
self.fracBitCount = fracBitCount
self.compCount = compCount
self.dataType = dataType
self.data = data
def getCompSize(self) -> int:
"""Calculates the size of a single element in bytes"""
return self.compCount.length * self.dataType.length
def debug(self):
print(" Attrib:", self.vType)
print(" fracBitCount:", self.fracBitCount)
print(" vCount:", len(self.data))
print(" Component Count:", self.compCount)
print(" Data Type:", self.dataType)
print(" Data Pointer:", common.hex4(self.dataPtr))
print(" Size:", len(self.data) * self.getCompSize())
print(" - - - - \n")
def writeData(self, fileW: fileHelper.FileWriter):
"""Writes the data and saves the pointer"""
self.dataPtr = fileW.tell()
if self.vType == enums.VertexAttribute.Color0 or self.vType == enums.VertexAttribute.Color1:
for e in self.data:
e.writeRGBA(fileW)
else:
for e in self.data:
e.write(fileW)
def writeAttrib(self, fileW: fileHelper.FileWriter):
"""Writes the attribute of the vertices (requires data to be written)"""
fileW.wByte(self.vType.value)
fileW.wByte(self.fracBitCount)
fileW.wShort(len(self.data))
datainfo = self.compCount.value | (self.dataType.value << 4)
fileW.wUInt(datainfo)
fileW.wUInt(self.dataPtr)
fileW.wUInt(len(self.data) * self.getCompSize())
@classmethod
def read(cls, fileR: fileHelper.FileReader, address: int):
vType = enums.VertexAttribute(fileR.rByte(address))
fracBitCount = fileR.rByte(address + 1)
vCount = fileR.rUShort(address +2)
dataComp = fileR.rByte(address+4)
compCount = enums.ComponentCount(dataComp & 0xF)
dataType = enums.DataType(dataComp >> 4)
dataPtr = fileR.rUInt(address + 8)
dataSize = fileR.rUInt(address + 12)
data = list()
vProps = Vertices(vType, fracBitCount, compCount, dataType, data)
compSize = vProps.getCompSize()
for i in range(vCount):
values = []
for c in range(compCount.length):
t = 0
if dataType == enums.DataType.Signed8:
t = fileR.rSByte(dataPtr)
elif dataType == enums.DataType.Unsigned8:
t = fileR.rByte(dataPtr)
elif dataType == enums.DataType.Signed16:
t = fileR.rShort(dataPtr)
elif dataType == enums.DataType.Unsigned16:
t = fileR.rUShort(dataPtr)
elif dataType == enums.DataType.Float32:
t = fileR.rFloat(dataPtr)
elif dataType == enums.DataType.RGB565:
t = fileR.rUShort(dataPtr)
elif dataType == enums.DataType.RGBA4:
t = fileR.rUShort(dataPtr)
t = ((t & 0xF) * 2) | ((((t & 0xF0) >> 4) * 2) << 8) | ((((t & 0xF00) >> 8) * 2) << 16) | ((((t & 0xF000) >> 12) * 2) << 24)
t = common.ColorARGB.fromRGBA(t)
elif dataType == enums.DataType.RGBA6:
t = fileR.rUInt(dataPtr)
t = round((t & 0x3F) * 1.5) | (round(((t & 0xFc0) >> 6) * 1.5) << 8) | (round(((t & 0x3F000) >> 12) * 1.5) << 16) | (round(((t & 0xFC0000) >> 18) * 1.5) << 24)
t = common.ColorARGB.fromRGBA(t)
elif dataType == enums.DataType.RGBX8 or dataType == enums.DataType.RGB8:
t = fileR.rUInt(dataPtr) | 0xFF000000
t = common.ColorARGB.fromRGBA(t)
elif dataType == enums.DataType.RGBA8:
t = fileR.rUInt(dataPtr)
t = common.ColorARGB.fromRGBA(t)
values.append(t)
dataPtr += dataType.length
if compCount == enums.ComponentCount.TexCoord_S or compCount == enums.ComponentCount.TexCoord_ST:
t = UV()
t.x = values[0]
if len(values) == 2:
t.y = values[1]
elif compCount == enums.ComponentCount.Position_XY or compCount == enums.ComponentCount.Position_XYZ or compCount == enums.ComponentCount.Normal_XYZ:
if len(values) == 2:
values.append(0)
t = Vector3(values)
data.append(t)
return Vertices(vType, fracBitCount, compCount, dataType, data)
class Attach:
"""Gamecube format attach"""
name: str
vertices: List[Vertices]
opaqueGeom: List[Geometry]
transparentGeom: List[Geometry]
bounds: BoundingBox
def __init__(self,
name: str,
vertices: List[Vertices],
opaqueGeom: List[Geometry],
transparentGeom: List[Geometry],
bounds: BoundingBox):
self.name = name
self.vertices = vertices
self.opaqueGeom = opaqueGeom
self.transparentGeom = transparentGeom
self.bounds = bounds
@classmethod
def fromMesh(cls, mesh: bpy.types.Mesh,
export_matrix: mathutils.Matrix,
materials: List[bpy.types.Material]):
# determining which data should be written
vertexType = mesh.saSettings.sa2ExportType
# the model has to either contain normals or vertex colors. it wont work with both or none of each
writeNRM = vertexType == 'NRM' or vertexType == 'NRMW' or (vertexType == 'VC' and len(mesh.vertex_colors) == 0)
writeVC = not writeNRM #vertexType == 'VC' and len(mesh.vertex_colors) > 0
writeUV = len(mesh.uv_layers) > 0
# aquiring the vertex data
vertices: List[Vertices] = list()
# position data is always required
posData = list()
posIDs = [0] * len(mesh.vertices)
for i, v in enumerate(mesh.vertices):
found = None
pos = Vector3(export_matrix @ v.co)
for j, p in enumerate(posData):
if p == pos:
found = j
break
if found is None:
posData.append(pos)
posIDs[i] = len(posData) - 1
else:
posIDs[i] = found
vertices.append( Vertices(enums.VertexAttribute.Position, 12, enums.ComponentCount.Position_XYZ, enums.DataType.Float32, posData))
# getting normal data
if writeNRM:
nrmData = list()
nrmIDs = [0] * len(mesh.vertices)
normals = common.getNormalData(mesh)
for i, v in enumerate(mesh.vertices):
found = None
nrm = Vector3(export_matrix @ normals[i])
for j, n in enumerate(nrmData):
if n == nrm:
found = j
break
if found is None:
nrmData.append(nrm)
nrmIDs[i] = len(nrmData) - 1
else:
nrmIDs[i] = found
vertices.append( Vertices(enums.VertexAttribute.Normal, 12, enums.ComponentCount.Normal_XYZ, enums.DataType.Float32, nrmData))
# getting vertex color data
if writeVC:
vcData = list()
vcIDs = [0] * len(mesh.vertex_colors[0].data)
for i, vc in enumerate(mesh.vertex_colors[0].data):
found = None
col = ColorARGB(vc.color)
for j, c in enumerate(vcData):
if col == c:
found = j
break
if found is None:
vcData.append(col)
vcIDs[i] = len(vcData) - 1
else:
vcIDs[i] = found
vertices.append( Vertices(enums.VertexAttribute.Color0, 4, enums.ComponentCount.Color_RGBA, enums.DataType.RGBA8, vcData))
# getting uv data
if writeUV:
uvData = list()
uvIDs = [0] * len(mesh.uv_layers[0].data)
for i, uv in enumerate(mesh.uv_layers[0].data):
found = None
newUV = UV(uv.uv)
for j, uvD in enumerate(uvData):
if newUV == uvD:
found = j
break
if found is None:
uvData.append(newUV)
uvIDs[i] = len(uvData) - 1
else:
uvIDs[i] = found
vertices.append( Vertices(enums.VertexAttribute.Tex0, 4, enums.ComponentCount.TexCoord_ST, enums.DataType.Signed16, uvData))
# assembling polygons
# preparing polygon lists
tris: List[List[PolyVert]] = [[] for n in range(len(mesh.materials))]
if len(tris) == 0:
tris.append([])
degTris = 0
for p in mesh.polygons:
triMat = tris[p.material_index]
for l in p.loop_indices:
loop = mesh.loops[l]
posID = posIDs[loop.vertex_index]
nrmID = None
vcID = None
uvID = None
if writeNRM:
nrmID = nrmIDs[loop.vertex_index]
if writeVC:
vcID = vcIDs[l]
if writeUV:
uvID = uvIDs[l]
triMat.append( PolyVert(posID, nrmID, vcID, uvID) )
# checking for degenerate triangle
if triMat[-1].posID == triMat[-2].posID or triMat[-2].posID == triMat[-3].posID or triMat[-1].posID == triMat[-3].posID:
tris[p.material_index] = tris[p.material_index][:-3]
degTris += 1
if degTris > 0:
debug("degTris:", degTris)
#strippifying the poly data
strips: List[List[List[PolyVert]]] = list() # material specific -> strip -> polygon
for l in tris:
if len(l) == 0:
strips.append(None)
continue
distinct = list()
IDs = [0] * len(l)
distinct, IDs = common.getDistinctwID(l)
stripIndices = strippifier.Strippify(IDs, doSwaps=False, concat=False, name = mesh.name)
polyStrips = [None] * len(stripIndices)
for i, strip in enumerate(stripIndices):
tStrip = [None] * len(strip)
for j, index in enumerate(strip):
tStrip[j] = distinct[index]
polyStrips[i] = tStrip
strips.append(polyStrips)
# generating geometry from the polygon strips
opaqueGeom = list()
transparentGeom = list()
for i, s in enumerate(strips):
if s is None:
continue
mat = None
if len(mesh.materials) > 0:
matName = mesh.materials[i].name
materials = mesh.materials
if matName in materials:
mat = materials[matName]
else:
print(" Material", matName, "not found")
# generating parameters
parameters = list()
# vtx attribute parameters come first
parameters.append(VtxAttrFmt(enums.VertexAttribute.Position))
idAttribs = enums.IndexAttributeFlags.HasPosition
if writeNRM:
parameters.append(VtxAttrFmt(enums.VertexAttribute.Normal))
idAttribs |= enums.IndexAttributeFlags.HasNormal
if writeVC:
parameters.append(VtxAttrFmt(enums.VertexAttribute.Color0))
idAttribs |= enums.IndexAttributeFlags.HasColor
if writeUV:
parameters.append(VtxAttrFmt(enums.VertexAttribute.Tex0))
idAttribs |= enums.IndexAttributeFlags.HasUV
# ID attributes
for l in s:
for p in l:
if p.posID > 0xFF:
idAttribs |= enums.IndexAttributeFlags.Position16BitIndex
if writeNRM and p.nrmID > 0xFF:
idAttribs |= enums.IndexAttributeFlags.Normal16BitIndex
if writeVC and p.vcID > 0xFF:
idAttribs |= enums.IndexAttributeFlags.Color16BitIndex
if writeUV and p.uvID > 0xFF:
idAttribs |= enums.IndexAttributeFlags.UV16BitIndex
parameters.append(IndexAttributes(idAttribs))
# material dependend things
if mat is None:
parameters.append(Lighting(1))
parameters.append(AlphaBlend(enums.AlphaInstruction.SrcAlpha, enums.AlphaInstruction.InverseSrcAlpha, False))
parameters.append(AmbientColor(ColorARGB((1,1,1,1))))
parameters.append(Texture(0, enums.TileMode.WrapU | enums.TileMode.WrapV))
parameters.append(unknown_9())
parameters.append(TexCoordGen(enums.TexGenMtx.Identity, enums.TexGenSrc.TexCoord0, enums.TexGenType.Matrix2x4, enums.TexCoordID.TexCoord0))
else:
matProps: SAMaterialSettings = mat.saSettings
parameters.append(Lighting(matProps.gc_shadowStencil))
srcInst = enums.AlphaInstruction.SrcAlpha
dstInst = enums.AlphaInstruction.InverseSrcAlpha
if matProps.b_useAlpha:
src = matProps.b_srcAlpha
if src == 'ONE':
srcInst = enums.AlphaInstruction.One
elif src == 'OTHER':
srcInst = enums.AlphaInstruction.SrcColor
elif src == 'INV_OTHER':
srcInst = enums.AlphaInstruction.InverseSrcColor
elif src == 'SRC':
srcInst = enums.AlphaInstruction.SrcAlpha
elif src == 'INV_SRC':
srcInst = enums.AlphaInstruction.InverseSrcAlpha
elif src == 'DST':
srcInst = enums.AlphaInstruction.DstAlpha
elif src == 'INV_DST':
srcInst = enums.AlphaInstruction.InverseDstAlpha
else:
srcInst = enums.AlphaInstruction.Zero
dst = matProps.b_destAlpha
if dst == 'ONE':
dstInst = enums.AlphaInstruction.One
elif dst == 'OTHER':
dstInst = enums.AlphaInstruction.SrcColor
elif dst == 'INV_OTHER':
dstInst = enums.AlphaInstruction.InverseSrcColor
elif dst == 'SRC':
dstInst = enums.AlphaInstruction.SrcAlpha
elif dst == 'INV_SRC':
dstInst = enums.AlphaInstruction.InverseSrcAlpha
elif dst == 'DST':
dstInst = enums.AlphaInstruction.DstAlpha
elif dst == 'INV_DST':
dstInst = enums.AlphaInstruction.InverseDstAlpha
else:
dstInst = enums.AlphaInstruction.Zero
saShader = None
saImage = None
if (mat.node_tree != None):
saShader = mat.node_tree.nodes.get('Group')
saImage = mat.node_tree.nodes.get('Image Texture')
parameters.append(AlphaBlend(srcInst, dstInst, matProps.b_useAlpha))
parameters.append(AmbientColor(common.GetColor(saShader, 2, matProps.b_Ambient)))
tileMode = enums.TileMode.null
if not matProps.b_clampV:
tileMode |= enums.TileMode.WrapV
if not matProps.b_clampU:
tileMode |= enums.TileMode.WrapU
if matProps.b_mirrorV:
tileMode |= enums.TileMode.MirrorV
if matProps.b_mirrorU:
tileMode |= enums.TileMode.MirrorU
parameters.append(Texture(common.FindTexture(saImage, matProps.b_TextureID), tileMode))
parameters.append(unknown_9())
#texMatrixID
if matProps.gc_texMatrixID == 'MATRIX0':
mtx = enums.TexGenMtx.Matrix0
elif matProps.gc_texMatrixID == 'MATRIX1':
mtx = enums.TexGenMtx.Matrix1
elif matProps.gc_texMatrixID == 'MATRIX2':
mtx = enums.TexGenMtx.Matrix2
elif matProps.gc_texMatrixID == 'MATRIX3':
mtx = enums.TexGenMtx.Matrix3
elif matProps.gc_texMatrixID == 'MATRIX4':
mtx = enums.TexGenMtx.Matrix4
elif matProps.gc_texMatrixID == 'MATRIX5':
mtx = enums.TexGenMtx.Matrix5
elif matProps.gc_texMatrixID == 'MATRIX6':
mtx = enums.TexGenMtx.Matrix6
elif matProps.gc_texMatrixID == 'MATRIX7':
mtx = enums.TexGenMtx.Matrix7
elif matProps.gc_texMatrixID == 'MATRIX8':
mtx = enums.TexGenMtx.Matrix8
elif matProps.gc_texMatrixID == 'MATRIX9':
mtx = enums.TexGenMtx.Matrix9
elif matProps.gc_texMatrixID == 'IDENTITY':
mtx = enums.TexGenMtx.Identity
#texGenSrc
if matProps.gc_texGenType[0] == 'M': #Matrix
if matProps.gc_texGenSourceMtx == 'POSITION':
src = enums.TexGenSrc.Position
elif matProps.gc_texGenSourceMtx == 'NORMAL':
src = enums.TexGenSrc.Normal
elif matProps.gc_texGenSourceMtx == 'BINORMAL':
src = enums.TexGenSrc.Binormal
elif matProps.gc_texGenSourceMtx == 'TANGENT':
src = enums.TexGenSrc.Tangent
elif matProps.gc_texGenSourceMtx == 'TEX0':
src = enums.TexGenSrc.Tex0
elif matProps.gc_texGenSourceMtx == 'TEX1':
src = enums.TexGenSrc.Tex1
elif matProps.gc_texGenSourceMtx == 'TEX2':
src = enums.TexGenSrc.Tex2
elif matProps.gc_texGenSourceMtx == 'TEX3':
src = enums.TexGenSrc.Tex3
elif matProps.gc_texGenSourceMtx == 'TEX4':
src = enums.TexGenSrc.Tex4
elif matProps.gc_texGenSourceMtx == 'TEX5':
src = enums.TexGenSrc.Tex5
elif matProps.gc_texGenSourceMtx == 'TEX6':
src = enums.TexGenSrc.Tex6
elif matProps.gc_texGenSourceMtx == 'TEX7':
src = enums.TexGenSrc.Tex7
elif matProps.gc_texGenType[0] == 'B': #Bump
if matProps.gc_texGenSourceBmp == 'TEXCOORD0':
src = enums.TexGenSrc.TexCoord0