catwalk3d 2.0.0

Headless model thumbnails rendered entirely in Rust
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
//! Static glTF and GLB scenes with bounded resources, first-pose deformation,
//! mesh instances and base-color texture appearance.
use super::{read_file, relative_resource, transform::Transform};
use crate::{
    Result,
    scene::{Line, Point, Scene, Triangle, Vec3},
};
use ::gltf::json::validation::Validate;
use base64::Engine;
use std::{collections::HashSet, path::Path};

/// Maximum decoded elements in any geometry accessor.
const MAX_ELEMENTS: usize = 5_000_000;

#[path = "gltf_deform.rs"]
mod deform;

#[path = "gltf_anim.rs"]
mod animation;

#[path = "gltf_instancing.rs"]
mod instancing;

#[path = "gltf_material.rs"]
mod material;

/// Load a glTF or GLB model into the selected scene and first animation pose.
///
/// # Errors
/// Returns an error for invalid references, unsupported required extensions, malformed
/// accessors or resources, non-finite geometry, and exceeded decoding budgets.
#[allow(
    clippy::too_many_lines,
    reason = "The bounded importer keeps required-extension, resource and scene selection validation in one pipeline."
)]
pub(super) fn load(path: &Path) -> Result<Scene> {
    let source = read_file(path)?;
    // Keep the original JSON: newer morph attribute semantics are absent from
    // gltf-json's typed representation and must not disappear during a roundtrip.
    let raw: serde_json::Value = if source.starts_with(b"glTF") {
        serde_json::from_slice(&::gltf::binary::Glb::from_slice(&source)?.json)?
    } else {
        serde_json::from_slice(&source)?
    };
    let model = if raw
        .get("animations")
        .and_then(serde_json::Value::as_array)
        .into_iter()
        .flatten()
        .any(|animation| {
            animation
                .get("channels")
                .and_then(serde_json::Value::as_array)
                .into_iter()
                .flatten()
                .any(|channel| {
                    channel
                        .get("target")
                        .and_then(serde_json::Value::as_object)
                        .is_some_and(|target| !target.contains_key("node"))
                })
        }) {
        // Current glTF permits inactive channels with no target node. The
        // older typed crate requires one, so remove only those inactive
        // channels from its view; the original JSON remains available below.
        let mut typed = raw.clone();
        for animation in typed["animations"].as_array_mut().into_iter().flatten() {
            if let Some(channels) = animation["channels"].as_array_mut() {
                channels.retain(|channel| {
                    channel
                        .get("target")
                        .and_then(serde_json::Value::as_object)
                        .is_none_or(|target| target.contains_key("node"))
                });
            }
        }
        let document =
            ::gltf::Document::from_json_without_validation(serde_json::from_value(typed)?);
        let blob = if source.starts_with(b"glTF") {
            ::gltf::binary::Glb::from_slice(&source)?
                .bin
                .map(std::borrow::Cow::into_owned)
        } else {
            None
        };
        ::gltf::Gltf { document, blob }
    } else {
        ::gltf::Gltf::from_slice_without_validation(&source)?
    };
    for required in model.extensions_required() {
        if ![
            "KHR_materials_unlit",
            "KHR_draco_mesh_compression",
            "KHR_mesh_quantization",
            "KHR_texture_transform",
            "EXT_mesh_gpu_instancing",
        ]
        .contains(&required)
        {
            return Err(format!("required glTF extension {required} is not implemented").into());
        }
    }
    // Preserve all core validation. The crate's extension allowlist predates
    // our separate Draco decoder, so its already-checked required names are
    // the only unsupported-extension reports we handle ourselves.
    let root = model.document.as_json();
    let compressed_accessors = compressed_accessors(&raw);
    let mut errors = Vec::new();
    root.validate(root, ::gltf::json::Path::new, &mut |path, error| {
        let path = path();
        let compressed_missing_view = matches!(error, ::gltf::json::validation::Error::Missing)
            && path
                .0
                .strip_prefix("accessors[")
                .and_then(|s| s.strip_suffix("].bufferView"))
                .and_then(|s| s.parse::<usize>().ok())
                .is_some_and(|i| compressed_accessors.contains(&i));
        if !(compressed_missing_view
            || matches!(error, ::gltf::json::validation::Error::Unsupported)
                && path.0.starts_with("extensionsRequired["))
        {
            errors.push((path, error));
        }
    });
    if !errors.is_empty() {
        return Err(::gltf::Error::Validation(errors).into());
    }
    let mut buffers = Vec::new();
    let mut total_bytes = 0u64;
    if model.buffers().len() > 256 {
        return Err("glTF exceeds the buffer reference limit".into());
    }
    let parent = path
        .parent()
        .filter(|p| !p.as_os_str().is_empty())
        .unwrap_or(Path::new("."));
    for buffer in model.buffers() {
        let bytes = match buffer.source() {
            ::gltf::buffer::Source::Bin => model.blob.clone().ok_or("missing GLB binary chunk")?,
            ::gltf::buffer::Source::Uri(uri) if uri.starts_with("data:") => {
                let (header, data) = uri.split_once(',').ok_or("malformed glTF data URI")?;
                if !header.ends_with(";base64") {
                    return Err("only base64 glTF data URIs are supported".into());
                }
                base64::engine::general_purpose::STANDARD.decode(data)?
            }
            ::gltf::buffer::Source::Uri(uri) => read_file(&relative_resource(parent, uri)?)?,
        };
        if bytes.len() < buffer.length() {
            return Err("glTF buffer is shorter than its declared length".into());
        }
        total_bytes = total_bytes
            .checked_add(bytes.len() as u64)
            .ok_or("glTF buffer byte count overflow")?;
        if total_bytes > super::MAX_INPUT_BYTES {
            return Err("glTF buffers exceed the aggregate 512 MiB limit".into());
        }
        buffers.push(bytes);
    }
    let poses = animation::first_pose(&model, &buffers, &raw)?;
    let views: Vec<_> = model.views().collect();
    let accessors: Vec<_> = model.accessors().collect();
    let morph_counts = deform::morph_target_counts(&raw)?;
    let mut output = Scene::default();
    let roots: Vec<_> = if let Some(scene) = model.default_scene().or_else(|| model.scenes().next())
    {
        scene.nodes().collect()
    } else {
        let children: HashSet<_> = model
            .nodes()
            .flat_map(|n| n.children().map(|c| c.index()).collect::<Vec<_>>())
            .collect();
        model
            .nodes()
            .filter(|n| !children.contains(&n.index()))
            .collect()
    };
    let globals = deform::global_transforms(&model, &roots, &poses)?;
    let materials = material::load(&model, &raw, parent, &buffers, &globals)?;
    let context = LoadContext {
        buffers: &buffers,
        views: &views,
        accessors: &accessors,
        morph_counts: &morph_counts,
        poses: &poses,
        quantized: model
            .extensions_required()
            .any(|name| name == "KHR_mesh_quantization"),
        globals: &globals,
        raw: &raw,
        materials: &materials,
        work: std::cell::Cell::new(0),
    };
    for node in roots {
        visit(node, &context, &mut output)?;
    }
    output.validate()?;
    Ok(output)
}

/// Compressed accessors describe decoded values and legitimately omit a view.
/// Only attributes actually named in this extension and its indices qualify.
fn compressed_accessors(root: &serde_json::Value) -> HashSet<usize> {
    let mut result = HashSet::new();
    for mesh in root
        .get("meshes")
        .and_then(serde_json::Value::as_array)
        .into_iter()
        .flatten()
    {
        for primitive in mesh
            .get("primitives")
            .and_then(serde_json::Value::as_array)
            .into_iter()
            .flatten()
        {
            if let Some(extension) = primitive
                .get("extensions")
                .and_then(|e| e.get("KHR_draco_mesh_compression"))
            {
                if let Some(attributes) = extension
                    .get("attributes")
                    .and_then(serde_json::Value::as_object)
                {
                    for name in attributes.keys() {
                        if let Some(index) = primitive
                            .get("attributes")
                            .and_then(|a| a.get(name))
                            .and_then(serde_json::Value::as_u64)
                            .and_then(|i| usize::try_from(i).ok())
                        {
                            result.insert(index);
                        }
                    }
                }
                if let Some(index) = primitive
                    .get("indices")
                    .and_then(serde_json::Value::as_u64)
                    .and_then(|i| usize::try_from(i).ok())
                {
                    result.insert(index);
                }
            }
        }
    }
    result
}

/// Validate the byte range occupied by a strided accessor without overflowing.
fn bounded_range(
    offset: usize,
    count: usize,
    stride: usize,
    item_size: usize,
    available: usize,
) -> Result<()> {
    let required = if count == 0 {
        offset
    } else {
        offset
            .checked_add(
                (count - 1)
                    .checked_mul(stride)
                    .ok_or("glTF accessor size overflow")?,
            )
            .and_then(|v| v.checked_add(item_size))
            .ok_or("glTF accessor size overflow")?
    };
    if count > MAX_ELEMENTS || stride < item_size || required > available {
        return Err("glTF accessor exceeds its buffer view or element limit".into());
    }
    Ok(())
}

/// Check accessor counts, alignment, buffer ranges and ordered sparse indices before iteration.
fn check_accessors(model: &::gltf::Gltf, buffers: &[Vec<u8>]) -> Result<()> {
    for view in model.views() {
        let end = view
            .offset()
            .checked_add(view.length())
            .ok_or("glTF buffer view size overflow")?;
        if end > view.buffer().length() || end > buffers[view.buffer().index()].len() {
            return Err("glTF buffer view exceeds its buffer".into());
        }
    }
    for accessor in model.accessors() {
        if accessor.count() == 0 {
            return Err("glTF accessor count must be positive".into());
        }
        if accessor.count() > MAX_ELEMENTS {
            return Err("glTF accessor exceeds the element limit".into());
        }
        if let Some(view) = accessor.view() {
            bounded_range(
                accessor.offset(),
                accessor.count(),
                view.stride().unwrap_or(accessor.size()),
                accessor.size(),
                view.length(),
            )?;
        }
        if let Some(sparse) = accessor.sparse() {
            if sparse.count() == 0 || sparse.count() > accessor.count() {
                return Err("glTF sparse accessor count exceeds accessor count".into());
            }
            let indices = sparse.indices();
            if indices.view().stride().is_some() || sparse.values().view().stride().is_some() {
                return Err("glTF sparse accessor views must not have strides".into());
            }
            let size = indices.index_type().size();
            bounded_range(
                indices.offset(),
                sparse.count(),
                size,
                size,
                indices.view().length(),
            )?;
            let values = sparse.values();
            bounded_range(
                values.offset(),
                sparse.count(),
                accessor.size(),
                accessor.size(),
                values.view().length(),
            )?;
            let view = indices.view();
            let bytes =
                &buffers[view.buffer().index()][view.offset()..view.offset() + view.length()];
            let mut previous = None;
            for i in 0..sparse.count() {
                let start = indices.offset() + i * size;
                let index = match size {
                    1 => bytes[start] as usize,
                    2 => u16::from_le_bytes(bytes[start..start + 2].try_into()?) as usize,
                    4 => u32::from_le_bytes(bytes[start..start + 4].try_into()?) as usize,
                    _ => return Err("invalid sparse index type".into()),
                };
                if index >= accessor.count() || previous.is_some_and(|p| p >= index) {
                    return Err(
                        "glTF sparse indices must be increasing and within the accessor".into(),
                    );
                }
                previous = Some(index);
            }
        }
    }
    Ok(())
}

/// Convert a linear RGB vertex color to display bytes.
#[allow(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    reason = "Color components are clamped and rounded to the inclusive byte range before conversion."
)]
fn rgb(value: [f32; 4]) -> [u8; 3] {
    // glTF color factors are linear; PNG stores sRGB.
    std::array::from_fn(|i| {
        let c = value[i].clamp(0., 1.);
        let srgb = if c <= 0.003_130_8 {
            12.92 * c
        } else {
            1.055 * c.powf(1. / 2.4) - 0.055
        };
        (srgb * 255.).round() as u8
    })
}

/// Decoded attributes and indices for one mesh primitive before morphing and skinning.
struct PrimitiveData {
    /// Vertex positions in the primitive coordinate system.
    positions: Vec<Vec3>,
    /// Authored vertex normals, retained at their original lengths until deformation.
    normals: Option<Vec<Vec3>>,
    /// Coordinates from the texture coordinate set selected by the material.
    texcoords: Option<Vec<[f64; 2]>>,
    /// Primitive vertex indices in source order.
    indices: Vec<u32>,
    /// Optional vertex colors with three or four components.
    colors: Option<Vec<[f32; 4]>>,
    /// Whether the attributes came from Draco decompression.
    compressed: bool,
    /// Paired joint-index and weight sets for each vertex.
    influences: Vec<deform::InfluenceSet>,
}

/// Shared validated resources and decoding budgets used while traversing a glTF scene.
struct LoadContext<'a, 'b> {
    /// Decoded bytes for each glTF buffer.
    buffers: &'a [Vec<u8>],
    /// Buffer views indexed by their document indices.
    views: &'a [::gltf::buffer::View<'b>],
    /// Accessors indexed by their document indices.
    accessors: &'a [::gltf::Accessor<'b>],
    /// Validated morph target counts indexed by mesh.
    morph_counts: &'a [usize],
    /// Local node transforms and morph weights evaluated at the first animation pose.
    poses: &'a [animation::Pose],
    /// Whether the required mesh quantization extension permits integer geometry attributes.
    quantized: bool,
    /// World transforms for nodes reachable from the selected scene.
    globals: &'a [Option<Transform>],
    /// Original JSON retaining attributes absent from the typed glTF representation.
    raw: &'a serde_json::Value,
    /// Materials used by selected nodes, including the default material.
    materials: &'a material::Materials,
    /// Accumulated vertex deformation and instance expansion work.
    work: std::cell::Cell<usize>,
}

/// Find a decompressed attribute by its extension ID and validate its semantic layout.
#[allow(
    clippy::needless_pass_by_value,
    reason = "glTF accessors are cheap document handles passed by value consistently with the reader API."
)]
fn decoded_attribute(
    geometry: &super::draco::DecodedGeometry,
    id: &serde_json::Value,
    accessor: ::gltf::Accessor<'_>,
) -> Result<super::draco::DecodedAttribute> {
    use ::gltf::accessor::DataType as A;
    use draco_core::DataType as D;
    let id = u32::try_from(
        id.as_u64()
            .ok_or("glTF Draco attribute ID must be an integer")?,
    )?;
    let attribute = geometry.attribute(id)?;
    if accessor.count() != geometry.point_count
        || accessor.dimensions().multiplicity() != attribute.components
    {
        return Err("glTF Draco attribute layout differs from its accessor".into());
    }
    if !matches!(
        (accessor.data_type(), attribute.data_type),
        (A::I8, D::Int8)
            | (A::U8, D::Uint8)
            | (A::I16, D::Int16)
            | (A::U16, D::Uint16)
            | (A::U32, D::Uint32)
            | (A::F32, D::Float32)
    ) {
        return Err("glTF Draco scalar type differs from its accessor".into());
    }
    Ok(attribute)
}

/// Decode a primitive and validate matching attribute counts, component layouts and indices.
#[allow(
    clippy::too_many_lines,
    clippy::needless_pass_by_value,
    clippy::cast_possible_truncation,
    reason = "Cheap glTF handles follow reader ownership; geometry budgets bound index casts, and decoded colors retain glTF f32 storage."
)]
fn primitive_data(
    primitive: ::gltf::mesh::Primitive<'_>,
    buffers: &[Vec<u8>],
    views: &[::gltf::buffer::View<'_>],
    quantized: bool,
    texcoord_set: Option<u32>,
) -> Result<PrimitiveData> {
    let reader = primitive.reader(|b| buffers.get(b.index()).map(Vec::as_slice));
    let fallback_colors = || {
        reader
            .read_colors(0)
            .map(|c| c.into_rgba_f32().collect::<Vec<_>>())
    };
    if let Some(extension) = primitive.extension_value("KHR_draco_mesh_compression") {
        if !matches!(
            primitive.mode(),
            ::gltf::mesh::Mode::Triangles | ::gltf::mesh::Mode::TriangleStrip
        ) {
            return Err("glTF Draco extension requires triangle or triangle-strip mode".into());
        }
        let view = usize::try_from(
            extension
                .get("bufferView")
                .and_then(serde_json::Value::as_u64)
                .ok_or("glTF Draco extension lacks a buffer view")?,
        )?;
        let view = views
            .get(view)
            .ok_or("glTF Draco buffer view index is out of bounds")?;
        if view.stride().is_some() {
            return Err("glTF Draco buffer view must not have an element stride".into());
        }
        let bytes = buffers
            .get(view.buffer().index())
            .and_then(|b| b.get(view.offset()..view.offset() + view.length()))
            .ok_or("glTF Draco buffer view exceeds input")?;
        let geometry = super::draco::decode_geometry(bytes)?;
        let faces = geometry
            .faces
            .as_ref()
            .ok_or("glTF Draco payload must be a triangle mesh")?;
        let attributes = extension
            .get("attributes")
            .and_then(serde_json::Value::as_object)
            .ok_or("glTF Draco extension lacks attribute IDs")?;
        let accessor = primitive
            .get(&::gltf::Semantic::Positions)
            .ok_or("glTF primitive has no POSITION accessor")?;
        deform::position_layout(&accessor, quantized, false)?;
        let positions = if let Some(id) = attributes.get("POSITION") {
            decoded_attribute(&geometry, id, accessor.clone())?
                .values
                .as_chunks::<3>()
                .0
                .iter()
                .map(|v| deform::position_vector([v[0], v[1], v[2]], &accessor))
                .collect()
        } else {
            let values = deform::read_positions(accessor, buffers, quantized, false)?;
            if values.len() != geometry.point_count {
                return Err("glTF fallback positions differ from Draco point count".into());
            }
            values
        };
        let colors = if let Some(id) = attributes.get("COLOR_0") {
            let accessor = primitive
                .get(&::gltf::Semantic::Colors(0))
                .ok_or("glTF Draco COLOR_0 has no accessor")?;
            let normalized = accessor.normalized();
            let color = decoded_attribute(&geometry, id, accessor)?;
            if ![3, 4].contains(&color.components) {
                return Err("glTF Draco colors require VEC3/VEC4".into());
            }
            let scale = match color.data_type {
                draco_core::DataType::Float32 => 1.0,
                draco_core::DataType::Uint8 if normalized => 255.0,
                draco_core::DataType::Uint16 if normalized => 65535.0,
                _ => {
                    return Err(
                        "glTF Draco colors require float32 or normalized uint8/uint16".into(),
                    );
                }
            };
            Some(
                color
                    .values
                    .chunks_exact(color.components)
                    .map(|v| {
                        std::array::from_fn(|i| {
                            if i < v.len() {
                                (v[i] / scale) as f32
                            } else {
                                1.0
                            }
                        })
                    })
                    .collect(),
            )
        } else {
            fallback_colors()
        };
        let indices: Vec<u32> = faces.iter().flat_map(|f| f.map(|i| i as u32)).collect();
        if let Some(accessor) = primitive.indices()
            && (accessor.dimensions() != ::gltf::accessor::Dimensions::Scalar
                || !matches!(
                    accessor.data_type(),
                    ::gltf::accessor::DataType::U8
                        | ::gltf::accessor::DataType::U16
                        | ::gltf::accessor::DataType::U32
                )
                || (primitive.mode() == ::gltf::mesh::Mode::Triangles
                    && accessor.count() != indices.len()))
        {
            return Err("glTF Draco index accessor differs from decoded triangles".into());
        }
        let influences =
            deform::read_influences(primitive.clone(), buffers, Some(&geometry), positions.len())?;
        let normals = if let Some(accessor) = primitive.get(&::gltf::Semantic::Normals) {
            Some(if let Some(id) = attributes.get("NORMAL") {
                deform::normal_layout(&accessor, quantized)?;
                decoded_attribute(&geometry, id, accessor.clone())?
                    .values
                    .as_chunks::<3>()
                    .0
                    .iter()
                    .map(|v| deform::normal_vector([v[0], v[1], v[2]], &accessor))
                    .collect()
            } else {
                deform::read_normals(accessor, buffers, quantized)?
            })
        } else {
            None
        };
        let texcoords = texcoord_set
            .map(|set| {
                material::read_texcoords(
                    primitive.clone(),
                    buffers,
                    Some(&geometry),
                    quantized,
                    set,
                )
            })
            .transpose()?;
        Ok(PrimitiveData {
            positions,
            normals,
            texcoords,
            indices,
            colors,
            compressed: true,
            influences,
        })
    } else {
        let accessor = primitive
            .get(&::gltf::Semantic::Positions)
            .ok_or("glTF primitive has no POSITION accessor")?;
        let positions = deform::read_positions(accessor, buffers, quantized, false)?;
        let indices = reader.read_indices().map_or_else(
            || (0..positions.len() as u32).collect(),
            |v| v.into_u32().collect(),
        );
        let influences =
            deform::read_influences(primitive.clone(), buffers, None, positions.len())?;
        let normals = primitive
            .get(&::gltf::Semantic::Normals)
            .map(|accessor| deform::read_normals(accessor, buffers, quantized))
            .transpose()?;
        let texcoords = texcoord_set
            .map(|set| material::read_texcoords(primitive.clone(), buffers, None, quantized, set))
            .transpose()?;
        Ok(PrimitiveData {
            positions,
            normals,
            texcoords,
            indices,
            colors: fallback_colors(),
            compressed: false,
            influences,
        })
    }
}

/// Traverse a selected node, deform its geometry and expand bounded mesh instances into the scene.
#[allow(
    clippy::too_many_lines,
    clippy::needless_pass_by_value,
    clippy::manual_midpoint,
    reason = "The bounded node visitor handles primitive modes together; cheap node handles follow glTF readers, and color averaging preserves snapshot rounding order."
)]
fn visit(node: ::gltf::Node<'_>, context: &LoadContext<'_, '_>, scene: &mut Scene) -> Result<()> {
    let transform = context.globals[node.index()].ok_or("glTF node has no scene transform")?;
    let skin = node
        .skin()
        .map(|skin| deform::skin_matrices(skin, context.buffers, context.globals))
        .transpose()?;
    let instances = instancing::transforms(
        context.raw,
        node.index(),
        context.accessors,
        context.buffers,
    )?;
    if instances.is_some() && skin.is_some() {
        return Err("glTF skin combined with EXT_mesh_gpu_instancing is not implemented".into());
    }
    let instances = instances.unwrap_or_else(|| vec![Transform::IDENTITY]);
    if let Some(mesh) = node.mesh() {
        for primitive in mesh.primitives() {
            let material = context
                .materials
                .get(&primitive.material().index())
                .ok_or("glTF selected material is missing")?;
            let mut data = primitive_data(
                primitive.clone(),
                context.buffers,
                context.views,
                context.quantized,
                material.texcoord_set,
            )?;
            deform::apply_morphs(&node, &mesh, primitive.index(), context, &mut data)?;
            if data
                .normals
                .as_ref()
                .is_some_and(|n| n.len() != data.positions.len())
                || data
                    .texcoords
                    .as_ref()
                    .is_some_and(|uv| uv.len() != data.positions.len())
            {
                return Err("glTF normal/texture coordinate count does not match positions".into());
            }
            if let Some(texcoords) = &mut data.texcoords {
                material::apply_uv_morphs(
                    &node,
                    &mesh,
                    primitive.index(),
                    context,
                    material
                        .texcoord_set
                        .ok_or("glTF texture coordinates lack a selected set")?,
                    texcoords,
                )?;
                material.transform_uvs(texcoords)?;
            }
            if data.colors.as_ref().is_some_and(|colors| {
                colors.len() != data.positions.len()
                    || colors
                        .iter()
                        .flatten()
                        .any(|v| !v.is_finite() || !(0.0..=1.0).contains(v))
            }) {
                return Err("glTF vertex colors must match positions and contain finite RGBA values between zero and one".into());
            }
            let work = data
                .positions
                .len()
                .checked_mul(instances.len())
                .and_then(|n| context.work.get().checked_add(n))
                .ok_or("glTF instanced vertex work overflow")?;
            if work > 50_000_000 {
                return Err("glTF exceeds the expanded vertex work limit".into());
            }
            context.work.set(work);
            let mode = if data.compressed {
                ::gltf::mesh::Mode::Triangles
            } else {
                primitive.mode()
            };
            let count = match mode {
                ::gltf::mesh::Mode::Triangles => data.indices.len() / 3,
                ::gltf::mesh::Mode::TriangleStrip | ::gltf::mesh::Mode::TriangleFan => {
                    data.indices.len().saturating_sub(2)
                }
                ::gltf::mesh::Mode::Points => data.indices.len(),
                ::gltf::mesh::Mode::Lines => data.indices.len() / 2,
                ::gltf::mesh::Mode::LineStrip => data.indices.len().saturating_sub(1),
                ::gltf::mesh::Mode::LineLoop => {
                    if data.indices.len() > 1 {
                        data.indices.len()
                    } else {
                        0
                    }
                }
            };
            let existing = scene.triangles.len() + scene.lines.len() + scene.points.len();
            if count
                .checked_mul(instances.len())
                .and_then(|n| n.checked_add(existing))
                .is_none_or(|n| n > MAX_ELEMENTS)
            {
                return Err("glTF scene exceeds the expanded geometry limit".into());
            }
            for instance in &instances {
                let transform = transform.then(*instance);
                let normals = data
                    .normals
                    .as_ref()
                    .map(|normals| {
                        deform::apply_normals(normals, &data.influences, skin.as_deref(), transform)
                            .map(|normals| {
                                normals
                                    .into_iter()
                                    .map(|v| Vec3::new(v.x, -v.z, v.y))
                                    .collect::<Vec<_>>()
                            })
                    })
                    .transpose()?;
                let world = deform::apply_skin(
                    data.positions.clone(),
                    &data.influences,
                    skin.as_deref(),
                    transform,
                )?;
                let vertices: Vec<_> = world
                    .into_iter()
                    .map(|v| Vec3::new(v.x, -v.z, v.y))
                    .collect();
                let indices = &data.indices;
                if indices.iter().any(|&i| i as usize >= vertices.len()) {
                    return Err("glTF triangle references a missing vertex".into());
                }
                let factor = primitive
                    .material()
                    .pbr_metallic_roughness()
                    .base_color_factor();
                let colors: Option<Vec<_>> = data.colors.as_ref().map(|v| {
                    v.iter()
                        .map(|c| std::array::from_fn::<_, 4, _>(|i| c[i] * factor[i]))
                        .collect()
                });
                if colors.as_ref().is_some_and(|c| c.len() != vertices.len()) {
                    return Err("glTF color count does not match position count".into());
                }
                let face_color = |face: [u32; 3]| -> [u8; 3] {
                    if let Some(colors) = &colors {
                        rgb(std::array::from_fn(|channel| {
                            face.iter()
                                .map(|&i| colors[i as usize][channel])
                                .sum::<f32>()
                                / 3.
                        }))
                    } else {
                        rgb(factor)
                    }
                };
                let mut add_triangle = |face: [u32; 3]| -> Result<()> {
                    if scene.triangles.len() >= MAX_ELEMENTS {
                        return Err("glTF scene exceeds triangle limit".into());
                    }
                    let mut face = face;
                    // glTF reverses front-face winding for mirrored node transforms.
                    if skin.is_none() && mirrored(transform) {
                        face.swap(1, 2);
                    }
                    let mut appearance = material.appearance.clone();
                    appearance.colors = data
                        .colors
                        .as_ref()
                        .map(|colors| face.map(|i| colors[i as usize].map(f64::from)));
                    appearance.texcoords = data
                        .texcoords
                        .as_ref()
                        .map(|uvs| face.map(|i| uvs[i as usize]));
                    appearance.normals = normals.as_ref().and_then(|normals| {
                        let values = face.map(|i| normals[i as usize]);
                        values.iter().all(|n| n.length() > 0.).then_some(values)
                    });
                    scene.appearances.insert(scene.triangles.len(), appearance);
                    scene.triangles.push(Triangle {
                        vertices: face.map(|i| vertices[i as usize]),
                        color: Some(face_color(face)),
                    });
                    Ok(())
                };
                match mode {
                    ::gltf::mesh::Mode::Triangles => {
                        if indices.len() % 3 != 0 {
                            return Err(
                                "glTF triangle index count is not a multiple of three".into()
                            );
                        }
                        for face in indices.as_chunks::<3>().0 {
                            add_triangle([face[0], face[1], face[2]])?;
                        }
                    }
                    ::gltf::mesh::Mode::TriangleStrip => {
                        for (i, face) in indices.windows(3).enumerate() {
                            add_triangle(if i % 2 == 0 {
                                [face[0], face[1], face[2]]
                            } else {
                                [face[1], face[0], face[2]]
                            })?;
                        }
                    }
                    ::gltf::mesh::Mode::TriangleFan => {
                        if let Some(&first) = indices.first() {
                            for face in indices[1..].windows(2) {
                                add_triangle([first, face[0], face[1]])?;
                            }
                        }
                    }
                    ::gltf::mesh::Mode::Points => {
                        for &i in indices {
                            if scene.points.len() >= MAX_ELEMENTS {
                                return Err("glTF scene exceeds point limit".into());
                            }
                            scene.points.push(Point {
                                position: vertices[i as usize],
                                color: Some(
                                    colors
                                        .as_ref()
                                        .map_or_else(|| rgb(factor), |c| rgb(c[i as usize])),
                                ),
                            });
                        }
                    }
                    ::gltf::mesh::Mode::Lines
                    | ::gltf::mesh::Mode::LineStrip
                    | ::gltf::mesh::Mode::LineLoop => {
                        let edges: Vec<[u32; 2]> = if mode == ::gltf::mesh::Mode::Lines {
                            if !indices.len().is_multiple_of(2) {
                                return Err("glTF line index count is not a multiple of two".into());
                            }
                            indices
                                .as_chunks::<2>()
                                .0
                                .iter()
                                .map(|v| [v[0], v[1]])
                                .collect()
                        } else {
                            let mut edges: Vec<_> =
                                indices.windows(2).map(|v| [v[0], v[1]]).collect();
                            if mode == ::gltf::mesh::Mode::LineLoop && indices.len() > 1 {
                                edges.push([*indices.last().unwrap(), indices[0]]);
                            }
                            edges
                        };
                        if scene.lines.len() + edges.len() > MAX_ELEMENTS {
                            return Err("glTF scene exceeds line limit".into());
                        }
                        for edge in edges {
                            let color = colors.as_ref().map_or_else(
                                || rgb(factor),
                                |colors| {
                                    rgb(std::array::from_fn(|i| {
                                        (colors[edge[0] as usize][i] + colors[edge[1] as usize][i])
                                            / 2.0
                                    }))
                                },
                            );
                            scene.lines.push(Line {
                                vertices: edge.map(|i| vertices[i as usize]),
                                color: Some(color),
                            });
                        }
                    }
                }
            }
        }
    }
    for child in node.children() {
        visit(child, context, scene)?;
    }
    Ok(())
}

/// Detect whether the linear transform reverses orientation.
fn mirrored(transform: Transform) -> bool {
    let columns: [Vec3; 3] =
        std::array::from_fn(|i| Vec3::new(transform.0[i][0], transform.0[i][1], transform.0[i][2]));
    // Scale before the sign test to avoid overflow for large finite transforms.
    let largest = columns
        .iter()
        .flat_map(|v| [v.x.abs(), v.y.abs(), v.z.abs()])
        .fold(0., f64::max);
    if largest == 0. {
        return false;
    }
    let columns = columns.map(|v| Vec3::new(v.x / largest, v.y / largest, v.z / largest));
    columns[0].dot(columns[1].cross(columns[2])) < 0.
}

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

    #[test]
    fn accessor_ranges_check_last_element_and_overflow() {
        assert!(bounded_range(0, 3, 12, 12, 36).is_ok());
        assert!(bounded_range(0, 3, 16, 12, 43).is_err());
        assert!(bounded_range(0, 3, 16, 12, 44).is_ok());
        assert!(bounded_range(usize::MAX, 3, 12, 12, 36).is_err());
    }
}