rive_rs/bones/
skin.rs

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
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::cell::Cell;
use std::iter;

use once_cell::unsync::OnceCell;

use crate::bones::skinnable::{self, Skinnable};
use crate::bones::Tendon;
use crate::component::Component;
use crate::component_dirt::ComponentDirt;
use crate::container_component::ContainerComponent;
use crate::core::{Core, CoreContext, Object, ObjectRef, OnAdded, Property};
use crate::dyn_vec::DynVec;
use crate::math::Mat;
use crate::option_cell::OptionCell;
use crate::shapes::PathVertex;
use crate::status_code::StatusCode;
use crate::TransformComponent;

#[derive(Debug)]
pub struct Skin {
    container_component: ContainerComponent,
    xx: Property<f32>,
    yx: Property<f32>,
    xy: Property<f32>,
    yy: Property<f32>,
    tx: Property<f32>,
    ty: Property<f32>,
    world_transform: Cell<Mat>,
    tendons: DynVec<Object<Tendon>>,
    bone_transforms: OnceCell<Box<[Cell<Mat>]>>,
    skinnable: OptionCell<Object>,
}

impl ObjectRef<'_, Skin> {
    pub fn xx(&self) -> f32 {
        self.xx.get()
    }

    pub fn set_xx(&self, xx: f32) {
        self.xx.set(xx);
    }

    pub fn yx(&self) -> f32 {
        self.yx.get()
    }

    pub fn set_yx(&self, yx: f32) {
        self.yx.set(yx);
    }

    pub fn xy(&self) -> f32 {
        self.xy.get()
    }

    pub fn set_xy(&self, xy: f32) {
        self.xy.set(xy);
    }

    pub fn yy(&self) -> f32 {
        self.yy.get()
    }

    pub fn set_yy(&self, yy: f32) {
        self.yy.set(yy);
    }

    pub fn tx(&self) -> f32 {
        self.tx.get()
    }

    pub fn set_tx(&self, tx: f32) {
        self.tx.set(tx);
    }

    pub fn ty(&self) -> f32 {
        self.ty.get()
    }

    pub fn set_ty(&self, ty: f32) {
        self.ty.set(ty);
    }
}

impl ObjectRef<'_, Skin> {
    pub fn push_tendon(&self, tendon: Object<Tendon>) {
        self.tendons.push(tendon);
    }

    pub fn deform(&self, vertices: impl Iterator<Item = Object<PathVertex>>) {
        let bone_transforms =
            self.bone_transforms.get().expect("bone_transforms should already be set");
        for vertex in vertices {
            vertex.as_ref().deform(self.world_transform.get(), bone_transforms);
        }
    }

    pub fn build_dependencies(&self) {
        for tendon in self.tendons.iter() {
            let tendon = tendon.as_ref();
            let bone = tendon.bone().expect("Tendon's bone must be set");
            bone.as_ref().cast::<Component>().push_dependent(self.cast::<Component>().as_object());
        }

        self.bone_transforms
            .set(iter::repeat(Cell::new(Mat::default())).take(self.tendons.len() + 1).collect())
            .expect("bone_transform has been set twice");
    }

    pub fn on_dirty(&self, _dirt: ComponentDirt) {
        let skinnable = self.skinnable.get().expect("Skinnable should already be set");
        skinnable::as_ref(skinnable.as_ref()).mark_skin_dirty();
    }

    pub fn update(&self, _value: ComponentDirt) {
        if let Some(bone_transforms) = self.bone_transforms.get() {
            for (bone_transform, tendon) in bone_transforms.iter().skip(1).zip(self.tendons.iter())
            {
                let tendon = tendon.as_ref();
                let bone =
                    tendon.bone().expect("Tendon's bone must be set").cast::<TransformComponent>();
                bone_transform.set(bone.as_ref().world_transform() * tendon.inverse_bind());
            }
        }
    }
}

impl Core for Skin {
    parent_types![(container_component, ContainerComponent)];

    properties![
        (104, xx, set_xx),
        (105, yx, set_yx),
        (106, xy, set_xy),
        (107, yy, set_yy),
        (108, tx, set_tx),
        (109, ty, set_ty),
        container_component,
    ];
}

impl OnAdded for ObjectRef<'_, Skin> {
    on_added!([on_added_dirty, import], ContainerComponent);

    fn on_added_clean(&self, _context: &dyn CoreContext) -> StatusCode {
        self.world_transform.set(Mat {
            scale_x: self.xx(),
            shear_y: self.xy(),
            shear_x: self.yx(),
            scale_y: self.yy(),
            translate_x: self.tx(),
            translate_y: self.ty(),
        });

        self.skinnable.set(
            self.cast::<Component>().parent().and_then(|parent| skinnable::try_from(parent.into())),
        );
        if let Some(skinnable) = self.skinnable.get() {
            skinnable::as_ref(skinnable.as_ref()).set_skin(self.as_object());
        } else {
            return StatusCode::MissingObject;
        }

        StatusCode::Ok
    }
}

impl Default for Skin {
    fn default() -> Self {
        Self {
            container_component: ContainerComponent::default(),
            xx: Property::new(1.0),
            yx: Property::new(0.0),
            xy: Property::new(0.0),
            yy: Property::new(1.0),
            tx: Property::new(0.0),
            ty: Property::new(0.0),
            world_transform: Cell::new(Mat::default()),
            tendons: DynVec::new(),
            bone_transforms: OnceCell::new(),
            skinnable: OptionCell::new(),
        }
    }
}