-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCRImport_MT.ms
More file actions
1606 lines (1424 loc) · 50.5 KB
/
Copy pathCRImport_MT.ms
File metadata and controls
1606 lines (1424 loc) · 50.5 KB
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
fn readCNTheader f =
(
readbyte f #unsigned
readbyte f #unsigned
readbyte f #unsigned
readbyte f #unsigned
)
fn GetMDLTexture folder materialName importTextures:false =
(
texmap = undefined
--format "Checking if material \"%\" exists: result = % importTextures =\n" (folder+materialName+".MT2") (((dotnetclass "System.IO.File").exists (folder+materialName+".MT2")) as string) (importTextures as string)
global CarmaSettings
if importTextures and ((dotnetclass "System.IO.File").exists (folder+materialName+".MT2")) then
(
--format "Material \"%\" exists!\n" (folder+materialName+".MT2")
dotNet.loadAssembly "system.xml"
xmlDoc=dotNetObject "system.xml.xmlDocument"
--clearListener()
--format "Properties\n"
--showProperties xmlDoc
--format "\nMethods\n"
--showMethods xmlDoc
xmlDoc.load (folder+materialName+".MT2")
if (xmlDoc.HasChildNodes)==false then xmlDoc.load (folder+materialName+".mt2")
basedOffTag = (xmlDoc.GetElementsByTagName "BasedOffOf").item 0
basedOffOf = (basedOffTag.Attributes.GetNamedItem "Name").Value
textureTags = xmlDoc.GetElementsByTagName "Texture"
--clearListener()
--format "Properties\n"
--showProperties textureTags
--format "\nMethods\n"
--showMethods textureTags
textureName = undefined
--format "\n------------\n"
for i=1 to textureTags.count do
(
textureItem = textureTags.item (i-1)
textureAlias = (textureItem.Attributes.GetNamedItem "Alias").Value
--format "Texture Tag #%: Alias = \"%\" Filename = \"%\"\n" i textureAlias (textureItem.Attributes.GetNamedItem "FileName").Value
if textureAlias == "DiffuseColour" or textureAlias == "Side1_DiffuseColour" or textureAlias == "Side1_DiffuseColour2" or textureAlias == "Decals" then
(
textureName = (textureItem.Attributes.GetNamedItem "FileName").Value
--format "Found diffuse texture %!\n" textureName
)
)
if textureName == undefined then
(
texmap = checker()
texmap.coords.U_Tiling =10
texmap.coords.V_Tiling =10
return texmap
)
--format "\n------------\n"
if (doesFileExist (CarmaSettings.TexturePath+"\\"+textureName+".png")) then
(
--format "Texture is already converted: %\n" (CarmaSettings.TexturePath+"\\"+textureName+".png")
texmap = bitmaptexture()
texmap.filename = CarmaSettings.TexturePath+"\\"+textureName+".png"
)
else if (doesFileExist (CarmaSettings.TexturePath+"\\"+textureName+".tga")) then
(
--format "Texture is already converted: %\n" (CarmaSettings.TexturePath+"\\"+textureName+".tga")
texmap = bitmaptexture()
texmap.filename = CarmaSettings.TexturePath+"\\"+textureName+".tga"
)
else if (doesFileExist (folder+textureName+".png")) then
(
--format "Texture is already converted: %\n" (folder+textureName+".png")
texmap = bitmaptexture()
texmap.filename = folder+textureName+".png"
)
else if (doesFileExist (folder+textureName+".tga")) then
(
--format "Texture is already converted: %\n" (folder+textureName+".tga")
texmap = bitmaptexture()
texmap.filename = folder+textureName+".tga"
)
else if (doesFileExist (CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\"+texturename+".png")) then
(
--format "Texture is already converted: %\n" (CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\"+texturename+".png")
texmap = bitmaptexture()
texmap.filename = CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\"+texturename+".png"
)
else if (doesFileExist (CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\"+texturename+".tga")) then
(
--format "Texture is already converted: %\n" (CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\"+texturename+".tga")
texmap = bitmaptexture()
texmap.filename = CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\"+texturename+".tga"
)
else if (doesFileExist (CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\NON_VT\\"+texturename+".png")) then
(
--format "Texture is already converted: %\n" (CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\"+texturename+".png")
texmap = bitmaptexture()
texmap.filename = CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\NON_VT\\"+texturename+".png"
)
else if (doesFileExist (CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\NON_VT\\"+texturename+".tga")) then
(
--format "Texture is already converted: %\n" (CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\"+texturename+".tga")
texmap = bitmaptexture()
texmap.filename = CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\NON_VT\\"+texturename+".tga"
)
else
(
--format "Texture needs convertng!\n"
startPath = CarmaSettings.TexturePath
if startPath == "" then startPath = folder
TDXFilename = undefined
if (doesFileExist (folder+texturename+".tdx")) then TDXFilename = folder+texturename+".tdx"
else if (doesFileExist (CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\"+texturename+".tdx")) then TDXFilename = CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\"+texturename+".tdx"
else if (doesFileExist (CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\NON_VT\\"+texturename+".tdx")) then TDXFilename = CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\NON_VT\\"+texturename+".tdx"
--format "Looking for %.tdx in \"%\" or in \"%\"\n" texturename folder (CarmaSettings.GamePath+"\\Data_Core\\Content\\Textures\\")
--format "TDXFilename: %\n" TDXFilename
if TDXFilename != undefined do
(
--HiddenDOSCommand (CarmaSettings.GibbedTools+" \""+TDXFilename+"\"") prompt:("Loading Texture: "+TDXFilename)
--format "Command For Converting Texture: %\n from start path: %" (CarmaSettings.GibbedTools+" \""+TDXFilename+"\"") startPath
--if CarmaSettings.TexturePath != "" then (dotnetClass "Gibbed.Stainless.TDXConvertLib.ConvertTDX").Convert TDXFilename CarmaSettings.TexturePath
ConvertTDX = dotnetClass "Gibbed.Stainless.TDXConvertLib.ConvertTDX"
ConvertTDX.Convert TDXFilename CarmaSettings.TexturePath false 5 CarmaSettings.MaxResolution
--format "TDX result is %\n" tdxresult
texmap = bitmaptexture()
texmap.filename = CarmaSettings.TexturePath+"\\"+textureName+".png"
)
)
--matFile = openfile folder+materialName+".MT2" "r"
)
if texmap==undefined do
(
--format "texmap undefined for \"%\", using checker!\n" materialName
texmap = checker()
texmap.coords.U_Tiling =10
texmap.coords.V_Tiling =10
)
return texmap
)
fn ImportMDL filename importTextures:false forcePREP:false useUniqueVerts:true useTriStrips:false mergeMDLs:false mergeSplits: true =
(
f = fopen filename "r"
magic1 = readbyte f #unsigned
magic2 = readbyte f #unsigned
mdlMinorVersion = readbyte f #unsigned
mdlMajorVersion = readbyte f #unsigned
version61orless=false
if mdlMinorVersion < 2 or mdlMajorVersion < 6 then version61orless=true
checksum = readlong f #unsigned
flags = readlong f #unsigned
--printFlagBits flags 32 "MDL Flags"
prepSize = readlong f #unsigned
firstFaceCount = readlong f #unsigned
uniqueVertexCount = readlong f #unsigned
fileSize = readlong f #unsigned
boundingSphereRadius = readfloat f
bboxMinX = readfloat f
bboxMinY = readfloat f
bboxMinZ = readfloat f
bboxMaxX = readfloat f
bboxMaxY = readfloat f
bboxMaxZ = readfloat f
bboxCenterX = readfloat f
bboxCenterY = readfloat f
bboxCenterZ = readfloat f
materialCount = readshort f #unsigned
meshMaterial = undefined
global globalMaterialNames
global globalMaterials
global merge_MatsIDsToNames
global merge_SubMat
if merge_SubMat == undefined then merge_SubMat = multisubmaterial()
if globalMaterialNames==undefined do globalMaterialNames = #()
if globalMaterials==undefined do globalMaterials = #()
materialNames = #()
merge_OldMatIDsToNew = #()
--format "=======================\nHandling Materials for mesh %\n=======================\n" filename
if(materialCount == 1) then
(
materialName = readpaddedstring f version61OrLess:version61orless
append materialNames materialName
--format "Checking if material #% \"%\" already has been loaded\n" i materialName
if(finditem globalMaterialNames materialName) > 0 then
(
--format "Material is in array at position %\n" (finditem globalMaterialNames materialName)
meshMaterial = globalMaterials[(finditem globalMaterialNames materialName)]
)
else
(
--format "Material is not already loaded\n"
)
if meshMaterial == undefined do
(
append globalMaterialNames materialName
meshMaterial = StandardMaterial()
meshMaterial.shaderType = 1
--format "\nAbout To Call GetMDLTexture for %\n" materialName
meshMaterial.diffuseMap = GetMDLTexture (getFilenamePath filename) materialName importTextures:importTextures
--format "Just Called GetMDLTexture for \n\n" materialName
meshMaterial.name = materialName
showtexturemap meshMaterial meshMaterial.diffuseMap true
append globalMaterials meshMaterial
)
if mergeMDLs == true then
(
matExistsInMerged = finditem merge_MatsIDsToNames materialName
if matExistsInMerged == 0 then
(
if merge_SubMat.material.count == 1 and merge_SubMat.material[1].name=="##DELETEME##" then
(
merge_SubMat.material[merge_SubMat.material.count] = globalMaterials[(finditem globalMaterialNames materialName)]
)
else
(
merge_SubMat.material[merge_SubMat.material.count+1] = globalMaterials[(finditem globalMaterialNames materialName)]
)
meshMaterial = merge_SubMat
merge_MatsIDsToNames[merge_SubMat.material.count] = materialName
merge_OldMatIDsToNew[1] = merge_SubMat.material.count
)
else
(
merge_OldMatIDsToNew[1] = matExistsInMerged
)
)
)
else
(
meshMaterial = multimaterial numsubs:materialCount
for i=1 to materialCount do
(
materialName = readpaddedstring f version61OrLess:version61orless
append materialNames materialName
--format "Checking if material #% \"%\" already has been loaded\n" i materialName
subMat = undefined
if(finditem globalMaterialNames materialName) != 0 then
(
--format "Material is in array at position %\n" (finditem globalMaterialNames materialName)
subMat = globalMaterials[(finditem globalMaterialNames materialName)]
)
else
(
--format "Material is not already loaded\n"
)
if subMat == undefined do
(
--format "Trying to load the material and texture!\n"
append globalMaterialNames materialName
subMat = StandardMaterial()
subMat.shaderType = 1
--format "\nAbout To Call GetMDLTexture for %\n" materialName
subMat.diffuseMap = GetMDLTexture (getFilenamePath filename) materialName importTextures:importTextures
--format "Just Called GetMDLTexture for \n\n" materialName
subMat.name = materialName
showtexturemap subMat subMat.diffuseMap true
append globalMaterials subMat
)
meshMaterial[i] = subMat
if mergeMDLs == true then
(
matExistsInMerged = finditem merge_MatsIDsToNames materialName
if matExistsInMerged == 0 then
(
if merge_SubMat.material.count == 1 and merge_SubMat.material[1].name=="##DELETEME##" then
(
merge_SubMat.material[merge_SubMat.material.count] = subMat
)
else
(
merge_SubMat.material[merge_SubMat.material.count+1] = subMat
)
merge_MatsIDsToNames[merge_SubMat.material.count] = materialName
merge_OldMatIDsToNew[i] = merge_SubMat.material.count
)
else
(
merge_OldMatIDsToNew[i] = matExistsInMerged
)
)
)
if mergeMDLs == true then
(
meshMaterial = merge_SubMat
)
)
--format "======== merge_SubMat.material after======\n"
--PrintArrayInFull merge_SubMat.material "merge_SubMat.material"
--PrintArrayInFull merge_OldMatIDsToNew "merge_OldMatIDsToNew"
--format "====================================\n"
if forcePREP then
(
PREPfaceCount = readlong f #unsigned
rawfaces = #()
for i=1 to PREPfaceCount do
(
matID = readshort f #unsigned
faceflags = readshort f #unsigned
--printFlagBits faceflags 32 "Face Flags"
v1 = readlong f #unsigned
v2 = readlong f #unsigned
v3 = readlong f #unsigned
append rawfaces (mdlFace matID:matID flags:faceflags v1:(v1+1) v2:(v2+1) v3:(v3+1))
)
PREPvertcount = readlong f #unsigned
--format "VertCount: %\n" PREPvertcount
verts = #()
uniqueVerts = #()
uniqueVertsColours = #()
oldVertsToNewVerts = #()
for i=1 to PREPvertCount do
(
pX = readfloat f
pY = readfloat f
pZ = readfloat f
nX = readfloat f
nY = readfloat f
nZ = readfloat f
uv1X = readfloat f
uv1Y = readfloat f
uv2X = readfloat f
uv2Y = readfloat f
colR = readbyte f #unsigned
colG = readbyte f #unsigned
colB = readbyte f #unsigned
colA = readbyte f #unsigned
--format "vert #% colour: % % % %\n" i colR colG colB colA
--format "R:% G:% B:% A:%\n" colR colG colB colA
append verts (mdlVert pos:[-pX,-pZ,pY] norm:[nX,-nZ,nY] tex:[uv1X,1-uv1Y] tex2:[uv2X,1-uv2Y] colour:(color colR colG colB colA))
isUnique = true
if mergeSplits == true then
(
for j=1 to uniqueVerts.count do
(
if verts[i].pos == uniqueVerts[j].pos then -- and verts[i].norm == uniqueVerts[j].norm then
(
isUnique = false
oldVertsToNewVerts[i] = j
)
)
)
if isUnique then
(
append uniqueVerts verts[i]
append uniqueVertsColours colA
oldVertsToNewVerts[i] = uniqueVerts.count
)
)
--format "Unique Verts:%\n" uniqueVerts.count
matGroups = #()
triStripIndices = #()
matGroupCount = readshort f #unsigned
totalMatGroupTriangles=0
for i = 1 to matGroupCount do
(
--format "Importing Mat Group %\n" i
if useTriStrips then
(
matGroup = MDLMatGroup matID:i triStrip:#() patchList:#() numTriangles:0
bb_cX = readfloat f
bb_cY = readfloat f
bb_cZ = readfloat f
bb_Rad = readfloat f
bb_minX = readfloat f
bb_minY = readfloat f
bb_minZ = readfloat f
bb_maxX = readfloat f
bb_maxY = readfloat f
bb_maxZ = readfloat f
matGroup.triStripVertOffset = readlong f #unsigned
triStripVertCount = readlong f #unsigned
triStripLength = readlong f #unsigned
format "Tristrip Vert Count: %\n Tristrip Length: % \n" triStripVertCount triStripLength
degenerateBit = bit.shift 1 31
antiDegenerateBit = bit.not degenerateBit
tsi1 = undefined
tsi2 = undefined
tsi3 = undefined
for j=1 to triStripLength do
(
tsi = (readlong f #unsigned)
isDegenerate=false
if (bit.and degenerateBit tsi)==degenerateBit do isDegenerate = true
append matGroup.triStrip #((bit.and antiDegenerateBit tsi), isDegenerate)
tsi1= tsi2
tsi2 = tsi3
tsi3 = (bit.and antiDegenerateBit tsi) + matGroup.triStripVertOffset
if j > 2 then
(
matGroup.numTriangles +=1
--tsi1 = bit.and antiDegenerateBit matGroup.triStrip[matGroup.triStrip.count-2]
--tsi2 = bit.and antiDegenerateBit matGroup.triStrip[matGroup.triStrip.count-1]
--tsi3 = bit.and antiDegenerateBit matGroup.triStrip[matGroup.triStrip.count]
if (bit.and degenerateBit matGroup.triStrip[matGroup.triStrip.count][1]) != degenerateBit then
(
if (bit.and j 1)==1 then
(
append triStripIndices (tsi1+1)
append triStripIndices (tsi2+1)
append triStripIndices (tsi3+1)
)
else
(
append triStripIndices (tsi1+1)
append triStripIndices (tsi3+1)
append triStripIndices (tsi2+1)
)
)
)
)
--format "Number Of TriStip Indices: %\n" matGroup.triStrip.count
matGroup.patchListVertOffset = readlong f #usigned
patchListVertCount = readlong f #unsigned
patchListLength = readlong f #unsigned
--format "Patch Vert Count: %\n Patch Length: % \n" patchListVertCount patchListLength
for j=1 to patchListLength do
(
tsi1 = (readlong f #unsigned)
append matGroup.patchList tsi1
append triStripIndices (tsi1+1+matGroup.patchListVertOffset)
)
matGroup.numTriangles += patchListLength/3
--format "Number Of Patch Indices: %\n" matGroup.patchList.count
append matGroups matGroup
totalMatGroupTriangles += matGroup.numTriangles
--PrintArrayInFull matGroup.patchList "Patch List"
)
else
(
fseek f 48 #seek_cur
triStripLength = readlong f #unsigned
fseek f ((triStripLength * 4)+8) #seek_cur
patchListLength = readlong f #unsigned
fseek f (patchListLength * 4) #seek_cur
)
)
prepSkinDataSet = CheckBitIsSet flags 5
local m = trimesh()
if useUniqueVerts then setNumVerts m uniqueVerts.count
else setNumVerts m verts.count
setNumTVerts m verts.count
--meshop.setNumMaps m 3
--meshop.setMapSupport m 2 true
--meshop.setNumMapVerts m 2 verts.count
setNumCPVVerts m verts.count
if useUniqueVerts then
(
for i=1 to uniqueVerts.count do
(
setVert m i uniqueVerts[i].pos
meshop.setVertAlpha m -2 #(i) (uniquevertscolours[i]/255)
--setNormal m i uniqueVerts[i].norm
)
)
for i=1 to verts.count do
(
if useUniqueVerts == false then
(
setVert m i verts[i].pos
meshop.setVertAlpha m -2 #(i) (verts[i].colour.a/255)
)
setTVert m i verts[i].tex.x verts[i].tex.y 0
--meshop.setMapVert m 2 i [verts[i].tex2.x, verts[i].tex2.y, 0]
setVertColor m i verts[i].colour
)
if useTriStrips then setNumFaces m totalMatGroupTriangles
else setNumFaces m rawfaces.count
buildTVFaces m
buildVCFaces m
--meshop.setNumMapFaces m 2 faces.count
numFacesToAdd = rawfaces.count
if useTriStrips then
(
format "Using TriStrips!\n"
faceNumber = 0
--format "Number Of TriStrip Indices: %\n" triStripIndices.count
for i = 1 to matGroups.count do
(
for j = 3 to matGroups[i].triStrip.count do
(
if matGroups[i].triStrip[j][2] do continue
if (bit.and j 1)==1 then
(
facev1 = matGroups[i].triStrip[j-2][1] + 1 + matGroups[i].triStripVertOffset
facev2 = matGroups[i].triStrip[j-1][1] + 1+ matGroups[i].triStripVertOffset
facev3 = matGroups[i].triStrip[j][1] + 1+ matGroups[i].triStripVertOffset
)
else
(
facev1 = matGroups[i].triStrip[j-2][1] + 1+ matGroups[i].triStripVertOffset
facev2 = matGroups[i].triStrip[j][1] + 1+ matGroups[i].triStripVertOffset
facev3 = matGroups[i].triStrip[j-1][1] + 1+ matGroups[i].triStripVertOffset
)
if facev1 == facev2 or facev1 == facev3 or facev2 == facev3 do continue
faceNumber += 1
if useUniqueVerts then setFace m faceNumber oldVertsToNewVerts[facev1] oldVertsToNewVerts[facev3] oldVertsToNewVerts[facev2]
else setFace m faceNumber facev1 facev3 facev2
setEdgeVis m faceNumber 1 true
setEdgeVis m faceNumber 2 true
setEdgeVis m faceNumber 3 true
setFaceNormal m faceNumber (normalize ((verts[facev1].norm+verts[facev2].norm+verts[facev3].norm)/3))
setFaceMatID m faceNumber (i)
setTVFace m faceNumber facev1 facev3 facev2
--meshop.setMapFace m 2 i [faces[i].v1, faces[i].v3, faces[i].v2]
setVCFace m faceNumber facev1 facev3 facev2
)
for j = 1 to (matGroups[i].patchList.count/3) do
(
patchind1 = j * 3 - 2
patchind2 = j * 3 - 1
patchind3 = j * 3
facev1 = matGroups[i].patchList[patchind1]
facev1 += 1 + matGroups[i].patchListVertOffset
facev2 = matGroups[i].patchList[patchind2]
facev2 += 1 + matGroups[i].patchListVertOffset
facev3 = matGroups[i].patchList[patchind3]
facev3 += 1 + matGroups[i].patchListVertOffset
if facev1 == facev2 or facev1 == facev3 or facev2 == facev3 do continue
faceNumber+=1
if useUniqueVerts then setFace m faceNumber oldVertsToNewVerts[facev1] oldVertsToNewVerts[facev3] oldVertsToNewVerts[facev2]
else setFace m faceNumber facev1 facev3 facev2
setEdgeVis m faceNumber 1 true
setEdgeVis m faceNumber 2 true
setEdgeVis m faceNumber 3 true
setFaceNormal m faceNumber (normalize ((verts[facev1].norm+verts[facev2].norm+verts[facev3].norm)/3))
if mergeMDLs == true then
(
setFaceMatID m faceNumber (merge_OldMatIDsToNew[i])
)
else
(
setFaceMatID m faceNumber (i)
)
setTVFace m faceNumber facev1 facev3 facev2
--meshop.setMapFace m 2 i [faces[i].v1, faces[i].v3, faces[i].v2]
setVCFace m faceNumber facev1 facev3 facev2
)
)
)
else
(
for i=1 to numFacesToAdd do
(
if useTriStrips then
(
facev1 = triStripIndices[i*3-2]
facev2 = triStripIndices[i*3-1]
facev3 = triStripIndices[i*3]
)
else
(
facev1=rawfaces[i].v1
facev2=rawfaces[i].v2
facev3=rawfaces[i].v3
)
if useUniqueVerts then setFace m i oldVertsToNewVerts[facev1] oldVertsToNewVerts[facev3] oldVertsToNewVerts[facev2]
else setFace m i facev1 facev3 facev2
setEdgeVis m i 1 true
setEdgeVis m i 2 true
setEdgeVis m i 3 true
setFaceNormal m i (normalize ((verts[facev1].norm+verts[facev2].norm+verts[facev3].norm)/3))
if mergeMDLs == true then
(
setFaceMatID m i (merge_OldMatIDsToNew[rawfaces[i].matID+1])
)
else
(
setFaceMatID m i (rawfaces[i].matID+1)
)
setTVFace m i facev1 facev3 facev2
--meshop.setMapFace m 2 i [faces[i].v1, faces[i].v3, faces[i].v2]
setVCFace m i facev1 facev3 facev2
)
)
m = mesh mesh:m
m.material = meshMaterial
if prepSkinDataSet then
(
--format "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\nOMG PANIC! THIS MDL HAS PREP SKIN DATA AND I DON'T KNOW WHAT TO DO WITH IT!!! At Position: %\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" (ftell f)
format "Num Verts: %\nNum Faces: %\n" PREPvertcount PREPfacecount
numBones = readshort f #unsigned
unknown1 = readshort f #unsigned
unknown2 = readshort f #unsigned
format "Unknown1 = %\nUnknown2 = %\n" unknown1 unknown2
skinMod = skin()
addmodifier m skinMod
boneNames = #()
for i=1 to numBones do
(
append boneNames (readstring f)
)
PrintArrayInFull boneNames "boneNames"
boneInfo = #()
for i = 1 to numbones do
(
v1X = readfloat f
v1Y = readfloat f
v1Z = readfloat f
v2X = readfloat f
v2Y = readfloat f
v2Z = readfloat f
v3X = readfloat f
v3Y = readfloat f
v3Z = readfloat f
p = readbyte f
c = readbyte f
s = readbyte f
boneInfo[i] = #([v1X,v1Y,v1Z], [v2X,v2Y,v2Z], [v3X,v3Y,v3Z], p, c, s)
--format "%: \"%\" : { x:% y:% z:%} : { x:% y:% z:%} : { x:% y:% z:%} p: % c: % s:% \n" i boneNames[i] v1X v1Y v1Z v2X v2Y v2Z v3X v3Y v3Z p c s
)
listOfBones = #()
select m
max modify mode
modPanel.setCurrentObject skinMod
for i=1 to numbones do
(
qX = readfloat f
qY = readfloat f
qZ = readfloat f
qW = readfloat f
posX = readfloat f
posY = readfloat f
posZ = readfloat f
unknown_f = readfloat f
fseek f -4 #seek_cur
unknown_l = readlong f
fseek f -4 #seek_cur
unknown_lu = readlong f #unsigned
fseek f -4 #seek_cur
unknown_s1 = readshort f
unknown_s2 = readshort f
fseek f -4 #seek_cur
unknown_su1 = readshort f #unsigned
unknown_su2 = readshort f #unsigned
boneTransform = (quat qz -qx qw qy) as matrix3
bonePos = [-posX, -posZ, posY]
newBone = bonesys.createbone bonePos (bonePos + 0.1 * boneTransform.row1) (normalize boneTransform.row3) --bone name:boneNames[i] rotation:(quat qz -qx qw qy)
listOfBones[i] = newBone
listOfBones[i].rotation = quat qz -qx qw qy
parentBone = boneInfo[i][4]+1
newpos = [-posX, -posZ, posY]
listOfBones[i].position = newpos
listOfBones[i].name = boneNames[i]
ik.SetAxisLimit newBone #rotational #{1, 2, 3}
ik.SetAxisEase newBone #rotational #{1, 2, 3}
format "boneInfo[%] = %\n" i boneInfo[i]
minLimit = ([boneInfo[i][1].x, boneInfo[i][1].y,boneInfo[i][1].z] * 360)-- * boneTransform
maxLimit = ([boneInfo[i][1].x, boneInfo[i][2].y,boneInfo[i][2].z] * 360)-- * boneTransform
ik.SetAxisMin newBone #rotational minLimit
ik.SetAxisMax newBone #rotational maxLimit
--skinops.addbone skinMod listOfBones[i] 0
format "%: \"%\" {X: % Y:% Z: % W: %} {X: % Y: % Z: %} ???: %\n" i boneNames[i] qx qy qz qw posx posy posz unknown_f
format "unknown: float: % long: % unsigned long: % shorts: % - % unsigned shorts: %\n\n" unknown_f unknown_l unknown_lu unknown_s1 unknown_s2 unknown_su1 unknown_su2
)
sortedBones = deepcopy boneNames
sort sortedBones
oldbonestonewbones = #()
newbonestooldbones = #()
currentBones = #()
if listOfBones[(finditem boneNames "hips")].children.count > 0 then
(
for i=1 to listOfBones[(finditem boneNames "hips")].children.count do append listOfBones[(finditem boneNames "hips")].children[i]
)
while currentBones.count > 0 do
(
currentBone = currentBones[1]
deleteItem currentBones 1
boneIndex = finditem boneNames currentBone.name
currentBone.position = boneInfo[boneIndex][3] * listOfBones[(boneInfo[boneIndex][4] + 1)].transform
if currentBone.children.count > 0 then
(
for i=1 to currentBone.children.count do append currentBone.children[i]
)
)
for i=1 to boneNames.count do
(
parentBone = boneInfo[i][4]+1
newpos = listOfBones[i].position
while parentBone > 0 do
(
offsetPos = [-boneInfo[parentBone][3].x, -boneInfo[parentBone][3].z, boneInfo[parentBone][3].y] * listOfBones[parentBone].rotation
newPos += offsetPos
parentBone = boneInfo[parentBone][4]+1
)
--listOfBones[i].position = newpos
for j=1 to sortedBones.count do
(
if sortedBones[j] == boneNames[i] then
(
oldbonestonewbones[i] = j
newbonestooldbones[j] = i
break;
)
)
)
for i=1 to listOfBones.count do
(
skinops.addbone skinMod listOfBones[newbonestooldbones[i]] 0
if boneInfo[i][4]+1 > 0 then listOfBones[i].parent = listOfBones[boneInfo[i][4]+1]
)
completeRedraw ()
weightsPerVert = #()
for i = 1 to PREPvertcount do
(
weightCount = readshort f #unsigned
unknown = readshort f #unsigned
weightIndex = readlong f #unsigned
format "%: % % %\n" i weightCount unknown weightIndex
printflagbits unknown 32 "Unknown Skin Data"
weightsPerVert[i] = #(weightCount, unknown, weightIndex, #(), #())
)
currentWeightIndex = 0
weightCount = readlong f #unsigned
for i=1 to weightsPerVert.count do
(
for j=1 to weightsPerVert[i][1] do
(
currentWeightIndex += 1
boneIndex = readshort f #unsigned
weightsPerVert[i][4][j] = oldbonestonewbones[boneIndex + 1]
)
)
currentWeightIndex = 0
for i=1 to weightsPerVert.count do
(
for j=1 to weightsPerVert[i][1] do
(
currentWeightIndex += 1
weight = readfloat f
weightsPerVert[i][5][j] = weight
--format "Vert %: weightIndex: % boneIndex: % weight: %\n" i currentWeightIndex weightsPerVert[i][4][j] weightsPerVert[i][5][j]
)
)
printarrayinfull bonenames "BoneNames"
printarrayinfull sortedBones "SortedBoneNames"
printarrayinfull oldbonestonewbones "SortedBoneNames"
printarrayinfull newbonestooldbones "SortedBoneNames"
format "SkinMod Bones:\n"
for i=1 to (skinops.getnumberbones skinmod) do
(
format "%: % (node: %)\n" i (skinops.getbonename skinmod i 1) (skinops.getbonename skinmod i 2)
)
--format "\nnum skin verts: %\nnum uniqueVerts: %\n\n" (skinOps.GetNumberVertices skinMod) uniqueVerts.count
for i=1 to uniqueVerts.count do
(
--format "Vert #%" i
vertIndex = finditem oldVertsToNewVerts i
weightCount = skinOps.GetVertexWeightCount skinMod i
--format "\nWeights on Vert Before Replacing (% found):\t" weightCount
for x=1 to weightCount do
(
boneId = skinOps.GetVertexWeightBoneID skinMod i x
boneName = skinOps.GetBoneName skinMod boneId 0
boneNum = 0
for j=1 to sortedBones.count do
(
if sortedBones[j] == boneName then
(
boneNum = j
break
)
)
weight = skinOps.GetVertexWeight skinMod i x
--format "(% [%/%] - %), " boneName boneNum boneId weight
)
--format "\nWeights Being Added:\t\t\t\t"
for x=1 to weightsPerVert[vertIndex][4].count do
(
--format "(% [%] - %), " sortedBones[weightsPerVert[vertIndex][4][x]] weightsPerVert[vertIndex][4][x] weightsPerVert[vertIndex][5][x]
)
for z=1 to sortedBones.count do
(
if (finditem weightsPerVert[vertIndex][4] z) == 0 then
(
append weightsPerVert[vertIndex][4] z
append weightsPerVert[vertIndex][5] 0.0
)
)
skinOps.ReplaceVertexWeights skinMod i weightsPerVert[vertIndex][4] weightsPerVert[vertIndex][5]
skinOps.Invalidate SkinMod 0
weightCount = skinOps.GetVertexWeightCount skinMod i
--format "\nWeights on Vert (% found):\t\t\t" weightCount
for x=1 to weightCount do
(
boneId = skinOps.GetVertexWeightBoneID skinMod i x
boneName = skinOps.GetBoneName skinMod boneId 0
boneNum = 0
for j=1 to sortedBones.count do
(
if sortedBones[j] == boneName then
(
boneNum = j
break
)
)
weight = skinOps.GetVertexWeight skinMod i x
--format "(% [%/%] - %), " boneName boneNum boneId weight
)
--format "\n\n"
)
--format "\nnum skin verts: %\n\n" (skinOps.GetNumberVertices skinMod)
)
gc()
--update m
return m
)
else
(
PREPfaceCount = readlong f #unsigned
fseek f (16 * PREPfaceCount) #seek_cur
PREPvertcount = readlong f #unsigned
fseek f (44 * PREPvertcount) #seek_cur
matGroupCount = readshort f #unsigned
for i = 1 to matGroupCount do
(
fseek f 48 #seek_cur
triStripLength = readlong f #unsigned
fseek f ((triStripLength * 4)+8) #seek_cur
patchListLength = readlong f #unsigned
fseek f (patchListLength * 4) #seek_cur
)
prepSkinDataSet = CheckBitIsSet flags 5
if prepSkinDataSet then
(
--format "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\nOMG PANIC! THIS MDL HAS PREP SKIN DATA AND I DON'T KNOW WHAT TO DO WITH IT!!! At Position: %\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" (ftell f)
format "Num Verts: %\nNum Faces: %\n" PREPvertcount PREPfacecount
numBones = readshort f #unsigned
unknown1 = readshort f #unsigned
unknown2 = readshort f #unsigned
format "Unknown1 = %\nUnknown2 = %\n" unknown1 unknown2
--skinMod = skin()
--addmodifier m skinMod
boneNames = #()
for i=1 to numBones do
(
append boneNames (readstring f)
)
PrintArrayInFull boneNames "boneNames"
boneInfo = #()
for i = 1 to numbones do
(
v1X = readfloat f
v1Y = readfloat f
v1Z = readfloat f
v2X = readfloat f
v2Y = readfloat f
v2Z = readfloat f
v3X = readfloat f
v3Y = readfloat f
v3Z = readfloat f
p = readbyte f
c = readbyte f
s = readbyte f
boneInfo[i] = #([v1X,v1Y,v1Z], [v2X,v2Y,v2Z], [v3X,v3Y,v3Z], p, c, s)
format "%: \"%\" : { x:% y:% z:%} : { x:% y:% z:%} : { x:% y:% z:%} p: % c: % s:% \n" i boneNames[i] v1X v1Y v1Z v2X v2Y v2Z v3X v3Y v3Z p c s
)
listOfBones = #()
--select m
--max modify mode
--modPanel.setCurrentObject skinMod
for i=1 to numbones do
(
qX = readfloat f
qY = readfloat f
qZ = readfloat f
qW = readfloat f
posX = readfloat f
posY = readfloat f
posZ = readfloat f
unknown = readfloat f
format "%: \"%\" {X: % Y:% Z: % W: %} {X: % Y: % Z: %} ???: %\n" i boneNames[i] qx qy qz qw posx posy posz unknown
)
for i=1 to listOfBones.count do
(
if boneInfo[i][4]+1 > 0 then listOfBones[i].parent = listOfBones[boneInfo[i][4]+1]
)
weightsPerVert = #()
for i = 1 to PREPvertcount do
(
weightCount = readshort f #unsigned
unknown = readshort f #unsigned
weightIndex = readlong f #unsigned
--format "%: % % %\n" i weightCount unknown weightIndex
weightsPerVert[i] = #(weightCount, unknown, weightIndex, #(), #())
)
currentWeightIndex = 0
weightCount = readlong f #unsigned
for i=1 to weightsPerVert.count do
(
for j=1 to weightsPerVert[i][1] do
(
currentWeightIndex += 1
boneIndex = readshort f #unsigned
weightsPerVert[i][4][j] = boneIndex + 1
)
)
currentWeightIndex = 0
for i=1 to weightsPerVert.count do
(
for j=1 to weightsPerVert[i][1] do
(
currentWeightIndex += 1
weight = readfloat f
weightsPerVert[i][5][j] = weight