Skip to content

Commit

Permalink
Add a bunch of easing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Aug 6, 2024
1 parent 4f02dc9 commit 5b89574
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- uses: awalsh128/cache-apt-pkgs-action@latest
if: matrix.os == 'ubuntu-latest'
with:
packages: libgl1-mesa-dev libasound2-dev libopenal-dev
packages: libgl1-mesa-dev libasound2-dev libopenal-dev libsdl2-dev pkg-config
version: 1.0

- uses: Swatinem/rust-cache@v1
Expand Down
115 changes: 115 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backends/wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ graphics = { package = "surreal-graphics", path = "../../core/graphics" }
input = { package = "surreal-input", path = "../../core/input" }

# platform dependencies
winit = { version = "0.30.4", default-features = false, features = ["rwh_06"] }
winit = { version = "0.30.4", default-features = false, features = ["rwh_06", "x11"] }
wgpu = { version = "22.1.0", default-features = false, features = ["metal"] }
8 changes: 2 additions & 6 deletions backends/wgpu/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
use std::error::Error;

use common::{Color, Rectangle, UVec2};
use graphics::{
BlendState, BufferError, BufferId, BufferKind, BufferUsage, CullingMode, GraphicsBackend, MemoryBarrier, MeshError,
MeshId, PrimitiveTopology, ScissorMode, ShaderError, ShaderId, ShaderKernel, ShaderUniform, TargetError, TargetId,
TextureError, TextureFilter, TextureFormat, TextureId, TextureSampler, VertexDescriptor,
};
use graphics::*;
use wgpu::{Backends, CompositeAlphaMode, InstanceDescriptor, InstanceFlags, StoreOp, SurfaceTargetUnsafe};

/// A wgpu-based graphics backend.
Expand Down Expand Up @@ -125,7 +121,7 @@ impl GraphicsBackend for WgpuGraphicsBackend {
}

fn clear_depth_buffer(&self, depth: f32) {
todo!()
// no-op
}

fn viewport_size(&self) -> (usize, usize) {
Expand Down
2 changes: 2 additions & 0 deletions core/common/src/maths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub use angles::*;
pub use cameras::*;
pub use colors::*;
pub use curves::*;
pub use easing::*;
pub use geometry::*;
pub use hex::*;
pub use lerp::*;
Expand All @@ -23,6 +24,7 @@ mod angles;
mod cameras;
mod colors;
mod curves;
mod easing;
mod geometry;
mod hex;
mod lerp;
Expand Down
97 changes: 97 additions & 0 deletions core/common/src/maths/easing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use crate::Scalar;

/// A tweening function.
pub type Easing<T> = fn(T, T, f32) -> T;

/// A trait for types that can be tweened.
pub trait Tweenable: Sized {
/// Tween the value towards the target.
fn tween(a: Self, b: Self, delta: f32, easing: Easing<Self>) -> Self {
easing(a, b, delta)
}
}

/// Linear easing.
pub fn linear<T: Scalar>(a: T, b: T, t: f32) -> T {
let a = a.to_f32();
let b = b.to_f32();

T::from_f32(a + (b - a) * t)
}

/// Quadratic in easing.
pub fn quadratic_in<T: Scalar>(a: T, b: T, t: f32) -> T {
let a = a.to_f32();
let b = b.to_f32();

T::from_f32(a + (b - a) * t * t)
}

/// Quadratic out easing.
pub fn quadratic_out<T: Scalar>(a: T, b: T, t: f32) -> T {
let a = a.to_f32();
let b = b.to_f32();

T::from_f32(a + (b - a) * (1.0 - t) * (1.0 - t))
}

/// Quadratic in-out easing.
pub fn quadratic_in_out<T: Scalar>(a: T, b: T, t: f32) -> T {
if t < 0.5 {
quadratic_in(a, b, t * 2.0)
} else {
quadratic_out(a, b, t * 2.0 - 1.0)
}
}

/// Cubic in easing.
pub fn cubic_in<T: Scalar>(a: T, b: T, t: f32) -> T {
let a = a.to_f32();
let b = b.to_f32();

T::from_f32(a + (b - a) * t * t * t)
}

/// Cubic out easing.
pub fn cubic_out<T: Scalar>(a: T, b: T, t: f32) -> T {
let a = a.to_f32();
let b = b.to_f32();
let t = t - 1.0;

T::from_f32(a + (b - a) * (t * t * t + 1.0))
}

/// Cubic in-out easing.
pub fn cubic_in_out<T: Scalar>(a: T, b: T, t: f32) -> T {
if t < 0.5 {
cubic_in(a, b, t * 2.0)
} else {
cubic_out(a, b, t * 2.0 - 1.0)
}
}

/// Quartic in easing.
pub fn quartic_in<T: Scalar>(a: T, b: T, t: f32) -> T {
let a = a.to_f32();
let b = b.to_f32();

T::from_f32(a + (b - a) * t * t * t * t)
}

/// Quartic out easing.
pub fn quartic_out<T: Scalar>(a: T, b: T, t: f32) -> T {
let a = a.to_f32();
let b = b.to_f32();
let t = t - 1.0;

T::from_f32(a + (b - a) * (1.0 - t * t * t * t))
}

/// Quartic in-out easing.
pub fn quartic_in_out<T: Scalar>(a: T, b: T, t: f32) -> T {
if t < 0.5 {
quartic_in(a, b, t * 2.0)
} else {
quartic_out(a, b, t * 2.0 - 1.0)
}
}

0 comments on commit 5b89574

Please sign in to comment.