whiteoutlib 0.1.3

Read and write Blizzard game assets from Rust: models (MDX, M2, M3), textures (BLP, DDS, PNG, JPEG, BMP, TGA, TIFF, GIF) and archives (CASC, MPQ).
Documentation
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

#pragma once

#include "../../../compatibility.h"
#include "../types.h"
#include "extensions.h"
#include "skin.h"

namespace whiteout {
namespace m2 {

enum class GlobalFlag : u32 {
    None = 0,
    TiltX = 0x00000001,
    TiltY = 0x00000002,
    AddBackReferences = 0x00000004,
    UseTextureCombinerCombos = 0x00000008,
    IsCamera = 0x00000010,
    Unused = 0x00000020,
    Unk_0x80 = 0x00000080,
    LoadPhysicsData = 0x00000100,
    NewParticleRecord = 0x00000200,
    Unk_0x400 = 0x00000400,
    TextureTransformsUsesBoneSequences = 0x00000800,
    Unk_0x1000 = 0x00001000,
    ChunkedAnimFiles = 0x00002000,
    UpgradedFormat = 0x00200000,
};

inline GlobalFlag operator|(GlobalFlag lhs, GlobalFlag rhs) {
    return static_cast<GlobalFlag>(static_cast<u32>(lhs) | static_cast<u32>(rhs));
}

inline GlobalFlag operator&(GlobalFlag lhs, GlobalFlag rhs) {
    return static_cast<GlobalFlag>(static_cast<u32>(lhs) & static_cast<u32>(rhs));
}

inline GlobalFlag operator|=(GlobalFlag& lhs, GlobalFlag rhs) {
    lhs = lhs | rhs;
    return lhs;
}

inline GlobalFlag operator&=(GlobalFlag& lhs, GlobalFlag rhs) {
    lhs = lhs & rhs;
    return lhs;
}

inline GlobalFlag operator~(GlobalFlag flag) {
    return static_cast<GlobalFlag>(~static_cast<u32>(flag));
}

inline bool hasFlag(GlobalFlag flags, GlobalFlag flag) {
    return (static_cast<u32>(flags) & static_cast<u32>(flag)) != 0;
}

struct GlobalFlags {
    GlobalFlag value = GlobalFlag::None;
};

struct GlobalSequence {
    u32 timestamp = 0;
};

enum class SequenceFlag : u32 {
    None = 0,
    TiltIn = 0x00000001,
    TiltOut = 0x00000002,
    TiltFixed = 0x00000004,
    Looping = 0x00000020,
    IsAlias = 0x00000040,
    AnimatedSetup = 0x00000080,
    StoredAnimated = 0x00000100,
    EnableComposite = 0x00000200,
};

inline SequenceFlag operator|(SequenceFlag lhs, SequenceFlag rhs) {
    return static_cast<SequenceFlag>(static_cast<u32>(lhs) | static_cast<u32>(rhs));
}

inline SequenceFlag operator&(SequenceFlag lhs, SequenceFlag rhs) {
    return static_cast<SequenceFlag>(static_cast<u32>(lhs) & static_cast<u32>(rhs));
}

inline SequenceFlag operator|=(SequenceFlag& lhs, SequenceFlag rhs) {
    lhs = lhs | rhs;
    return lhs;
}

inline SequenceFlag operator&=(SequenceFlag& lhs, SequenceFlag rhs) {
    lhs = lhs & rhs;
    return lhs;
}

inline SequenceFlag operator~(SequenceFlag flag) {
    return static_cast<SequenceFlag>(~static_cast<u32>(flag));
}

inline bool hasFlag(SequenceFlag flags, SequenceFlag flag) {
    return (static_cast<u32>(flags) & static_cast<u32>(flag)) != 0;
}

struct Sequence {
    u16 id = 0;
    u16 variationIndex = 0;
    u32 duration = 0;
    f32 movespeed = 0.0f;
    SequenceFlag flags = SequenceFlag::None;
    i16 frequency = 0;
    u16 padding = 0;

    u32 replayMin = 0;
    u32 replayMax = 0;

    u16 blendTimeIn = 0;
    u16 blendTimeOut = 0;

    Extent bounding;

    i16 variationNext = -1;
    u16 aliasNext = 0;
};

struct Vertex {
    Vector3f position;
    std::array<u8, 4> boneWeights = {0};
    std::array<u8, 4> boneIndices = {0};
    Vector3f normal;
    std::array<Vector2f, 2> texCoords;
};

struct Bone {
    i32 keyBoneId = -1;
    u32 flags = 0;
    i16 parentBoneId = -1;
    u16 submeshId = 0;

    u32 boneNameCRC = 0;

    AnimationTrack<Vector3f> translation;
    AnimationTrack<CompatQuaternion> rotation;
    AnimationTrack<Vector3f> scale;
    Vector3f pivot;
};

enum class BoneFlag : u32 {
    None = 0,
    IgnoreParentTranslate = 0x001,
    IgnoreParentScale = 0x002,
    IgnoreParentRotation = 0x004,
    SphericalBillboard = 0x008,
    CylindricalBillboardX = 0x010,
    CylindricalBillboardY = 0x020,
    CylindricalBillboardZ = 0x040,
    Transformed = 0x200,
    Kinematic = 0x400,
    HelmetAnimScaled = 0x1000,
};

inline BoneFlag operator|(BoneFlag lhs, BoneFlag rhs) {
    return static_cast<BoneFlag>(static_cast<u32>(lhs) | static_cast<u32>(rhs));
}

inline BoneFlag operator&(BoneFlag lhs, BoneFlag rhs) {
    return static_cast<BoneFlag>(static_cast<u32>(lhs) & static_cast<u32>(rhs));
}

inline BoneFlag operator|=(BoneFlag& lhs, BoneFlag rhs) {
    lhs = lhs | rhs;
    return lhs;
}

inline BoneFlag operator&=(BoneFlag& lhs, BoneFlag rhs) {
    lhs = lhs & rhs;
    return lhs;
}

inline BoneFlag operator~(BoneFlag flag) {
    return static_cast<BoneFlag>(~static_cast<u32>(flag));
}

inline bool hasFlag(BoneFlag flags, BoneFlag flag) {
    return (static_cast<u32>(flags) & static_cast<u32>(flag)) != 0;
}

struct Texture {
    u32 type = 0;
    u32 flags = 0;
    std::string filename;
};

enum class MaterialFlag : u16 {
    None = 0,
    Unlit = 0x01,
    Unfogged = 0x02,
    TwoSided = 0x04,
    DepthTest = 0x08,
    DepthWrite = 0x10,
    NoAlphaComposite = 0x800,
};

inline MaterialFlag operator|(MaterialFlag lhs, MaterialFlag rhs) {
    return static_cast<MaterialFlag>(static_cast<u16>(lhs) | static_cast<u16>(rhs));
}

inline MaterialFlag operator&(MaterialFlag lhs, MaterialFlag rhs) {
    return static_cast<MaterialFlag>(static_cast<u16>(lhs) & static_cast<u16>(rhs));
}

inline MaterialFlag operator|=(MaterialFlag& lhs, MaterialFlag rhs) {
    lhs = lhs | rhs;
    return lhs;
}

inline MaterialFlag operator&=(MaterialFlag& lhs, MaterialFlag rhs) {
    lhs = lhs & rhs;
    return lhs;
}

inline MaterialFlag operator~(MaterialFlag flag) {
    return static_cast<MaterialFlag>(~static_cast<u16>(flag));
}

inline bool hasFlag(MaterialFlag flags, MaterialFlag flag) {
    return (static_cast<u16>(flags) & static_cast<u16>(flag)) != 0;
}

struct Material {
    u16 flags = 0;
    u16 blendingMode = 0;
};

struct TextureWeight {
    AnimationTrack<i16> weight;
};

struct TextureTransform {
    AnimationTrack<Vector3f> translation;
    AnimationTrack<CompatQuaternion> rotation;
    AnimationTrack<Vector3f> scaling;
};

struct ColorAnimation {
    AnimationTrack<Vector3f> color;
    AnimationTrack<i16> alpha;
};

struct Light {
    u16 type = 0;
    i16 boneId = -1;
    Vector3f position;

    AnimationTrack<Vector3f> ambientColor;
    AnimationTrack<f32> ambientIntensity;
    AnimationTrack<Vector3f> diffuseColor;
    AnimationTrack<f32> diffuseIntensity;
    AnimationTrack<f32> attenuationStart;
    AnimationTrack<f32> attenuationEnd;
    AnimationTrack<u8> visibility;
};

struct CameraSpline {
    Vector3f value;
    Vector3f inTangent;
    Vector3f outTangent;
};

struct Camera {
    u32 type = 0;
    f32 fieldOfView = 0.0f;
    f32 farClip = 0.0f;
    f32 nearClip = 0.0f;

    AnimationTrack<CameraSpline> positions;
    Vector3f positionBase;

    AnimationTrack<CameraSpline> targetPositions;
    Vector3f targetPositionBase;

    AnimationTrack<f32> roll;
    AnimationTrack<f32> fieldOfViewTrack;
};

struct Attachment {
    u32 id = 0;
    u16 boneId = 0;
    u16 unknown = 0;
    Vector3f position;
    AnimationTrack<u8> animate;
};

struct RibbonEmitter {
    u32 ribbonId = UINT32_MAX;
    u32 boneId = 0;
    Vector3f position;

    std::vector<u16> textureIndices;
    std::vector<u16> materialIndices;

    AnimationTrack<Vector3f> colorTrack;
    AnimationTrack<i16> alphaTrack;
    AnimationTrack<f32> heightAbove;
    AnimationTrack<f32> heightBelow;

    f32 edgesPerSecond = 0.0f;
    f32 edgeLifetime = 0.0f;
    f32 gravity = 0.0f;
    u16 textureRows = 1;
    u16 textureCols = 1;
    AnimationTrack<u16> texSlot;
    AnimationTrack<u8> visibility;
    i16 priorityPlane = 0;
    i8 ribbonColorIndex = -1;
    i8 textureTransformIndex = -1;
};

struct M2Box {
    Vector3f minimum;
    Vector3f maximum;
};

enum class ParticleEmitterType : u8 {
    Plane = 1,
    Sphere = 2,
    Spline = 3,
    Bone = 4,
};

enum class ParticleBlending : u8 {
    Opaque = 0,
    AlphaBlend = 1,
    Additive = 2,
    AlphaTest = 3,
    AdditiveAlphaTest = 4,
};

enum class ParticleFlag : u32 {
    None = 0,
    Shaded = 0x1,         // ~flags & 1 → CParticleMat bit 0 (inverted); lighting enabled by default
    SortParticles = 0x2,  // Depth-sorted rendering via s_pq priority queue
    VelocityOrient = 0x4, // Billboard aligns along velocity vector
    Unshaded = 0x8,       // Clears lit flag on CParticleMat
    WorldSpace = 0x10,    // Particles operate in world space; skips bone matrix transform in
                          // CreateParticle/UpdateXform.
    InheritBoneScale = 0x20, // In IBuildVertices: multiplies particle size by m_scaleFactor, which
                             // is sqrt(length(boneMatrix column 0)) extracted in UpdateXform.
                             // Particles scale proportionally to attached bone
    InheritVelocity = 0x40,  // Child emitter inherits parent velocity
    ImplosionFilter = 0x80,  // Particles going away from the center (world space or bone matrix if
                             // local) are killed.
    HemisphereUpDirection = 0x100, // Sets velocity direction to be Z-up in SphereEmitters.
    NegateSpinRandom = 0x200,      // Negate spin angle for particles with random bit & 1
    ClampTailToAge = 0x400,        // Clamp tail length to min(tailLength, age) in IBuildVertices
    InheritPosition = 0x800,       // Child inherits parent position; random emission spacing
    XYQuad = 0x1000,               // Use s_quadToView matrix instead of screen-aligned billboard
    ProjectParticle = 0x2000,      // Snap particle to terrain via s_projectCallback
    FollowPosition = 0x4000,       // Add emitter delta-position to particle when 2*dt < age
    Squirt =
        0x8000, // Particle Emitter only emits as bursts particles when emissionRate is animated.
    ChooseRandomTexture = 0x10000,   // Random flipbook frame via CRandom::dice_
    HeadStyle = 0x20000,             // Controls head particle style bit pattern in SetParticleStyle
    TailStyle = 0x40000,             // Controls tail particle style bit pattern in SetParticleStyle
    UnscaledSizeVariation = 0x80000, // Independent X/Y scale variation (2 random floats)
    Unfogged = 0x100000,             // Sets unfogged flag on CParticleMat
    RandFlipbookStart = 0x200000,    // Random starting frame via SetRandFlipBookStart
    Unk_0x400000 = 0x400000,         // ?
    CompressedGravity = 0x800000,    // Gravity is compressed.
    BoneGeneratorBone =
        0x1000000, // Select CBoneGeneratorBone (1) vs CBoneGeneratorJoint (0); emitterType 4
    NoGlobalViewScale = 0x2000000, // Skip s_globalViewScale multiplication on emission rate
    LodIgnoreDistance =
        0x4000000, // Skip distance-based LOD emissionrate scaling in EmitNewParticles
    OffsetHeadBySpin = 0x8000000, // Offset head particle position along spin rotation axis
    MultiTexture = 0x10000000,    // Route through multi-texture particle creation path
    MultitexUseModx4 =
        0x20000000, // CParticleMat bit 3; uses Modx4 instead of Modx2 requires MultiTexture
    MultitexUse3Colors =
        0x40000000, // CParticleMat bit 4; uses 3 colors instead of 2 requires MultiTexture
    DynamicWind =
        0x80000000, // Enables dynamic wind; SET=dynamic callback, CLEAR=static from M2 data
};

struct ParticleEmitter {
    u32 particleId = UINT32_MAX;
    ParticleFlag flags = ParticleFlag::None;
    Vector3f position;
    u16 boneId = 0;
    WHITEOUT_ANON union {
        u16 textureId;
        struct {
            u16 textureId1 : 5;
            u16 textureId2 : 5;
            u16 textureId3 : 5;
            u16 padding : 1;
        };
    };

    // If the path is valid, the particle system uses model particles
    std::string particleModelFilename;
    // Child emitters are obtained from it.
    std::string childEmittersModelFilename; // Emitted as trail per particle.

    ParticleBlending blendingType = ParticleBlending::Opaque;
    ParticleEmitterType emitterType = ParticleEmitterType::Plane;
    u16 particleColorIndex = 0;

    std::array<fixed8_5, 2> multiTexScale; // Scale per layer
    i16 textureTilerotation;
    u16 rows;
    u16 columns;

    AnimationTrack<f32> emissionSpeed;
    AnimationTrack<f32> speedVariation;
    AnimationTrack<f32> verticalRange;
    AnimationTrack<f32> horizontalRange;
    AnimationTrack<f32> gravity;
    AnimationTrack<f32> lifespan;
    f32 lifespanVariation = 0.0f;
    AnimationTrack<f32> emissionRate;
    f32 emissionRateVariation = 0.0f;
    AnimationTrack<f32> emissionAreaWidth;
    AnimationTrack<f32> emissionAreaLength;
    AnimationTrack<f32> zSource;
    ParticleAnimationTrack<Vector3f> colorTrack;
    ParticleAnimationTrack<unorm16> alphaTrack;
    ParticleAnimationTrack<Vector2f> scaleTrack;
    Vector2f scaleVary = Vector2f(0.0f, 0.0f);
    ParticleAnimationTrack<unorm16> headUVScroll;
    ParticleAnimationTrack<unorm16> tailUVScroll;
    f32 tailLength = 0.0f;

    // Twinkle effect, causes twinkling like effects on particles.
    // they scale up and down randomly and are culled based on a thresshole.
    f32 twinkleSpeed = 0.0f;   // blinking speed
    f32 twinklePercent = 1.0f; // how visible is the particle. 1.0 - 100% time, 0.5 - 50% of time.
    Vector2f twinkleScale = Vector2f(0.0f, 0.0f); // min and max scale variation

    // Scales the velocity inherited from the parent particle.
    f32 inheritVelocityScale = 1.0f;

    // Drag effect, causes particles to slow down over time.
    f32 drag = 0.0f;

    // 2D Billboard spin rotation
    // Quad and Multitex Particles only
    f32 baseSpin = 0.0f;
    f32 baseSpinVariation = 0.0f;
    f32 spinSpeed = 0.0f;
    f32 spinSpeedVariation = 0.0f;

    // 3D Model particle rotation (ModelParticles only)
    M2Box tumble; // angularVelocity min and max

    // Static wind parameters, ignored if DynamicWind flag is set
    Vector3f windVector;
    f32 windTime = 0.0f;

    f32 followSpeed1 = 0.0f;
    f32 followScale1 = 0.0f;
    f32 followSpeed2 = 0.0f;
    f32 followScale2 = 0.0f;

    std::vector<Vector3f> splinePoints;
    AnimationTrack<u8> enabledIn;

    std::array<std::array<fixed16_9, 2>, 2> multiTexScrollMid{};
    std::array<std::array<fixed16_9, 2>, 2> multiTexScrollRange{};

    std::optional<ParticleEmitterExtension> extension;
};

struct Event {
    u32 identifier = 0;
    u32 data = 0;
    u32 boneId = 0;
    Vector3f position;
    AnimationTrackBase enabled;
};

} // namespace m2
} // namespace whiteout