|
1 |
| -pub fn add(left: u64, right: u64) -> u64 { |
2 |
| - left + right |
| 1 | +//! General-purpose [`Transform`] interpolation for the Bevy game engine. |
| 2 | +
|
| 3 | +mod interpolation; |
| 4 | + |
| 5 | +use bevy::prelude::*; |
| 6 | +pub use interpolation::*; |
| 7 | + |
| 8 | +/// Performs transform interpolation. |
| 9 | +#[derive(Debug, Default)] |
| 10 | +pub struct TransformInterpolationPlugin { |
| 11 | + pub global_translation_interpolation: bool, |
| 12 | + pub global_rotation_interpolation: bool, |
| 13 | + pub global_scale_interpolation: bool, |
| 14 | +} |
| 15 | + |
| 16 | +impl Plugin for TransformInterpolationPlugin { |
| 17 | + fn build(&self, app: &mut App) { |
| 18 | + app.register_type::<( |
| 19 | + TranslationEasingState, |
| 20 | + RotationEasingState, |
| 21 | + ScaleEasingState, |
| 22 | + )>(); |
| 23 | + app.register_type::<( |
| 24 | + TranslationInterpolation, |
| 25 | + RotationInterpolation, |
| 26 | + ScaleInterpolation, |
| 27 | + )>(); |
| 28 | + |
| 29 | + app.configure_sets( |
| 30 | + PostUpdate, |
| 31 | + TransformInterpolationSet.before(TransformSystem::TransformPropagate), |
| 32 | + ); |
| 33 | + |
| 34 | + app.add_systems( |
| 35 | + FixedFirst, |
| 36 | + ( |
| 37 | + ( |
| 38 | + reset_translation_interpolation, |
| 39 | + reset_rotation_interpolation, |
| 40 | + reset_scale_interpolation, |
| 41 | + ), |
| 42 | + ( |
| 43 | + update_translation_interpolation_start, |
| 44 | + update_rotation_interpolation_start, |
| 45 | + update_scale_interpolation_start, |
| 46 | + ), |
| 47 | + ) |
| 48 | + .chain(), |
| 49 | + ); |
| 50 | + app.add_systems( |
| 51 | + FixedLast, |
| 52 | + ( |
| 53 | + update_translation_interpolation_end, |
| 54 | + update_rotation_interpolation_end, |
| 55 | + update_scale_interpolation_end, |
| 56 | + ), |
| 57 | + ); |
| 58 | + |
| 59 | + app.add_systems( |
| 60 | + PostUpdate, |
| 61 | + (ease_translation, ease_rotation, ease_scale).in_set(TransformInterpolationSet), |
| 62 | + ); |
| 63 | + |
| 64 | + let interpolate_translation = self.global_translation_interpolation; |
| 65 | + let interpolate_rotation = self.global_rotation_interpolation; |
| 66 | + let interpolate_scale = self.global_scale_interpolation; |
| 67 | + |
| 68 | + app.observe( |
| 69 | + move |trigger: Trigger<OnAdd, Transform>, mut commands: Commands| { |
| 70 | + if interpolate_translation { |
| 71 | + commands |
| 72 | + .entity(trigger.entity()) |
| 73 | + .insert(TranslationInterpolation); |
| 74 | + } |
| 75 | + if interpolate_rotation { |
| 76 | + commands |
| 77 | + .entity(trigger.entity()) |
| 78 | + .insert(RotationInterpolation); |
| 79 | + } |
| 80 | + if interpolate_scale { |
| 81 | + commands.entity(trigger.entity()).insert(ScaleInterpolation); |
| 82 | + } |
| 83 | + }, |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// A system set for [transform interpolation]. Runs in [`PostUpdate`], before [`TransformSystem::TransformPropagate`]. |
| 89 | +/// |
| 90 | +/// [transform interpolation]: TransformInterpolation |
| 91 | +#[derive(SystemSet, Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 92 | +pub struct TransformInterpolationSet; |
| 93 | + |
| 94 | +/// Stores the start and end states used for interpolating the translation of an entity. |
| 95 | +/// The change in translation is smoothed from `start` to `end` in between [`FixedUpdate`] runs. |
| 96 | +/// |
| 97 | +/// On its own, this component is not updated automatically. To perform automatic interpolation, |
| 98 | +/// add the [`TranslationInterpolation`] component. |
| 99 | +
|
| 100 | +#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Reflect)] |
| 101 | +#[reflect(Component, Debug, Default)] |
| 102 | +pub struct TranslationEasingState { |
| 103 | + pub start: Option<Vec3>, |
| 104 | + pub end: Option<Vec3>, |
| 105 | +} |
| 106 | + |
| 107 | +/// Stores the start and end states used for interpolating the rotation of an entity. |
| 108 | +/// The change in rotation is smoothed from `start` to `end` in between [`FixedUpdate`] runs. |
| 109 | +/// |
| 110 | +/// On its own, this component is not updated automatically. To perform automatic interpolation, |
| 111 | +/// add the [`RotationInterpolation`] component. |
| 112 | +#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Reflect)] |
| 113 | +#[reflect(Component, Debug, Default)] |
| 114 | +pub struct RotationEasingState { |
| 115 | + pub start: Option<Quat>, |
| 116 | + pub end: Option<Quat>, |
| 117 | +} |
| 118 | + |
| 119 | +/// Stores the start and end states used for interpolating the scale of an entity. |
| 120 | +/// The change in scale is smoothed from `start` to `end` in between [`FixedUpdate`] runs. |
| 121 | +/// |
| 122 | +/// On its own, this component is not updated automatically. To perform automatic interpolation, |
| 123 | +/// add the [`ScaleInterpolation`] component. |
| 124 | +#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Reflect)] |
| 125 | +#[reflect(Component, Debug, Default)] |
| 126 | +pub struct ScaleEasingState { |
| 127 | + pub start: Option<Vec3>, |
| 128 | + pub end: Option<Vec3>, |
| 129 | +} |
| 130 | + |
| 131 | +fn reset_translation_interpolation( |
| 132 | + mut query: Query<(&mut Transform, &mut TranslationEasingState)>, |
| 133 | +) { |
| 134 | + for (mut transform, mut easing) in &mut query { |
| 135 | + // Make sure the previous easing is fully applied. |
| 136 | + if let Some(end) = easing.end { |
| 137 | + transform.translation = end; |
| 138 | + } |
| 139 | + |
| 140 | + easing.start = None; |
| 141 | + easing.end = None; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +fn reset_rotation_interpolation(mut query: Query<(&mut Transform, &mut RotationEasingState)>) { |
| 146 | + for (mut transform, mut easing) in &mut query { |
| 147 | + // Make sure the previous easing is fully applied. |
| 148 | + if let Some(end) = easing.end { |
| 149 | + transform.rotation = end; |
| 150 | + } |
| 151 | + |
| 152 | + easing.start = None; |
| 153 | + easing.end = None; |
| 154 | + } |
3 | 155 | }
|
4 | 156 |
|
5 |
| -#[cfg(test)] |
6 |
| -mod tests { |
7 |
| - use super::*; |
| 157 | +fn reset_scale_interpolation(mut query: Query<(&mut Transform, &mut ScaleEasingState)>) { |
| 158 | + for (mut transform, mut easing) in &mut query { |
| 159 | + // Make sure the previous easing is fully applied. |
| 160 | + if let Some(end) = easing.end { |
| 161 | + transform.scale = end; |
| 162 | + } |
8 | 163 |
|
9 |
| - #[test] |
10 |
| - fn it_works() { |
11 |
| - let result = add(2, 2); |
12 |
| - assert_eq!(result, 4); |
| 164 | + easing.start = None; |
| 165 | + easing.end = None; |
13 | 166 | }
|
14 | 167 | }
|
| 168 | + |
| 169 | +fn ease_translation( |
| 170 | + mut query: Query<(&mut Transform, &TranslationEasingState)>, |
| 171 | + time: Res<Time<Fixed>>, |
| 172 | +) { |
| 173 | + let overstep = time.overstep_fraction(); |
| 174 | + |
| 175 | + query.iter_mut().for_each(|(mut transform, interpolation)| { |
| 176 | + if let (Some(start), Some(end)) = (interpolation.start, interpolation.end) { |
| 177 | + transform.translation = start.lerp(end, overstep); |
| 178 | + } |
| 179 | + }); |
| 180 | +} |
| 181 | + |
| 182 | +fn ease_rotation(mut query: Query<(&mut Transform, &RotationEasingState)>, time: Res<Time<Fixed>>) { |
| 183 | + let overstep = time.overstep_fraction(); |
| 184 | + |
| 185 | + query |
| 186 | + .par_iter_mut() |
| 187 | + .for_each(|(mut transform, interpolation)| { |
| 188 | + if let (Some(start), Some(end)) = (interpolation.start, interpolation.end) { |
| 189 | + transform.rotation = start.slerp(end, overstep); |
| 190 | + } |
| 191 | + }); |
| 192 | +} |
| 193 | + |
| 194 | +fn ease_scale(mut query: Query<(&mut Transform, &ScaleEasingState)>, time: Res<Time<Fixed>>) { |
| 195 | + let overstep = time.overstep_fraction(); |
| 196 | + |
| 197 | + query.iter_mut().for_each(|(mut transform, interpolation)| { |
| 198 | + if let (Some(start), Some(end)) = (interpolation.start, interpolation.end) { |
| 199 | + transform.scale = start.lerp(end, overstep); |
| 200 | + } |
| 201 | + }); |
| 202 | +} |
0 commit comments