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
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! End-to-end pipeline: base mesh + targets + params → GLB file.

use anyhow::{Context, Result};

use oxihuman_core::parser::obj::parse_obj;
use oxihuman_core::parser::target::parse_target;
use oxihuman_core::policy::{Policy, PolicyProfile};
use oxihuman_mesh::mesh::MeshBuffers;
use oxihuman_mesh::normals::compute_normals;
use oxihuman_mesh::suit::apply_suit_flag;
use oxihuman_morph::engine::HumanEngine;
use oxihuman_morph::params::ParamState;

use crate::glb::export_glb;

/// Configuration for the full build pipeline.
pub struct PipelineConfig {
    /// Path to the base .obj mesh file.
    pub base_obj_path: std::path::PathBuf,
    /// Optional directory of .target files to load.
    pub targets_dir: Option<std::path::PathBuf>,
    /// Maximum number of targets to load (None = all).
    pub max_targets: Option<usize>,
    /// Policy profile for target filtering.
    pub policy: Policy,
    /// Parameters for morphing.
    pub params: ParamState,
    /// Output GLB path.
    pub output_path: std::path::PathBuf,
}

impl PipelineConfig {
    pub fn new(
        base_obj_path: impl Into<std::path::PathBuf>,
        output_path: impl Into<std::path::PathBuf>,
    ) -> Self {
        PipelineConfig {
            base_obj_path: base_obj_path.into(),
            targets_dir: None,
            max_targets: None,
            policy: Policy::new(PolicyProfile::Standard),
            params: ParamState::default(),
            output_path: output_path.into(),
        }
    }
}

/// Run the full OxiHuman pipeline and write a GLB file.
///
/// Steps:
/// 1. Parse base .obj
/// 2. Create HumanEngine
/// 3. Load targets from directory (if configured)
/// 4. Set params
/// 5. Build morphed mesh
/// 6. Compute normals
/// 7. Apply suit flag
/// 8. Export GLB
pub fn run_pipeline(config: PipelineConfig) -> Result<MeshBuffers> {
    // Step 1: Parse base mesh
    let obj_src = std::fs::read_to_string(&config.base_obj_path)
        .with_context(|| format!("reading base OBJ: {}", config.base_obj_path.display()))?;
    let base = parse_obj(&obj_src).with_context(|| "parsing base OBJ")?;

    // Step 2: Create engine
    let mut engine = HumanEngine::new(base, config.policy);

    // Step 3: Load targets
    if let Some(ref targets_dir) = config.targets_dir {
        if targets_dir.exists() {
            let max = config.max_targets.unwrap_or(usize::MAX);
            let mut loaded = 0usize;
            for entry in std::fs::read_dir(targets_dir)
                .with_context(|| format!("reading targets dir: {}", targets_dir.display()))?
            {
                if loaded >= max {
                    break;
                }
                let entry = entry?;
                let path = entry.path();
                if path.extension().map(|e| e == "target").unwrap_or(false) {
                    let name = path
                        .file_stem()
                        .and_then(|s| s.to_str())
                        .unwrap_or("unknown")
                        .to_string();
                    if let Ok(src) = std::fs::read_to_string(&path) {
                        if let Ok(target) = parse_target(&name, &src) {
                            engine.load_target(target, Box::new(|p: &ParamState| p.weight));
                            loaded += 1;
                        }
                    }
                }
            }
        }
    }

    // Step 4: Set params
    engine.set_params(config.params);

    // Step 5: Build morphed mesh
    let morph_buffers = engine.build_mesh();

    // Step 6: Construct MeshBuffers and compute normals
    let mut mesh = MeshBuffers::from_morph(morph_buffers);
    compute_normals(&mut mesh);

    // Step 7: Apply suit flag
    apply_suit_flag(&mut mesh);

    // Step 8: Export GLB
    export_glb(&mesh, &config.output_path)
        .with_context(|| format!("exporting GLB to {}", config.output_path.display()))?;

    Ok(mesh)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::glb::verify_glb_header;
    use oxihuman_morph::params::ParamState;
    use proptest::prelude::*;

    fn makehuman_data_dir() -> std::path::PathBuf {
        std::env::var("MAKEHUMAN_DATA_DIR")
            .map(std::path::PathBuf::from)
            .unwrap_or_else(|_| std::path::PathBuf::from("/tmp/oxihuman_nonexistent_data"))
    }
    fn base_obj() -> std::path::PathBuf {
        makehuman_data_dir().join("3dobjs/base.obj")
    }
    fn targets_dir() -> std::path::PathBuf {
        makehuman_data_dir().join("targets/bodyshapes")
    }

    #[test]
    fn pipeline_produces_valid_glb() {
        if !base_obj().exists() {
            return; // skip in CI without assets
        }
        let out = std::path::PathBuf::from("/tmp/oxihuman_pipeline_test.glb");
        let config = PipelineConfig {
            base_obj_path: base_obj(),
            targets_dir: Some(targets_dir()),
            max_targets: Some(5),
            policy: Policy::new(PolicyProfile::Standard),
            params: ParamState::new(0.6, 0.4, 0.5, 0.3),
            output_path: out.clone(),
        };
        let mesh = run_pipeline(config).expect("pipeline failed");
        assert!(
            mesh.positions.len() > 10_000,
            "base mesh should have many vertices"
        );
        assert!(mesh.has_suit, "suit flag must be set");
        verify_glb_header(&out).expect("GLB header invalid");
        std::fs::remove_file(&out).ok();
    }

    #[test]
    fn pipeline_no_targets_still_works() {
        if !base_obj().exists() {
            return;
        }
        let out = std::path::PathBuf::from("/tmp/oxihuman_notargets.glb");
        let config = PipelineConfig::new(base_obj(), out.clone());
        let mesh = run_pipeline(config).expect("pipeline (no targets) failed");
        assert!(!mesh.positions.is_empty());
        verify_glb_header(&out).expect("should succeed");
        std::fs::remove_file(&out).ok();
    }

    #[test]
    fn pipeline_vertex_positions_finite() {
        if !base_obj().exists() {
            return;
        }
        let out = std::path::PathBuf::from("/tmp/oxihuman_finite.glb");
        let config = PipelineConfig {
            base_obj_path: base_obj(),
            targets_dir: Some(targets_dir()),
            max_targets: Some(10),
            policy: Policy::new(PolicyProfile::Standard),
            params: ParamState::new(1.0, 1.0, 1.0, 1.0),
            output_path: out.clone(),
        };
        let mesh = run_pipeline(config).expect("should succeed");
        for pos in &mesh.positions {
            assert!(pos[0].is_finite(), "non-finite x at {:?}", pos);
            assert!(pos[1].is_finite(), "non-finite y at {:?}", pos);
            assert!(pos[2].is_finite(), "non-finite z at {:?}", pos);
        }
        std::fs::remove_file(&out).ok();
    }

    proptest! {
        #[test]
        fn random_params_pipeline_finite_positions(
            h in 0.0f32..=1.0f32,
            w in 0.0f32..=1.0f32,
            m in 0.0f32..=1.0f32,
            a in 0.0f32..=1.0f32,
        ) {
            let base_obj_path = base_obj();
            if !base_obj_path.exists() { return Ok(()); }

            // Use a simple in-memory OBJ (not the 19k vertex one — too slow for proptest)
            use oxihuman_core::parser::obj::parse_obj;
            let simple_obj = "v 0 0 0\nv 1 0 0\nv 0 1 0\nvt 0 0\nvt 1 0\nvt 0 1\nvn 0 0 1\nf 1/1/1 2/2/1 3/3/1\n";
            let base = parse_obj(simple_obj).expect("should succeed");

            use oxihuman_morph::engine::HumanEngine;
            use oxihuman_core::policy::{Policy, PolicyProfile};
            let mut engine = HumanEngine::new(base, Policy::new(PolicyProfile::Standard));
            engine.set_params(ParamState::new(h, w, m, a));
            let morph_buf = engine.build_mesh();

            use oxihuman_mesh::mesh::MeshBuffers;
            use oxihuman_mesh::normals::compute_normals;
            use oxihuman_mesh::integrity::check_positions_finite;
            let mut mesh = MeshBuffers::from_morph(morph_buf);
            compute_normals(&mut mesh);

            prop_assert!(
                check_positions_finite(&mesh),
                "non-finite positions with h={} w={} m={} a={}",
                h, w, m, a
            );
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// ExportPipeline builder
// ─────────────────────────────────────────────────────────────────────────────

use bytemuck::cast_slice;
use serde_json::json;
use std::io::Write;

use crate::blend_shapes::BlendShape;
use crate::material::PbrMaterial;
use crate::metadata::OxiHumanMeta;
use oxihuman_mesh::skeleton::Skeleton;

// GLB magic constants (re-declared to keep this section self-contained)
const EP_GLB_MAGIC: u32 = 0x46546C67; // "glTF"
const EP_GLB_VER: u32 = 2;
const EP_CHUNK_JSON: u32 = 0x4E4F534A; // "JSON"
const EP_CHUNK_BIN: u32 = 0x004E4942; // "BIN\0"

/// Builder for composing multiple export features into a single GLB file.
///
/// Enables material + metadata + skeleton + blend shapes to be written in one
/// call, eliminating the need to invoke multiple specialised export functions.
#[derive(Default)]
pub struct ExportPipeline {
    material: Option<PbrMaterial>,
    meta: Option<OxiHumanMeta>,
    skeleton: Option<Skeleton>,
    blend_shapes: Vec<BlendShape>,
    /// If true, include TANGENT accessor.
    include_tangents: bool,
    /// If true, include COLOR_0 accessor.
    include_colors: bool,
    /// If true, embed a NORMAL accessor.
    #[allow(dead_code)]
    include_normals: bool,
}

impl ExportPipeline {
    /// Create a new empty pipeline.
    pub fn new() -> Self {
        Self::default()
    }

    /// Include a PBR material.
    pub fn with_material(mut self, m: PbrMaterial) -> Self {
        self.material = Some(m);
        self
    }

    /// Embed OxiHuman metadata in `asset.extras`.
    pub fn with_meta(mut self, m: OxiHumanMeta) -> Self {
        self.meta = Some(m);
        self
    }

    /// Include skeleton / skin data.
    pub fn with_skeleton(mut self, s: Skeleton) -> Self {
        self.skeleton = Some(s);
        self
    }

    /// Add morph targets (blend shapes).
    pub fn with_blend_shapes(mut self, shapes: Vec<BlendShape>) -> Self {
        self.blend_shapes = shapes;
        self
    }

    /// Include TANGENT accessor (requires `mesh.tangents` to be non-empty).
    pub fn with_tangents(mut self) -> Self {
        self.include_tangents = true;
        self
    }

    /// Include COLOR_0 accessor (requires `mesh.colors` to be `Some`).
    pub fn with_colors(mut self) -> Self {
        self.include_colors = true;
        self
    }

    /// Returns a human-readable description of the enabled features.
    pub fn describe(&self) -> String {
        let mut parts: Vec<&str> = Vec::new();
        if self.material.is_some() {
            parts.push("material");
        }
        if self.meta.is_some() {
            parts.push("metadata");
        }
        if self.skeleton.is_some() {
            parts.push("skeleton");
        }
        if !self.blend_shapes.is_empty() {
            parts.push("blend_shapes");
        }
        if self.include_tangents {
            parts.push("tangents");
        }
        if self.include_colors {
            parts.push("colors");
        }
        if self.include_normals {
            parts.push("normals");
        }
        if parts.is_empty() {
            "ExportPipeline[minimal]".to_string()
        } else {
            format!("ExportPipeline[{}]", parts.join(", "))
        }
    }

    /// Execute the pipeline: build and write a GLB to `path`.
    ///
    /// Writes a self-contained GLB 2.0 binary incorporating all configured
    /// features: positions, normals, UVs, indices, optional tangents, optional
    /// colors, optional morph targets, optional material, optional metadata in
    /// `asset.extras`, and optional skin/skeleton.
    pub fn export(self, mesh: &MeshBuffers, path: &std::path::Path) -> anyhow::Result<()> {
        let n_verts = mesh.positions.len();
        let n_idx = mesh.indices.len();

        // ── 1. Build BIN chunk ────────────────────────────────────────────────
        // Layout:
        //   POSITION   : f32*3 x n_verts
        //   NORMAL     : f32*3 x n_verts
        //   TEXCOORD_0 : f32*2 x n_verts
        //   INDEX      : u32   x n_idx
        //   TANGENT    : f32*4 x n_verts  (optional)
        //   COLOR_0    : f32*4 x n_verts  (optional)
        //   morph_i    : f32*3 x n_verts  (one block per blend shape)

        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 cursor = idx_offset + idx_bytes.len();

        // Tangents (optional)
        let has_tangents = self.include_tangents && !mesh.tangents.is_empty();
        let tangent_offset = cursor;
        if has_tangents {
            let tb: &[u8] = cast_slice(&mesh.tangents);
            cursor += tb.len();
        }

        // Colors (optional)
        let has_colors = self.include_colors && mesh.colors.is_some();
        let color_offset = cursor;
        if has_colors {
            if let Some(cols) = mesh.colors.as_ref() {
                let cb: &[u8] = cast_slice(cols.as_slice());
                cursor += cb.len();
            }
        }

        // Blend shape delta blocks
        let mut morph_offsets: Vec<usize> = Vec::with_capacity(self.blend_shapes.len());
        for shape in &self.blend_shapes {
            morph_offsets.push(cursor);
            cursor += shape.position_deltas.len() * std::mem::size_of::<[f32; 3]>();
        }

        // Assemble BIN buffer
        let mut bin_data: Vec<u8> = Vec::with_capacity(cursor);
        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 has_tangents {
            let tb: &[u8] = cast_slice(&mesh.tangents);
            bin_data.extend_from_slice(tb);
        }
        if has_colors {
            if let Some(cols) = mesh.colors.as_ref() {
                let cb: &[u8] = cast_slice(cols.as_slice());
                bin_data.extend_from_slice(cb);
            }
        }
        for shape in &self.blend_shapes {
            let db: &[u8] = cast_slice(&shape.position_deltas);
            bin_data.extend_from_slice(db);
        }

        // Pad BIN to 4-byte boundary
        while !bin_data.len().is_multiple_of(4) {
            bin_data.push(0x00);
        }
        let total_bin = bin_data.len() as u32;

        // ── 2. Build GLTF accessors and buffer views ───────────────────────────
        let mut accessors: Vec<serde_json::Value> = Vec::new();
        let mut buffer_views: Vec<serde_json::Value> = Vec::new();

        // POSITION
        buffer_views.push(json!({
            "buffer": 0,
            "byteOffset": pos_offset,
            "byteLength": pos_bytes.len()
        }));
        let pos_acc = accessors.len();
        accessors.push(json!({
            "bufferView": buffer_views.len() - 1,
            "componentType": 5126,
            "count": n_verts,
            "type": "VEC3"
        }));

        // NORMAL
        buffer_views.push(json!({
            "buffer": 0,
            "byteOffset": norm_offset,
            "byteLength": norm_bytes.len()
        }));
        let norm_acc = accessors.len();
        accessors.push(json!({
            "bufferView": buffer_views.len() - 1,
            "componentType": 5126,
            "count": n_verts,
            "type": "VEC3"
        }));

        // TEXCOORD_0
        buffer_views.push(json!({
            "buffer": 0,
            "byteOffset": uv_offset,
            "byteLength": uv_bytes.len()
        }));
        let uv_acc = accessors.len();
        accessors.push(json!({
            "bufferView": buffer_views.len() - 1,
            "componentType": 5126,
            "count": n_verts,
            "type": "VEC2"
        }));

        // INDEX
        buffer_views.push(json!({
            "buffer": 0,
            "byteOffset": idx_offset,
            "byteLength": idx_bytes.len()
        }));
        let idx_acc = accessors.len();
        accessors.push(json!({
            "bufferView": buffer_views.len() - 1,
            "componentType": 5125,
            "count": n_idx,
            "type": "SCALAR"
        }));

        // Optional TANGENT
        let tangent_acc: Option<usize> = if has_tangents {
            let tb: &[u8] = cast_slice(&mesh.tangents);
            buffer_views.push(json!({
                "buffer": 0,
                "byteOffset": tangent_offset,
                "byteLength": tb.len()
            }));
            let acc = accessors.len();
            accessors.push(json!({
                "bufferView": buffer_views.len() - 1,
                "componentType": 5126,
                "count": n_verts,
                "type": "VEC4"
            }));
            Some(acc)
        } else {
            None
        };

        // Optional COLOR_0
        let color_acc: Option<usize> = if let (true, Some(cols)) = (has_colors, mesh.colors.as_ref()) {
            let cb: &[u8] = cast_slice(cols.as_slice());
            buffer_views.push(json!({
                "buffer": 0,
                "byteOffset": color_offset,
                "byteLength": cb.len()
            }));
            let acc = accessors.len();
            accessors.push(json!({
                "bufferView": buffer_views.len() - 1,
                "componentType": 5126,
                "count": n_verts,
                "type": "VEC4"
            }));
            Some(acc)
        } else {
            None
        };

        // Morph target delta accessors
        let mut morph_targets: Vec<serde_json::Value> = Vec::new();
        for (i, shape) in self.blend_shapes.iter().enumerate() {
            let delta_len = shape.position_deltas.len() * std::mem::size_of::<[f32; 3]>();
            buffer_views.push(json!({
                "buffer": 0,
                "byteOffset": morph_offsets[i],
                "byteLength": delta_len
            }));
            let acc = accessors.len();
            let mut acc_node = json!({
                "bufferView": buffer_views.len() - 1,
                "componentType": 5126,
                "count": n_verts,
                "type": "VEC3"
            });
            if !shape.position_deltas.is_empty() {
                let mut mn = [f32::INFINITY; 3];
                let mut mx = [f32::NEG_INFINITY; 3];
                for d in &shape.position_deltas {
                    for k in 0..3 {
                        mn[k] = mn[k].min(d[k]);
                        mx[k] = mx[k].max(d[k]);
                    }
                }
                acc_node["min"] = json!([mn[0], mn[1], mn[2]]);
                acc_node["max"] = json!([mx[0], mx[1], mx[2]]);
            }
            accessors.push(acc_node);
            morph_targets.push(json!({ "POSITION": acc }));
        }

        // ── 3. Build primitive and mesh node ──────────────────────────────────
        let mut attributes = json!({
            "POSITION": pos_acc,
            "NORMAL": norm_acc,
            "TEXCOORD_0": uv_acc
        });
        if let Some(t) = tangent_acc {
            attributes["TANGENT"] = json!(t);
        }
        if let Some(c) = color_acc {
            attributes["COLOR_0"] = json!(c);
        }

        let mut primitive = json!({
            "attributes": attributes,
            "indices": idx_acc
        });
        if !morph_targets.is_empty() {
            primitive["targets"] = json!(morph_targets);
        }

        let morph_weights: Vec<f32> = vec![0.0; self.blend_shapes.len()];
        let target_names: Vec<&str> = self.blend_shapes.iter().map(|s| s.name.as_str()).collect();

        let mut mesh_node = json!({
            "primitives": [primitive]
        });
        if !morph_weights.is_empty() {
            mesh_node["weights"] = json!(morph_weights);
            mesh_node["extras"] = json!({ "targetNames": target_names });
        }

        // ── 4. Material ────────────────────────────────────────────────────────
        let mut materials_arr: Vec<serde_json::Value> = Vec::new();
        if let Some(ref mat) = self.material {
            materials_arr.push(mat.to_gltf_json());
            mesh_node["primitives"][0]["material"] = json!(0);
        }

        // ── 5. Skeleton / skin ─────────────────────────────────────────────────
        let skins_val: Option<serde_json::Value>;
        let mut nodes_arr: Vec<serde_json::Value> = Vec::new();

        if let Some(ref skel) = self.skeleton {
            let joint_node_start = 1usize; // node 0 = mesh node
            for joint in &skel.joints {
                nodes_arr.push(json!({
                    "name": joint.name,
                    "translation": [
                        joint.translation[0],
                        joint.translation[1],
                        joint.translation[2]
                    ]
                }));
            }
            let joint_indices: Vec<usize> =
                (joint_node_start..joint_node_start + skel.joints.len()).collect();
            nodes_arr.insert(0, json!({ "mesh": 0, "skin": 0 }));
            skins_val = Some(json!([{
                "joints": joint_indices,
                "name": "Armature"
            }]));
        } else {
            nodes_arr.push(json!({ "mesh": 0 }));
            skins_val = None;
        }

        // ── 6. Asset node (with optional extras) ──────────────────────────────
        let mut asset = json!({
            "version": "2.0",
            "generator": "oxihuman-export"
        });
        if let Some(ref m) = self.meta {
            asset["extras"] = m.to_json();
        }

        // ── 7. Assemble GLTF JSON ─────────────────────────────────────────────
        let mut gltf = json!({
            "asset": asset,
            "scene": 0,
            "scenes": [{ "nodes": [0] }],
            "nodes": nodes_arr,
            "meshes": [mesh_node],
            "accessors": accessors,
            "bufferViews": buffer_views,
            "buffers": [{ "byteLength": total_bin }]
        });
        if !materials_arr.is_empty() {
            gltf["materials"] = json!(materials_arr);
        }
        if let Some(s) = skins_val {
            gltf["skins"] = s;
        }

        // ── 8. Write GLB binary ───────────────────────────────────────────────
        let mut json_bytes = serde_json::to_vec(&gltf)?;
        while !json_bytes.len().is_multiple_of(4) {
            json_bytes.push(b' ');
        }

        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(&EP_GLB_MAGIC.to_le_bytes())?;
        file.write_all(&EP_GLB_VER.to_le_bytes())?;
        file.write_all(&total_len.to_le_bytes())?;
        file.write_all(&json_chunk_len.to_le_bytes())?;
        file.write_all(&EP_CHUNK_JSON.to_le_bytes())?;
        file.write_all(&json_bytes)?;
        file.write_all(&bin_chunk_len.to_le_bytes())?;
        file.write_all(&EP_CHUNK_BIN.to_le_bytes())?;
        file.write_all(&bin_data)?;

        Ok(())
    }
}

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

    fn triangle() -> MeshBuffers {
        MeshBuffers {
            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]; 3],
            tangents: vec![[1.0, 0.0, 0.0, 1.0]; 3],
            colors: Some(vec![[1.0, 1.0, 1.0, 1.0]; 3]),
            indices: vec![0, 1, 2],
            has_suit: true,
        }
    }

    #[test]
    fn pipeline_minimal_export() {
        let mesh = triangle();
        let path = std::path::Path::new("/tmp/test_pipeline_minimal.glb");
        ExportPipeline::new()
            .export(&mesh, path)
            .expect("minimal export failed");
        assert!(path.exists(), "GLB file not created");
        std::fs::remove_file(path).ok();
    }

    #[test]
    fn pipeline_with_material() {
        let mesh = triangle();
        let path = std::path::Path::new("/tmp/test_pipeline_material.glb");
        ExportPipeline::new()
            .with_material(PbrMaterial::skin())
            .export(&mesh, path)
            .expect("material export failed");
        assert!(path.exists(), "GLB file not created");
        std::fs::remove_file(path).ok();
    }

    #[test]
    fn pipeline_with_meta() {
        let mesh = triangle();
        let path = std::path::Path::new("/tmp/test_pipeline_meta.glb");
        ExportPipeline::new()
            .with_meta(OxiHumanMeta::minimal())
            .export(&mesh, path)
            .expect("meta export failed");
        assert!(path.exists(), "GLB file not created");
        let bytes = std::fs::read(path).expect("should succeed");
        assert!(bytes.len() >= 4);
        assert_eq!(&bytes[0..4], b"glTF", "invalid GLB magic");
        std::fs::remove_file(path).ok();
    }

    #[test]
    fn pipeline_describe_empty() {
        let desc = ExportPipeline::new().describe();
        assert!(
            !desc.is_empty(),
            "describe() should return non-empty string"
        );
    }

    #[test]
    fn pipeline_describe_with_features() {
        let desc = ExportPipeline::new()
            .with_material(PbrMaterial::skin())
            .with_meta(OxiHumanMeta::minimal())
            .describe();
        assert!(
            desc.contains("material"),
            "describe() should mention 'material', got: {desc}"
        );
    }

    #[test]
    fn pipeline_with_blend_shapes() {
        let mesh = triangle();
        let path = std::path::Path::new("/tmp/test_pipeline_blend_shapes.glb");
        let shapes = vec![BlendShape::zero("neutral", 3)];
        ExportPipeline::new()
            .with_blend_shapes(shapes)
            .export(&mesh, path)
            .expect("blend shapes export failed");
        assert!(path.exists(), "GLB file not created");
        std::fs::remove_file(path).ok();
    }

    #[test]
    fn pipeline_with_tangents_and_colors() {
        let mesh = triangle();
        let path = std::path::Path::new("/tmp/test_pipeline_tangents_colors.glb");
        ExportPipeline::new()
            .with_tangents()
            .with_colors()
            .export(&mesh, path)
            .expect("tangents+colors export failed");
        assert!(path.exists(), "GLB file not created");
        std::fs::remove_file(path).ok();
    }
}