polysmelt 0.11.1

Pure-Rust 3D asset interchange: glTF/GLB reading and writing, meshopt compression, and readers for common mesh formats
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
//! Finding the files a model refers to (MTL libraries, textures) and turning texture bytes into
//! embeddable images.

use std::borrow::Cow;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use crate::mesh::Image;

/// Where a reader finds the files a model refers to, and how it turns texture files into
/// images it can embed.
pub trait Assets {
    /// The file a model in `model_dir` refers to as `reference`: a relative path, a bare name,
    /// or another machine's absolute path.
    fn find(&self, reference: &str, model_dir: &Path) -> Option<PathBuf>;

    /// The bytes of a file [`find`](Self::find) returned, or of one a model names exactly (a
    /// glTF buffer, a USD sublayer). Reads the file by default.
    #[cfg(feature = "fs")]
    fn read(&self, path: &Path) -> Option<Cow<'_, [u8]>> {
        std::fs::read(path).ok().map(Cow::Owned)
    }

    /// The bytes of a file [`find`](Self::find) returned, or of one a model names exactly (a
    /// glTF buffer, a USD sublayer). Without the `fs` feature there is no default: the bytes
    /// come from wherever the assets keep them.
    #[cfg(not(feature = "fs"))]
    fn read(&self, path: &Path) -> Option<Cow<'_, [u8]>>;

    /// Texture bytes as an embeddable image; `name` is the file's name or path, for its format.
    /// By default PNG and JPEG pass, and WebP is re-encoded as PNG with the `webp` feature.
    /// Override to convert others (TGA, BMP, DDS, PSD, ...).
    fn image(&self, bytes: &[u8], name: &Path) -> Option<Image> {
        let _ = name;
        Image::png_or_jpeg(bytes).or_else(|| Image::webp(bytes))
    }

    /// A texture file read as an embeddable image.
    fn load_image(&self, path: &Path) -> Option<Image> {
        self.image(&self.read(path)?, path)
    }

    /// A color texture for a model at `model` that names none, by naming convention, from the
    /// model's part of the pack: its folder's parent and below (`Models/` and `Textures/` side by
    /// side), so a sprite of the same name in another kit of a bundle isn't taken. `None` by
    /// default.
    fn color_texture_for(&self, model: &Path) -> Option<PathBuf> {
        let _ = model;
        None
    }
}

/// Assets in a folder tree laid out like the pack a model came in: references are tried next to
/// the model, then found anywhere under `root` by file name (or the same name in another image
/// format). A model naming no color texture takes one by name from its part of the pack:
/// `Barrel.png` or `Barrel_Albedo.png` for `Barrel.obj`.
#[cfg(feature = "fs")]
#[derive(Debug, Clone)]
pub struct Folder {
    /// The top of the tree searched.
    pub root: PathBuf,
}

#[cfg(feature = "fs")]
impl Folder {
    /// A folder tree rooted at `root`.
    pub fn new(root: impl Into<PathBuf>) -> Self {
        Self { root: root.into() }
    }
}

#[cfg(feature = "fs")]
impl Assets for Folder {
    fn find(&self, reference: &str, model_dir: &Path) -> Option<PathBuf> {
        find_texture(reference, model_dir, &self.root)
    }

    fn color_texture_for(&self, model: &Path) -> Option<PathBuf> {
        // The model's folder's parent, when that's still inside the pack.
        let home = model
            .parent()
            .and_then(Path::parent)
            .filter(|h| h.starts_with(&self.root))
            .unwrap_or(&self.root);
        color_texture_by_name(model, |visit| walk_images(home, visit))
    }
}

/// Files held in memory by path: assets for loading without file access, found the way
/// [`Folder`] finds them on disk. Paths are relative to the top of the pack and `/`-separated
/// (`\` and a leading `./` or `/` read the same). A reference is tried next to the model, then
/// anywhere by file name, then as the same name in another image format; a model naming no
/// color texture takes one by name.
#[derive(Debug, Clone, Default)]
pub struct Memory {
    files: BTreeMap<String, Vec<u8>>,
}

impl Memory {
    /// No files.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds (or replaces) the file at `path`.
    pub fn insert(&mut self, path: &str, bytes: impl Into<Vec<u8>>) -> &mut Self {
        self.files.insert(memory_key(path), bytes.into());
        self
    }

    /// With the file at `path` added (or replaced).
    #[must_use]
    pub fn with(mut self, path: &str, bytes: impl Into<Vec<u8>>) -> Self {
        self.insert(path, bytes);
        self
    }

    /// The file at `path`.
    pub fn get(&self, path: &str) -> Option<&[u8]> {
        self.files.get(&memory_key(path)).map(Vec::as_slice)
    }

    /// The files' paths, normalized, in order.
    pub fn paths(&self) -> impl Iterator<Item = &str> {
        self.files.keys().map(String::as_str)
    }

    fn images(&self, visit: &mut dyn FnMut(&Path, &str) -> bool) {
        for k in self.files.keys() {
            let p = Path::new(k);
            if let Some(stem) = image_stem(p)
                && visit(p, &stem)
            {
                return;
            }
        }
    }
}

impl Assets for Memory {
    fn find(&self, reference: &str, model_dir: &Path) -> Option<PathBuf> {
        let cleaned = reference.trim().replace('\\', "/");
        if cleaned.is_empty() {
            return None;
        }
        if !cleaned.starts_with('/') && !cleaned.contains(':') {
            let rel = memory_key(&format!("{}/{cleaned}", model_dir.to_string_lossy()));
            if self.files.contains_key(&rel) {
                return Some(rel.into());
            }
        }
        let name = cleaned.rsplit('/').next()?.to_lowercase();
        let by_name = self
            .files
            .keys()
            .find(|k| k.rsplit('/').next().is_some_and(|n| n.to_lowercase() == name));
        if let Some(k) = by_name {
            return Some(k.into());
        }
        same_name_other_format(&name, |visit| self.images(visit))
    }

    fn read(&self, path: &Path) -> Option<Cow<'_, [u8]>> {
        self.files
            .get(&memory_key(&path.to_string_lossy()))
            .map(|b| Cow::Borrowed(b.as_slice()))
    }

    fn color_texture_for(&self, model: &Path) -> Option<PathBuf> {
        // The model's folder's parent: `a/b` for `a/b/Models/Barrel.obj`.
        let key = memory_key(&model.to_string_lossy());
        let home = key.rsplitn(3, '/').nth(2).map(|h| format!("{h}/")).unwrap_or_default();
        color_texture_by_name(model, |visit| {
            self.images(&mut |p, stem| p.to_string_lossy().starts_with(&home) && visit(p, stem));
        })
    }
}

/// A path as a [`Memory`] keys it: `/`-separated, without empty or `.` parts, and `..` stepping
/// up (no further than the top).
pub(crate) fn memory_key(path: &str) -> String {
    let mut parts: Vec<&str> = Vec::new();
    for part in path.split(['/', '\\']) {
        match part {
            "" | "." => {}
            ".." => {
                parts.pop();
            }
            p => parts.push(p),
        }
    }
    parts.join("/")
}

/// A model's color texture by its name, from the images `walk` visits (until told to stop).
fn color_texture_by_name(model: &Path, walk: impl FnOnce(&mut dyn FnMut(&Path, &str) -> bool)) -> Option<PathBuf> {
    let stem = model.file_stem()?.to_string_lossy().to_lowercase();
    if stem.len() < 3 || stem.contains('@') {
        return None;
    }
    // A name that says it's the color wins; one that says nothing ("Barrel.png") will do.
    let mut plain = None;
    let mut color = None;
    walk(&mut |p, file_stem| {
        let Some(rest) = file_stem.strip_prefix(&stem) else {
            return false;
        };
        if !(rest.is_empty() || rest.starts_with(['_', '-', ' ', '.'])) {
            return false;
        }
        match texture_role(file_stem) {
            Some(Role::Color) if color.is_none() => color = Some(p.to_path_buf()),
            None if plain.is_none() => plain = Some(p.to_path_buf()),
            _ => {}
        }
        color.is_some()
    });
    color.or(plain)
}

/// An image with the same name as `name` (lowercase) in another format (`rock.png` for
/// `rock.tga`, as packs often ship), preferring PNG and JPEG, from the images `walk` visits.
fn same_name_other_format(name: &str, walk: impl FnOnce(&mut dyn FnMut(&Path, &str) -> bool)) -> Option<PathBuf> {
    let (stem, ext) = name.rsplit_once('.')?;
    if !IMAGE_EXTS.contains(&ext) {
        return None;
    }
    let mut best: Option<(u8, PathBuf)> = None;
    walk(&mut |p, s| {
        if s == stem {
            let ext = p
                .extension()
                .map(|x| x.to_string_lossy().to_lowercase())
                .unwrap_or_default();
            let rank = u8::from(!matches!(ext.as_str(), "png" | "jpg" | "jpeg"));
            if best.as_ref().is_none_or(|(r, _)| rank < *r) {
                best = Some((rank, p.to_path_buf()));
            }
        }
        best.as_ref().is_some_and(|(r, _)| *r == 0)
    });
    best.map(|(_, p)| p)
}

/// An image file's lowercased stem; `None` for a file that isn't an image.
fn image_stem(p: &Path) -> Option<String> {
    let ext = p.extension()?.to_string_lossy().to_lowercase();
    IMAGE_EXTS.contains(&ext.as_str()).then(|| {
        p.file_stem()
            .map(|x| x.to_string_lossy().to_lowercase())
            .unwrap_or_default()
    })
}

#[derive(Debug, PartialEq, Eq)]
enum Role {
    Color,
    Other,
}

/// What a texture's name says it holds: `None` when it says nothing.
fn texture_role(stem: &str) -> Option<Role> {
    let tail = stem.rsplit(['-', '_', '.', ' ']).next().unwrap_or(stem);
    let split = tail.len() < stem.len();
    let ends = |list: &[&str]| split && list.contains(&tail);
    let has = |list: &[&str]| list.iter().any(|w| stem.contains(w));
    if has(&[
        "normal",
        "nrm",
        "emissi",
        "emit",
        "glow",
        "rough",
        "metal",
        "occlusion",
        "height",
        "disp",
        "bump",
    ]) || has(&["mask", "opacity", "alpha", "spec", "gloss", "smooth"])
        || ends(&[
            "n", "nor", "norm", "e", "r", "m", "mt", "ao", "occ", "h", "orm", "arm", "mra", "rma", "s", "g",
        ])
    {
        return Some(Role::Other);
    }
    if has(&[
        "albedo",
        "basecolor",
        "base_color",
        "base color",
        "basecolour",
        "diffuse",
    ]) || ends(&["diff", "dif", "col", "color", "colour", "d", "bc", "a", "c"])
    {
        return Some(Role::Color);
    }
    None
}

const IMAGE_EXTS: &[&str] = &[
    "png", "jpg", "jpeg", "tga", "bmp", "webp", "gif", "tif", "tiff", "dds", "psd",
];

/// Visit the image files under `root` (at most 20 000 entries), with their lowercased stems,
/// until `visit` says stop.
#[cfg(feature = "fs")]
fn walk_images(root: &Path, visit: &mut dyn FnMut(&Path, &str) -> bool) {
    let mut stack = vec![root.to_path_buf()];
    let mut seen = 0;
    while let Some(dir) = stack.pop() {
        let Ok(entries) = std::fs::read_dir(&dir) else {
            continue;
        };
        for e in entries.flatten() {
            seen += 1;
            if seen > 20_000 {
                return;
            }
            let p = e.path();
            if p.is_dir() {
                stack.push(p);
                continue;
            }
            if let Some(stem) = image_stem(&p)
                && visit(&p, &stem)
            {
                return;
            }
        }
    }
}

/// Find a texture a model refers to: next to it, relative to it, or anywhere under `root` by
/// file name (artists' absolute paths like `C:\Users\me\tex\rock.tga` survive only as names).
/// Failing that, an image with the same name in another format.
#[cfg(feature = "fs")]
fn find_texture(reference: &str, model_dir: &Path, root: &Path) -> Option<PathBuf> {
    let cleaned = reference.trim().replace('\\', "/");
    if cleaned.is_empty() {
        return None;
    }
    let rel = model_dir.join(&cleaned);
    if !cleaned.starts_with('/') && !cleaned.contains(':') && !cleaned.split('/').any(|c| c == "..") && rel.is_file() {
        return Some(rel);
    }
    let name = cleaned.rsplit('/').next()?.to_lowercase();
    let mut stack = vec![root.to_path_buf()];
    let mut seen = 0;
    while let Some(dir) = stack.pop() {
        let Ok(entries) = std::fs::read_dir(&dir) else {
            continue;
        };
        for e in entries.flatten() {
            seen += 1;
            if seen > 20_000 {
                return None;
            }
            let p = e.path();
            if p.is_dir() {
                stack.push(p);
            } else if p
                .file_name()
                .is_some_and(|n| n.to_string_lossy().to_lowercase() == name)
            {
                return Some(p);
            }
        }
    }
    same_name_other_format(&name, |visit| walk_images(root, visit))
}

#[cfg(all(test, feature = "fs"))]
mod tests {
    use super::*;

    #[test]
    fn texture_roles_by_name() {
        assert_eq!(texture_role("barrel_normal"), Some(Role::Other));
        assert_eq!(texture_role("barrel_d"), Some(Role::Color));
        assert_eq!(texture_role("barrel_albedo"), Some(Role::Color));
        assert_eq!(texture_role("barrel"), None);
    }

    #[test]
    fn memory_finds_like_a_folder() {
        let m = Memory::new()
            .with("Models/crate.obj", "")
            .with("Models\\textures\\Crate.PNG", "png")
            .with("textures/rock.jpg", "jpg")
            .with("Barrel_Albedo.png", "a")
            .with("Barrel_Normal.png", "n");
        let found = |r: &str| m.find(r, Path::new("Models")).map(|p| p.to_string_lossy().into_owned());
        let krate = Some("Models/textures/Crate.PNG");
        assert_eq!(found("textures/Crate.PNG").as_deref(), krate);
        assert_eq!(found("./textures\\Crate.PNG").as_deref(), krate);
        assert_eq!(found("D:\\Work\\crate.png").as_deref(), krate);
        assert_eq!(found("../textures/rock.jpg").as_deref(), Some("textures/rock.jpg"));
        assert_eq!(found("rock.tga").as_deref(), Some("textures/rock.jpg"));
        assert_eq!(found("missing.png"), None);
        assert_eq!(
            m.read(Path::new("./Models/textures/Crate.PNG")).as_deref(),
            Some(&b"png"[..])
        );
        assert_eq!(
            m.color_texture_for(Path::new("Barrel.obj")),
            Some(PathBuf::from("Barrel_Albedo.png"))
        );
    }

    #[test]
    fn color_textures_by_name_come_from_the_models_part_of_the_pack() {
        let m = Memory::new()
            .with("Bundle/Kit2D/Sprites/Stairs.png", "2d")
            .with("Bundle/Kit3D/Textures/Crate.png", "3d");
        let guess = |model: &str| m.color_texture_for(Path::new(model));
        assert_eq!(guess("Bundle/Kit3D/Models/Stairs.fbx"), None);
        assert_eq!(
            guess("Bundle/Kit3D/Models/Crate.fbx"),
            Some(PathBuf::from("Bundle/Kit3D/Textures/Crate.png"))
        );
        assert_eq!(
            guess("Models/Stairs.fbx"),
            Some(PathBuf::from("Bundle/Kit2D/Sprites/Stairs.png")),
            "a model near the top searches the whole pack"
        );
    }
}