concinnity-cook 0.19.16

Authored world model, validation, and the asset cook pipeline that bakes a Concinnity world into a blob
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
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
//! glTF-sourced Mesh and SkinnedMesh assets: the referenced `.glb`/`.gltf` is
//! parsed once per source and its geometry, skeleton, and morph targets land in
//! the asset's args.

use std::path::Path;

use crate::authoring::world::WorldJsonlAsset;

use super::super::pack::MeshCacheEntry;
use super::super::{MESH_TYPE, SKINNED_MESH_TYPE};
use super::skin_index_arg;

// Expand glTF-sourced SkinnedMesh assets in place: parse the referenced .glb
// and write the imported geometry + skeleton into the asset's inline
// `vertices` / `indices` / `skeleton` args. A SkinnedMesh with no `source` is
// left untouched, so an inline-authored mesh is byte-for-byte unchanged;
// `.fbx` sources belong to `desugar_fbx_skinned_meshes`.
// Skips an asset whose cache probe found a precompiled payload: there is no
// reason to parse the .glb when the bytes are already in hand.
pub(in crate::pipeline) fn desugar_gltf_skinned_meshes(
    assets: &mut [WorldJsonlAsset],
    mesh_cache: &std::collections::HashMap<String, MeshCacheEntry>,
    assets_dir: Option<&Path>,
) -> std::io::Result<()> {
    for asset in assets.iter_mut() {
        if asset.asset_type != SKINNED_MESH_TYPE {
            continue;
        }
        let source = asset
            .args
            .get("source")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let character_model = asset.args.get("character_model").cloned();
        if character_model.is_none()
            && (source.is_empty() || source.to_lowercase().ends_with(".fbx"))
        {
            continue;
        }
        // Cache probe found a compiled payload for this asset, no need
        // to parse the .glb. compile_and_pack_payloads will use the bytes
        // directly. Leave the args un-desugared so they keep matching the
        // pre-desugar cache key on the next build.
        if matches!(
            mesh_cache.get(&asset.name),
            Some(MeshCacheEntry { bytes: Some(_), .. })
        ) {
            continue;
        }

        let invalid = |msg: String| std::io::Error::new(std::io::ErrorKind::InvalidData, msg);
        let imported = match character_model {
            Some(arg) => {
                let arg: crate::compile::character::import::CharacterModelArg =
                    serde_json::from_value(arg).map_err(|e| {
                        invalid(format!("Asset '{}': character_model: {e}", asset.name))
                    })?;
                crate::compile::character::import::import_model(
                    &asset.name,
                    &arg.schema,
                    &arg.model,
                    assets_dir,
                )
                .map_err(invalid)?
            }
            None => {
                crate::import::gltf::import_skinned_glb(&source, skin_index_arg(asset), assets_dir)
                    .map_err(|e| {
                        invalid(format!("Asset '{}': glTF import failed: {}", asset.name, e))
                    })?
            }
        };

        let name = asset.name.clone();
        let obj = asset.args.as_object_mut().ok_or_else(|| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Asset '{}': args is not a JSON object", name),
            )
        })?;
        let encode = |field: &str, value: serde_json::Result<serde_json::Value>| {
            value.map_err(|e| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!(
                        "Asset '{}': failed to encode imported {}: {}",
                        name, field, e
                    ),
                )
            })
        };
        obj.insert(
            "vertices".to_string(),
            encode("vertices", serde_json::to_value(&imported.vertices))?,
        );
        obj.insert(
            "indices".to_string(),
            encode("indices", serde_json::to_value(&imported.indices))?,
        );
        obj.insert(
            "skeleton".to_string(),
            encode("skeleton", serde_json::to_value(&imported.skeleton))?,
        );
        if !imported.morph_target_names.is_empty() {
            obj.insert(
                "morph_target_names".to_string(),
                encode(
                    "morph_target_names",
                    serde_json::to_value(&imported.morph_target_names),
                )?,
            );
            obj.insert(
                "morph_deltas".to_string(),
                encode("morph_deltas", serde_json::to_value(&imported.morph_deltas))?,
            );
        }
        obj.remove("character_model");
        tracing::info!(
            "Asset '{}': imported glTF '{}': {} vertices, {} indices, {} joints, {} morph target(s)",
            asset.name,
            if source.is_empty() {
                "character model"
            } else {
                &source
            },
            imported.vertices.len(),
            imported.indices.len(),
            imported.skeleton.len(),
            imported.morph_target_names.len()
        );
    }
    Ok(())
}

// Expand glTF-sourced static `Mesh` assets in place: parse the referenced
// `.glb` and write the imported primitive geometry into the asset's inline
// `vertices` / `indices` args. A Mesh with no `source` is left untouched. The
// GLB is parsed once per unique path; ABeautifulGame fans 35+ Mesh assets out
// of one file, so memoization keeps this O(files) rather than O(primitives).
pub(in crate::pipeline) fn desugar_gltf_meshes(
    assets: &mut [WorldJsonlAsset],
    mesh_cache: &std::collections::HashMap<String, MeshCacheEntry>,
    assets_dir: Option<&Path>,
) -> std::io::Result<()> {
    use crate::components::VertexData;
    use std::collections::HashMap;

    // One split chunk: its vertices and index buffer.
    type Chunk = (Vec<VertexData>, Vec<u16>);

    let mut parsed_cache: HashMap<String, crate::import::gltf_source::GltfDoc> = HashMap::new();
    // Memoize the chunk split per (source, primitive_index) so an oversized
    // primitive that fans into N chunked Mesh assets is split exactly once.
    let mut chunk_cache: HashMap<(String, u32), Vec<Chunk>> = HashMap::new();

    for asset in assets.iter_mut() {
        if asset.asset_type != MESH_TYPE {
            continue;
        }
        let source = asset
            .args
            .get("source")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        if source.is_empty() {
            continue;
        }
        // `.fbx` sources are handled by `desugar_fbx_meshes`; this pass owns
        // only the glTF containers.
        let lower = source.to_lowercase();
        if !lower.ends_with(".glb") && !lower.ends_with(".gltf") {
            continue;
        }
        // Skip the .glb parse when the cache probe already produced bytes
        // for this asset (see `desugar_gltf_skinned_meshes` for the same
        // pattern). Args stay pre-desugar so the next build's probe hits.
        if matches!(
            mesh_cache.get(&asset.name),
            Some(MeshCacheEntry { bytes: Some(_), .. })
        ) {
            continue;
        }
        let primitive_index = asset
            .args
            .get("primitive_index")
            .and_then(|v| v.as_u64())
            .unwrap_or(0) as u32;
        let chunk_index = asset
            .args
            .get("chunk_index")
            .and_then(|v| v.as_u64())
            .map(|n| n as usize);

        if !parsed_cache.contains_key(&source) {
            let doc = crate::import::glb::parse_glb(&source, assets_dir).map_err(|e| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!("Asset '{}': glTF import failed: {}", asset.name, e),
                )
            })?;
            parsed_cache.insert(source.clone(), doc);
        }
        let doc = parsed_cache.get(&source).expect("just inserted");

        let (vertices, indices) = if let Some(chunk_idx) = chunk_index {
            let key = (source.clone(), primitive_index);
            if !chunk_cache.contains_key(&key) {
                let (verts, indices32) =
                    crate::import::glb::read_primitive_geometry(doc, &source, primitive_index)
                        .map_err(|e| {
                            std::io::Error::new(
                                std::io::ErrorKind::InvalidData,
                                format!("Asset '{}': glTF import failed: {}", asset.name, e),
                            )
                        })?;
                let chunks = crate::import::glb::split_into_u16_chunks(&verts, &indices32);
                chunk_cache.insert(key.clone(), chunks);
            }
            let chunks = chunk_cache.get(&key).expect("just inserted");
            let chunk = chunks.get(chunk_idx).ok_or_else(|| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!(
                        "Asset '{}': chunk_index {} out of range, '{}' primitive {} \
                         splits into {} chunk(s)",
                        asset.name,
                        chunk_idx,
                        source,
                        primitive_index,
                        chunks.len(),
                    ),
                )
            })?;
            chunk.clone()
        } else {
            crate::import::glb::import_static_glb_primitive_from_doc(doc, &source, primitive_index)
                .map_err(|e| {
                    std::io::Error::new(
                        std::io::ErrorKind::InvalidData,
                        format!("Asset '{}': glTF import failed: {}", asset.name, e),
                    )
                })?
        };

        let name = asset.name.clone();
        let obj = asset.args.as_object_mut().ok_or_else(|| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("Asset '{}': args is not a JSON object", name),
            )
        })?;
        let encode = |field: &str, value: serde_json::Result<serde_json::Value>| {
            value.map_err(|e| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!(
                        "Asset '{}': failed to encode imported {}: {}",
                        name, field, e
                    ),
                )
            })
        };
        let vlen = vertices.len();
        let ilen = indices.len();
        obj.insert(
            "vertices".to_string(),
            encode("vertices", serde_json::to_value(&vertices))?,
        );
        obj.insert(
            "indices".to_string(),
            encode("indices", serde_json::to_value(&indices))?,
        );
        match chunk_index {
            Some(c) => tracing::info!(
                "Asset '{}': imported glTF '{}' primitive {} chunk {}: {} vertices, {} indices",
                asset.name,
                source,
                primitive_index,
                c,
                vlen,
                ilen,
            ),
            None => tracing::info!(
                "Asset '{}': imported glTF '{}' primitive {}: {} vertices, {} indices",
                asset.name,
                source,
                primitive_index,
                vlen,
                ilen,
            ),
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pipeline::desugar::fixtures::{hit_cache, morphing_skinned_glb};
    use crate::pipeline::fixtures::{wja, write_fixture};

    #[test]
    fn desugar_gltf_skinned_meshes_leaves_inline_and_cached_untouched() {
        let inline_args = serde_json::json!({"vertices": [], "indices": []});
        let cached_args = serde_json::json!({"source": "/no/such/hero.glb"});
        let mut assets = vec![
            wja("inline", SKINNED_MESH_TYPE, inline_args.clone()),
            wja("cached", SKINNED_MESH_TYPE, cached_args.clone()),
        ];
        desugar_gltf_skinned_meshes(&mut assets, &hit_cache("cached"), None).expect("desugar");
        // No source: untouched. Cache hit: the missing .glb is never parsed
        // and the args stay pre-desugar so the next probe key matches.
        assert_eq!(assets[0].args, inline_args);
        assert_eq!(assets[1].args, cached_args);
    }

    // A source-backed SkinnedMesh has its geometry and skeleton written into
    // the asset's inline args, replacing the `source` reference for the
    // compile step that follows.
    #[test]
    fn desugar_gltf_skinned_meshes_inlines_geometry_and_skeleton() {
        let dir = tempfile::tempdir().expect("tempdir");
        let src = write_fixture(
            &dir,
            "hero.glb",
            &crate::import::glb::test_fixtures::skinned_glb(),
        );
        let mut assets = vec![wja(
            "hero",
            SKINNED_MESH_TYPE,
            serde_json::json!({"source": src}),
        )];
        desugar_gltf_skinned_meshes(&mut assets, &Default::default(), None).expect("desugar");

        let args = &assets[0].args;
        assert_eq!(args["vertices"].as_array().unwrap().len(), 3);
        assert_eq!(args["indices"].as_array().unwrap(), &vec![0, 1, 2]);
        // The two-joint skin is reordered parents-before-children, so the
        // skeleton lands with the root first.
        assert_eq!(args["skeleton"].as_array().unwrap().len(), 2);
        // The fixture carries no morph targets, so no morph args appear.
        assert!(args.get("morph_target_names").is_none());
        assert!(args.get("morph_deltas").is_none());
    }

    // A source carrying morph targets writes the target names and the dense
    // delta block into the asset alongside the base geometry.
    #[test]
    fn desugar_gltf_skinned_meshes_inlines_morph_targets() {
        let dir = tempfile::tempdir().expect("tempdir");
        let src = write_fixture(&dir, "hero.glb", &morphing_skinned_glb());
        let mut assets = vec![wja(
            "hero",
            SKINNED_MESH_TYPE,
            serde_json::json!({"source": src}),
        )];
        desugar_gltf_skinned_meshes(&mut assets, &Default::default(), None).expect("desugar");

        let args = &assets[0].args;
        assert_eq!(args["morph_target_names"], serde_json::json!(["bulge"]));
        // One target over three vertices: a dense target-major delta block.
        assert_eq!(args["morph_deltas"].as_array().unwrap().len(), 3);
    }

    #[test]
    fn desugar_gltf_skinned_meshes_missing_source_errors() {
        let mut assets = vec![wja(
            "hero",
            SKINNED_MESH_TYPE,
            serde_json::json!({"source": "/no/such/hero.glb"}),
        )];
        let err = desugar_gltf_skinned_meshes(&mut assets, &Default::default(), None)
            .expect_err("missing .glb");
        assert!(err.to_string().contains("Asset 'hero'"), "got: {err}");
    }

    #[test]
    fn desugar_gltf_meshes_skips_non_glb_sources_and_cache_hits() {
        let fbx_args = serde_json::json!({"source": "/no/such/scene.fbx"});
        let cached_args = serde_json::json!({"source": "/no/such/scene.glb"});
        let inline_args = serde_json::json!({"vertices": [], "indices": []});
        let mut assets = vec![
            wja("from_fbx", MESH_TYPE, fbx_args.clone()),
            wja("cached", MESH_TYPE, cached_args.clone()),
            wja("inline", MESH_TYPE, inline_args.clone()),
        ];
        desugar_gltf_meshes(&mut assets, &hit_cache("cached"), None).expect("desugar");
        assert_eq!(
            assets[0].args, fbx_args,
            ".fbx sources belong to the fbx pass"
        );
        assert_eq!(assets[1].args, cached_args, "cache hit skips the parse");
        assert_eq!(assets[2].args, inline_args, "no source: untouched");
    }

    #[test]
    fn desugar_gltf_meshes_missing_source_errors() {
        let mut assets = vec![wja(
            "crate_mesh",
            MESH_TYPE,
            serde_json::json!({"source": "/no/such/scene.glb"}),
        )];
        let err =
            desugar_gltf_meshes(&mut assets, &Default::default(), None).expect_err("missing .glb");
        assert!(err.to_string().contains("Asset 'crate_mesh'"), "got: {err}");
    }

    // The text `.gltf` container flows through the same desugar as `.glb`:
    // geometry lands inline from the external `.bin` beside the source.
    #[test]
    fn desugar_gltf_meshes_imports_a_text_gltf_with_an_external_buffer() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("geo.bin"),
            crate::import::glb::test_fixtures::static_triangle_bin(),
        )
        .unwrap();
        let mut json = crate::import::glb::test_fixtures::static_triangle_json();
        json["buffers"][0]["uri"] = "geo.bin".into();
        let gltf = dir.path().join("tri.gltf");
        std::fs::write(&gltf, serde_json::to_vec(&json).unwrap()).unwrap();

        let mut assets = vec![wja(
            "tri",
            MESH_TYPE,
            serde_json::json!({"source": gltf.to_str().unwrap(), "primitive_index": 0}),
        )];
        desugar_gltf_meshes(&mut assets, &Default::default(), None).expect("desugar");
        let vertices = assets[0].args.get("vertices").expect("inline vertices");
        assert_eq!(vertices.as_array().unwrap().len(), 3);
        assert_eq!(
            assets[0]
                .args
                .get("indices")
                .unwrap()
                .as_array()
                .unwrap()
                .len(),
            3
        );
    }

    // Two Mesh assets fanned out of one container parse the file once; both
    // still land with their own inline geometry.
    #[test]
    fn desugar_gltf_meshes_parses_a_shared_source_once_for_every_asset() {
        let dir = tempfile::tempdir().expect("tempdir");
        let src = write_fixture(
            &dir,
            "scene.glb",
            &crate::import::glb::test_fixtures::static_triangle_glb(),
        );
        let mut assets = vec![
            wja(
                "part_a",
                MESH_TYPE,
                serde_json::json!({"source": src, "primitive_index": 0}),
            ),
            wja(
                "part_b",
                MESH_TYPE,
                serde_json::json!({"source": src, "primitive_index": 0}),
            ),
        ];
        desugar_gltf_meshes(&mut assets, &Default::default(), None).expect("desugar");
        for asset in &assets {
            assert_eq!(asset.args["vertices"].as_array().unwrap().len(), 3);
            assert_eq!(asset.args["indices"].as_array().unwrap().len(), 3);
        }
    }

    // A primitive the container does not have fails on both the chunked and
    // the whole-primitive route, naming the asset either way.
    #[test]
    fn desugar_gltf_meshes_reports_a_primitive_the_file_does_not_have() {
        let dir = tempfile::tempdir().expect("tempdir");
        let src = write_fixture(
            &dir,
            "scene.glb",
            &crate::import::glb::test_fixtures::static_triangle_glb(),
        );
        for extra in [serde_json::json!({}), serde_json::json!({"chunk_index": 0})] {
            let mut args = serde_json::json!({"source": src, "primitive_index": 7});
            for (k, v) in extra.as_object().unwrap() {
                args[k] = v.clone();
            }
            let mut assets = vec![wja("ghost", MESH_TYPE, args)];
            let err = desugar_gltf_meshes(&mut assets, &Default::default(), None)
                .expect_err("primitive 7 does not exist");
            let msg = err.to_string();
            assert!(msg.contains("Asset 'ghost'"), "got: {msg}");
            assert!(msg.contains("glTF import failed"), "got: {msg}");
        }
    }

    // An oversized primitive fanned into several chunked Mesh assets is split
    // exactly once; every asset still gets its own inline geometry.
    #[test]
    fn desugar_gltf_meshes_splits_a_primitive_once_for_every_chunk_asset() {
        let dir = tempfile::tempdir().expect("tempdir");
        let src = write_fixture(
            &dir,
            "scene.glb",
            &crate::import::glb::test_fixtures::static_triangle_glb(),
        );
        let chunk = |name: &str| {
            wja(
                name,
                MESH_TYPE,
                serde_json::json!({"source": src, "primitive_index": 0, "chunk_index": 0}),
            )
        };
        let mut assets = vec![chunk("part_a"), chunk("part_b")];
        desugar_gltf_meshes(&mut assets, &Default::default(), None).expect("desugar");
        for asset in &assets {
            assert_eq!(asset.args["vertices"].as_array().unwrap().len(), 3);
        }
    }

    // An authored `chunk_index` routes through the u16 chunk split instead of
    // the whole-primitive import; an index past the last chunk names the file,
    // the primitive, and how many chunks it really produced.
    #[test]
    fn desugar_gltf_meshes_reads_a_chunk_and_rejects_one_out_of_range() {
        let dir = tempfile::tempdir().expect("tempdir");
        let src = write_fixture(
            &dir,
            "scene.glb",
            &crate::import::glb::test_fixtures::static_triangle_glb(),
        );
        let mut assets = vec![wja(
            "chunk0",
            MESH_TYPE,
            serde_json::json!({"source": src, "chunk_index": 0}),
        )];
        desugar_gltf_meshes(&mut assets, &Default::default(), None).expect("desugar");
        assert_eq!(assets[0].args["vertices"].as_array().unwrap().len(), 3);

        let mut past_end = vec![wja(
            "chunk9",
            MESH_TYPE,
            serde_json::json!({"source": src, "chunk_index": 9}),
        )];
        let err = desugar_gltf_meshes(&mut past_end, &Default::default(), None)
            .expect_err("chunk 9 does not exist");
        let msg = err.to_string();
        assert!(msg.contains("Asset 'chunk9'"), "got: {msg}");
        assert!(msg.contains("chunk_index 9 out of range"), "got: {msg}");
        assert!(msg.contains("1 chunk(s)"), "got: {msg}");
    }

    #[test]
    fn desugar_skinned_meshes_import_the_selected_skin() {
        use crate::import::glb::test_fixtures::two_skin_glb;

        let dir = tempfile::tempdir().expect("tempdir");
        let src = write_fixture(&dir, "hero.glb", &two_skin_glb());
        let mut assets = vec![
            wja(
                "body",
                SKINNED_MESH_TYPE,
                serde_json::json!({"source": src, "skin_index": 0}),
            ),
            wja(
                "hair",
                SKINNED_MESH_TYPE,
                serde_json::json!({"source": src, "skin_index": 1}),
            ),
        ];
        desugar_gltf_skinned_meshes(&mut assets, &Default::default(), None).expect("desugar");

        // Each asset inlines its own part's geometry.
        assert_eq!(
            assets[0].args["vertices"][0]["pos"],
            serde_json::json!([0.0, 0.0, 0.0])
        );
        assert_eq!(
            assets[1].args["vertices"][0]["pos"],
            serde_json::json!([5.0, 0.0, 0.0])
        );
    }
}