polysmelt 0.17.0

Pure-Rust 3D asset interchange: glTF/GLB reading and writing, meshopt compression, and readers for common mesh formats, SketchUp included
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
//! One glTF document grafted into another under a node: every index the grafted one holds moved past what the
//! other already has.
#![allow(
    clippy::cast_possible_truncation,
    reason = "indices into a glTF document in memory fit in usize"
)]

use serde_json::{Map, Value, json};

use crate::document::Document;

/// Where a graft's nodes went, so a static one can be placed again sharing its meshes.
pub(super) struct Placed {
    first: usize,
    count: usize,
    roots: Vec<usize>,
    /// Whether it has no skins or animations, which name its nodes and so can't be shared.
    pub shareable: bool,
}

/// How many entries `json[key]` has.
fn len(json: &Map<String, Value>, key: &str) -> usize {
    json.get(key).and_then(Value::as_array).map_or(0, Vec::len)
}

/// How many punctual lights the document has.
fn lights(json: &Map<String, Value>) -> usize {
    json.get("extensions")
        .and_then(|e| e.get("KHR_lights_punctual"))
        .and_then(|l| l.get("lights"))
        .and_then(Value::as_array)
        .map_or(0, Vec::len)
}

/// The index counts of a document: where the grafted one's entries start.
#[derive(Clone, Copy, Default)]
struct Offsets {
    nodes: usize,
    meshes: usize,
    materials: usize,
    textures: usize,
    images: usize,
    samplers: usize,
    accessors: usize,
    views: usize,
    buffers: usize,
    skins: usize,
    cameras: usize,
    lights: usize,
    variants: usize,
}

impl Offsets {
    fn of(json: &Map<String, Value>) -> Self {
        Self {
            nodes: len(json, "nodes"),
            meshes: len(json, "meshes"),
            materials: len(json, "materials"),
            textures: len(json, "textures"),
            images: len(json, "images"),
            samplers: len(json, "samplers"),
            accessors: len(json, "accessors"),
            views: len(json, "bufferViews"),
            buffers: len(json, "buffers"),
            skins: len(json, "skins"),
            cameras: len(json, "cameras"),
            lights: lights(json),
            variants: json
                .get("extensions")
                .and_then(|e| e.get("KHR_materials_variants"))
                .and_then(|v| v.get("variants"))
                .and_then(Value::as_array)
                .map_or(0, Vec::len),
        }
    }
}

/// Adds `by` to the index at `v[key]`, when there is one.
fn bump(v: &mut Value, key: &str, by: usize) {
    if let Some(x) = v.get_mut(key)
        && let Some(i) = x.as_u64()
    {
        *x = json!(i as usize + by);
    }
}

/// Adds `by` to every index in the list at `v[key]`.
fn bump_all(v: &mut Value, key: &str, by: usize) {
    if let Some(Value::Array(list)) = v.get_mut(key) {
        for x in list {
            if let Some(i) = x.as_u64() {
                *x = json!(i as usize + by);
            }
        }
    }
}

/// Adds `by` to every value of the object at `v[key]` (a primitive's attributes, a morph target).
fn bump_values(v: &mut Value, by: usize) {
    if let Some(o) = v.as_object_mut() {
        for x in o.values_mut() {
            if let Some(i) = x.as_u64() {
                *x = json!(i as usize + by);
            }
        }
    }
}

/// Moves every texture reference (`{"index": n}` under a key ending in `Texture`) in a material.
fn bump_textures(v: &mut Value, by: usize) {
    match v {
        Value::Object(o) => {
            for (k, x) in o.iter_mut() {
                if k.ends_with("Texture") && x.is_object() {
                    bump(x, "index", by);
                }
                bump_textures(x, by);
            }
        }
        Value::Array(list) => list.iter_mut().for_each(|x| bump_textures(x, by)),
        _ => {}
    }
}

/// An animation pointer with the index it names moved.
fn pointer(p: &str, o: &Offsets) -> String {
    let rules: [(&str, usize); 6] = [
        ("/nodes/", o.nodes),
        ("/materials/", o.materials),
        ("/meshes/", o.meshes),
        ("/cameras/", o.cameras),
        ("/textures/", o.textures),
        ("/extensions/KHR_lights_punctual/lights/", o.lights),
    ];
    for (prefix, by) in rules {
        if let Some(rest) = p.strip_prefix(prefix) {
            let (num, tail) = rest.split_at(rest.find('/').unwrap_or(rest.len()));
            if let Ok(i) = num.parse::<usize>() {
                return format!("{prefix}{}{tail}", i + by);
            }
        }
    }
    p.to_owned()
}

/// The entries of `src[key]`, each changed by `f`, appended to `dst[key]`.
fn append(dst: &mut Map<String, Value>, src: &mut Map<String, Value>, key: &str, mut f: impl FnMut(&mut Value)) {
    let Some(Value::Array(mut list)) = src.remove(key) else {
        return;
    };
    if list.is_empty() {
        return;
    }
    list.iter_mut().for_each(&mut f);
    if let Value::Array(d) = dst.entry(key).or_insert_with(|| json!([])) {
        d.extend(list);
    }
}

/// Adds `child` to node `parent`'s children.
fn adopt(json: &mut Map<String, Value>, parent: usize, children: &[usize]) {
    if children.is_empty() {
        return;
    }
    if let Some(Value::Array(nodes)) = json.get_mut("nodes")
        && let Some(Value::Object(p)) = nodes.get_mut(parent)
        && let Value::Array(c) = p.entry("children").or_insert_with(|| json!([]))
    {
        c.extend(children.iter().map(|&i| json!(i)));
    }
}

/// Grafts `src` into `dst` under node `parent`. With `materials`, a list as long as the grafted document's
/// materials, its primitives use those of `dst` in their place.
pub(super) fn graft(dst: &mut Document, src: Document, parent: usize, materials: Option<&[usize]>) -> Placed {
    let o = Offsets::of(&dst.json);
    let Document {
        json: mut s,
        buffers,
        refs,
        lines_and_points,
        ..
    } = src;
    let materials = materials.filter(|m| m.len() == len(&s, "materials"));
    let map_material = |i: usize| materials.map_or(i + o.materials, |m| m[i]);
    let shareable = len(&s, "skins") == 0 && len(&s, "animations") == 0;
    let scene = s.get("scene").and_then(Value::as_u64).unwrap_or(0) as usize;
    let roots: Vec<usize> = s
        .get("scenes")
        .and_then(|v| v.get(scene))
        .and_then(|v| v.get("nodes"))
        .and_then(Value::as_array)
        .map(|r| {
            r.iter()
                .filter_map(Value::as_u64)
                .map(|i| i as usize + o.nodes)
                .collect()
        })
        .unwrap_or_default();
    let count = len(&s, "nodes");
    let d = &mut dst.json;

    append(d, &mut s, "nodes", |n| {
        bump_all(n, "children", o.nodes);
        bump(n, "mesh", o.meshes);
        bump(n, "skin", o.skins);
        bump(n, "camera", o.cameras);
        if let Some(e) = n.get_mut("extensions") {
            if let Some(l) = e.get_mut("KHR_lights_punctual") {
                bump(l, "light", o.lights);
            }
            if let Some(a) = e
                .get_mut("EXT_mesh_gpu_instancing")
                .and_then(|i| i.get_mut("attributes"))
            {
                bump_values(a, o.accessors);
            }
        }
    });
    append(d, &mut s, "meshes", |m| {
        let Some(Value::Array(prims)) = m.get_mut("primitives") else {
            return;
        };
        for p in prims {
            if let Some(a) = p.get_mut("attributes") {
                bump_values(a, o.accessors);
            }
            bump(p, "indices", o.accessors);
            if let Some(x) = p.get_mut("material")
                && let Some(i) = x.as_u64()
            {
                *x = json!(map_material(i as usize));
            }
            if let Some(Value::Array(targets)) = p.get_mut("targets") {
                for t in targets.iter_mut() {
                    bump_values(t, o.accessors);
                }
            }
            if let Some(e) = p.get_mut("extensions") {
                if let Some(draco) = e.get_mut("KHR_draco_mesh_compression") {
                    bump(draco, "bufferView", o.views);
                }
                if let Some(Value::Array(maps)) =
                    e.get_mut("KHR_materials_variants").and_then(|v| v.get_mut("mappings"))
                {
                    for map in maps {
                        if let Some(x) = map.get_mut("material")
                            && let Some(i) = x.as_u64()
                        {
                            *x = json!(map_material(i as usize));
                        }
                        bump_all(map, "variants", o.variants);
                    }
                }
            }
        }
    });
    if materials.is_some() {
        s.remove("materials");
    } else {
        append(d, &mut s, "materials", |m| bump_textures(m, o.textures));
    }
    append(d, &mut s, "textures", |t| {
        bump(t, "source", o.images);
        bump(t, "sampler", o.samplers);
        if let Some(Value::Object(e)) = t.get_mut("extensions") {
            e.values_mut().for_each(|x| bump(x, "source", o.images));
        }
    });
    append(d, &mut s, "images", |i| bump(i, "bufferView", o.views));
    append(d, &mut s, "samplers", |_| {});
    append(d, &mut s, "accessors", |a| {
        bump(a, "bufferView", o.views);
        if let Some(sp) = a.get_mut("sparse") {
            for part in ["indices", "values"] {
                if let Some(x) = sp.get_mut(part) {
                    bump(x, "bufferView", o.views);
                }
            }
        }
    });
    append(d, &mut s, "bufferViews", |v| {
        bump(v, "buffer", o.buffers);
        if let Some(Value::Object(e)) = v.get_mut("extensions") {
            e.values_mut().for_each(|x| bump(x, "buffer", o.buffers));
        }
    });
    append(d, &mut s, "buffers", |_| {});
    append(d, &mut s, "skins", |k| {
        bump(k, "inverseBindMatrices", o.accessors);
        bump(k, "skeleton", o.nodes);
        bump_all(k, "joints", o.nodes);
    });
    append(d, &mut s, "cameras", |_| {});
    append(d, &mut s, "animations", |a| {
        if let Some(Value::Array(samplers)) = a.get_mut("samplers") {
            for x in samplers {
                bump(x, "input", o.accessors);
                bump(x, "output", o.accessors);
            }
        }
        if let Some(Value::Array(channels)) = a.get_mut("channels") {
            for c in channels {
                let Some(t) = c.get_mut("target") else { continue };
                bump(t, "node", o.nodes);
                if let Some(Value::String(p)) = t.pointer_mut("/extensions/KHR_animation_pointer/pointer") {
                    *p = pointer(p, &o);
                }
            }
        }
    });

    // Root extensions: the lights and variants are lists to join; the names used, a set.
    if let Some(Value::Object(mut ext)) = s.remove("extensions") {
        for (name, list) in [
            ("KHR_lights_punctual", "lights"),
            ("KHR_materials_variants", "variants"),
        ] {
            let Some(Value::Array(items)) = ext.remove(name).and_then(|mut e| e.get_mut(list).map(Value::take)) else {
                continue;
            };
            let de = d.entry("extensions").or_insert_with(|| json!({}));
            if let Some(de) = de.as_object_mut() {
                let entry = de
                    .entry(name)
                    .or_insert_with(|| Value::Object(Map::from_iter([(list.to_owned(), json!([]))])));
                if let Some(Value::Array(dl)) = entry.get_mut(list) {
                    dl.extend(items);
                }
            }
        }
    }
    for key in ["extensionsUsed", "extensionsRequired"] {
        if let Some(Value::Array(names)) = s.remove(key)
            && let Value::Array(dl) = d.entry(key).or_insert_with(|| json!([]))
        {
            for n in names {
                if !dl.contains(&n) {
                    dl.push(n);
                }
            }
        }
    }
    adopt(d, parent, &roots);
    dst.buffers.extend(buffers);
    dst.refs.extend(refs.into_iter().map(|(i, r)| (i + o.images, r)));
    dst.lines_and_points |= lines_and_points;
    Placed {
        first: o.nodes,
        count,
        roots,
        shareable,
    }
}

/// Places a static graft again under node `parent`: its nodes copied, sharing its meshes.
pub(super) fn repeat(dst: &mut Document, placed: &Placed, parent: usize) {
    let at = len(&dst.json, "nodes");
    let Some(Value::Array(nodes)) = dst.json.get_mut("nodes") else {
        return;
    };
    let Some(copies) = nodes
        .get(placed.first..placed.first + placed.count)
        .map(<[Value]>::to_vec)
    else {
        return;
    };
    for mut n in copies {
        if let Some(Value::Array(c)) = n.get_mut("children") {
            for x in c {
                if let Some(i) = x.as_u64() {
                    *x = json!(i as usize - placed.first + at);
                }
            }
        }
        nodes.push(n);
    }
    let roots: Vec<usize> = placed.roots.iter().map(|r| r - placed.first + at).collect();
    adopt(&mut dst.json, parent, &roots);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn moves_pointers() {
        let o = Offsets {
            nodes: 3,
            materials: 2,
            lights: 1,
            ..Offsets::default()
        };
        assert_eq!(pointer("/nodes/0/translation", &o), "/nodes/3/translation");
        assert_eq!(
            pointer("/materials/1/pbrMetallicRoughness/baseColorFactor", &o),
            "/materials/3/pbrMetallicRoughness/baseColorFactor"
        );
        assert_eq!(
            pointer("/extensions/KHR_lights_punctual/lights/0/color", &o),
            "/extensions/KHR_lights_punctual/lights/1/color"
        );
        let mut m = json!({ "normalTexture": { "index": 0 }, "extensions": { "KHR_materials_clearcoat": { "clearcoatTexture": { "index": 1 } } } });
        bump_textures(&mut m, 5);
        assert_eq!(m["normalTexture"]["index"], 5);
        assert_eq!(
            m["extensions"]["KHR_materials_clearcoat"]["clearcoatTexture"]["index"],
            6
        );
    }
}