whiteoutlib 0.2.0

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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Fernando Sahmkow

#include <whiteout/models/wem/materials/common.h>

namespace whiteout {
namespace models {
namespace wem {

// ============================================================================
// Matrix3x2f
// ============================================================================

bool Matrix3x2f::isIdentity() const {
    return m[0][0] == 1.0f && m[0][1] == 0.0f && m[0][2] == 0.0f && m[1][0] == 0.0f &&
           m[1][1] == 1.0f && m[1][2] == 0.0f;
}

Vector2f Matrix3x2f::apply(const Vector2f& uv) const {
    return Vector2f{m[0][0] * uv.x + m[0][1] * uv.y + m[0][2],
                    m[1][0] * uv.x + m[1][1] * uv.y + m[1][2]};
}

// ============================================================================
// Names
// ============================================================================

const char* ToString(UVMappingMode mode) {
    switch (mode) {
    case UVMappingMode::ExplicitUV:
        return "explicit_uv";
    case UVMappingMode::EnvSphere:
        return "env_sphere";
    case UVMappingMode::EnvCube:
        return "env_cube";
    }
    return "invalid";
}

const char* ToString(SurfaceChannel channel) {
    switch (channel) {
    case SurfaceChannel::Color:
        return "color";
    case SurfaceChannel::Emissive:
        return "emissive";
    case SurfaceChannel::Specular:
        return "specular";
    case SurfaceChannel::Normal:
        return "normal";
    case SurfaceChannel::AmbientOcclusion:
        return "ambient_occlusion";
    case SurfaceChannel::Environment:
        return "environment";
    case SurfaceChannel::Coverage:
        return "coverage";
    case SurfaceChannel::Gloss:
        return "gloss";
    case SurfaceChannel::Count:
        break;
    }
    return "invalid";
}

const char* ToString(CompositeOp op) {
    switch (op) {
    case CompositeOp::Set:
        return "set";
    case CompositeOp::AlphaKey:
        return "alpha_key";
    case CompositeOp::AlphaBlend:
        return "alpha_blend";
    case CompositeOp::Add:
        return "add";
    case CompositeOp::AddAlpha:
        return "add_alpha";
    case CompositeOp::Modulate:
        return "modulate";
    case CompositeOp::Modulate2x:
        return "modulate_2x";
    case CompositeOp::Count:
        break;
    }
    return "invalid";
}

const char* ToString(CombinerOp op) {
    switch (op) {
    case CombinerOp::Opaque:
        return "opaque";
    case CombinerOp::Mod:
        return "mod";
    case CombinerOp::Mod2x:
        return "mod2x";
    case CombinerOp::Add:
        return "add";
    case CombinerOp::Decal:
        return "decal";
    case CombinerOp::Fade:
        return "fade";
    case CombinerOp::Pass:
        return "pass";
    case CombinerOp::AddAlpha:
        return "add_alpha";
    case CombinerOp::MaskedMod:
        return "masked_mod";
    case CombinerOp::MaskedMod2x:
        return "masked_mod2x";
    case CombinerOp::Count:
        break;
    }
    return "invalid";
}

const char* ToString(LegacySlot slot) {
    switch (slot) {
    case LegacySlot::Diffuse:
        return "diffuse";
    case LegacySlot::Normal:
        return "normal";
    case LegacySlot::Specular:
        return "specular";
    case LegacySlot::Gloss:
        return "gloss";
    case LegacySlot::Emissive:
        return "emissive";
    case LegacySlot::Environment:
        return "environment";
    case LegacySlot::AmbientOcclusion:
        return "ambient_occlusion";
    case LegacySlot::Height:
        return "height";
    case LegacySlot::Lightmap:
        return "lightmap";
    case LegacySlot::Detail:
        return "detail";
    case LegacySlot::Count:
        break;
    }
    return "invalid";
}

const char* ToString(PbrSlot slot) {
    switch (slot) {
    case PbrSlot::BaseColor:
        return "base_color";
    case PbrSlot::Normal:
        return "normal";
    case PbrSlot::Orm:
        return "orm";
    case PbrSlot::Metallic:
        return "metallic";
    case PbrSlot::Roughness:
        return "roughness";
    case PbrSlot::AmbientOcclusion:
        return "ambient_occlusion";
    case PbrSlot::Emissive:
        return "emissive";
    case PbrSlot::Environment:
        return "environment";
    case PbrSlot::TeamColorMask:
        return "team_color_mask";
    case PbrSlot::Count:
        break;
    }
    return "invalid";
}

const char* ToString(FeatureKind kind) {
    switch (kind) {
    case FeatureKind::Fresnel:
        return "fresnel";
    case FeatureKind::UvAnimation:
        return "uv_animation";
    case FeatureKind::LayerShading:
        return "layer_shading";
    case FeatureKind::Count:
        break;
    }
    return "invalid";
}

// ============================================================================
// Features (features.h)
// ============================================================================

const MaterialFeature* FindFeature(const std::vector<MaterialFeature>& features, FeatureKind kind,
                                   u32 layer) {
    for (const MaterialFeature& feature : features) {
        if (feature.kind() == kind && feature.layer == layer) {
            return &feature;
        }
    }
    return nullptr;
}

u32 NextFeatureId(const std::vector<MaterialFeature>& features) {
    u32 next = 0;
    for (const MaterialFeature& feature : features) {
        if (feature.id >= next) {
            next = feature.id + 1;
        }
    }
    return next;
}

// ============================================================================
// Bodies
// ============================================================================

std::vector<u32> CompositeBody::layersOf(SurfaceChannel channel) const {
    std::vector<u32> out;
    for (std::size_t i = 0; i < layers.size(); ++i) {
        if (layers[i].target == channel) {
            out.push_back(static_cast<u32>(i));
        }
    }
    return out;
}

namespace {

/// One lookup shared by both deferred bodies; they differ only in the key type.
template <class Slot>
const TextureInput* findSlot(const std::vector<std::pair<Slot, TextureInput>>& slots, Slot slot) {
    for (const auto& entry : slots) {
        if (entry.first == slot) {
            return &entry.second;
        }
    }
    return nullptr;
}

template <class Slot>
TextureInput& setSlot(std::vector<std::pair<Slot, TextureInput>>& slots, Slot slot,
                      const TextureInput& input) {
    for (auto& entry : slots) {
        if (entry.first == slot) {
            entry.second = input;
            return entry.second;
        }
    }
    slots.emplace_back(slot, input);
    return slots.back().second;
}

} // namespace

const TextureInput* LegacyDeferredBody::find(LegacySlot slot) const {
    return findSlot(slots, slot);
}

TextureInput& LegacyDeferredBody::set(LegacySlot slot, const TextureInput& input) {
    return setSlot(slots, slot, input);
}

const TextureInput* PbrDeferredBody::find(PbrSlot slot) const {
    return findSlot(slots, slot);
}

TextureInput& PbrDeferredBody::set(PbrSlot slot, const TextureInput& input) {
    return setSlot(slots, slot, input);
}

// ============================================================================
// CommonMaterial
// ============================================================================

void CommonMaterial::setKind(MaterialKind kind) {
    switch (kind) {
    case MaterialKind::Composite:
        body = CompositeBody{};
        return;
    case MaterialKind::Combiners:
        body = CombinersBody{};
        return;
    case MaterialKind::LegacyDeferred:
        body = LegacyDeferredBody{};
        return;
    case MaterialKind::PBRDeferred:
        body = PbrDeferredBody{};
        return;
    case MaterialKind::Count:
        break;
    }
}

u32 CommonMaterial::ordinalCount() const {
    if (const CompositeBody* composite = std::get_if<CompositeBody>(&body)) {
        return static_cast<u32>(composite->layers.size());
    }
    if (const CombinersBody* combiners = std::get_if<CombinersBody>(&body)) {
        return static_cast<u32>(combiners->stages.size());
    }
    if (const LegacyDeferredBody* legacy = std::get_if<LegacyDeferredBody>(&body)) {
        return static_cast<u32>(legacy->slots.size());
    }
    if (const PbrDeferredBody* pbrBody = std::get_if<PbrDeferredBody>(&body)) {
        return static_cast<u32>(pbrBody->slots.size());
    }
    return 0;
}

const TextureInput* CommonMaterial::inputAt(u32 ordinal) const {
    return const_cast<CommonMaterial*>(this)->inputAt(ordinal);
}

TextureInput* CommonMaterial::inputAt(u32 ordinal) {
    if (ordinal >= ordinalCount()) {
        return nullptr;
    }
    const std::size_t index = static_cast<std::size_t>(ordinal);
    if (CompositeBody* composite = std::get_if<CompositeBody>(&body)) {
        return &composite->layers[index].input;
    }
    if (CombinersBody* combiners = std::get_if<CombinersBody>(&body)) {
        return &combiners->stages[index].input;
    }
    if (LegacyDeferredBody* legacy = std::get_if<LegacyDeferredBody>(&body)) {
        return &legacy->slots[index].second;
    }
    if (PbrDeferredBody* pbrBody = std::get_if<PbrDeferredBody>(&body)) {
        return &pbrBody->slots[index].second;
    }
    return nullptr;
}

// ============================================================================
// Colour space
// ============================================================================

ColorSpace AutoColorSpaceFor(SurfaceChannel channel) {
    switch (channel) {
    case SurfaceChannel::Color:
    case SurfaceChannel::Emissive:
    case SurfaceChannel::Specular:
    case SurfaceChannel::Environment:
        return ColorSpace::Srgb;
    case SurfaceChannel::Normal:
    case SurfaceChannel::AmbientOcclusion:
    case SurfaceChannel::Coverage:
    case SurfaceChannel::Gloss:
    case SurfaceChannel::Count:
        break;
    }
    return ColorSpace::Linear;
}

ColorSpace AutoColorSpaceFor(LegacySlot slot) {
    switch (slot) {
    case LegacySlot::Diffuse:
    case LegacySlot::Specular:
    case LegacySlot::Emissive:
    case LegacySlot::Environment:
    case LegacySlot::Lightmap:
    case LegacySlot::Detail:
        return ColorSpace::Srgb;
    case LegacySlot::Normal:
    case LegacySlot::Gloss:
    case LegacySlot::AmbientOcclusion:
    case LegacySlot::Height:
    case LegacySlot::Count:
        break;
    }
    return ColorSpace::Linear;
}

ColorSpace AutoColorSpaceFor(PbrSlot slot) {
    switch (slot) {
    case PbrSlot::BaseColor:
    case PbrSlot::Emissive:
    case PbrSlot::Environment:
        return ColorSpace::Srgb;
    case PbrSlot::Normal:
    case PbrSlot::Orm:
    case PbrSlot::Metallic:
    case PbrSlot::Roughness:
    case PbrSlot::AmbientOcclusion:
    // A team-colour mask is a coverage value, not a colour: it selects how much
    // of the team tint lands, and tone-mapping it would bend the ramp.
    case PbrSlot::TeamColorMask:
    case PbrSlot::Count:
        break;
    }
    return ColorSpace::Linear;
}

ColorSpace AutoColorSpaceForCombinerStage() {
    return ColorSpace::Srgb;
}

ColorSpace ResolvedColorSpace(const TextureInput& input, SurfaceChannel channel) {
    return input.colorSpace == ColorSpace::Auto ? AutoColorSpaceFor(channel) : input.colorSpace;
}

ColorSpace ResolvedColorSpace(const TextureInput& input, LegacySlot slot) {
    return input.colorSpace == ColorSpace::Auto ? AutoColorSpaceFor(slot) : input.colorSpace;
}

ColorSpace ResolvedColorSpace(const TextureInput& input, PbrSlot slot) {
    return input.colorSpace == ColorSpace::Auto ? AutoColorSpaceFor(slot) : input.colorSpace;
}

// ============================================================================
// Flatten
// ============================================================================

namespace {

std::optional<LegacySlot> legacySlotFor(SurfaceChannel channel) {
    switch (channel) {
    case SurfaceChannel::Color:
        return LegacySlot::Diffuse;
    case SurfaceChannel::Emissive:
        return LegacySlot::Emissive;
    case SurfaceChannel::Specular:
        return LegacySlot::Specular;
    case SurfaceChannel::Normal:
        return LegacySlot::Normal;
    case SurfaceChannel::AmbientOcclusion:
        return LegacySlot::AmbientOcclusion;
    case SurfaceChannel::Environment:
        return LegacySlot::Environment;
    case SurfaceChannel::Gloss:
        return LegacySlot::Gloss;
    // Per-texel opacity has no slot in a slot map, so a stack that carries a
    // coverage layer is not degenerate and `Flatten` refuses it.
    case SurfaceChannel::Coverage:
    case SurfaceChannel::Count:
        break;
    }
    return std::nullopt;
}

} // namespace

std::optional<LegacyDeferredBody> Flatten(const CompositeBody& composite) {
    LegacyDeferredBody out;
    bool seen[static_cast<std::size_t>(SurfaceChannel::Count)] = {};

    for (const CompositeLayer& layer : composite.layers) {
        const std::size_t channel = static_cast<std::size_t>(layer.target);
        if (channel >= static_cast<std::size_t>(SurfaceChannel::Count)) {
            return std::nullopt;
        }
        // Degenerate means "already a slot map": one layer per channel, and that
        // layer replacing rather than folding. Anything else has an evaluation
        // order a slot map cannot hold.
        if (seen[channel] || layer.op != CompositeOp::Set) {
            return std::nullopt;
        }
        const std::optional<LegacySlot> slot = legacySlotFor(layer.target);
        if (!slot.has_value()) {
            return std::nullopt;
        }
        seen[channel] = true;
        out.set(*slot, layer.input);
    }

    out.diffuseFactor = composite.diffuseFactor;
    out.emissiveFactor = composite.emissiveFactor;
    out.specularFactor = composite.specularFactor;
    out.specularExponent = composite.specularExponent;
    out.environmentFactor = composite.environmentFactor;
    return out;
}

} // namespace wem
} // namespace models
} // namespace whiteout