alice-sdf 1.11.0

ALICE-SDF: 3D/Spatial Data Specialist — Store laws, not polygons. GLSL/WGSL/HLSL transpile, SVO, GPU marching cubes, texture-fit, procedural CSG. Bridges to alice-codec/physics/asp/cache/font are not part of the crates.io release (path / git dependency only).
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
//! glTF 2.0 export with `EXT_meshopt_compression` extension
//!
//! Provides a compact GLB writer that applies meshopt binary-compat encoding
//! to POSITION / NORMAL / TEXCOORD_0 / index buffer views The output is a
//! valid glTF 2.0 with `extensionsUsed` and `extensionsRequired` set to
//! `["EXT_meshopt_compression"]` — loaders that support the extension will
//! decompress on load
//!
//! # Limitations
//!
//! - Single-mesh, single-scene structure (mesh + material + node + scene)
//! - Supports POSITION (VEC3 f32) / NORMAL (VEC3 f32) / TEXCOORD_0 (VEC2 f32) / indices (u32)
//! - No PBR material extensions (see `io::gltf` for full material support)
//! - Fallback bytes are zero-filled (compression is REQUIRED for load)
//!
//! # Reference
//!
//! - <https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Vendor/EXT_meshopt_compression/README.md>
//!
//! Author: Moroya Sakamoto

use crate::io::IoError;
use crate::mesh::meshopt_index_codec::encode_index_buffer;
use crate::mesh::meshopt_vertex_codec::encode_vertex_buffer_level;
use crate::mesh::Mesh;
use std::io::Write;
use std::path::Path;

// GLB constants
const GLB_MAGIC: u32 = 0x4654_6C67; // "glTF"
const GLB_VERSION: u32 = 2;
const GLB_CHUNK_JSON: u32 = 0x4E4F_534A; // "JSON"
const GLB_CHUNK_BIN: u32 = 0x004E_4942; // "BIN\0"

const FLOAT: u32 = 5126;
const UNSIGNED_INT: u32 = 5125;
const UNSIGNED_BYTE: u32 = 5121;
const ARRAY_BUFFER: u32 = 34962;
const ELEMENT_ARRAY_BUFFER: u32 = 34963;

/// Optional per-vertex skinning data (JOINTS_0 + WEIGHTS_0)
///
/// Both arrays MUST have the same length as `mesh.vertices` Weights should
/// sum to 255 per vertex for correct skinning (glTF `normalized: true`
/// interprets `u8` as `f32 / 255`)
///
/// # Fields
///
/// - `joints[i]`: 4 bone indices affecting vertex `i` (up to 256 bones total)
/// - `weights[i]`: 4 weights (u8, normalized) for the joints in `joints[i]`
#[derive(Debug, Clone, Default)]
pub struct MeshoptSkinning {
    /// Per-vertex bone indices (4 per vertex)
    pub joints: Vec<[u8; 4]>,
    /// Per-vertex bone weights (4 per vertex, u8 normalized)
    pub weights: Vec<[u8; 4]>,
}

/// Configuration for meshopt-compressed glTF export
#[derive(Debug, Clone)]
pub struct MeshoptGltfConfig {
    /// Include normals (NORMAL attribute)
    pub export_normals: bool,
    /// Include TEXCOORD_0 (uv attribute)
    pub export_uvs: bool,
    /// Compression level (0=scalar, 2=u8/u16 estimate, 3=u8/u16/u32 XOR)
    pub level: u8,
    /// Force `doubleSided` on the material
    pub double_sided: bool,
}

impl Default for MeshoptGltfConfig {
    fn default() -> Self {
        Self {
            export_normals: true,
            export_uvs: false,
            level: 2,
            double_sided: false,
        }
    }
}

/// Metadata describing a single meshopt-compressed buffer view
///
/// Used to construct the JSON `EXT_meshopt_compression` extension block for
/// each bufferView
#[derive(Debug, Clone, Copy)]
struct MeshoptView {
    /// Original uncompressed byte length
    original_length: usize,
    /// Original stride in bytes (0 = tightly packed)
    stride: usize,
    /// Item count (vertex or index count)
    count: usize,
    /// Offset within the buffer where compressed bytes begin
    compressed_offset: usize,
    /// Length of the compressed bytes
    compressed_length: usize,
    /// Meshopt mode: "ATTRIBUTES" | "TRIANGLES" | "INDICES"
    mode: &'static str,
}

impl MeshoptView {
    fn ext_json(&self) -> String {
        format!(
            r#"{{"buffer":0,"byteOffset":{},"byteLength":{},"byteStride":{},"count":{},"mode":"{}"}}"#,
            self.compressed_offset, self.compressed_length, self.stride, self.count, self.mode,
        )
    }
}

/// Export mesh to a meshopt-compressed GLB (`.glb`) file
///
/// # Errors
///
/// Returns `IoError` on file I/O failure or on invalid mesh
pub fn export_glb_meshopt<P: AsRef<Path>>(
    mesh: &Mesh,
    path: P,
    config: &MeshoptGltfConfig,
) -> Result<(), IoError> {
    export_glb_meshopt_skinned(mesh, None, path, config)
}

/// Export mesh with optional skinning data to a meshopt-compressed GLB file
///
/// # Errors
///
/// Returns `IoError` on file I/O failure, invalid mesh, or skinning length
/// mismatch
pub fn export_glb_meshopt_skinned<P: AsRef<Path>>(
    mesh: &Mesh,
    skinning: Option<&MeshoptSkinning>,
    path: P,
    config: &MeshoptGltfConfig,
) -> Result<(), IoError> {
    let bytes = export_glb_meshopt_bytes_skinned(mesh, skinning, config)?;
    let mut file = std::fs::File::create(path.as_ref())?;
    file.write_all(&bytes)?;
    Ok(())
}

/// Export mesh to meshopt-compressed GLB bytes
///
/// # Errors
///
/// Returns `IoError` if the mesh has no vertices or triangles
pub fn export_glb_meshopt_bytes(
    mesh: &Mesh,
    config: &MeshoptGltfConfig,
) -> Result<Vec<u8>, IoError> {
    export_glb_meshopt_bytes_skinned(mesh, None, config)
}

/// Export mesh with optional skinning data to meshopt-compressed GLB bytes
///
/// # Errors
///
/// Returns `IoError` if the mesh has no vertices/triangles, or if skinning
/// joint/weight length doesn't match vertex count
pub fn export_glb_meshopt_bytes_skinned(
    mesh: &Mesh,
    skinning: Option<&MeshoptSkinning>,
    config: &MeshoptGltfConfig,
) -> Result<Vec<u8>, IoError> {
    if mesh.vertices.is_empty() {
        return Err(IoError::InvalidFormat("empty mesh".into()));
    }
    if mesh.indices.is_empty() || mesh.indices.len() % 3 != 0 {
        return Err(IoError::InvalidFormat(
            "mesh indices must be non-empty triangles".into(),
        ));
    }
    if let Some(skin) = skinning {
        if skin.joints.len() != mesh.vertices.len() || skin.weights.len() != mesh.vertices.len() {
            return Err(IoError::InvalidFormat(
                "skinning joints/weights length must match vertex count".into(),
            ));
        }
    }

    let vert_count = mesh.vertices.len();
    let idx_count = mesh.indices.len();

    // Build raw buffers per attribute
    let positions_raw: Vec<u8> = mesh
        .vertices
        .iter()
        .flat_map(|v| {
            let mut b = Vec::with_capacity(12);
            b.extend_from_slice(&v.position.x.to_le_bytes());
            b.extend_from_slice(&v.position.y.to_le_bytes());
            b.extend_from_slice(&v.position.z.to_le_bytes());
            b
        })
        .collect();

    let normals_raw: Option<Vec<u8>> = config.export_normals.then(|| {
        mesh.vertices
            .iter()
            .flat_map(|v| {
                let mut b = Vec::with_capacity(12);
                b.extend_from_slice(&v.normal.x.to_le_bytes());
                b.extend_from_slice(&v.normal.y.to_le_bytes());
                b.extend_from_slice(&v.normal.z.to_le_bytes());
                b
            })
            .collect()
    });

    let uvs_raw: Option<Vec<u8>> = config.export_uvs.then(|| {
        mesh.vertices
            .iter()
            .flat_map(|v| {
                let mut b = Vec::with_capacity(8);
                b.extend_from_slice(&v.uv.x.to_le_bytes());
                b.extend_from_slice(&v.uv.y.to_le_bytes());
                b
            })
            .collect()
    });

    // Skinning attributes (VEC4 UNSIGNED_BYTE for both JOINTS_0 and WEIGHTS_0)
    let joints_raw: Option<Vec<u8>> =
        skinning.map(|s| s.joints.iter().flat_map(|j| j.iter().copied()).collect());
    let weights_raw: Option<Vec<u8>> =
        skinning.map(|s| s.weights.iter().flat_map(|w| w.iter().copied()).collect());

    let indices_raw: Vec<u8> = mesh.indices.iter().flat_map(|&i| i.to_le_bytes()).collect();

    // Compress each
    let positions_compressed = encode_vertex_buffer_level(&positions_raw, 12, config.level);
    let normals_compressed = normals_raw
        .as_ref()
        .map(|raw| encode_vertex_buffer_level(raw, 12, config.level));
    let uvs_compressed = uvs_raw
        .as_ref()
        .map(|raw| encode_vertex_buffer_level(raw, 8, config.level));
    // Skinning: 4 bytes/vertex, use level 0 (scalar delta, common for indices/weights)
    let joints_compressed = joints_raw
        .as_ref()
        .map(|raw| encode_vertex_buffer_level(raw, 4, 0));
    let weights_compressed = weights_raw
        .as_ref()
        .map(|raw| encode_vertex_buffer_level(raw, 4, 0));
    // Index buffer uses meshopt index codec (VEC3 triangles)
    let indices_compressed = encode_index_buffer(&mesh.indices);

    // AABB for POSITION accessor
    let mut min_pos = [f32::MAX; 3];
    let mut max_pos = [f32::MIN; 3];
    for v in &mesh.vertices {
        min_pos[0] = min_pos[0].min(v.position.x);
        min_pos[1] = min_pos[1].min(v.position.y);
        min_pos[2] = min_pos[2].min(v.position.z);
        max_pos[0] = max_pos[0].max(v.position.x);
        max_pos[1] = max_pos[1].max(v.position.y);
        max_pos[2] = max_pos[2].max(v.position.z);
    }

    // Build BIN: fallback (zero-filled) sections + compressed sections
    // Layout:
    //   [fallback positions][fallback normals?][fallback uvs?][fallback indices]
    //   [compressed positions][compressed normals?][compressed uvs?][compressed indices]
    //
    // BufferView primary offsets point to fallback (zero) region; extension
    // offsets point to compressed region Compression is REQUIRED, so
    // fallback is never used but present to satisfy the required-length rules
    let mut bin: Vec<u8> = Vec::new();

    // Fallback region offsets
    let fb_pos_off = bin.len();
    bin.resize(bin.len() + positions_raw.len(), 0);
    let fb_norm_off = normals_raw.as_ref().map(|raw| {
        let o = bin.len();
        bin.resize(bin.len() + raw.len(), 0);
        o
    });
    let fb_uv_off = uvs_raw.as_ref().map(|raw| {
        let o = bin.len();
        bin.resize(bin.len() + raw.len(), 0);
        o
    });
    let fb_joints_off = joints_raw.as_ref().map(|raw| {
        let o = bin.len();
        bin.resize(bin.len() + raw.len(), 0);
        o
    });
    let fb_weights_off = weights_raw.as_ref().map(|raw| {
        let o = bin.len();
        bin.resize(bin.len() + raw.len(), 0);
        o
    });
    let fb_idx_off = bin.len();
    bin.resize(bin.len() + indices_raw.len(), 0);

    // Compressed region offsets
    let cmp_pos_off = bin.len();
    bin.extend_from_slice(&positions_compressed);
    let cmp_norm_off = normals_compressed.as_ref().map(|c| {
        let o = bin.len();
        bin.extend_from_slice(c);
        o
    });
    let cmp_uv_off = uvs_compressed.as_ref().map(|c| {
        let o = bin.len();
        bin.extend_from_slice(c);
        o
    });
    let cmp_joints_off = joints_compressed.as_ref().map(|c| {
        let o = bin.len();
        bin.extend_from_slice(c);
        o
    });
    let cmp_weights_off = weights_compressed.as_ref().map(|c| {
        let o = bin.len();
        bin.extend_from_slice(c);
        o
    });
    let cmp_idx_off = bin.len();
    bin.extend_from_slice(&indices_compressed);

    // Build bufferView + extension JSON
    let mut buffer_views: Vec<String> = Vec::new();
    let mut accessors: Vec<String> = Vec::new();
    let mut attributes: Vec<String> = Vec::new();

    // Position
    let pos_view = MeshoptView {
        original_length: positions_raw.len(),
        stride: 12,
        count: vert_count,
        compressed_offset: cmp_pos_off,
        compressed_length: positions_compressed.len(),
        mode: "ATTRIBUTES",
    };
    buffer_views.push(format!(
        r#"{{"buffer":0,"byteOffset":{},"byteLength":{},"byteStride":12,"target":{},"extensions":{{"EXT_meshopt_compression":{}}}}}"#,
        fb_pos_off,
        pos_view.original_length,
        ARRAY_BUFFER,
        pos_view.ext_json(),
    ));
    accessors.push(format!(
        r#"{{"bufferView":{},"componentType":{},"count":{},"type":"VEC3","min":[{},{},{}],"max":[{},{},{}]}}"#,
        buffer_views.len() - 1,
        FLOAT,
        vert_count,
        min_pos[0],
        min_pos[1],
        min_pos[2],
        max_pos[0],
        max_pos[1],
        max_pos[2],
    ));
    attributes.push(format!(r#""POSITION":{}"#, accessors.len() - 1));

    // Normal
    if let (Some(off), Some(cmp), Some(raw)) = (fb_norm_off, &normals_compressed, &normals_raw) {
        let view = MeshoptView {
            original_length: raw.len(),
            stride: 12,
            count: vert_count,
            compressed_offset: cmp_norm_off.unwrap(),
            compressed_length: cmp.len(),
            mode: "ATTRIBUTES",
        };
        buffer_views.push(format!(
            r#"{{"buffer":0,"byteOffset":{},"byteLength":{},"byteStride":12,"target":{},"extensions":{{"EXT_meshopt_compression":{}}}}}"#,
            off,
            view.original_length,
            ARRAY_BUFFER,
            view.ext_json(),
        ));
        accessors.push(format!(
            r#"{{"bufferView":{},"componentType":{},"count":{},"type":"VEC3"}}"#,
            buffer_views.len() - 1,
            FLOAT,
            vert_count,
        ));
        attributes.push(format!(r#""NORMAL":{}"#, accessors.len() - 1));
    }

    // UV
    if let (Some(off), Some(cmp), Some(raw)) = (fb_uv_off, &uvs_compressed, &uvs_raw) {
        let view = MeshoptView {
            original_length: raw.len(),
            stride: 8,
            count: vert_count,
            compressed_offset: cmp_uv_off.unwrap(),
            compressed_length: cmp.len(),
            mode: "ATTRIBUTES",
        };
        buffer_views.push(format!(
            r#"{{"buffer":0,"byteOffset":{},"byteLength":{},"byteStride":8,"target":{},"extensions":{{"EXT_meshopt_compression":{}}}}}"#,
            off,
            view.original_length,
            ARRAY_BUFFER,
            view.ext_json(),
        ));
        accessors.push(format!(
            r#"{{"bufferView":{},"componentType":{},"count":{},"type":"VEC2"}}"#,
            buffer_views.len() - 1,
            FLOAT,
            vert_count,
        ));
        attributes.push(format!(r#""TEXCOORD_0":{}"#, accessors.len() - 1));
    }

    // JOINTS_0 (VEC4 UNSIGNED_BYTE, byteStride 4)
    if let (Some(off), Some(cmp), Some(raw)) = (fb_joints_off, &joints_compressed, &joints_raw) {
        let view = MeshoptView {
            original_length: raw.len(),
            stride: 4,
            count: vert_count,
            compressed_offset: cmp_joints_off.unwrap(),
            compressed_length: cmp.len(),
            mode: "ATTRIBUTES",
        };
        buffer_views.push(format!(
            r#"{{"buffer":0,"byteOffset":{},"byteLength":{},"byteStride":4,"target":{},"extensions":{{"EXT_meshopt_compression":{}}}}}"#,
            off,
            view.original_length,
            ARRAY_BUFFER,
            view.ext_json(),
        ));
        accessors.push(format!(
            r#"{{"bufferView":{},"componentType":{},"count":{},"type":"VEC4"}}"#,
            buffer_views.len() - 1,
            UNSIGNED_BYTE,
            vert_count,
        ));
        attributes.push(format!(r#""JOINTS_0":{}"#, accessors.len() - 1));
    }

    // WEIGHTS_0 (VEC4 UNSIGNED_BYTE normalized, byteStride 4)
    if let (Some(off), Some(cmp), Some(raw)) = (fb_weights_off, &weights_compressed, &weights_raw) {
        let view = MeshoptView {
            original_length: raw.len(),
            stride: 4,
            count: vert_count,
            compressed_offset: cmp_weights_off.unwrap(),
            compressed_length: cmp.len(),
            mode: "ATTRIBUTES",
        };
        buffer_views.push(format!(
            r#"{{"buffer":0,"byteOffset":{},"byteLength":{},"byteStride":4,"target":{},"extensions":{{"EXT_meshopt_compression":{}}}}}"#,
            off,
            view.original_length,
            ARRAY_BUFFER,
            view.ext_json(),
        ));
        accessors.push(format!(
            r#"{{"bufferView":{},"componentType":{},"count":{},"type":"VEC4","normalized":true}}"#,
            buffer_views.len() - 1,
            UNSIGNED_BYTE,
            vert_count,
        ));
        attributes.push(format!(r#""WEIGHTS_0":{}"#, accessors.len() - 1));
    }

    // Indices
    let idx_view = MeshoptView {
        original_length: indices_raw.len(),
        stride: 0,
        count: idx_count,
        compressed_offset: cmp_idx_off,
        compressed_length: indices_compressed.len(),
        mode: "TRIANGLES",
    };
    buffer_views.push(format!(
        r#"{{"buffer":0,"byteOffset":{},"byteLength":{},"target":{},"extensions":{{"EXT_meshopt_compression":{}}}}}"#,
        fb_idx_off,
        idx_view.original_length,
        ELEMENT_ARRAY_BUFFER,
        idx_view.ext_json(),
    ));
    accessors.push(format!(
        r#"{{"bufferView":{},"componentType":{},"count":{},"type":"SCALAR"}}"#,
        buffer_views.len() - 1,
        UNSIGNED_INT,
        idx_count,
    ));
    let idx_accessor = accessors.len() - 1;

    // Material
    let mat_json = if config.double_sided {
        r#"{"pbrMetallicRoughness":{"baseColorFactor":[0.7,0.7,0.7,1.0],"metallicFactor":0.0,"roughnessFactor":0.9},"doubleSided":true}"#
    } else {
        r#"{"pbrMetallicRoughness":{"baseColorFactor":[0.7,0.7,0.7,1.0],"metallicFactor":0.0,"roughnessFactor":0.9}}"#
    };

    // Build final JSON
    let json = format!(
        r#"{{"asset":{{"version":"2.0","generator":"ALICE-SDF meshopt-gltf"}},"extensionsUsed":["EXT_meshopt_compression"],"extensionsRequired":["EXT_meshopt_compression"],"buffers":[{{"byteLength":{buffer_len}}}],"bufferViews":[{buffer_views_json}],"accessors":[{accessors_json}],"materials":[{mat_json}],"meshes":[{{"primitives":[{{"attributes":{{{attributes_json}}},"indices":{idx_accessor},"material":0}}]}}],"nodes":[{{"mesh":0}}],"scenes":[{{"nodes":[0]}}],"scene":0}}"#,
        buffer_len = bin.len(),
        buffer_views_json = buffer_views.join(","),
        accessors_json = accessors.join(","),
        attributes_json = attributes.join(","),
    );

    // Assemble GLB
    let mut json_bytes = json.into_bytes();
    while json_bytes.len() % 4 != 0 {
        json_bytes.push(b' ');
    }
    let bin_padded_len = (bin.len() + 3) & !3;
    let bin_pad = bin_padded_len - bin.len();

    let total_len = 12 + 8 + json_bytes.len() + 8 + bin_padded_len;

    let mut out = Vec::with_capacity(total_len);
    out.extend_from_slice(&GLB_MAGIC.to_le_bytes());
    out.extend_from_slice(&GLB_VERSION.to_le_bytes());
    out.extend_from_slice(&(total_len as u32).to_le_bytes());
    // JSON chunk
    out.extend_from_slice(&(json_bytes.len() as u32).to_le_bytes());
    out.extend_from_slice(&GLB_CHUNK_JSON.to_le_bytes());
    out.extend_from_slice(&json_bytes);
    // BIN chunk
    out.extend_from_slice(&(bin_padded_len as u32).to_le_bytes());
    out.extend_from_slice(&GLB_CHUNK_BIN.to_le_bytes());
    out.extend_from_slice(&bin);
    out.resize(out.len() + bin_pad, 0);

    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mesh::{sdf_to_mesh, MarchingCubesConfig};
    use crate::types::SdfNode;
    use glam::Vec3;

    fn sphere_mesh(resolution: usize) -> Mesh {
        let sphere = SdfNode::sphere(1.0);
        sdf_to_mesh(
            &sphere,
            Vec3::splat(-2.0),
            Vec3::splat(2.0),
            &MarchingCubesConfig {
                resolution,
                iso_level: 0.0,
                compute_normals: true,
                ..Default::default()
            },
        )
    }

    fn parse_glb_header(bytes: &[u8]) -> (u32, u32, u32) {
        let magic = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
        let version = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]);
        let length = u32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]);
        (magic, version, length)
    }

    #[test]
    fn test_export_glb_meshopt_bytes_valid_header() {
        let mesh = sphere_mesh(8);
        let bytes = export_glb_meshopt_bytes(&mesh, &MeshoptGltfConfig::default()).unwrap();

        let (magic, version, length) = parse_glb_header(&bytes);
        assert_eq!(magic, GLB_MAGIC);
        assert_eq!(version, GLB_VERSION);
        assert_eq!(length, bytes.len() as u32);
    }

    #[test]
    fn test_export_glb_meshopt_json_has_extensions() {
        let mesh = sphere_mesh(4);
        let bytes = export_glb_meshopt_bytes(&mesh, &MeshoptGltfConfig::default()).unwrap();

        // Extract JSON chunk (offset 20 after headers)
        let json_len = u32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]) as usize;
        let json = std::str::from_utf8(&bytes[20..20 + json_len]).unwrap();

        assert!(json.contains(r#""extensionsUsed":["EXT_meshopt_compression"]"#));
        assert!(json.contains(r#""extensionsRequired":["EXT_meshopt_compression"]"#));
        assert!(json.contains(r#""EXT_meshopt_compression":"#));
        assert!(json.contains(r#""mode":"ATTRIBUTES""#));
        assert!(json.contains(r#""mode":"TRIANGLES""#));
    }

    #[test]
    fn test_export_glb_meshopt_compression_ratio() {
        let mesh = sphere_mesh(16);
        let uncompressed_size = mesh.vertices.len() * 12 /*pos*/ + mesh.vertices.len() * 12 /*normal*/
            + mesh.indices.len() * 4;
        let bytes = export_glb_meshopt_bytes(&mesh, &MeshoptGltfConfig::default()).unwrap();

        // The compressed .glb should be smaller than raw payload + reasonable overhead
        eprintln!(
            "sphere mesh: {} verts / {} indices, raw {} bytes, glb {} bytes",
            mesh.vertices.len(),
            mesh.indices.len(),
            uncompressed_size,
            bytes.len()
        );
    }

    #[test]
    fn test_export_glb_meshopt_double_sided_flag() {
        let mesh = sphere_mesh(4);
        let cfg = MeshoptGltfConfig {
            double_sided: true,
            ..MeshoptGltfConfig::default()
        };
        let bytes = export_glb_meshopt_bytes(&mesh, &cfg).unwrap();

        let json_len = u32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]) as usize;
        let json = std::str::from_utf8(&bytes[20..20 + json_len]).unwrap();
        assert!(json.contains(r#""doubleSided":true"#));
    }

    #[test]
    fn test_export_glb_meshopt_no_normals() {
        let mesh = sphere_mesh(4);
        let cfg = MeshoptGltfConfig {
            export_normals: false,
            ..MeshoptGltfConfig::default()
        };
        let bytes = export_glb_meshopt_bytes(&mesh, &cfg).unwrap();

        let json_len = u32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]) as usize;
        let json = std::str::from_utf8(&bytes[20..20 + json_len]).unwrap();
        assert!(!json.contains(r#""NORMAL":"#));
    }

    #[test]
    fn test_export_glb_meshopt_with_skinning() {
        let mesh = sphere_mesh(4);
        let vc = mesh.vertices.len();
        let skin = MeshoptSkinning {
            joints: vec![[0, 1, 2, 3]; vc],
            weights: vec![[64, 64, 64, 63]; vc], // sum 255
        };
        let bytes =
            export_glb_meshopt_bytes_skinned(&mesh, Some(&skin), &MeshoptGltfConfig::default())
                .unwrap();

        let json_len = u32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]) as usize;
        let json = std::str::from_utf8(&bytes[20..20 + json_len]).unwrap();
        assert!(json.contains(r#""JOINTS_0":"#));
        assert!(json.contains(r#""WEIGHTS_0":"#));
        assert!(json.contains(r#""normalized":true"#));
    }

    #[test]
    fn test_export_glb_meshopt_skinning_length_mismatch_errors() {
        let mesh = sphere_mesh(4);
        let skin = MeshoptSkinning {
            joints: vec![[0u8, 1, 2, 3]; 5], // wrong length
            weights: vec![[64u8, 64, 64, 63]; 5],
        };
        let result =
            export_glb_meshopt_bytes_skinned(&mesh, Some(&skin), &MeshoptGltfConfig::default());
        assert!(result.is_err());
    }

    #[test]
    fn test_export_glb_meshopt_empty_mesh_errors() {
        let mesh = Mesh {
            vertices: vec![],
            indices: vec![],
        };
        let result = export_glb_meshopt_bytes(&mesh, &MeshoptGltfConfig::default());
        assert!(result.is_err());
    }
}