mikktspace/
lib.rs

1mod generated;
2
3#[cfg(feature = "nalgebra")]
4type Vec3 = nalgebra::Vector3<f32>;
5#[cfg(feature = "nalgebra")]
6type Vec2 = nalgebra::Vector2<f32>;
7
8#[cfg(feature = "glam")]
9type Vec3 = glam::Vec3;
10#[cfg(feature = "glam")]
11type Vec2 = glam::Vec2;
12
13/// The interface by which mikktspace interacts with your geometry.
14pub trait Geometry {
15    /// Returns the number of faces.
16    fn num_faces(&self) -> usize;
17
18    /// Returns the number of vertices of a face.
19    fn num_vertices_of_face(&self, face: usize) -> usize;
20
21    /// Returns the position of a vertex.
22    fn position(&self, face: usize, vert: usize) -> [f32; 3];
23
24    /// Returns the normal of a vertex.
25    fn normal(&self, face: usize, vert: usize) -> [f32; 3];
26
27    /// Returns the texture coordinate of a vertex.
28    fn tex_coord(&self, face: usize, vert: usize) -> [f32; 2];
29
30    /// Sets the generated tangent for a vertex.
31    /// Leave this function unimplemented if you are implementing
32    /// `set_tangent_encoded`.
33    fn set_tangent(
34        &mut self,
35        tangent: [f32; 3],
36        _bi_tangent: [f32; 3],
37        _f_mag_s: f32,
38        _f_mag_t: f32,
39        bi_tangent_preserves_orientation: bool,
40        face: usize,
41        vert: usize,
42    ) {
43        let sign = if bi_tangent_preserves_orientation {
44            1.0
45        } else {
46            -1.0
47        };
48        self.set_tangent_encoded([tangent[0], tangent[1], tangent[2], sign], face, vert);
49    }
50
51    /// Sets the generated tangent for a vertex with its bi-tangent encoded as the 'W' (4th)
52    /// component in the tangent. The 'W' component marks if the bi-tangent is flipped. This
53    /// is called by the default implementation of `set_tangent`; therefore, this function will
54    /// not be called by the crate unless `set_tangent` is unimplemented.
55    fn set_tangent_encoded(&mut self, _tangent: [f32; 4], _face: usize, _vert: usize) {}
56}
57
58/// Generates tangents for the input geometry.
59///
60/// # Errors
61///
62/// Returns `false` if the geometry is unsuitable for tangent generation including,
63/// but not limited to, lack of vertices.
64pub fn generate_tangents<I: Geometry>(geometry: &mut I) -> bool {
65    unsafe { generated::genTangSpace(geometry, 180.0) }
66}
67
68fn get_position<I: Geometry>(geometry: &mut I, index: usize) -> Vec3 {
69    let (face, vert) = index_to_face_vert(index);
70    geometry.position(face, vert).into()
71}
72
73fn get_tex_coord<I: Geometry>(geometry: &mut I, index: usize) -> Vec3 {
74    let (face, vert) = index_to_face_vert(index);
75    let tex_coord: Vec2 = geometry.tex_coord(face, vert).into();
76    #[cfg(feature = "nalgebra")]
77    let val = tex_coord.insert_row(2, 1.0);
78    #[cfg(feature = "glam")]
79    let val = tex_coord.extend(1.0);
80    val
81}
82
83fn get_normal<I: Geometry>(geometry: &mut I, index: usize) -> Vec3 {
84    let (face, vert) = index_to_face_vert(index);
85    geometry.normal(face, vert).into()
86}
87
88fn index_to_face_vert(index: usize) -> (usize, usize) {
89    (index >> 2, index & 0x3)
90}
91
92fn face_vert_to_index(face: usize, vert: usize) -> usize {
93    face << 2 | vert & 0x3
94}