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
use rinecs::{Component, NToOneComponent, OneToNComponent, HierarchicalComponent, Entity};
use rin_math::{Mat4, Mat3, Pnt3, Vec3, UnitQuat, Vec4, DualQuat};
use hashbrown::HashMap;
use std::fmt::{self, Display, Debug};
use serde_derive::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy)]
pub struct SkinWeights {
    pub weights: Vec4<f32>,
    pub bone_indices: Vec4<u32>,
}

#[derive(Clone, Component)]
#[debug_as_string]
pub struct GeometryWeights{
    pub dvert: Vec<Option<rinblender::mesh::MDeformVert>>,
    pub dweight: Vec<rinblender::mesh::MDeformWeight>,
    pub final_dverts: Vec<Option<rinblender::mesh::MDeformVert>>,
    pub final_weights: Vec<rinblender::mesh::MDeformWeight>,
    pub gpu_weights: Vec<SkinWeights>,
    pub deformflags: rinblender::ArmatureDeformFlag,
}

impl Debug for GeometryWeights{
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result{
        fmt.write_str("GeometryWeights")
    }
}

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

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

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

#[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 orientation: UnitQuat,
    pub orientation_delta: UnitQuat,
    pub prev_blend_pos: HashMap<String, Option<(f32, Pnt3)>>,
    pub prev_blend_orientation: HashMap<String, Option<(f32, UnitQuat)>>,
    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);
}

#[derive(Clone,Debug,Component, Serialize, Deserialize)]
pub struct ArmatureCache{
    // TODO: Can we remove this hashmap, is only used by the blender bundle on load to setup the
    // bone indices and matrices caches
    pub index: HashMap<String, usize>,
    pub skinning_bones_index: Vec<Entity>,
    pub changed: bool,
    pub postmat: Option<Mat4>,
    pub postmat3: Option<Mat3>,
    pub premat: Option<Mat4>,
    pub premat3: Option<Mat3>,
}

#[derive(Clone,Copy,Debug,OneToNComponent, Serialize, Deserialize)]
pub struct ArmatureMatrices{
    pub bone_mat: Mat4,
}

#[derive(Clone,Copy,Debug,OneToNComponent, Serialize, Deserialize)]
pub struct ArmatureDualQuats{
    pub dual_quat: DualQuat,
    pub scale: Mat4,
}

#[derive(Clone,Debug,Component)]
#[debug_as_string]
pub struct BoneWeightsAndIndicesBuffer(pub glin::SharedBuffer<SkinWeights>);

#[derive(Clone,Debug,Component)]
#[debug_as_string]
pub struct ArmatureMatricesBuffer(pub glin::SharedBuffer<ArmatureMatrices>);

#[derive(Clone,Debug,Component)]
#[debug_as_string]
pub struct ArmatureDualQuatsBuffer(pub glin::SharedBuffer<ArmatureDualQuats>);