vpin 0.26.0

Rust library for working with Visual Pinball VPX files
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
use std::cell::RefCell;
use std::path::Path;
use wasm_bindgen::prelude::*;

use crate::filesystem::{FileSystem, MemoryFileSystem};
use crate::vpx;
use crate::vpx::expanded::{ExpandOptions, PrimitiveMeshFormat, read_fs, write_fs};

thread_local! {
    static PROGRESS_CALLBACK: RefCell<Option<js_sys::Function>> = const { RefCell::new(None) };
}

fn set_progress_callback(callback: Option<js_sys::Function>) {
    PROGRESS_CALLBACK.with(|cb| {
        *cb.borrow_mut() = callback;
    });
}

fn emit_progress(message: &str) {
    PROGRESS_CALLBACK.with(|cb| {
        if let Some(callback) = cb.borrow().as_ref() {
            let _ = callback.call1(&JsValue::NULL, &JsValue::from_str(message));
        }
    });
}

#[wasm_bindgen(start)]
pub fn init() {
    #[cfg(feature = "wasm")]
    console_error_panic_hook::set_once();
}

#[wasm_bindgen]
pub fn extract(data: &[u8], callback: Option<js_sys::Function>) -> Result<js_sys::Object, JsError> {
    set_progress_callback(callback);

    emit_progress("Parsing VPX file...");
    let vpx_data = vpx::from_bytes(data).map_err(|e| {
        set_progress_callback(None);
        JsError::new(&e.to_string())
    })?;

    let fs = MemoryFileSystem::new();
    let root_dir = "/vpx".to_string();

    emit_progress(&format!("Extracting {} images...", vpx_data.images.len()));
    emit_progress(&format!("Extracting {} sounds...", vpx_data.sounds.len()));
    emit_progress(&format!(
        "Extracting {} game items...",
        vpx_data.gameitems.len()
    ));

    let expand_options = ExpandOptions::new()
        .mesh_format(PrimitiveMeshFormat::Obj)
        .generate_derived_meshes(false);
    write_fs(&vpx_data, &root_dir, &expand_options, &fs).map_err(|e| {
        set_progress_callback(None);
        JsError::new(&format!("Failed to extract VPX: {}", e))
    })?;

    emit_progress("Building file map...");
    let result = js_sys::Object::new();
    for path in fs.list_files() {
        if let Some(data) = fs.get_file(&path) {
            let key = JsValue::from_str(&path);
            let value = js_sys::Uint8Array::from(data.as_slice());
            js_sys::Reflect::set(&result, &key, &value).map_err(|e| {
                set_progress_callback(None);
                JsError::new(&format!("Failed to set file in result: {:?}", e))
            })?;
        }
    }

    emit_progress("Extraction complete");
    set_progress_callback(None);

    Ok(result)
}

#[wasm_bindgen]
pub fn assemble(
    files: js_sys::Object,
    callback: Option<js_sys::Function>,
) -> Result<Vec<u8>, JsError> {
    set_progress_callback(callback);

    emit_progress("Reading files...");
    let fs = MemoryFileSystem::new();
    let keys = js_sys::Object::keys(&files);

    for i in 0..keys.length() {
        let key = keys.get(i);
        let path = key
            .as_string()
            .ok_or_else(|| JsError::new("Invalid file path"))?;

        let value = js_sys::Reflect::get(&files, &key).map_err(|e| {
            set_progress_callback(None);
            JsError::new(&format!("Failed to get file: {:?}", e))
        })?;

        let array = js_sys::Uint8Array::from(value);
        let data = array.to_vec();

        fs.write_file(Path::new(&path), &data).map_err(|e| {
            set_progress_callback(None);
            JsError::new(&format!("Failed to write file to memory: {}", e))
        })?;
    }

    emit_progress("Assembling VPX...");
    let root_dir = "/vpx".to_string();
    let vpx_data = read_fs(&root_dir, &fs).map_err(|e| {
        set_progress_callback(None);
        JsError::new(&format!("Failed to assemble VPX: {}", e))
    })?;

    emit_progress(&format!("Assembling {} images...", vpx_data.images.len()));
    emit_progress(&format!("Assembling {} sounds...", vpx_data.sounds.len()));
    emit_progress(&format!(
        "Assembling {} game items...",
        vpx_data.gameitems.len()
    ));

    emit_progress("Writing VPX data...");
    let bytes = vpx::to_bytes(&vpx_data).map_err(|e| {
        set_progress_callback(None);
        JsError::new(&e.to_string())
    })?;

    emit_progress("Assembly complete");
    set_progress_callback(None);

    Ok(bytes)
}

// ---------------------------------------------------------------------------
// Mesh I/O surface for vpx-editor and other wasm consumers.
//
// `obj_to_mesh` parses an OBJ into renderer-ready typed arrays;
// `mesh_to_obj` is its symmetric inverse. Both take a
// `convert_to_left_handed` flag matching vpinball's `ObjLoader::Load`
// flag of the same name (the "Convert coordinate system" checkbox in
// vpinball's mesh-import dialog):
// - `true`: apply Z negate / V flip / winding reverse to convert
//   between the OBJ's right-handed Y-up convention (Blender / standard)
//   and vpx-internal left-handed Z-up. Symmetric with `extract` /
//   `assemble`.
// - `false`: skip the conversion. Input is assumed to already be in
//   vpx-internal convention; values pass through unchanged.
// ---------------------------------------------------------------------------

/// Mesh data for a single primitive: positions, texture coordinates,
/// normals and triangle indices, packed as flat typed arrays for direct
/// upload into a WebGL / Three.js / GPU buffer.
///
/// All vertex data is aligned: `positions[3*i..3*i+3]`, `tex_coords[2*i..2*i+2]`
/// and `normals[3*i..3*i+3]` describe corner `i`. Triangles are 0-based
/// indices into that aligned array.
///
/// Coordinates are in vpx-internal convention (the same form `read_fs`
/// produces and `write_fs` consumes), not raw OBJ values - see
/// [`obj_to_mesh`] / [`mesh_to_obj`] for the transform details.
///
/// The published wasm package is built with `wasm-bindgen --weak-refs`,
/// so the Rust-owned vectors backing this struct are reclaimed
/// automatically via `FinalizationRegistry` when the JS wrapper is
/// garbage-collected. Calling `.free()` manually is still allowed for
/// deterministic cleanup of large meshes.
#[wasm_bindgen]
pub struct PrimitiveMesh {
    name: String,
    positions: Vec<f32>,
    tex_coords: Vec<f32>,
    normals: Vec<f32>,
    indices: Vec<u32>,
}

#[wasm_bindgen]
impl PrimitiveMesh {
    #[wasm_bindgen(getter)]
    pub fn name(&self) -> String {
        self.name.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn positions(&self) -> js_sys::Float32Array {
        js_sys::Float32Array::from(self.positions.as_slice())
    }

    #[wasm_bindgen(getter, js_name = texCoords)]
    pub fn tex_coords(&self) -> js_sys::Float32Array {
        js_sys::Float32Array::from(self.tex_coords.as_slice())
    }

    #[wasm_bindgen(getter)]
    pub fn normals(&self) -> js_sys::Float32Array {
        js_sys::Float32Array::from(self.normals.as_slice())
    }

    #[wasm_bindgen(getter)]
    pub fn indices(&self) -> js_sys::Uint32Array {
        js_sys::Uint32Array::from(self.indices.as_slice())
    }

    /// Bounding-box midpoint of the mesh's positions, in the same
    /// coordinate space as `positions` (vpx-internal). Returns
    /// `[mid_x, mid_y, mid_z]`. Used by editor flows that center the
    /// mesh on origin or move the primitive to the mesh's absolute
    /// position - both need to know the midpoint to shift vertices
    /// (and, for the absolute-position case, to set the primitive's
    /// `vPosition` field). Mirrors vpinball's `Mesh::middlePoint`,
    /// which is used by `IDC_CENTER_MESH` / `IDC_ABS_POSITION_RADIO`
    /// in the mesh-import dialog (`primitive.cpp:1729-1745`).
    ///
    /// Returns `[0, 0, 0]` for an empty mesh.
    #[wasm_bindgen(getter)]
    pub fn midpoint(&self) -> js_sys::Float32Array {
        if self.positions.is_empty() {
            return js_sys::Float32Array::from([0.0_f32, 0.0, 0.0].as_slice());
        }
        let mut min = [f32::INFINITY; 3];
        let mut max = [f32::NEG_INFINITY; 3];
        for chunk in self.positions.chunks_exact(3) {
            for axis in 0..3 {
                if chunk[axis] < min[axis] {
                    min[axis] = chunk[axis];
                }
                if chunk[axis] > max[axis] {
                    max[axis] = chunk[axis];
                }
            }
        }
        let mid = [
            (min[0] + max[0]) * 0.5,
            (min[1] + max[1]) * 0.5,
            (min[2] + max[2]) * 0.5,
        ];
        js_sys::Float32Array::from(mid.as_slice())
    }
}

/// Parse a Wavefront OBJ into a [`PrimitiveMesh`].
///
/// Accepts any OBJ flavor (vpinball-format from `extract`, Blender-format,
/// anything in between): n-gons are fan-triangulated and `(position, uv,
/// normal)` corners are deduplicated so the result is renderer-ready.
///
/// `convert_to_left_handed` mirrors vpinball's `ObjLoader::Load` flag of
/// the same name (the "Convert coordinate system" checkbox in the
/// vpinball mesh-import dialog):
///
/// - `true` (matches `assemble`'s read path and vpinball's dialog
///   default): the input is treated as right-handed (Blender / standard
///   convention). Vertex Z is negated, normal Z is negated, V is flipped
///   (`vpx_tv = 1 - obj_v`), and the per-triangle corner order is
///   reversed. The returned mesh data ends up in vpx-internal,
///   left-handed convention.
/// - `false`: the input is assumed to already be in vpx-internal
///   convention (e.g. produced by a previous `mesh_to_obj` with the same
///   flag). The transforms are skipped and values pass through verbatim.
#[wasm_bindgen]
pub fn obj_to_mesh(data: &[u8], convert_to_left_handed: bool) -> Result<PrimitiveMesh, JsError> {
    use crate::vpx::obj::read_obj_from_reader_with_options;
    use std::io::BufReader;

    let mut reader = BufReader::new(data);
    let result = read_obj_from_reader_with_options(&mut reader, convert_to_left_handed)
        .map_err(|e| JsError::new(&format!("OBJ parse failed: {}", e)))?;

    let mut positions = Vec::with_capacity(result.final_vertices.len() * 3);
    let mut tex_coords = Vec::with_capacity(result.final_vertices.len() * 2);
    let mut normals = Vec::with_capacity(result.final_vertices.len() * 3);
    for v in &result.final_vertices {
        positions.push(v.x);
        positions.push(v.y);
        positions.push(v.z);
        tex_coords.push(v.tu);
        tex_coords.push(v.tv);
        normals.push(v.nx);
        normals.push(v.ny);
        normals.push(v.nz);
    }
    let mut indices = Vec::with_capacity(result.indices.len() * 3);
    for face in &result.indices {
        indices.push(face.i0 as u32);
        indices.push(face.i1 as u32);
        indices.push(face.i2 as u32);
    }

    Ok(PrimitiveMesh {
        name: result.name,
        positions,
        tex_coords,
        normals,
        indices,
    })
}

/// Serialize a mesh as a Wavefront OBJ.
///
/// `name` becomes the `o` directive; pass an empty string to use
/// `"object"`. Vertex / texcoord / normal arrays must have aligned
/// lengths (`positions.len() / 3 == tex_coords.len() / 2 ==
/// normals.len() / 3`); index values must be valid 0-based offsets into
/// that vertex array.
///
/// `convert_to_left_handed` is the symmetric inverse of the same flag
/// on [`obj_to_mesh`]:
///
/// - `true` (matches `extract`'s write path): the input is treated as
///   vpx-internal data and converted out: vertex Z is negated, normal Z
///   is negated, V is flipped (`obj_v = 1 - vpx_tv`), and per-triangle
///   corner order is reversed. The result is a vpinball-format OBJ
///   that `assemble` (or `obj_to_mesh(.., true)`) reads back identically.
/// - `false`: the input vpx-internal data is written out verbatim, no
///   transforms applied. Round-trips with `obj_to_mesh(.., false)`.
#[wasm_bindgen]
pub fn mesh_to_obj(
    name: &str,
    positions: &[f32],
    tex_coords: &[f32],
    normals: &[f32],
    indices: &[u32],
    convert_to_left_handed: bool,
) -> Result<Vec<u8>, JsError> {
    use wavefront_obj_io::{IoObjWriter, ObjWriter};

    if !positions.len().is_multiple_of(3) {
        return Err(JsError::new("positions length must be a multiple of 3"));
    }
    if !tex_coords.len().is_multiple_of(2) {
        return Err(JsError::new("tex_coords length must be a multiple of 2"));
    }
    if !normals.len().is_multiple_of(3) {
        return Err(JsError::new("normals length must be a multiple of 3"));
    }
    if !indices.len().is_multiple_of(3) {
        return Err(JsError::new("indices length must be a multiple of 3"));
    }
    let vert_count = positions.len() / 3;
    if tex_coords.len() / 2 != vert_count || normals.len() / 3 != vert_count {
        return Err(JsError::new(
            "positions / tex_coords / normals must describe the same vertex count",
        ));
    }

    let mut buffer = Vec::with_capacity(positions.len() * 4);
    {
        let mut writer: IoObjWriter<&mut Vec<u8>, f32> = IoObjWriter::new(&mut buffer);
        writer
            .write_comment(format!(
                "numVerts: {} numFaces: {}",
                vert_count,
                indices.len() / 3
            ))
            .map_err(|e| JsError::new(&format!("write failed: {e}")))?;
        let object_name = if name.is_empty() { "object" } else { name };
        writer
            .write_object_name(object_name)
            .map_err(|e| JsError::new(&format!("write failed: {e}")))?;

        let z_sign = if convert_to_left_handed { -1.0 } else { 1.0 };
        for chunk in positions.chunks_exact(3) {
            writer
                .write_vertex(chunk[0], chunk[1], z_sign * chunk[2], None)
                .map_err(|e| JsError::new(&format!("write failed: {e}")))?;
        }
        for chunk in tex_coords.chunks_exact(2) {
            let v_out = if convert_to_left_handed {
                1.0 - chunk[1]
            } else {
                chunk[1]
            };
            writer
                .write_texture_coordinate(chunk[0], Some(v_out), None)
                .map_err(|e| JsError::new(&format!("write failed: {e}")))?;
        }
        for chunk in normals.chunks_exact(3) {
            writer
                .write_normal(chunk[0], chunk[1], z_sign * chunk[2])
                .map_err(|e| JsError::new(&format!("write failed: {e}")))?;
        }
        // OBJ indices are 1-based. With `convert_to_left_handed=true`
        // we reverse the per-triangle corner order (matching vpinball's
        // `WriteFaceInfoLong`); with `false` we keep source winding so
        // round-trips with `obj_to_mesh(.., false)` preserve indices.
        for tri in indices.chunks_exact(3) {
            for &idx in tri {
                if idx as usize >= vert_count {
                    return Err(JsError::new(&format!(
                        "triangle index {idx} out of range (have {vert_count} vertices)"
                    )));
                }
            }
            let (a, b, c) = if convert_to_left_handed {
                (
                    (tri[2] + 1) as usize,
                    (tri[1] + 1) as usize,
                    (tri[0] + 1) as usize,
                )
            } else {
                (
                    (tri[0] + 1) as usize,
                    (tri[1] + 1) as usize,
                    (tri[2] + 1) as usize,
                )
            };
            writer
                .write_face(&[
                    (a, Some(a), Some(a)),
                    (b, Some(b), Some(b)),
                    (c, Some(c), Some(c)),
                ])
                .map_err(|e| JsError::new(&format!("write failed: {e}")))?;
        }
    }

    Ok(buffer)
}

/// Generate the procedural mesh used by vpinball primitives that
/// don't load a `.obj` file (`use_3d_mesh = false`). Mirrors
/// `Primitive::CalculateBuiltinOriginal` from vpinball: a regular
/// polygon prism with `sides` faces, top and bottom caps, fitting
/// in `[-r, r] x [-r, r] x [-0.5, 0.5]`.
///
/// Use this to render the placeholder shape for a primitive that
/// has `use_3d_mesh = false`, or to seed the editor's "Add
/// Primitive" workflow.
///
/// `sides` must be at least 3; otherwise the call errors out
/// (vpinball clamps to 3 in its own editor).
///
/// `draw_textures_inside = true` doubles the index count so back
/// faces are also rendered (matches vpinball's flag of the same
/// name on `Primitive`). Vertex / texcoord / normal arrays are
/// unaffected.
#[wasm_bindgen]
pub fn generate_builtin_primitive(
    sides: u32,
    draw_textures_inside: bool,
) -> Result<PrimitiveMesh, JsError> {
    use crate::vpx::mesh::builtin_primitive::build_builtin_primitive_mesh;

    let (vertices, faces) = build_builtin_primitive_mesh(sides, draw_textures_inside)
        .ok_or_else(|| JsError::new(&format!("sides must be >= 3 (got {sides})")))?;

    let mut positions = Vec::with_capacity(vertices.len() * 3);
    let mut tex_coords = Vec::with_capacity(vertices.len() * 2);
    let mut normals = Vec::with_capacity(vertices.len() * 3);
    for vw in &vertices {
        let v = &vw.vertex;
        positions.push(v.x);
        positions.push(v.y);
        positions.push(v.z);
        tex_coords.push(v.tu);
        tex_coords.push(v.tv);
        normals.push(v.nx);
        normals.push(v.ny);
        normals.push(v.nz);
    }
    let mut indices = Vec::with_capacity(faces.len() * 3);
    for face in &faces {
        indices.push(face.i0 as u32);
        indices.push(face.i1 as u32);
        indices.push(face.i2 as u32);
    }

    Ok(PrimitiveMesh {
        name: String::from("primitive"),
        positions,
        tex_coords,
        normals,
        indices,
    })
}

#[cfg(all(test, target_family = "wasm"))]
mod tests {
    use super::*;
    use wasm_bindgen_test::*;

    #[wasm_bindgen_test]
    fn test_extract_with_invalid_data() {
        let invalid_data = b"invalid vpx data";
        let result = extract(invalid_data, None);
        assert!(result.is_err());
    }

    #[wasm_bindgen_test]
    fn test_assemble_with_empty_files() {
        let files = js_sys::Object::new();
        let result = assemble(files, None);
        assert!(result.is_err());
    }

    #[wasm_bindgen_test]
    fn test_obj_to_mesh_blender_cube() {
        // Blender's default cube exported as OBJ. 6 quads -> 12 triangles,
        // 8 unique positions but 24 unique combined corners after dedup
        // (each quad has its own normal, so no corner can be reused
        // across adjacent faces).
        let blender = include_bytes!("../testdata/blender_square.obj");
        let mesh = obj_to_mesh(blender, true).expect("parse should succeed");
        assert_eq!(mesh.name(), "Cube");

        let positions = mesh.positions();
        let tex_coords = mesh.tex_coords();
        let normals = mesh.normals();
        let indices = mesh.indices();

        // 24 combined corners across 12 triangles.
        assert_eq!(positions.length(), 24 * 3);
        assert_eq!(tex_coords.length(), 24 * 2);
        assert_eq!(normals.length(), 24 * 3);
        assert_eq!(indices.length(), 12 * 3);
    }

    #[wasm_bindgen_test]
    fn test_obj_to_mesh_rejects_unparseable_input() {
        let result = obj_to_mesh(b"this is not an obj", true);
        // The lenient reader skips unknown lines; this fails on the
        // post-parse "no vertices" check.
        assert!(result.is_err());
    }

    #[wasm_bindgen_test]
    fn test_mesh_to_obj_round_trip() {
        // obj_to_mesh -> mesh_to_obj -> obj_to_mesh: structure preserved.
        let blender = include_bytes!("../testdata/blender_square.obj");
        let mesh = obj_to_mesh(blender, true).expect("parse should succeed");

        let positions: Vec<f32> = mesh.positions().to_vec();
        let tex_coords: Vec<f32> = mesh.tex_coords().to_vec();
        let normals: Vec<f32> = mesh.normals().to_vec();
        let indices: Vec<u32> = mesh.indices().to_vec();

        let obj_bytes = mesh_to_obj("Cube", &positions, &tex_coords, &normals, &indices, true)
            .expect("write should succeed");

        let round_tripped = obj_to_mesh(&obj_bytes, true).expect("reparse should succeed");
        assert_eq!(round_tripped.positions().length(), positions.len() as u32);
        assert_eq!(round_tripped.tex_coords().length(), tex_coords.len() as u32);
        assert_eq!(round_tripped.normals().length(), normals.len() as u32);
        assert_eq!(round_tripped.indices().length(), indices.len() as u32);
    }

    #[wasm_bindgen_test]
    fn test_mesh_to_obj_round_trip_no_convert() {
        // With `convert_to_left_handed=false`, vpx-internal data passes
        // through verbatim - both vertex Z and triangle indices keep
        // their original values across a round trip.
        let positions: Vec<f32> = vec![0.0, 0.0, 0.5, 1.0, 0.0, 0.5, 0.0, 1.0, 0.5];
        let tex_coords: Vec<f32> = vec![0.0, 0.25, 1.0, 0.25, 0.0, 0.75];
        let normals: Vec<f32> = vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0];
        let indices: Vec<u32> = vec![0, 1, 2];

        let obj_bytes = mesh_to_obj("tri", &positions, &tex_coords, &normals, &indices, false)
            .expect("write should succeed");

        let parsed = obj_to_mesh(&obj_bytes, false).expect("reparse should succeed");
        let parsed_positions: Vec<f32> = parsed.positions().to_vec();
        let parsed_tex_coords: Vec<f32> = parsed.tex_coords().to_vec();
        let parsed_indices: Vec<u32> = parsed.indices().to_vec();

        assert_eq!(parsed_positions, positions);
        assert_eq!(parsed_tex_coords, tex_coords);
        assert_eq!(parsed_indices, indices);
    }

    #[wasm_bindgen_test]
    fn test_mesh_to_obj_validates_aligned_arrays() {
        // 3 positions but only 2 tex coords - should error.
        let result = mesh_to_obj(
            "bad",
            &[0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0],
            &[0.0, 0.0, 1.0, 0.0],
            &[0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0],
            &[0, 1, 2],
            true,
        );
        assert!(result.is_err());
    }

    #[wasm_bindgen_test]
    fn test_generate_builtin_primitive_shape() {
        // A 4-sided builtin primitive: 4*4+2 = 18 vertices,
        // 12*4 = 48 indices = 16 triangles. With
        // draw_textures_inside, indices double to 32 triangles.
        let mesh = generate_builtin_primitive(4, false).expect("should succeed");
        assert_eq!(mesh.positions().length(), 18 * 3);
        assert_eq!(mesh.tex_coords().length(), 18 * 2);
        assert_eq!(mesh.normals().length(), 18 * 3);
        assert_eq!(mesh.indices().length(), 16 * 3);

        let mesh = generate_builtin_primitive(4, true).expect("should succeed");
        assert_eq!(mesh.indices().length(), 32 * 3);
    }

    #[wasm_bindgen_test]
    fn test_generate_builtin_primitive_rejects_too_few_sides() {
        for sides in 0..3 {
            let result = generate_builtin_primitive(sides, false);
            assert!(result.is_err(), "sides={sides} should error");
        }
    }

    #[wasm_bindgen_test]
    fn test_generate_builtin_primitive_round_trips_via_mesh_to_obj() {
        // The builtin mesh feeds straight into mesh_to_obj; the
        // resulting OBJ parses back to the same vertex/index counts.
        let mesh = generate_builtin_primitive(8, false).expect("should succeed");
        let positions: Vec<f32> = mesh.positions().to_vec();
        let tex_coords: Vec<f32> = mesh.tex_coords().to_vec();
        let normals: Vec<f32> = mesh.normals().to_vec();
        let indices: Vec<u32> = mesh.indices().to_vec();

        let obj_bytes = mesh_to_obj("octa", &positions, &tex_coords, &normals, &indices, false)
            .expect("write should succeed");
        let parsed = obj_to_mesh(&obj_bytes, false).expect("reparse should succeed");
        assert_eq!(parsed.positions().to_vec(), positions);
        assert_eq!(parsed.indices().to_vec(), indices);
    }

    #[wasm_bindgen_test]
    fn test_extract() {
        let original_data = include_bytes!("../testdata/completely_blank_table_10_7_4.vpx");
        let extract_result = extract(original_data, None).expect("Extraction failed");
        assert_eq!(95, js_sys::Object::keys(&extract_result).length());
        // print all keys
        // to see the results use:
        // cargo test --target wasm32-unknown-unknown --features wasm -- --nocapture
        let keys = js_sys::Object::keys(&extract_result);
        for i in 0..keys.length() {
            let key = keys.get(i);
            let key_str = key.as_string().unwrap();
            web_sys::console::log_1(&JsValue::from_str(&key_str));
        }
        let version_key = JsValue::from_str("/vpx/version.txt");
        let version_value = js_sys::Reflect::get(&extract_result, &version_key).unwrap();
        let version_array = js_sys::Uint8Array::from(version_value);
        let version_str = String::from_utf8(version_array.to_vec()).unwrap();
        assert_eq!("1072", version_str);
    }

    #[wasm_bindgen_test]
    fn test_assemble() {
        let original_data = include_bytes!("../testdata/completely_blank_table_10_7_4.vpx");
        let extract_result = extract(original_data, None).expect("Extraction failed");

        let assemble_result = assemble(extract_result.clone(), None).expect("Assembly failed");

        let extract_result2 = extract(&assemble_result, None).expect("Re-extraction failed");
        // compare key count
        assert_eq!(
            js_sys::Object::keys(&extract_result).length(),
            js_sys::Object::keys(&extract_result2).length()
        );
        // compare all keys and values one by one
        let keys = js_sys::Object::keys(&extract_result);
        for i in 0..keys.length() {
            let key = keys.get(i);
            let original_value = js_sys::Reflect::get(&extract_result, &key).unwrap();
            let reassembled_value = js_sys::Reflect::get(&extract_result2, &key).unwrap();
            let original_array = js_sys::Uint8Array::from(original_value);
            let reassembled_array = js_sys::Uint8Array::from(reassembled_value);
            assert_eq!(
                original_array.length(),
                reassembled_array.length(),
                "Mismatched length for key {:?}",
                key
            );
            let original_bytes = original_array.to_vec();
            let reassembled_bytes = reassembled_array.to_vec();
            assert_eq!(
                original_bytes, reassembled_bytes,
                "Mismatched content for key {:?}",
                key
            );
        }
    }
}