-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmesh.rs
35 lines (30 loc) · 1.07 KB
/
mesh.rs
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
use glam::Vec3A;
pub type MeshVertexId = u32;
pub const NULL_MESH_VERTEX_ID: MeshVertexId = MeshVertexId::MAX;
/// Repair normals for vertices on sharp edges.
///
/// This may add vertices to the mesh in order to allow multiple normals at the
/// same position.
pub fn repair_sharp_normals(
normal_similarity_threshold: f32,
tri_indices: &mut [u32],
positions: &mut Vec<Vec3A>,
normals: &mut Vec<Vec3A>,
) {
for t in tri_indices.chunks_exact_mut(3) {
let mut tri = [t[0], t[1], t[2]];
let n = tri.map(|v| normals[v as usize]);
let p = tri.map(|v| positions[v as usize]);
let tri_normal = (p[1] - p[0]).cross(p[2] - p[0]).normalize();
// Force dissident normals to use the triangle's normal.
for ti in 0..3 {
if n[ti].dot(tri_normal) < normal_similarity_threshold {
let new_vert = positions.len() as MeshVertexId;
positions.push(p[ti]);
normals.push(tri_normal);
tri[ti] = new_vert;
}
}
t.copy_from_slice(&tri);
}
}