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
/*!
 * Renderer independent geometry. This module only contains Components that allow to specify geometry
 * for a model. Each renderer module should specify how to draw this geometries with the corresponding
 * materials
 */

use rin::graphics::{Mesh, IndexT};
use rinecs::Entity;
use std::ops::{Deref, DerefMut};
use std::fmt::{self, Debug};

#[derive(Component)]
#[debug_as_string]
pub struct Geometry<T: 'static + Clone>{
    pub(crate) mesh: Mesh<T>,
    has_changed: bool
}

impl<T: 'static + Clone> Debug for Geometry<T>{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result{
        fmt.debug_struct("Geometry")
            .field("vertices", &self.mesh.vertices().len())
            .field("indices", &self.mesh.indices().len())
            .field("has_changed", &self.has_changed)
            .finish()
    }
}

impl<T: 'static + Clone> Geometry<T>{
    pub fn new(mesh: Mesh<T>) -> Geometry<T>{
        Geometry{
            mesh,
            has_changed: false,
        }
    }

    pub fn has_changed(&self) -> bool{
        self.has_changed
    }

    pub fn reset_has_changed(&mut self){
        self.has_changed = false;
    }

    pub fn set(&mut self, mesh: Mesh<T>){
        self.has_changed = true;
        self.mesh = mesh;
    }
}

impl<T: 'static + Clone> Deref for Geometry<T>{
    type Target = Mesh<T>;
    fn deref(&self) -> &Mesh<T>{
        &self.mesh
    }
}

impl<T: 'static + Clone> DerefMut for Geometry<T>{
    fn deref_mut(&mut self) -> &mut Mesh<T>{
        self.has_changed = true;
        &mut self.mesh
    }
}

#[derive(Component, Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd, Serialize, Deserialize, Hash)]
pub struct GeometryRef(pub(crate) Entity);

#[derive(OneToNComponent, Serialize, Deserialize)]
pub struct Submesh(pub(crate) Vec<IndexT>);

impl Debug for Submesh{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result{
        fmt.debug_struct("mutiny::geometry::Submesh")
            .field("indices", &self.0.len())
            .finish()
    }
}


#[derive(Clone,Debug,Component,Default, Serialize, Deserialize)]
pub struct VertexGroups {
    pub vertex_groups: Vec<String>,
    pub index: Vec<Option<usize>>,
    // pub skeleton_name: Option<String>,
    pub default_group: Option<usize>,
}