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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*!
 * 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 std::ops::{Deref, DerefMut};
use std::fmt::{self, Debug};
#[cfg(feature="debug_geometry")]
use rin_material::MaterialRef;
use rinecs::{Entity, Changes, Component, NToOneComponent, OneToNComponent};
use serde_derive::{Deserialize, Serialize};

#[derive(Component, Clone)]
#[debug_as_string]
#[autochanges]
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 set(&mut self, mesh: Mesh<T>){
        self.has_changed = true;
        self.mesh = mesh;
    }
}

impl<T: 'static + Clone> Changes for Geometry<T>{
    fn has_changed(&self) -> bool{
        self.has_changed
    }

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

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(NToOneComponent, Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd, Serialize, Deserialize, Hash)]
#[autochanges]
pub struct GeometryRef(Entity, bool);

impl Changes for GeometryRef{
    fn has_changed(&self) -> bool{
        self.1
    }

    fn reset_changed(&mut self){
        self.1 = false;
    }
}

impl GeometryRef{
    pub fn new(entity: Entity) -> GeometryRef{
        GeometryRef(entity, true)
    }
}

impl Deref for GeometryRef{
    type Target = Entity;
    fn deref(&self) -> &Entity{
        &self.0
    }
}

#[derive(OneToNComponent, Serialize, Deserialize)]
pub struct Submesh(pub 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 default_group: Option<usize>,
}


#[derive(Clone, Component)]
#[debug_as_string]
pub struct AnimatedGeometry<T: 'static>{
    pub geom: Vec<T>,
    pub changed: bool,
}

impl<T: 'static> Debug for AnimatedGeometry<T>{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result{
        fmt.write_str("AnimatedGeometry<T>")
    }
}

#[cfg(feature="debug_geometry")]
#[derive(Component, Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
#[autochanges]
pub struct DebugGeometryRef {
    geometry: GeometryRef,
    material: MaterialRef,
    visible: bool,
    has_changed: bool,
}

#[cfg(feature="debug_geometry")]
impl DebugGeometryRef{
    pub fn new(geometry: GeometryRef, material: MaterialRef) -> DebugGeometryRef{
        DebugGeometryRef{
            geometry,
            material,
            visible: false,
            has_changed: true,
        }
    }

    pub fn show(&mut self){
        self.has_changed = true;
        self.visible = true;
    }

    pub fn hide(&mut self){
        self.has_changed = true;
        self.visible = false;
    }

    pub fn set_visible(&mut self, visible: bool){
        self.has_changed = true;
        self.visible = visible;
    }

    pub fn geometry(&self) -> &GeometryRef{
        &self.geometry
    }

    pub fn material(&self) -> &MaterialRef{
        &self.material
    }

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

#[cfg(feature="debug_geometry")]
impl Changes for DebugGeometryRef{
    fn has_changed(&self) -> bool{
        self.has_changed
    }

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

// impl AnimatedGeometry{
    // TODO: recalculate normals only works for meshes without indices right now since
    // other meshes have separate vertices to account for different materials
    // pub fn recalculate_normals(&mut self, indices: &[graphics::IndexT]){
    //     // fn newell(prev: &Vec3, curr: &Vec3) -> Vec3{
    //     //     vec3(
    //     //         (prev.y - curr.y) * (prev.z + curr.z),
    //     //         (prev.z - curr.z) * (prev.x + curr.x),
    //     //         (prev.x - curr.x) * (prev.y + curr.y)
    //     //     )
    //     // }
    //
    //     let zero3: Vec3 = zero();
    //     for v in self.iter_mut(){
    //         v.normal = zero3;
    //     }
    //
    //     if indices.is_empty(){
    //         for face in self.chunks_mut(3){
    //             let normal = {
    //                 let p1 = &face[0].position;
    //                 let p2 = &face[1].position;
    //                 let p3 = &face[2].position;
    //                 let v1 = *p1 - *p2;
    //                 let v2 = *p1 - *p3;
    //                 v1.xyz().cross(&v2.xyz())
    //             };
    //
    //             // let last = [*face.last().unwrap(), *face.first().unwrap()];
    //             // let normal = face.windows(2).chain(iter::once(last.as_ref())).fold(zero3, |normal, prev_curr|{
    //             //     let prev = prev_curr[0];
    //             //     let curr = prev_curr[1];
    //             //     let new = newell(&prev.position.xyz(), &curr.position.xyz());
    //             //     normal + new
    //             // });
    //
    //             // let normal = newell(&face[0].position.xyz(), &face[1].position.xyz()) +
    //             //     newell(&face[1].position.xyz(), &face[2].position.xyz()) +
    //             //     newell(&face[2].position.xyz(), &face[0].position.xyz());
    //
    //             for v in face.iter_mut(){
    //                 v.normal += normal;
    //             }
    //         }
    //     }else{
    //
    //     }
    //
    //     for mvert in self.iter_mut(){
    //         let normal = normalize(&mvert.normal);
    //         mvert.normal = normal;// / num as f32;
    //     }
    // }
// }