-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathw3dimporter.ms
More file actions
7102 lines (6623 loc) · 269 KB
/
Copy pathw3dimporter.ms
File metadata and controls
7102 lines (6623 loc) · 269 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
-- AUTO-GENERATED by build-w3dimporter.ps1 — edit modules/, not this file --
-- w3d importer --
-- scripted by coolfile (his original called itself "version 3.1") --
-- msn: coolfile_cn@hotmail.com --
-- edited and augmented by NDC --
-- edited by tria --
-- credits: Seagle & Sloth --
-- Single source of truth for the importer version. Everything that displays
-- a version reads this: the rltMain window title (set in its open handler)
-- and the cfW3DImporter debug banner. Bump it with any non-trivial logic
-- change so the loaded copy is identifiable from the log.
global W3DImporterVersion = "v21.13"
-- Reload helper: a runtime error inside cfW3DImporter can leave the rollout's
-- event-handler thread stuck, making every button in the dialog unresponsive.
-- The dialog itself can't recover from this (its own handlers are dead), so
-- recovery has to come from outside. Type `w3di.reload()` into the MAXScript
-- Listener after a crash and it'll tear the dialog down and rebuild it from a
-- clean state. (`w3di` is a struct used as a namespace so we can write
-- `w3di.reload`. `w3d` itself is reserved by 3dsmax.)
struct W3DImporterNamespace ( fn reload =
(
try ( destroyDialog rltMain ) catch ()
createDialog rltMain menu:rltMainMenu
))
global w3di = W3DImporterNamespace()
-- Forward declarations: these globals are assigned further down the file
-- (W3DImporterInit at ~line 5341, rnPendingFile at ~line 6182) but are
-- referenced inside function/rollout bodies defined above those assignments
-- (cfW3DImporter at ~line 1853, rltMain handler at ~line 6177). Manual
-- `Evaluate All` from the editor tolerates this because the whole buffer
-- runs in one shared scope; but 3ds Max's scripts/startup/ autoload
-- compiles function bodies before the later assignments execute, capturing
-- the names as undefined locals. Forward-declaring them here binds the
-- global slots up front so both paths behave identically.
global W3DImporterInit
global rnPendingFile
-- Skipped-content collector (v21.12): chunk readers in 02-chunkreader.ms
-- append human-readable notes here when they skip data the engines consume
-- (lightmap prelit, DIG, adaptive-delta anim, emitters, dazzles, ...);
-- cfW3DImporter resets it per import and prints a deduplicated summary at the
-- end, so dropped content is visible instead of silent. Forward-declared for
-- the same autoload reason as the names above (readers compile before the
-- assignment in cfW3DImporter executes).
global w3dSkippedContent
-- rltMain is defined at ~line 5979, after rltRenegade (~line 4655) whose
-- doImport handler reads rltMain.ckb*.checked. Under scripts/startup/ that
-- read otherwise fails (undefined) and the try/catch around it silently
-- defaults every Advanced-tab option to false — making the imported file
-- appear correct but with all advanced options ignored.
global rltMain
-- reference table for adaptive delta motion channels --
deltaTable = #()
-- Cached W3DMaterial plugin availability. Probed once by `rltMain open`
-- (the rollout open handler) and, as a fallback, by cfW3DImporter on entry
-- when the rollout has not been opened (batch path). The two material
-- builder functions short-circuit when this is false, so a missing plugin
-- produces a clear "falling back to Standard materials" log line instead of
-- ~30 silent setProperty failures further down.
global w3dMatlAvailable = undefined
-- Duplicate-object name disambiguation.
-- The W3D exporter requires every Max node to have a unique name and aborts
-- with "duplicate names found" otherwise (w3dexport.cpp:2128). Renegade's
-- convention for instanced/placeholder objects is a trailing "~NN": the
-- exporter strips everything from the first '~' onward (CopyW3DName /
-- ProxyExportTaskClass, w3dexport.cpp:9761) and re-references the base name, so
-- "GDI SPAWNER", "GDI SPAWNER~01", "GDI SPAWNER~02" all round-trip back to the
-- single render object "GDI SPAWNER". We keep the first occurrence bare and
-- give each subsequent duplicate ~01, ~02, ... (2-digit minimum, growing).
-- Comparison is case-insensitive because the exporter upper-cases W3D names,
-- and the check is against the live scene so it also avoids colliding with
-- nodes from a previous import in the same session.
fn w3dUniqueNodeName baseName =
(
if (getNodeByName baseName exact:true ignoreCase:true) == undefined then baseName
else
(
local result = baseName
local n = 1
do
(
result = baseName + "~" + (formattedPrint n format:"02d")
n += 1
)
while (getNodeByName result exact:true ignoreCase:true) != undefined
result
)
)
struct HierarchyHeader
(
version,
hierName,
pivotCount,
centrePos
)
struct HierarchyPivot
(
pivotName,
parentID,
pos,
eulerAngles,
rotation
)
struct Hierarchy
(
header,
pivots
)
struct AnimationHeader
(
version,
animName,
skelName,
frameCount,
frameRate
)
struct CompressedAnimationHeader
(
version,
animName,
skelName,
frameCount,
frameRate,
flavour
)
struct AnimationChannel
(
firstFrame,
lastFrame,
vectorLen,
flags,
pivotID,
pad,
values
)
struct AnimationTimeCodedKey
(
keyTime,
keyValue
)
struct AnimationTimeCodedChannel
(
type,
timeCodesCount,
pivotID,
vectorLen,
flags,
values
)
struct AnimationAdaptiveDeltaChannel
(
type,
framesCount,
pivotID,
vectorLen,
flags,
initDelta,
increment,
values
)
struct adInterval
(
coef,
startInterval,
endInterval
)
struct AnimationBitChannel
(
firstFrame,
lastFrame,
flags,
pivotID,
defaultVal,
values
)
struct Animation
(
header,
channels,
bitChannels
)
struct W3dMeshHeader
(
version, attrs, meshName, containerName,
faceCount, vertCount, matlCount, damageStageCount,
sortLevel, prelitVersion, futureCount, vertChannelCount, faceChannelCount,
minCorner, maxCorner, sphCentre, sphRadius
)
struct W3dMeshFace
(
vertIds,
attrs,
normal,
distance
)
struct W3dMeshMaterialSetInfo
(
passCount,
vertMatlCount,
shaderCount,
textureCount
)
struct W3dMeshShader
(
depthCompare, depthMask, colorMask, destBlend, fogFunc, priGradient, secGradient, srcBlend, texturing,
detailColorFunc, detailAlphaFunc, shaderPreset, alphaTest, postDetailColorFunc, postDetailAlphaFunc, pad
)
struct W3dMeshVertMaterialInfo
(
attrs,
ambient,
diffuse,
specular,
emissive,
shininess,
opacity,
translucency
)
struct W3dMeshVertMaterial
(
vmName,
vmInfo,
vmArgs0,
vmArgs1
)
struct W3dMeshTextureInfo
(
attrs,
animType,
frameCount,
frameRate
)
struct W3dMeshTexture
(
txFileName,
txInfo
)
struct W3dMeshTextureCoord
(
u, v
)
struct W3dMeshTextureStage
(
txIds,
txCoords
)
struct W3dMeshMaterialPass
(
vmIds,
shaderIds,
textureStage, -- legacy: first stage (== textureStages[1] when present); kept for back-compat readers
textureStages, -- array of W3dMeshTextureStage, one per W3D_CHUNK_TEXTURE_STAGE in this pass
dcg -- W3D_CHUNK_DCG per-vertex diffuse colors (array of Max Color, one per mesh vertex); undefined if absent
)
struct W3dMeshVertInfs
(
boneIds1,
boneIds2,
boneWgts1,
boneWgts2
)
struct W3dMesh
(
header,
verts,
normals,
vertInfs,
faces,
shadeIds,
matlheader,
shaders,
vertMatls,
textures,
matlPass,
matlPasses,
userText -- W3D_CHUNK_MESH_USER_TEXT: MAX user-defined-properties / comment field (NULL-terminated); undefined if absent
)
struct HLodHeader
(
version,
lodCount,
hlodName,
hierarchyName
)
struct HLodObjectArrayHeader
(
modelCount,
maxScreenSize
)
struct HLodObject
(
pivotIndex,
lodName
)
struct HLodObjectArray
(
header,
lodObjects
)
struct HLodAggregate --renegade
(
pivotIndex,
name
)
struct HLod
(
header,
lodArray,
lodArrays, --renegade: all LOD levels (lodArray == lodArrays[1] for Generals compat)
aggregates, --renegade: 0x705 HLOD_AGGREGATE_ARRAY entries
proxies --renegade: 0x706 HLOD_PROXY_ARRAY entries (pivotIndex only)
)
struct AABox
(
version,
attrs,
boxName,
boxColor,
centre,
extent
)
struct Dependency
(
locBone,
Vertices
)
struct DependencyStack
(
locMesh,
Dependencies
)
fn GetChunkSize chunkSize =
( result = bit.and chunkSize 0x7FFFFFFF )
fn ReadLongArray fstream arrayEnd=
(
longArray = #()
while (ftell fstream < arrayEnd) do
( append longArray (ReadLong fstream #unsigned) )
return longArray
)
fn ReadShortArray fstream arrayEnd=
(
shortArray = #()
while (ftell fstream < arrayEnd) do
(
shortArray[shortArray.count+1] = ReadShort fstream #unsigned
)
return shortArray
)
fn ReadFixedString fstream =
(
strend = (ftell fstream) + 16
instr = ReadString fstream
fseek fstream strend #seek_set
result = instr
)
fn ReadLongFixedString fstream =
(
strend = (ftell fstream) + 32
instr = ReadString fstream
fseek fstream strend #seek_set
result = instr
)
fn ReadColor fstream =
(
r = ReadByte fstream #unsigned
g = ReadByte fstream #unsigned
b = ReadByte fstream #unsigned
a = ReadByte fstream #unsigned
result = Color r g b a
--the following code will miss one byte sometime. but why?
--result = Color (ReadByte fstream #unsigned) (ReadByte fstream #unsigned) (ReadByte fstream #unsigned) (ReadByte fstream #unsigned)
)
fn ReadHierarchyHeader fstream =
(
version = ReadLong fstream #unsigned
hierName = ReadFixedString fstream
pivotCount = ReadLong fstream #unsigned
centrePos = Point3 (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream)
result = HierarchyHeader version hierName pivotCount centrePos
--return result
)
fn ReadHierarchyPivots fstream pivotsEnd =
(
pivots = #()
while (ftell fstream < pivotsEnd) do
(
pivotName = ReadFixedString fstream
parentID = ReadLong fstream #unsigned -- 0xffffffff = root pivot; no parent
--if parentID != 0xffffffff do parentID += 1
pos = Point3 (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream) -- translation to pivot point
eulerAngles = Point3 (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream) -- orientation of the pivot point
rotation = Quat (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream) -- orientation of the pivot point
pivots[(pivots.count + 1)] = HierarchyPivot pivotName parentID pos eulerAngles rotation
)
result = pivots
--return result
)
fn ReadHierarchy fstream chunkEnd =
(
while (ftell fstream < chunkEnd) do
(
chunkType = ReadLong fstream #unsigned
chunkSize = GetChunkSize(ReadLong fstream #unsigned)
------ debug start ------
--print ("Type: 0x" + (bit.intAsHex chunkType))
--chunkSize = ReadLong fstream
--print ("Size: 0x" + (bit.intAsHex chunkSize))
--chunkSize = GetChunkSize(chunkSize)
--print (bit.intAsHex chunkSize)
------ debug end ------
case chunkType of
(
0x00000101: --W3D_CHUNK_HIERARCHY_HEADER
(
header = ReadHierarchyHeader fstream
)
0x00000102: --W3D_CHUNK_PIVOTS
(
subChunkEnd = (ftell fstream) + chunkSize
pivots = ReadHierarchyPivots fstream subChunkEnd
)
0x00000103: --W3D_CHUNK_PIVOT_FIXUPS -- only needed by the exporter...
(
--fixups[(fixups.count + 1)] = ReadHierarchyFixUps fstream
--cracked format: there are 64 bytes in 12 dwords in 3 channels in each fixup.
--channel X : ScaleX, Unknown, Unknown, Unknown (Long/DWord)
--channel Y : ScaleY, Unknown, Unknown, Unknown (Long/DWord)
--channel Z : ScaleZ, Unknown, Unknown, Unknown (Long/DWord)
fseek fstream chunkSize #seek_cur
)
default:
(
fseek fstream chunkSize #seek_cur
)
)
------ debug start ------
--print ("Pos: 0x" + (bit.intAsHex (ftell fstream)))
--print ("End: 0x" + (bit.intAsHex chunkEnd))
------ debug end ------
)
result = Hierarchy header pivots
--return result
)
fn ReadAnimationHeader fstream =
(
version = ReadLong fstream #unsigned
animName = ReadFixedString fstream
skelName = ReadFixedString fstream
frameCount = ReadLong fstream #unsigned
frameRate = ReadLong fstream #unsigned
result = AnimationHeader version animName skelName frameCount frameRate
--return result
)
fn ReadCompressedAnimationHeader fstream =
(
version = ReadLong fstream #unsigned
animName = ReadFixedString fstream
skelName = ReadFixedString fstream
frameCount = ReadLong fstream #unsigned
frameRate = ReadShort fstream #unsigned
flavour = ReadShort fstream #unsigned
result = CompressedAnimationHeader version animName skelName frameCount frameRate flavour
--return result
)
fn ReadAnimationChannel fstream chunkEnd =
(
firstFrame = ReadShort fstream #unsigned
lastFrame = ReadShort fstream #unsigned
vectorLen = ReadShort fstream #unsigned
flags = ReadShort fstream #unsigned
pivotID = ReadShort fstream #unsigned
--if pivotID != 0xffffffff then pivotID += 1 -- v1.00, cancelled at v1.04
pad = ReadShort fstream #unsigned
values = #()
i = 1
while (ftell fstream < chunkEnd) do
(
case flags of
(
0x0006: --ANIM_CHANNEL_Q
values[i] = quat (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream)
default: --ANIM_CHANNEL_X/Y/Z
values[i] = ReadFloat fstream
)
i += 1
)
result = AnimationChannel firstFrame lastFrame vectorLen flags pivotID pad values
--return result
)
fn ReadAnimationTimeCodedChannel fstream chunkEnd =
(
timeCodesCount = ReadLong fstream #unsigned
pivotID = ReadShort fstream #unsigned
vectorLen = ReadByte fstream #unsigned
flags = ReadByte fstream #unsigned
values = #()
i = 1
while (ftell fstream < chunkEnd) do
(
-- Bit 31 of the timecode is W3D_TIMECODED_BINARY_MOVEMENT_FLAG (w3d.h):
-- it marks a non-interpolated key, it is NOT part of the frame number.
-- Left unmasked it yields keyTime ~2.1e9 and the per-frame fill loop in
-- the playback code effectively hangs Max.
tCode = bit.and (ReadLong fstream #unsigned) 0x7FFFFFFF
case flags of
(
0x0006: --ANIM_CHANNEL_TIMECODED_Q
tValue = quat (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream)
default: --ANIM_CHANNEL_TIMECODED_X/Y/Z
tValue = ReadFloat fstream
)
values[i] = AnimationTimeCodedKey tCode tValue
i += 1
)
result = AnimationTimeCodedChannel 1 timeCodesCount pivotID vectorLen flags values
--return result
)
fn procDeltaBlockData fstream nBits scale initVal =
(
procData = #()
v0 = initVal
for i=1 to 2*nBits do
(
currByte = readByte fstream #unsigned
if (nBits == 4) then
(
-- Splitting the byte into halves
lVal = bit.and currByte 0x0F
uVal = bit.shift currByte -4
-- Restoring halves' signs (bit flipping)
if (lVal >= 8) do lVal -= 16
if (uVal >= 8) do uVal -= 16
append procData (v0 + scale*lVal)
v0 += scale*lVal
append procData (v0 + scale*uVal)
v0 += scale*uVal
)
else
(
currByte += 128
if (currByte >= 128) do currByte -= 256
append procData (v0 + scale*currByte)
v0 += scale*currByte
)
)
return procData
)
fn ReadAnimationCompressedMotionChannel fstream chunkEnd =
(
fseek fstream 1 #seek_cur -- skipping a zero byte
chnType = readByte fstream #unsigned
keyFrameChn = (chnType == 0)
vectorLen = readByte fstream #unsigned
flags = readByte fstream #unsigned
values = #()
nTimeCodes = readShort fstream #unsigned
pivotID = readShort fstream #unsigned
-- Simple time key channel
if (keyFrameChn) then
(
for i=1 to nTimeCodes do
(
tCode = readShort fstream #unsigned
values[i] = AnimationTimeCodedKey tCode 0
)
if ((mod nTimeCodes 2) != 0) do --skipping padding
( fseek fstream 2 #seek_cur )
for i=1 to nTimeCodes do
(
case flags of
(
0x0006: --ANIM_CHANNEL_TIMECODED_Q
values[i].keyValue = quat (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream)
default: --ANIM_CHANNEL_TIMECODED_X/Y/Z
values[i].keyValue = readFloat fstream
)
)
result = AnimationTimeCodedChannel 1 nTimeCodes pivotID vectorLen flags values
return result
)
-- Adaptive delta channel
scale = readFloat fstream
nBits = 4*chnType
nBlocks = bit.shift (nTimeCodes+15) -4 -- In how many sections are data encoded? +15 is to make sure we start from 1
scaleFactor = float 1
if (nBits == 8) do
( scaleFactor /= 16 )
decData = #()
values = #()
deltaTableIx = 0
case flags of
(
0x0006: --ANIM_MOTION_CHANNEL_DELTA_Q
(
decData[1] = #(readFloat fstream)
decData[2] = #(readFloat fstream)
decData[3] = #(readFloat fstream)
decData[4] = #(readFloat fstream)
for i=1 to nBlocks do
(
for j=1 to 4 do
(
deltaTableIx = readByte fstream #unsigned
deltaScale = scale * scaleFactor * deltaTable[deltaTableIx+1]
if (deltaTableIx <= 15) do deltaScale /= 100
join decData[j] (procDeltaBlockData fstream nBits deltaScale decData[j][decData[j].count])
)
)
decData[1].count = nTimeCodes
decData[2].count = nTimeCodes
decData[3].count = nTimeCodes
decData[4].count = nTimeCodes
for t=1 to decData[1].count do
( values[t] = AnimationTimeCodedKey (t-1) (quat decData[1][t] decData[2][t] decData[3][t] decData[4][t]) )
)
default: --ANIM_MOTION_CHANNEL_DELTA_X/Y/Z
(
decData[1] = ReadFloat fstream
for i=1 to nBlocks do
(
deltaTableIx = readByte fstream #unsigned
deltaScale = scale * scaleFactor * deltaTable[deltaTableIx+1]
if (deltaTableIx <= 15) do deltaScale /= 100
join decData (procDeltaBlockData fstream nBits deltaScale decData[decData.count])
)
decData.count = nTimeCodes
for t=1 to decData.count do
( values[t] = AnimationTimeCodedKey (t-1) (decData[t]) )
)
)
result = AnimationTimeCodedChannel 1 nTimeCodes pivotID vectorLen flags values
return result
)
fn ReadAnimationBitChannel fstream chunkEnd =
(
firstFrame = ReadShort fstream #unsigned
lastFrame = ReadShort fstream #unsigned
flags = ReadShort fstream #unsigned
pivotID = ReadShort fstream #unsigned
--if pivotID != 0xffffffff then pivotID += 1 -- v.100 cancelled at v.104
defaultVal = ReadByte fstream #unsigned
values = #()
i = 1
while (ftell fstream < chunkEnd) do
(
values[i] = ReadByte fstream #unsigned
i += 1
)
result = AnimationBitChannel firstFrame lastFrame flags pivotID defaultVal values
)
--fn ReadAnimationTimeCodedBitChannel fstream chunkEnd =
--(
-- timeCodesCount = ReadLong fstream #unsigned
-- pivotID = ReadShort fstream #unsigned
-- flags = ReadByte fstream #unsigned
-- defaultVal = ReadByte fstream #unsigned
-- values = #()
-- i = 1
-- while (ftell fstream < chunkEnd) do
-- (
-- values[i] = ReadByte fstream #unsigned
-- i += 1
-- )
-- result = AnimationBitChannel firstFrame lastFrame flags pivotID defaultVal values
-- --return result
--)
fn ReadAnimation fstream chunkEnd =
(
channels = #()
bitchannels = #()
while (ftell fstream < chunkEnd) do
(
chunkType = ReadLong fstream #unsigned
chunkSize = GetChunkSize(ReadLong fstream #unsigned)
case chunkType of
(
0x00000201: --W3D_CHUNK_ANIMATION_HEADER
(
header = ReadAnimationHeader fstream
)
0x00000202: --W3D_CHUNK_ANIMATION_CHANNEL -- channel of vectors
(
subChunkEnd = (ftell fstream) + chunkSize
channels[(channels.count + 1)] = ReadAnimationChannel fstream subChunkEnd --frameCount
)
0x00000203: --W3D_CHUNK_BIT_CHANNEL -- channel of boolean values (e.g. visibility)
(
subChunkEnd = (ftell fstream) + chunkSize
bitchannels[(bitchannels.count + 1)] = ReadAnimationBitChannel fstream subChunkEnd
)
default:
(
fseek fstream chunkSize #seek_cur
)
)
------ debug start ------
--print ("type: " + bit.intAsHex chunkType)
--print ("pos: " + bit.intAsHex (ftell fstream))
--print ("end: " + bit.intAsHex chunkEnd)
------ debug end ------
)
result = Animation header channels bitchannels
--return result
)
fn ReadCompressedAnimation fstream chunkEnd =
(
channels = #()
bitchannels = #()
while (ftell fstream < chunkEnd) do
(
chunkType = ReadLong fstream #unsigned
chunkSize = GetChunkSize(ReadLong fstream #unsigned)
case chunkType of
(
0x00000281: --W3D_CHUNK_COMPRESSED_ANIMATION_HEADER
(
header = ReadCompressedAnimationHeader fstream
)
0x00000282: --W3D_CHUNK_COMPRESSED_ANIMATION_CHANNEL -- channel of vectors
(
subChunkEnd = (ftell fstream) + chunkSize
if header != undefined then
animMode = header.flavour
else
animMode = -1
case animMode of
(
0x0: --ANIM_FLAVOUR_TIMECODED
(
channels[(channels.count + 1)] = ReadAnimationTimeCodedChannel fstream subChunkEnd --frameCount
)
0x1: --ANIM_FLAVOUR_ADAPTIVE_DELTA
(
-- Deliberately not imported (roadmap decision: modders don't
-- author compressed anims). The old empty arm never advanced
-- the stream, so the next loop iteration parsed channel DATA
-- as chunk headers and derailed the rest of the file --
-- skip explicitly and record the loss.
if w3dSkippedContent != undefined do append w3dSkippedContent "adaptive-delta compressed animation channel -- animation not imported"
fseek fstream subChunkEnd #seek_set
)
default: --unknown flavour: skip safely, never derail the parse
( fseek fstream subChunkEnd #seek_set )
)
)
0x00000283: --W3D_CHUNK_COMPRESSED_BIT_CHANNEL -- timecoded visibility
(
if w3dSkippedContent != undefined do append w3dSkippedContent "timecoded visibility bit channel (COMPRESSED_BIT_CHANNEL) -- not imported"
fseek fstream chunkSize #seek_cur
)
0x00000284: --W3D_CHUNK_COMPRESSED_ADAPTIVE_DELTA_MOTION_ANIMATION_CHANNEL
(
subChunkEnd = (ftell fstream) + chunkSize
if header != undefined then
animMode = header.flavour
else
animMode = -1
case animMode of
(
0x0: --ANIM_FLAVOUR_TIMECODED
( channels[(channels.count + 1)] = ReadAnimationCompressedMotionChannel fstream subChunkEnd )
default: --adaptive-delta / unknown: skip safely (see 0x282 note)
(
if w3dSkippedContent != undefined do append w3dSkippedContent "adaptive-delta compressed animation channel -- animation not imported"
fseek fstream subChunkEnd #seek_set
)
)
)
default:
( fseek fstream chunkSize #seek_cur )
)
)
result = Animation header channels bitchannels
--return result
)
-- Decode a raw W3D_MESH_HEADER3.Attributes (or W3DBOX.Attributes) word into a list
-- of human-readable W3D Tools property names. Used by the "Import extended W3D
-- Info" debug log so the user can see exactly which bits the .w3d encoded and
-- which properties wwSetFlags / wwSetSorting will toggle on the imported nodes.
-- Constants mirror src\scripts\w3d.h.
fn cfDecodeW3DAttrs a =
(
local parts = #()
local gt = bit.and a 0x00FF0000 -- W3D_MESH_FLAG_GEOMETRY_TYPE_MASK
case gt of
(
0x00000000: append parts "GeomType=Normal"
0x00010000: append parts "GeomType=CamParal"
0x00020000: append parts "GeomType=Skin"
0x00040000: append parts "GeomType=AABox"
0x00050000: append parts "GeomType=OBBox"
0x00060000: append parts "GeomType=CamOrient"
0x00070000: append parts "GeomType=CamZOrient"
default: append parts ("GeomType=0x" + (bit.intAsHex gt))
)
if (bit.and a 0x00001000) != 0 do append parts "Hide"
if (bit.and a 0x00002000) != 0 do append parts "TwoSided"
if (bit.and a 0x00008000) != 0 do append parts "CastShadow"
if (bit.and a 0x10000000) != 0 do append parts "Shatter"
if (bit.and a 0x20000000) != 0 do append parts "Tangents"
if (bit.and a 0x40000000) != 0 do append parts "Prelit"
if (bit.and a 0x80000000) != 0 do append parts "AlwaysDynLight"
-- Collision bits (W3D_MESH_FLAG_COLLISION_TYPE_MASK = 0x00000FF0).
if (bit.and a 0x00000010) != 0 do append parts "Coll:Physical"
if (bit.and a 0x00000020) != 0 do append parts "Coll:Projectile"
if (bit.and a 0x00000040) != 0 do append parts "Coll:Vis"
if (bit.and a 0x00000080) != 0 do append parts "Coll:Camera"
if (bit.and a 0x00000100) != 0 do append parts "Coll:Vehicle"
-- W3DBOX-only attribute bits.
if (bit.and a 0x00000001) != 0 do append parts "Box:Aligned"
if (bit.and a 0x00000002) != 0 do append parts "Box:Oriented"
return parts
)
fn ReadMeshHeader fstream =
(
version = ReadLong fstream #unsigned
attrs = ReadLong fstream #unsigned
meshName = ReadFixedString fstream
containerName = ReadFixedString fstream
-- Counts, these can be regarded as an inventory of what is to come in the file.
faceCount = ReadLong fstream #unsigned --number of triangles
vertCount = ReadLong fstream #unsigned --number of unique vertices
matlCount = ReadLong fstream #unsigned --number of unique materials
damageStageCount = ReadLong fstream #unsigned --number of damage offset chunks
sortLevel = ReadLong fstream #unsigned --static sorting level of this mesh
prelitVersion = ReadLong fstream #unsigned --mesh generated by this version of Lightmap Tool
futureCount = ReadLong fstream #unsigned --future counts
vertChannelCount = ReadLong fstream #unsigned --bits for presence of types of per-vertex info
faceChannelCount = ReadLong fstream #unsigned --bits for presence of types of per-face info
-- Bounding volumes
minCorner = Point3 (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream) --Min corner of the bounding box
maxCorner = Point3 (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream) --Max corner of the bounding box
sphCentre = Point3 (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream) --Centre of bounding sphere
sphRadius = ReadFloat fstream --Bounding sphere radius
result = W3dMeshHeader version attrs meshName containerName faceCount vertCount matlCount damageStageCount sortLevel prelitVersion futureCount vertChannelCount faceChannelCount minCorner maxCorner sphCentre sphRadius
)
fn ReadMeshVertArray fstream chunkEnd =
(
verts = #()
while (ftell fstream < chunkEnd) do
(
verts[verts.count+1] = Point3 (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream)
)
return verts
)
fn ReadMeshFace fstream =
(
--vertIds = #()
--vertIds[1] = ReadLong fstream #unsigned -- vertex,vnormal,texcoord,color indices
--vertIds[2] = ReadLong fstream #unsigned
--vertIds[3] = ReadLong fstream #unsigned
vertIds = Point3 (ReadLong fstream #unsigned) (ReadLong fstream #unsigned) (ReadLong fstream #unsigned)
attrs = ReadLong fstream #unsigned -- attributes bits
normal = Point3 (ReadFloat fstream) (ReadFloat fstream) (ReadFloat fstream) -- plane normal
distance = ReadFloat fstream -- plane distance
result = W3dMeshFace vertIds attrs normal distance
)
fn ReadMeshFaceArray fstream chunkEnd =
(
faces = #()
while (ftell fstream < chunkEnd) do
( append faces (ReadMeshFace fstream) )
return faces
)
--replaced by ReadLongArray since v1.02
--fn ReadMeshVertShadeIds fstream chunkEnd =
--(
-- shadeIds = #()
-- while (ftell fstream < chunkEnd) do
-- (
-- shadeIds[shadeIds.count+1] = ReadLong fstream #unsigned
-- )
-- return shadeIds
--)
fn ReadMeshVertInfs fstream chunkEnd =
(
boneIds1 = #()
boneIds2 = #()
boneWgts1 = #()
boneWgts2 = #()
while (ftell fstream < chunkEnd) do
(
append boneIds1 (ReadShort fstream #unsigned)
append boneIds2 (ReadShort fstream #unsigned)
append boneWgts1 (ReadShort fstream #unsigned)
append boneWgts2 (ReadShort fstream #unsigned)
--if (boneIds2[boneIds2.count] > 0) do break()
--fseek fstream 6 #seek_cur --skip pad 6 bytes
)
return W3dMeshVertInfs boneIds1 boneIds2 boneWgts1 boneWgts2
)
fn ReadMeshMaterialSetInfo fstream chunkEnd =
(
passCount = ReadLong fstream #unsigned --how many material passes this render object uses
vertMatlCount = ReadLong fstream #unsigned --how many vertex materials are used
shaderCount = ReadLong fstream #unsigned --how many shaders are used
textureCount = ReadLong fstream #unsigned --how many textures are used
result = W3dMeshMaterialSetInfo passCount vertMatlCount shaderCount textureCount
)
fn ReadMeshShader fstream =
(
depthCompare = ReadByte fstream #unsigned
depthMask = ReadByte fstream #unsigned
colorMask = ReadByte fstream #unsigned --now obsolete and ignored
destBlend = ReadByte fstream #unsigned
fogFunc = ReadByte fstream #unsigned --now obsolete and ignored
priGradient = ReadByte fstream #unsigned
secGradient = ReadByte fstream #unsigned