aboutsummaryrefslogtreecommitdiff
path: root/src/mesh.rs
blob: d491a97404c1b9f94ef08edeb2c838fcc819d637 (plain)
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
use crate::proto::Mesh;
use anyhow::Result;
use glam::vec3a;
use weareshared::{packets::Resource, resources::MeshPart, store::ResourceStore};

pub fn convert_mesh(m: Mesh, store: &ResourceStore) -> Result<Resource<MeshPart>> {
    let mut index_strip = Vec::new();
    let strip_len = m.indices[0];
    let mut zeros = 0;
    for i in 0..strip_len {
        let val = m.indices[i as usize + 1];
        index_strip.push((zeros - val) as u32);
        if val == 0 {
            zeros += 1;
        }
    }
    let mut index = Vec::new();
    for i in 0..index_strip.len() - 2 {
        if i & 1 == 0 {
            index.push([index_strip[i + 0], index_strip[i + 1], index_strip[i + 2]]);
        } else {
            index.push([index_strip[i + 0], index_strip[i + 2], index_strip[i + 1]]);
        }
    }

    let mut positions = Vec::new();
    let vert = m.vertices();
    let vertex_count = vert.len() / 3;
    let (mut x, mut y, mut z) = (0u8, 0u8, 0u8);
    for i in 0..vertex_count {
        x = x.wrapping_add(vert[vertex_count * 0 + i]);
        y = y.wrapping_add(vert[vertex_count * 1 + i]);
        z = z.wrapping_add(vert[vertex_count * 2 + i]);
        positions.push(vec3a(x as f32, y as f32, z as f32));
    }

    Ok(store.set(&MeshPart {
        index: Some(store.set(&index)?),
        va_position: Some(store.set(&positions)?),
        g_double_sided: Some(()),
        ..Default::default()
    })?)
}