Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Quat animation #1

Merged
merged 10 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,3 @@ icon = "@mipmap/ic_launcher"
build_targets = ["aarch64-linux-android", "armv7-linux-androideabi"]
min_sdk_version = 16
target_sdk_version = 29

2 changes: 1 addition & 1 deletion crates/bevy_animation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ bevy_transform = { path = "../bevy_transform", version = "0.3.0" }
bevy_render = { path = "../bevy_render", version = "0.3.0" }
bevy_property = { path = "../bevy_property", version = "0.3.0" }

splines = { version = "3.5.0", features = ["serialization"] }
splines = { version = "3.5.0", features = ["serialization", "impl-glam"] }
5 changes: 3 additions & 2 deletions crates/bevy_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ pub mod prelude {
plugin::AnimationPlugin,
spline_group::{LoopStyle, SplineGroup},
spline_groups::{
one::AnimationSplineOne, three::AnimationSplineThree,
transform::AnimationSplineTransform,
one::AnimationSplineOne,
three::AnimationSplineThree,
transform::{AnimationSplineTransform, SplineQuatExt},
},
vec3_option::Vec3Option,
};
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_animation/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
use bevy_app::{AppBuilder, Plugin};
use bevy_core::Time;
use bevy_ecs::{IntoSystem, Query, Res};
use bevy_math::{Quat, Vec3};
use bevy_math::Vec3;
use bevy_transform::components::Transform;

#[derive(Default)]
Expand Down Expand Up @@ -41,15 +41,15 @@ fn advance_animation_transform(
for (mut transform, mut splines) in q.iter_mut() {
let mut scale = transform.scale;
splines.advance(time.delta_seconds);
let s = splines.current();
s.translation.alter(&mut transform.translation);
if let Some(sample_scale) = s.scale {
let sample = splines.current();
sample.translation.alter(&mut transform.translation);
if let Some(sample_scale) = sample.scale {
scale = Vec3::one() * sample_scale;
}
let mut rot = Vec3::zero();
s.rotation.alter(&mut rot);
*transform = Transform::from_translation(transform.translation);
transform.rotation = Quat::from_rotation_ypr(rot.x, rot.y, rot.z);
if let Some(rotation) = sample.rotation {
transform.rotation = rotation;
}
transform.apply_non_uniform_scale(scale);
}
}
68 changes: 18 additions & 50 deletions crates/bevy_animation/src/spline_group.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
use core::time::Duration;
use splines::Spline;

pub(crate) trait SplineExt {
fn start_time(&self) -> Option<f32>;
fn end_time(&self) -> Option<f32>;
}

impl<V> SplineExt for Spline<f32, V> {
fn start_time(&self) -> Option<f32> {
self.keys().first().map(|k| k.t)
}

fn end_time(&self) -> Option<f32> {
self.keys().last().map(|k| k.t)
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LoopStyle {
Once,
Expand All @@ -11,8 +26,6 @@ pub enum LoopStyle {
pub trait SplineGroup {
type Sample;

fn splines(&self) -> Vec<&Spline<f32, f32>>;

fn loop_style(&self) -> LoopStyle;
fn loop_style_mut(&mut self) -> &mut LoopStyle;

Expand All @@ -34,46 +47,11 @@ pub trait SplineGroup {
self.sample(self.time())
}

fn is_empty(&self) -> bool {
let any_not_empty = self.splines().into_iter().any(|v| !v.is_empty());
!any_not_empty
}
fn is_empty(&self) -> bool;

fn start_time(&self) -> Option<f32> {
let starts: Vec<f32> = self
.splines()
.into_iter()
.filter_map(spline_start_time)
.collect();

if starts.is_empty() {
None
} else {
Some(
starts
.iter()
.fold(starts[0], |acc, v| if *v < acc { *v } else { acc }),
)
}
}
fn start_time(&self) -> Option<f32>;

fn end_time(&self) -> Option<f32> {
let ends: Vec<f32> = self
.splines()
.into_iter()
.map(|s| spline_end_time(s))
.filter_map(|s| s)
.collect();

if ends.is_empty() {
None
} else {
Some(
ends.iter()
.fold(ends[0], |acc, v| if *v > acc { *v } else { acc }),
)
}
}
fn end_time(&self) -> Option<f32>;

fn duration(&self) -> Option<Duration> {
self.start_time()
Expand Down Expand Up @@ -147,13 +125,3 @@ pub trait SplineGroup {
*self.paused_mut() = !paused;
}
}

fn spline_start_time(spline: &Spline<f32, f32>) -> Option<f32> {
spline.get(0).map(|first_key| first_key.t)
}

fn spline_end_time(spline: &Spline<f32, f32>) -> Option<f32> {
spline
.get(spline.len().saturating_sub(1))
.map(|last_key| last_key.t)
}
14 changes: 11 additions & 3 deletions crates/bevy_animation/src/spline_groups/one.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::spline_group::{LoopStyle, SplineGroup};
use crate::spline_group::{LoopStyle, SplineExt, SplineGroup};
use splines::Spline;

pub struct AnimationSplineOne {
Expand Down Expand Up @@ -26,8 +26,16 @@ impl Default for AnimationSplineOne {
impl SplineGroup for AnimationSplineOne {
type Sample = Option<f32>;

fn splines(&self) -> Vec<&Spline<f32, f32>> {
vec![&self.spline]
fn is_empty(&self) -> bool {
self.spline.is_empty()
}

fn start_time(&self) -> Option<f32> {
self.spline.start_time()
}

fn end_time(&self) -> Option<f32> {
self.spline.end_time()
}

fn loop_style(&self) -> LoopStyle {
Expand Down
28 changes: 25 additions & 3 deletions crates/bevy_animation/src/spline_groups/three.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
spline_group::{LoopStyle, SplineGroup},
spline_group::{LoopStyle, SplineExt, SplineGroup},
vec3_option::Vec3Option,
};
use splines::Spline;
Expand Down Expand Up @@ -31,11 +31,33 @@ impl Default for AnimationSplineThree {
}
}

fn cmp_f32(a: &f32, b: &f32) -> std::cmp::Ordering {
a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
}

impl SplineGroup for AnimationSplineThree {
type Sample = Vec3Option;

fn splines(&self) -> Vec<&Spline<f32, f32>> {
vec![&self.x, &self.y, &self.z]
fn is_empty(&self) -> bool {
self.x.is_empty() && self.y.is_empty() && self.z.is_empty()
}

fn start_time(&self) -> Option<f32> {
self.x
.start_time()
.into_iter()
.chain(self.y.start_time())
.chain(self.z.start_time())
.min_by(cmp_f32)
}

fn end_time(&self) -> Option<f32> {
self.x
.end_time()
.into_iter()
.chain(self.y.end_time())
.chain(self.z.end_time())
.max_by(cmp_f32)
}

fn loop_style(&self) -> LoopStyle {
Expand Down
Loading