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
use rinecs::{WriteAndParent, ReadNot, Not};
use super::{DynamicTransformation, Bone };
use rin::graphics::{Node, NodeMut, NodeRef, node};
use na::{one, Pnt3, UnitQuat, Vec3};
use serde::ser::Serialize;

#[derive(Clone, HierarchicalComponent, Debug, Serialize, Deserialize)]
#[debug_custom]
pub struct Transformation{
    pub node: Node,
    pub parent_bone: Option<String>,
    pub is_bone: bool,
    has_changed: bool,
}

impl rinecs::DebugParameter for Transformation{
    fn debug<S: serde::Serializer>(&self, serializer: S) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>{
        #[derive(Serialize)]
        struct Position{
            x: f32,
            y: f32,
            z: f32,
        }

        #[derive(Serialize)]
        struct Orientation{
            x: f32,
            y: f32,
            z: f32,
            w: f32,
        }

        #[derive(Serialize)]
        struct Scale{
            x: f32,
            y: f32,
            z: f32,
        }


        #[derive(Serialize)]
        struct Transformation{
            position: Position,
            orientation: Orientation,
            scale: Scale,
            global_position: Position,
            global_orientation: Orientation,
            global_scale: Scale,
        }

        let p = self.position();
        let q = self.orientation();
        let s = self.scale();

        let gp = self.global_position();
        let gq = self.global_orientation();
        let gs = self.global_scale();

        Transformation{
            position: Position{
                x: p[0], y: p[1], z: p[2]
            },
            orientation: Orientation{
                x: q[0], y: q[1], z: q[2], w: q[3]
            },
            scale: Scale{
                x: s.x, y: s.y, z: s.z
            },
            global_position: Position{
                x: gp[0], y: gp[1], z: gp[2]
            },
            global_orientation: Orientation{
                x: gq[0], y: gq[1], z: gq[2], w: gq[3]
            },
            global_scale: Scale{
                x: gs.x, y: gs.y, z: gs.z
            },
        }.serialize(serializer)
    }
}

impl Default for Transformation{
    fn default() -> Transformation{
        Transformation{ node: one(), parent_bone: None, is_bone: false, has_changed: false,}
    }
}

impl Transformation{
    pub fn new(pos: Pnt3, rot: UnitQuat, scale: Vec3) -> Transformation{
        Transformation{ node: Node::new(pos, rot, scale), parent_bone: None, is_bone: false, has_changed: false,}
    }

    pub fn from_position(pos: Pnt3) -> Transformation{
        Transformation{ node: Node::new(pos, one(), vec3!(1.)), parent_bone: None, is_bone: false, has_changed: false,}
    }

    // pub fn from_axis_angle(axis: &Unit<Vec3>, angle: Rad<f32>) -> Transformation{
    //     Transformation(graphics::Node::new(origin(), UnitQuat::from_axis_angle(axis, angle.value()), vec3!(1.)))
    // }

    pub fn from_node(node: Node) -> Transformation{
        Transformation{ node: node, parent_bone: None, is_bone: false, has_changed: false,}
    }

    pub fn from_node_parent_bone(node: Node, parent_bone: String) -> Transformation{
        Transformation{ node, parent_bone: Some(parent_bone), is_bone: false, has_changed: false,}
    }

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

    pub fn into_parameter(&self) -> String{
        let pos = self.node.global_position();
        let quat = self.node.global_orientation().into_inner();
        let scale = self.node.global_scale();
format!("Transformation {{
    position: Pnt3 {{
        x: {},
        y: {},
        z: {},
    }},
    orientation: UnitQuat {{
        x: {},
        y: {},
        z: {},
        w: {},
    }},
    scale: Vec3 {{
        x: {},
        y: {},
        z: {},
    }}
}}", pos.x, pos.y, pos.z,
    quat.coords.x, quat.coords.y, quat.coords.z, quat.coords.w,
    scale.x, scale.y, scale.z)
    }
}

impl NodeRef for Transformation{
    fn node(&self) -> &Node{
        &self.node
    }
}

impl NodeMut for Transformation{
    fn node_mut(&mut self) -> &mut Node{
        &mut self.node
    }

    fn update_with_parent_flags(&mut self, parent: Option<&Node>, flags: node::Flags) -> bool{
        self.has_changed |= self.node.update_with_parent_flags(parent, flags);
        self.has_changed
    }
}

pub fn update_dynamic(entities: rinecs::Entities, _: rinecs::Resources){
    for ((trafo, parent), _) in entities.ordered_iter_for::<(WriteAndParent<Transformation>, ReadNot<DynamicTransformation, Bone>)>(){
        // trafo.set_position(&pnt3!(1.));
        trafo.has_changed = false;
        trafo.update_with_parent(parent.map(|t| &t.node));
    }
}

pub fn update_static(entities: rinecs::Entities, _: rinecs::Resources){
    for ((trafo, parent), _) in entities.ordered_iter_for::<(WriteAndParent<Transformation>, Not<DynamicTransformation>)>(){
        trafo.has_changed = false;
        trafo.update_with_parent(parent.map(|t| &t.node));
    }
}

pub fn update_all(entities: rinecs::Entities, _: rinecs::Resources){
    for ((trafo, parent), _) in entities.ordered_iter_for::<(WriteAndParent<Transformation>, Not<Bone>)>(){
        // Bone transformations are set when updating the bones
        // updating them here would get an incorrect transformation
        // since the hierarchy chain includes the animated and original
        // nodes
        trafo.has_changed = false;
        trafo.update_with_parent(parent.map(|t| &t.node));
    }
}