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
use rinecs::Component;
use na::{Mat4, Mat3, Pnt3, Vec3};
use std::collections::HashMap;
use rayon::prelude::*;
use std::fmt::{self, Display, Debug};

#[derive(Clone, Component)]
#[debug_as_string]
pub struct AnimatedGeometry<T: 'static>{
    pub geom: Vec<T>,
    pub dvert: Vec<Option<rinblender::mesh::MDeformVert>>,
    pub dweight: Vec<rinblender::mesh::MDeformWeight>,
    pub changed: bool,
}

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

impl<T: 'static + Send + Sync> AnimatedGeometry<T>{
    pub fn par_deform_geom_mut(&mut self) -> impl IndexedParallelIterator<Item = Option<(&[rinblender::mesh::MDeformWeight], &mut T)>>{
        let dweight = &self.dweight;
        self.dvert.par_iter()
            .zip(self.geom.par_iter_mut())
            .map(move |(dvert, animated)| dvert.as_ref().map(move |dvert| {
                (&dweight[dvert.dw_start .. dvert.dw_end], animated)
            }))
    }

    pub fn deform_geom_mut(&mut self) -> impl Iterator<Item = Option<(&[rinblender::mesh::MDeformWeight], &mut T)>>{
        let dweight = &self.dweight;
        self.dvert.iter()
            .zip(self.geom.iter_mut())
            .map(move |(dvert, animated)| dvert.as_ref().map(move |dvert| {
                (&dweight[dvert.dw_start .. dvert.dw_end], animated)
            }))
    }

    pub fn deform_geom(&self) -> impl Iterator<Item = Option<(&[rinblender::mesh::MDeformWeight], &T)>>{
        let dweight = &self.dweight;
        self.dvert.iter()
            .zip(self.geom.iter())
            .map(move |(dvert, animated)| dvert.as_ref().map(move |dvert| {
                (&dweight[dvert.dw_start .. dvert.dw_end], animated)
            }))
    }
}

// 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;
    //     }
    // }
// }


#[derive(Component,Clone,Debug,Eq,PartialEq, Serialize, Deserialize)]
pub struct SkeletonRef(pub rinecs::Entity);

#[derive(Clone,Debug,Component,Eq,PartialEq, Serialize, Deserialize)]
pub struct SkeletonName(pub String);

impl Display for SkeletonName{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result{
        Display::fmt(&self.0, fmt)
    }
}

//TODO: this matrices should go in contiguous memory somehow
#[derive(Clone,Debug,Component, Serialize, Deserialize)]
pub struct ArmatureCache{
    pub index: HashMap<String, usize>,
    pub changed: bool,
    pub postmat: Mat4,
    pub postmat3: Mat3,
}

#[derive(Clone,Debug,OneToNComponent, Serialize, Deserialize)]
pub struct ArmatureMatrices{
    pub vertices: Mat4<f32>,
    pub normals: Mat3<f32>,
}

#[derive(Component, Debug, Serialize, Deserialize)]
pub struct Skeleton {
    pub changed: bool,
}

impl Skeleton {
    pub fn new() -> Skeleton {
        Skeleton {
            changed: true,
        }
    }
}

#[derive(Clone, Debug, OneToNComponent, Serialize, Deserialize)]
pub struct BoneBase(pub rinecs::Entity);


#[derive(HierarchicalComponent,Clone,Eq,PartialEq,Debug, Serialize, Deserialize)]
pub struct BoneName(pub String);

impl Display for BoneName{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result{
        Display::fmt(&self.0, fmt)
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BoneRef{
    pub entity: rinecs::Entity,
    pub name: String,
    pub position: Pnt3,
    pub delta: Vec3,
    pub prev_blend_pos: HashMap<String, Option<Pnt3>>,
    pub changed: bool,
}

#[derive(Clone, Component, Debug, Serialize, Deserialize)]
pub struct RootMotionBone(pub BoneRef);


#[derive(Clone, Component, Debug, Serialize, Deserialize)]
#[storage(DenseVec)]
pub struct FootBones{
    pub left: Vec<BoneRef>,
    pub right: Vec<BoneRef>,
}


pub trait DefaultWeight{
    fn default_weight(&self) -> f32;
    fn set_default_weight(&mut self, dw: f32);
}