oxihuman-export 0.1.2

Export pipeline for OxiHuman — glTF, COLLADA, STL, and streaming 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
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
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

use anyhow::{bail, Result};
use bytemuck::cast_slice;
use oxihuman_mesh::mesh::MeshBuffers;
use oxihuman_mesh::suit::ensure_suit_mesh;
use serde_json::json;
use std::io::Write;
use std::path::Path;

// GLB magic constants
const GLB_MAGIC: u32 = 0x46546C67; // "glTF"
const GLB_VERSION: u32 = 2;
const CHUNK_JSON: u32 = 0x4E4F534A; // "JSON"
const CHUNK_BIN: u32 = 0x004E4942; // "BIN\0"

/// Export a MeshBuffers to a GLB 2.0 file.
/// Returns Err if the mesh has no suit applied (safety check).
/// If `mesh.colors` is Some, a COLOR_0 accessor is included in the output.
/// If `mesh.tangents.len() == mesh.positions.len()`, a TANGENT accessor is included.
pub fn export_glb(mesh: &MeshBuffers, path: &Path) -> Result<()> {
    ensure_suit_mesh(mesh)?;

    // ── 1. Build BIN chunk data ──────────────────────────────────────────────
    // Layout: [positions f32*3*n] [normals f32*3*n] [uvs f32*2*n] [indices u32*m]
    //         [colors f32*4*n (optional)] [tangents f32*4*n (optional)]
    let n_verts = mesh.positions.len();
    let n_idx = mesh.indices.len();

    let pos_bytes: &[u8] = cast_slice(&mesh.positions);
    let norm_bytes: &[u8] = cast_slice(&mesh.normals);
    let uv_bytes: &[u8] = cast_slice(&mesh.uvs);
    let idx_bytes: &[u8] = cast_slice(&mesh.indices);

    let pos_offset = 0usize;
    let norm_offset = pos_offset + pos_bytes.len();
    let uv_offset = norm_offset + norm_bytes.len();
    let idx_offset = uv_offset + uv_bytes.len();
    let mut bin_len = idx_offset + idx_bytes.len();

    // Optional colors
    let has_color = mesh.colors.is_some();
    let color_offset;
    let color_bytes_opt: Option<&[u8]> = if let Some(ref cols) = mesh.colors {
        let cb: &[u8] = cast_slice(cols.as_slice());
        color_offset = bin_len;
        bin_len += cb.len();
        Some(cb)
    } else {
        color_offset = 0;
        None
    };

    // Optional tangents (only if count matches vertex count)
    let has_tangents = n_verts > 0 && mesh.tangents.len() == n_verts;
    let tangent_offset;
    let tangent_bytes_opt: Option<&[u8]> = if has_tangents {
        let tb: &[u8] = cast_slice(mesh.tangents.as_slice());
        tangent_offset = bin_len;
        bin_len += tb.len();
        Some(tb)
    } else {
        tangent_offset = 0;
        None
    };

    let mut bin_data: Vec<u8> = Vec::with_capacity(bin_len + 3);
    bin_data.extend_from_slice(pos_bytes);
    bin_data.extend_from_slice(norm_bytes);
    bin_data.extend_from_slice(uv_bytes);
    bin_data.extend_from_slice(idx_bytes);
    if let Some(cb) = color_bytes_opt {
        bin_data.extend_from_slice(cb);
    }
    if let Some(tb) = tangent_bytes_opt {
        bin_data.extend_from_slice(tb);
    }
    // Pad BIN to 4-byte boundary
    while !bin_data.len().is_multiple_of(4) {
        bin_data.push(0x00);
    }

    // ── 2. Build GLTF JSON ───────────────────────────────────────────────────
    // Accessor and bufferView indices are assigned dynamically:
    //   0: POSITION (VEC3)
    //   1: NORMAL   (VEC3)
    //   2: TEXCOORD_0 (VEC2)
    //   3: indices  (SCALAR)
    //   4: COLOR_0  (VEC4) [if has_color]
    //   4 or 5: TANGENT (VEC4) [if has_tangents]
    let total_bin = bin_data.len() as u32;

    let mut accessors: Vec<serde_json::Value> = vec![
        json!({ "bufferView": 0, "componentType": 5126, "count": n_verts, "type": "VEC3" }),
        json!({ "bufferView": 1, "componentType": 5126, "count": n_verts, "type": "VEC3" }),
        json!({ "bufferView": 2, "componentType": 5126, "count": n_verts, "type": "VEC2" }),
        json!({ "bufferView": 3, "componentType": 5125, "count": n_idx,   "type": "SCALAR" }),
    ];
    let mut buffer_views: Vec<serde_json::Value> = vec![
        json!({ "buffer": 0, "byteOffset": pos_offset,  "byteLength": pos_bytes.len()  }),
        json!({ "buffer": 0, "byteOffset": norm_offset, "byteLength": norm_bytes.len() }),
        json!({ "buffer": 0, "byteOffset": uv_offset,   "byteLength": uv_bytes.len()   }),
        json!({ "buffer": 0, "byteOffset": idx_offset,  "byteLength": idx_bytes.len()  }),
    ];

    let mut attributes = json!({
        "POSITION":   0,
        "NORMAL":     1,
        "TEXCOORD_0": 2
    });

    if has_color {
        let color_byte_len =
            mesh.colors.as_ref().map_or(0, |c| c.len()) * std::mem::size_of::<[f32; 4]>();
        let color_acc_idx = accessors.len();
        accessors.push(json!({
            "bufferView": buffer_views.len(),
            "componentType": 5126,
            "count": n_verts,
            "type": "VEC4"
        }));
        buffer_views.push(json!({
            "buffer": 0,
            "byteOffset": color_offset,
            "byteLength": color_byte_len
        }));
        attributes["COLOR_0"] = json!(color_acc_idx);
    }

    if has_tangents {
        let tangent_byte_len = mesh.tangents.len() * std::mem::size_of::<[f32; 4]>();
        let tangent_acc_idx = accessors.len();
        accessors.push(json!({
            "bufferView": buffer_views.len(),
            "componentType": 5126,
            "count": n_verts,
            "type": "VEC4"
        }));
        buffer_views.push(json!({
            "buffer": 0,
            "byteOffset": tangent_offset,
            "byteLength": tangent_byte_len
        }));
        attributes["TANGENT"] = json!(tangent_acc_idx);
    }

    let gltf = json!({
        "asset": { "version": "2.0", "generator": "OxiHuman 0.1.0" },
        "scene": 0,
        "scenes": [{ "nodes": [0] }],
        "nodes": [{ "mesh": 0 }],
        "meshes": [{
            "name": "human",
            "primitives": [{
                "attributes": attributes,
                "indices": 3,
                "mode": 4
            }]
        }],
        "accessors":   accessors,
        "bufferViews": buffer_views,
        "buffers": [{ "byteLength": total_bin }]
    });

    let mut json_bytes = serde_json::to_vec(&gltf)?;
    // Pad JSON to 4-byte boundary with spaces
    while json_bytes.len() % 4 != 0 {
        json_bytes.push(b' ');
    }

    // ── 3. Write GLB ─────────────────────────────────────────────────────────
    let json_chunk_len = json_bytes.len() as u32;
    let bin_chunk_len = bin_data.len() as u32;
    let total_len = 12 + 8 + json_chunk_len + 8 + bin_chunk_len;

    let mut file = std::fs::File::create(path)?;

    // GLB header (12 bytes)
    file.write_all(&GLB_MAGIC.to_le_bytes())?;
    file.write_all(&GLB_VERSION.to_le_bytes())?;
    file.write_all(&total_len.to_le_bytes())?;

    // JSON chunk
    file.write_all(&json_chunk_len.to_le_bytes())?;
    file.write_all(&CHUNK_JSON.to_le_bytes())?;
    file.write_all(&json_bytes)?;

    // BIN chunk
    file.write_all(&bin_chunk_len.to_le_bytes())?;
    file.write_all(&CHUNK_BIN.to_le_bytes())?;
    file.write_all(&bin_data)?;

    Ok(())
}

/// Read the first 12 bytes of a GLB file and verify the magic/version header.
pub fn verify_glb_header(path: &Path) -> Result<()> {
    use std::io::Read;
    let mut f = std::fs::File::open(path)?;
    let mut header = [0u8; 12];
    f.read_exact(&mut header)?;
    let magic = u32::from_le_bytes(
        header[0..4]
            .try_into()
            .map_err(|_| anyhow::anyhow!("failed to read GLB magic bytes"))?,
    );
    let version = u32::from_le_bytes(
        header[4..8]
            .try_into()
            .map_err(|_| anyhow::anyhow!("failed to read GLB version bytes"))?,
    );
    if magic != GLB_MAGIC {
        bail!("invalid GLB magic: 0x{:08X}", magic);
    }
    if version != GLB_VERSION {
        bail!("unexpected GLB version: {}", version);
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use oxihuman_mesh::mesh::MeshBuffers;
    use oxihuman_mesh::normals::compute_tangents;
    use oxihuman_mesh::set_uniform_color;
    use oxihuman_morph::engine::MeshBuffers as MB;

    fn suited_mesh() -> MeshBuffers {
        MeshBuffers::from_morph(MB {
            positions: vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
            normals: vec![[0.0, 0.0, 1.0]; 3],
            uvs: vec![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
            indices: vec![0, 1, 2],
            has_suit: true,
        })
    }

    fn unsuited_mesh() -> MeshBuffers {
        MeshBuffers::from_morph(MB {
            positions: vec![[0.0, 0.0, 0.0]],
            normals: vec![[0.0, 1.0, 0.0]],
            uvs: vec![[0.0, 0.0]],
            indices: vec![],
            has_suit: false,
        })
    }

    #[test]
    fn export_glb_creates_valid_file() {
        let mesh = suited_mesh();
        let path = std::path::PathBuf::from("/tmp/test_oxihuman.glb");
        export_glb(&mesh, &path).expect("export failed");
        verify_glb_header(&path).expect("header invalid");
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn export_refuses_unsuited_mesh() {
        let mesh = unsuited_mesh();
        let path = std::path::PathBuf::from("/tmp/test_unsuited.glb");
        let result = export_glb(&mesh, &path);
        assert!(result.is_err(), "should refuse unsuited mesh");
    }

    #[test]
    fn glb_header_magic() {
        let mesh = suited_mesh();
        let path = std::path::PathBuf::from("/tmp/test_magic.glb");
        export_glb(&mesh, &path).expect("should succeed");
        // Read raw bytes
        let bytes = std::fs::read(&path).expect("should succeed");
        assert!(bytes.len() >= 12, "GLB too short");
        let magic = u32::from_le_bytes(bytes[0..4].try_into().expect("should succeed"));
        assert_eq!(magic, 0x46546C67u32, "wrong magic");
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn glb_with_colors_has_larger_bin() {
        let mesh_no_color = suited_mesh();
        let mut mesh_with_color = suited_mesh();
        set_uniform_color(&mut mesh_with_color, [1.0, 0.0, 0.0, 1.0]);

        let path_no = std::path::PathBuf::from("/tmp/test_no_color.glb");
        let path_col = std::path::PathBuf::from("/tmp/test_with_color.glb");

        export_glb(&mesh_no_color, &path_no).expect("should succeed");
        export_glb(&mesh_with_color, &path_col).expect("should succeed");

        let size_no = std::fs::metadata(&path_no).expect("should succeed").len();
        let size_col = std::fs::metadata(&path_col).expect("should succeed").len();

        assert!(
            size_col > size_no,
            "colored GLB ({} bytes) should be larger than plain ({} bytes)",
            size_col,
            size_no
        );

        std::fs::remove_file(&path_no).ok();
        std::fs::remove_file(&path_col).ok();
    }

    #[test]
    fn glb_with_colors_header_still_valid() {
        let mut mesh = suited_mesh();
        set_uniform_color(&mut mesh, [0.5, 0.5, 0.5, 1.0]);
        let path = std::path::PathBuf::from("/tmp/test_color_header.glb");
        export_glb(&mesh, &path).expect("should succeed");

        let bytes = std::fs::read(&path).expect("should succeed");
        assert!(bytes.len() >= 12, "GLB too short");
        let magic = u32::from_le_bytes(bytes[0..4].try_into().expect("should succeed"));
        assert_eq!(magic, 0x46546C67u32, "wrong GLB magic in colored file");

        verify_glb_header(&path).expect("should succeed");
        std::fs::remove_file(&path).ok();
    }

    // ── Tangent-specific tests ─────────────────────────────────────────────

    #[test]
    fn tangent_glb_has_larger_bin() {
        // Base mesh from from_morph has tangents already (same count as positions),
        // so we need a mesh WITHOUT tangents to compare against.
        // We create a mesh and clear its tangents to simulate "no tangent" export.
        let mut mesh_no_tang = suited_mesh();
        mesh_no_tang.tangents = Vec::new(); // force no-tangent path

        let mut mesh_with_tang = suited_mesh();
        compute_tangents(&mut mesh_with_tang);

        let path_no = std::path::PathBuf::from("/tmp/test_no_tangent.glb");
        let path_tang = std::path::PathBuf::from("/tmp/test_with_tangent.glb");

        export_glb(&mesh_no_tang, &path_no).expect("should succeed");
        export_glb(&mesh_with_tang, &path_tang).expect("should succeed");

        let size_no = std::fs::metadata(&path_no).expect("should succeed").len();
        let size_tang = std::fs::metadata(&path_tang).expect("should succeed").len();

        assert!(
            size_tang > size_no,
            "tangent GLB ({} bytes) should be larger than plain ({} bytes)",
            size_tang,
            size_no
        );

        std::fs::remove_file(&path_no).ok();
        std::fs::remove_file(&path_tang).ok();
    }

    #[test]
    fn glb_tangent_header_valid() {
        let mut mesh = suited_mesh();
        compute_tangents(&mut mesh);

        let path = std::path::PathBuf::from("/tmp/test_tangent_header.glb");
        export_glb(&mesh, &path).expect("should succeed");

        let bytes = std::fs::read(&path).expect("should succeed");
        assert!(bytes.len() >= 12, "GLB too short");
        let magic = u32::from_le_bytes(bytes[0..4].try_into().expect("should succeed"));
        assert_eq!(magic, 0x46546C67u32, "wrong GLB magic after tangent export");

        verify_glb_header(&path).expect("should succeed");
        std::fs::remove_file(&path).ok();
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Skinned GLB export
// ─────────────────────────────────────────────────────────────────────────────

use oxihuman_mesh::skeleton::Skeleton;

/// Export a mesh with a skeleton as a skinned GLB 2.0 file.
/// Produces a GLB with a `skins` node containing the joint hierarchy.
/// Vertex skinning data (JOINTS_0, WEIGHTS_0) is not included in this
/// stub — the mesh uses the skeleton purely for the node hierarchy.
pub fn export_glb_with_skeleton(
    mesh: &MeshBuffers,
    skeleton: &Skeleton,
    path: &Path,
) -> anyhow::Result<()> {
    // ── 1. Build BIN chunk (same layout as export_glb, no suit check) ────────
    let n_verts = mesh.positions.len();
    let n_idx = mesh.indices.len();

    let pos_bytes: &[u8] = cast_slice(&mesh.positions);
    let norm_bytes: &[u8] = cast_slice(&mesh.normals);
    let uv_bytes: &[u8] = cast_slice(&mesh.uvs);
    let idx_bytes: &[u8] = cast_slice(&mesh.indices);

    let pos_offset = 0usize;
    let norm_offset = pos_offset + pos_bytes.len();
    let uv_offset = norm_offset + norm_bytes.len();
    let idx_offset = uv_offset + uv_bytes.len();
    let bin_len = idx_offset + idx_bytes.len();

    let mut bin_data: Vec<u8> = Vec::with_capacity(bin_len + 3);
    bin_data.extend_from_slice(pos_bytes);
    bin_data.extend_from_slice(norm_bytes);
    bin_data.extend_from_slice(uv_bytes);
    bin_data.extend_from_slice(idx_bytes);
    while !bin_data.len().is_multiple_of(4) {
        bin_data.push(0x00);
    }
    let total_bin = bin_data.len() as u32;

    // ── 2. Build nodes array ─────────────────────────────────────────────────
    // One node per joint, then one mesh node at the end.
    let n_joints = skeleton.joints.len();
    let mesh_node_idx = n_joints; // index of the mesh node in the nodes array

    let mut nodes: Vec<serde_json::Value> = skeleton
        .joints
        .iter()
        .enumerate()
        .map(|(i, joint)| {
            let children: Vec<usize> = skeleton.children_of(i);
            let mut node = serde_json::json!({
                "name":        joint.name,
                "translation": joint.translation,
                "rotation":    joint.rotation,
                "scale":       joint.scale
            });
            if !children.is_empty() {
                node["children"] = serde_json::json!(children);
            }
            node
        })
        .collect();

    // Mesh node — references mesh 0 and skin 0
    nodes.push(serde_json::json!({
        "mesh": 0,
        "skin": 0
    }));

    // scene[0].nodes = root joint indices + mesh node index
    let mut scene_nodes: Vec<usize> = skeleton.roots();
    scene_nodes.push(mesh_node_idx);

    // all joint node indices (0 .. n_joints-1)
    let all_joint_indices: Vec<usize> = (0..n_joints).collect();
    // skeleton root = first root joint (or 0 if no roots somehow)
    let skeleton_root = skeleton.roots().into_iter().next().unwrap_or(0);

    // ── 3. Build GLTF JSON ───────────────────────────────────────────────────
    let gltf = serde_json::json!({
        "asset": { "version": "2.0", "generator": "OxiHuman 0.1.0" },
        "scene": 0,
        "scenes": [{ "nodes": scene_nodes }],
        "nodes": nodes,
        "skins": [{
            "joints":   all_joint_indices,
            "skeleton": skeleton_root
        }],
        "meshes": [{
            "name": "human",
            "primitives": [{
                "attributes": {
                    "POSITION":   0,
                    "NORMAL":     1,
                    "TEXCOORD_0": 2
                },
                "indices": 3,
                "mode":    4
            }]
        }],
        "accessors": [
            {
                "bufferView": 0,
                "componentType": 5126,
                "count": n_verts,
                "type": "VEC3"
            },
            {
                "bufferView": 1,
                "componentType": 5126,
                "count": n_verts,
                "type": "VEC3"
            },
            {
                "bufferView": 2,
                "componentType": 5126,
                "count": n_verts,
                "type": "VEC2"
            },
            {
                "bufferView": 3,
                "componentType": 5125,
                "count": n_idx,
                "type": "SCALAR"
            }
        ],
        "bufferViews": [
            { "buffer": 0, "byteOffset": pos_offset,  "byteLength": pos_bytes.len()  },
            { "buffer": 0, "byteOffset": norm_offset, "byteLength": norm_bytes.len() },
            { "buffer": 0, "byteOffset": uv_offset,   "byteLength": uv_bytes.len()   },
            { "buffer": 0, "byteOffset": idx_offset,  "byteLength": idx_bytes.len()  }
        ],
        "buffers": [{ "byteLength": total_bin }]
    });

    let mut json_bytes = serde_json::to_vec(&gltf)?;
    while json_bytes.len() % 4 != 0 {
        json_bytes.push(b' ');
    }

    // ── 4. Write GLB ─────────────────────────────────────────────────────────
    let json_chunk_len = json_bytes.len() as u32;
    let bin_chunk_len = bin_data.len() as u32;
    let total_len = 12 + 8 + json_chunk_len + 8 + bin_chunk_len;

    let mut file = std::fs::File::create(path)?;
    file.write_all(&GLB_MAGIC.to_le_bytes())?;
    file.write_all(&GLB_VERSION.to_le_bytes())?;
    file.write_all(&total_len.to_le_bytes())?;
    file.write_all(&json_chunk_len.to_le_bytes())?;
    file.write_all(&CHUNK_JSON.to_le_bytes())?;
    file.write_all(&json_bytes)?;
    file.write_all(&bin_chunk_len.to_le_bytes())?;
    file.write_all(&CHUNK_BIN.to_le_bytes())?;
    file.write_all(&bin_data)?;

    Ok(())
}

#[cfg(test)]
mod skeleton_glb_tests {
    use super::*;
    use oxihuman_mesh::skeleton::Skeleton;
    use oxihuman_morph::engine::MeshBuffers as MB;

    fn suited_mesh_for_skin() -> MeshBuffers {
        MeshBuffers::from_morph(MB {
            positions: vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
            normals: vec![[0.0, 0.0, 1.0]; 3],
            uvs: vec![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
            indices: vec![0, 1, 2],
            has_suit: true,
        })
    }

    #[test]
    fn export_glb_with_skeleton_creates_file() {
        let mesh = suited_mesh_for_skin();
        let skeleton = Skeleton::human_body();
        let path = std::path::Path::new("/tmp/test_skeleton.glb");
        export_glb_with_skeleton(&mesh, &skeleton, path).expect("export_glb_with_skeleton failed");

        assert!(path.exists(), "GLB file was not created");

        // Verify GLB header
        verify_glb_header(path).expect("GLB header invalid");

        std::fs::remove_file(path).ok();
    }

    #[test]
    fn skeleton_glb_has_nodes_array() {
        let mesh = suited_mesh_for_skin();
        let skeleton = Skeleton::human_body();
        let path = std::path::Path::new("/tmp/test_skeleton_nodes.glb");
        export_glb_with_skeleton(&mesh, &skeleton, path).expect("export_glb_with_skeleton failed");

        // Read the file and parse the JSON chunk
        use std::io::Read;
        let mut f = std::fs::File::open(path).expect("should succeed");

        // Skip 12-byte GLB header
        let mut buf12 = [0u8; 12];
        f.read_exact(&mut buf12).expect("should succeed");

        // Read JSON chunk header (8 bytes: length + type)
        let mut chunk_hdr = [0u8; 8];
        f.read_exact(&mut chunk_hdr).expect("should succeed");
        let json_len = u32::from_le_bytes(chunk_hdr[0..4].try_into().expect("should succeed")) as usize;

        // Read JSON bytes
        let mut json_buf = vec![0u8; json_len];
        f.read_exact(&mut json_buf).expect("should succeed");

        let json_str = std::str::from_utf8(&json_buf)
            .expect("should succeed")
            .trim_end_matches(' ');
        let parsed: serde_json::Value = serde_json::from_str(json_str).expect("should succeed");

        let nodes = parsed["nodes"].as_array().expect("nodes must be an array");
        let expected_len = skeleton.joints.len() + 1; // joints + mesh node
        assert_eq!(
            nodes.len(),
            expected_len,
            "expected {} nodes (joints + mesh node), got {}",
            expected_len,
            nodes.len()
        );

        std::fs::remove_file(path).ok();
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Material-aware GLB export
// ─────────────────────────────────────────────────────────────────────────────

use crate::material::PbrMaterial;

/// Export a MeshBuffers to a GLB 2.0 file with an explicit PBR material.
/// The material is embedded in the `materials` array and referenced from the
/// mesh primitive.
#[allow(dead_code)]
pub fn export_glb_with_material(
    mesh: &MeshBuffers,
    material: &PbrMaterial,
    path: &Path,
) -> anyhow::Result<()> {
    ensure_suit_mesh(mesh)?;

    // ── 1. Build BIN chunk ───────────────────────────────────────────────────
    let n_verts = mesh.positions.len();
    let n_idx = mesh.indices.len();

    let pos_bytes: &[u8] = cast_slice(&mesh.positions);
    let norm_bytes: &[u8] = cast_slice(&mesh.normals);
    let uv_bytes: &[u8] = cast_slice(&mesh.uvs);
    let idx_bytes: &[u8] = cast_slice(&mesh.indices);

    let pos_offset = 0usize;
    let norm_offset = pos_offset + pos_bytes.len();
    let uv_offset = norm_offset + norm_bytes.len();
    let idx_offset = uv_offset + uv_bytes.len();
    let bin_len = idx_offset + idx_bytes.len();

    let mut bin_data: Vec<u8> = Vec::with_capacity(bin_len + 3);
    bin_data.extend_from_slice(pos_bytes);
    bin_data.extend_from_slice(norm_bytes);
    bin_data.extend_from_slice(uv_bytes);
    bin_data.extend_from_slice(idx_bytes);
    while !bin_data.len().is_multiple_of(4) {
        bin_data.push(0x00);
    }
    let total_bin = bin_data.len() as u32;

    // ── 2. Build GLTF JSON ───────────────────────────────────────────────────
    let material_json = material.to_gltf_json();

    let gltf = json!({
        "asset": { "version": "2.0", "generator": "OxiHuman 0.1.0" },
        "scene": 0,
        "scenes": [{ "nodes": [0] }],
        "nodes": [{ "mesh": 0 }],
        "meshes": [{
            "name": "human",
            "primitives": [{
                "attributes": {
                    "POSITION":   0,
                    "NORMAL":     1,
                    "TEXCOORD_0": 2
                },
                "indices": 3,
                "mode": 4,
                "material": 0
            }]
        }],
        "materials": [material_json],
        "accessors": [
            { "bufferView": 0, "componentType": 5126, "count": n_verts, "type": "VEC3" },
            { "bufferView": 1, "componentType": 5126, "count": n_verts, "type": "VEC3" },
            { "bufferView": 2, "componentType": 5126, "count": n_verts, "type": "VEC2" },
            { "bufferView": 3, "componentType": 5125, "count": n_idx,   "type": "SCALAR" }
        ],
        "bufferViews": [
            { "buffer": 0, "byteOffset": pos_offset,  "byteLength": pos_bytes.len()  },
            { "buffer": 0, "byteOffset": norm_offset, "byteLength": norm_bytes.len() },
            { "buffer": 0, "byteOffset": uv_offset,   "byteLength": uv_bytes.len()   },
            { "buffer": 0, "byteOffset": idx_offset,  "byteLength": idx_bytes.len()  }
        ],
        "buffers": [{ "byteLength": total_bin }]
    });

    let mut json_bytes = serde_json::to_vec(&gltf)?;
    while json_bytes.len() % 4 != 0 {
        json_bytes.push(b' ');
    }

    // ── 3. Write GLB ─────────────────────────────────────────────────────────
    let json_chunk_len = json_bytes.len() as u32;
    let bin_chunk_len = bin_data.len() as u32;
    let total_len = 12 + 8 + json_chunk_len + 8 + bin_chunk_len;

    let mut file = std::fs::File::create(path)?;
    file.write_all(&GLB_MAGIC.to_le_bytes())?;
    file.write_all(&GLB_VERSION.to_le_bytes())?;
    file.write_all(&total_len.to_le_bytes())?;
    file.write_all(&json_chunk_len.to_le_bytes())?;
    file.write_all(&CHUNK_JSON.to_le_bytes())?;
    file.write_all(&json_bytes)?;
    file.write_all(&bin_chunk_len.to_le_bytes())?;
    file.write_all(&CHUNK_BIN.to_le_bytes())?;
    file.write_all(&bin_data)?;

    Ok(())
}

#[cfg(test)]
mod material_glb_tests {
    use super::*;
    use crate::material::PbrMaterial;
    use oxihuman_morph::engine::MeshBuffers as MB;

    fn suited_mesh_for_material() -> MeshBuffers {
        MeshBuffers::from_morph(MB {
            positions: vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
            normals: vec![[0.0, 0.0, 1.0]; 3],
            uvs: vec![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
            indices: vec![0, 1, 2],
            has_suit: true,
        })
    }

    #[test]
    fn export_glb_with_material_creates_file() {
        let mesh = suited_mesh_for_material();
        let path = std::path::Path::new("/tmp/test_material.glb");
        export_glb_with_material(&mesh, &PbrMaterial::skin(), path)
            .expect("export_glb_with_material failed");
        assert!(path.exists(), "GLB file was not created");
        std::fs::remove_file(path).ok();
    }

    #[test]
    fn glb_with_material_references_material_zero() {
        let mesh = suited_mesh_for_material();
        let path = std::path::Path::new("/tmp/test_material_ref.glb");
        export_glb_with_material(&mesh, &PbrMaterial::skin(), path)
            .expect("export_glb_with_material failed");

        // Parse the JSON chunk from the GLB
        use std::io::Read;
        let mut f = std::fs::File::open(path).expect("should succeed");

        // Skip 12-byte GLB header
        let mut buf12 = [0u8; 12];
        f.read_exact(&mut buf12).expect("should succeed");

        // Read JSON chunk header (8 bytes: length + type)
        let mut chunk_hdr = [0u8; 8];
        f.read_exact(&mut chunk_hdr).expect("should succeed");
        let json_len = u32::from_le_bytes(chunk_hdr[0..4].try_into().expect("should succeed")) as usize;

        // Read JSON bytes
        let mut json_buf = vec![0u8; json_len];
        f.read_exact(&mut json_buf).expect("should succeed");

        let json_str = std::str::from_utf8(&json_buf)
            .expect("should succeed")
            .trim_end_matches(' ');
        let parsed: serde_json::Value = serde_json::from_str(json_str).expect("should succeed");

        let material_idx = parsed["meshes"][0]["primitives"][0]["material"]
            .as_u64()
            .expect("material field should be an integer");
        assert_eq!(material_idx, 0, "primitive should reference material 0");

        std::fs::remove_file(path).ok();
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Metadata-aware GLB export
// ─────────────────────────────────────────────────────────────────────────────

/// Export a GLB with OxiHuman metadata embedded in `asset.extras`.
///
/// Calls the same BIN/JSON construction as [`export_glb`] and then patches
/// `gltf_json["asset"]["extras"]` with the serialised [`crate::metadata::OxiHumanMeta`].
pub fn export_glb_with_meta(
    mesh: &MeshBuffers,
    meta: &crate::metadata::OxiHumanMeta,
    path: &Path,
) -> anyhow::Result<()> {
    ensure_suit_mesh(mesh)?;

    // ── 1. Build BIN chunk ───────────────────────────────────────────────────
    let n_verts = mesh.positions.len();
    let n_idx = mesh.indices.len();

    let pos_bytes: &[u8] = cast_slice(&mesh.positions);
    let norm_bytes: &[u8] = cast_slice(&mesh.normals);
    let uv_bytes: &[u8] = cast_slice(&mesh.uvs);
    let idx_bytes: &[u8] = cast_slice(&mesh.indices);

    let pos_offset = 0usize;
    let norm_offset = pos_offset + pos_bytes.len();
    let uv_offset = norm_offset + norm_bytes.len();
    let idx_offset = uv_offset + uv_bytes.len();
    let bin_len = idx_offset + idx_bytes.len();

    let mut bin_data: Vec<u8> = Vec::with_capacity(bin_len + 3);
    bin_data.extend_from_slice(pos_bytes);
    bin_data.extend_from_slice(norm_bytes);
    bin_data.extend_from_slice(uv_bytes);
    bin_data.extend_from_slice(idx_bytes);
    while !bin_data.len().is_multiple_of(4) {
        bin_data.push(0x00);
    }
    let total_bin = bin_data.len() as u32;

    // ── 2. Build GLTF JSON and patch asset.extras ────────────────────────────
    let mut gltf_json = json!({
        "asset": { "version": "2.0", "generator": "OxiHuman 0.1.0" },
        "scene": 0,
        "scenes": [{ "nodes": [0] }],
        "nodes": [{ "mesh": 0 }],
        "meshes": [{
            "name": "human",
            "primitives": [{
                "attributes": {
                    "POSITION":   0,
                    "NORMAL":     1,
                    "TEXCOORD_0": 2
                },
                "indices": 3,
                "mode": 4
            }]
        }],
        "accessors": [
            { "bufferView": 0, "componentType": 5126, "count": n_verts, "type": "VEC3" },
            { "bufferView": 1, "componentType": 5126, "count": n_verts, "type": "VEC3" },
            { "bufferView": 2, "componentType": 5126, "count": n_verts, "type": "VEC2" },
            { "bufferView": 3, "componentType": 5125, "count": n_idx,   "type": "SCALAR" }
        ],
        "bufferViews": [
            { "buffer": 0, "byteOffset": pos_offset,  "byteLength": pos_bytes.len()  },
            { "buffer": 0, "byteOffset": norm_offset, "byteLength": norm_bytes.len() },
            { "buffer": 0, "byteOffset": uv_offset,   "byteLength": uv_bytes.len()   },
            { "buffer": 0, "byteOffset": idx_offset,  "byteLength": idx_bytes.len()  }
        ],
        "buffers": [{ "byteLength": total_bin }]
    });

    gltf_json["asset"]["extras"] = meta.to_json();

    let mut json_bytes = serde_json::to_vec(&gltf_json)?;
    while json_bytes.len() % 4 != 0 {
        json_bytes.push(b' ');
    }

    // ── 3. Write GLB ─────────────────────────────────────────────────────────
    let json_chunk_len = json_bytes.len() as u32;
    let bin_chunk_len = bin_data.len() as u32;
    let total_len = 12 + 8 + json_chunk_len + 8 + bin_chunk_len;

    let mut file = std::fs::File::create(path)?;
    file.write_all(&GLB_MAGIC.to_le_bytes())?;
    file.write_all(&GLB_VERSION.to_le_bytes())?;
    file.write_all(&total_len.to_le_bytes())?;
    file.write_all(&json_chunk_len.to_le_bytes())?;
    file.write_all(&CHUNK_JSON.to_le_bytes())?;
    file.write_all(&json_bytes)?;
    file.write_all(&bin_chunk_len.to_le_bytes())?;
    file.write_all(&CHUNK_BIN.to_le_bytes())?;
    file.write_all(&bin_data)?;

    Ok(())
}

#[cfg(test)]
mod meta_glb_tests {
    use super::*;
    use crate::metadata::OxiHumanMeta;
    use oxihuman_morph::engine::MeshBuffers as MB;

    fn suited_mesh_for_meta() -> MeshBuffers {
        MeshBuffers::from_morph(MB {
            positions: vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
            normals: vec![[0.0, 0.0, 1.0]; 3],
            uvs: vec![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
            indices: vec![0, 1, 2],
            has_suit: true,
        })
    }

    #[test]
    fn export_glb_with_meta_creates_file() {
        let mesh = suited_mesh_for_meta();
        let meta = OxiHumanMeta::minimal();
        let path = std::path::Path::new("/tmp/test_meta.glb");
        export_glb_with_meta(&mesh, &meta, path).expect("export_glb_with_meta failed");
        assert!(path.exists(), "GLB file was not created");
        verify_glb_header(path).expect("GLB header invalid");
        std::fs::remove_file(path).ok();
    }

    #[test]
    fn glb_with_meta_has_extras_in_json() {
        let mesh = suited_mesh_for_meta();
        let meta = OxiHumanMeta::minimal();
        let path = std::path::Path::new("/tmp/test_meta_extras.glb");
        export_glb_with_meta(&mesh, &meta, path).expect("export_glb_with_meta failed");

        use std::io::Read;
        let mut f = std::fs::File::open(path).expect("should succeed");

        // Skip 12-byte GLB header
        let mut buf12 = [0u8; 12];
        f.read_exact(&mut buf12).expect("should succeed");

        // Read JSON chunk header (8 bytes)
        let mut chunk_hdr = [0u8; 8];
        f.read_exact(&mut chunk_hdr).expect("should succeed");
        let json_len = u32::from_le_bytes(chunk_hdr[0..4].try_into().expect("should succeed")) as usize;

        let mut json_buf = vec![0u8; json_len];
        f.read_exact(&mut json_buf).expect("should succeed");

        let json_str = std::str::from_utf8(&json_buf)
            .expect("should succeed")
            .trim_end_matches(' ');
        let parsed: serde_json::Value = serde_json::from_str(json_str).expect("should succeed");

        let generator = parsed["asset"]["extras"]["generator"]
            .as_str()
            .expect("asset.extras.generator must be a string");
        assert_eq!(generator, "oxihuman-export");

        std::fs::remove_file(path).ok();
    }
}