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
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0
use oxihuman_morph::engine::MeshBuffers as MorphMeshBuffers;
/// Final mesh buffers ready for export or GPU upload.
///
/// Constructed from [`oxihuman_morph::engine::MeshBuffers`] via
/// [`MeshBuffers::from_morph`], which initialises tangents to a default value.
/// Call [`crate::normals::compute_normals`] and
/// [`crate::normals::compute_tangents`] on the result before exporting
/// to formats that require correct lighting vectors.
///
/// # Safety flag
///
/// `has_suit` must be `true` before calling GLB/GLTF exporters. Set it after
/// applying the suit mesh via [`crate::suit::ensure_suit_mesh`].
///
/// # Layout
///
/// All per-vertex arrays (`positions`, `normals`, `tangents`, `uvs`, `colors`)
/// share the same length. `indices` is a flat triangle list (length % 3 == 0).
#[derive(Debug, Clone)]
pub struct MeshBuffers {
/// Per-vertex XYZ positions in world space.
pub positions: Vec<[f32; 3]>,
/// Per-vertex unit normals (XYZ).
pub normals: Vec<[f32; 3]>,
/// Per-vertex tangents (XYZW, W = bitangent sign: ±1).
pub tangents: Vec<[f32; 4]>,
/// Per-vertex UV texture coordinates.
pub uvs: Vec<[f32; 2]>,
/// Triangle index list (groups of 3, CCW winding).
pub indices: Vec<u32>,
/// Optional per-vertex RGBA color (each channel in 0..1).
pub colors: Option<Vec<[f32; 4]>>,
/// Safety flag: exporters refuse to write dressed formats if `false`.
pub has_suit: bool,
}
impl MeshBuffers {
/// Create from the morph engine's raw output.
pub fn from_morph(src: MorphMeshBuffers) -> Self {
let tangents = vec![[1.0f32, 0.0, 0.0, 1.0]; src.positions.len()];
MeshBuffers {
positions: src.positions,
normals: src.normals,
tangents,
uvs: src.uvs,
indices: src.indices,
colors: None,
has_suit: src.has_suit,
}
}
pub fn vertex_count(&self) -> usize {
self.positions.len()
}
pub fn face_count(&self) -> usize {
self.indices.len() / 3
}
}
#[cfg(test)]
mod tests {
use super::*;
use oxihuman_morph::engine::MeshBuffers as MB;
fn sample_morph_mesh() -> MB {
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]; 3],
indices: vec![0, 1, 2],
has_suit: false,
}
}
#[test]
fn from_morph_preserves_data() {
let m = MeshBuffers::from_morph(sample_morph_mesh());
assert_eq!(m.vertex_count(), 3);
assert_eq!(m.face_count(), 1);
assert_eq!(m.tangents.len(), 3);
}
}