Crates.io | buttery |
lib.rs | buttery |
version | 0.3.0 |
source | src |
created_at | 2023-04-14 22:25:14.656138 |
updated_at | 2024-09-03 04:11:10.65644 |
description | Buttery smooth transformations with a nice API |
homepage | |
repository | https://github.com/OptimisticPeach/buttery.git |
max_upload_size | |
id | 839544 |
size | 22,373 |
buttery
Exponentially smoothed transformations with a clean API.
use glam::{Mat4, Quat, Vec3};
use buttery::{Rotate, Scaffold, TransformComponent, Translate};
struct Camera {
position: TransformComponent<Translate<Vec3>>,
looking: TransformComponent<Rotate>,
}
let mut camera = Camera {
position: TransformComponent::new_translate(Vec3::ZERO),
looking: TransformComponent::new_rotate(Quat::IDENTITY),
};
// Simulate user input:
camera.position.target += Vec3::X;
camera.looking.target *= Quat::from_rotation_x(0.3);
// For each frame:
// Time since last frame in seconds.
let delta_time = 0.016;
let transform = camera.position.begin(|translation| Mat4::from_translation(translation))
.and_then(&mut camera.looking, |quat| Mat4::from_quat(quat))
.drive(delta_time);
let view_matrix = transform.inverse();
This example makes the camera follow a target.
In this case, we can't quite use the .and_then
api:
use glam::{Mat4, Quat, Vec3};
use buttery::{Rotate, Scaffold, TransformComponent, Translate};
struct Camera {
position: TransformComponent<Translate<Vec3>>,
target: TransformComponent<Translate<Vec3>>,
}
let mut camera = Camera {
position: TransformComponent::new_translate(Vec3::ZERO),
// We'll want to follow our actual target much more closely.
target: TransformComponent::new(0.001, Vec3::X),
};
// Simulate user input:
camera.position.target += Vec3::X;
camera.target.target += Vec3::NEG_Y;
// For each frame:
// Time since last frame in seconds.
let delta_time = 0.016;
let camera_position = camera.position.drive(delta_time);
let partial = camera.target.begin(|target_pos| Mat4::look_at_rh(camera_position, target_pos, Vec3::Y))
.drive(delta_time);
let transform = partial * Mat4::from_translation(camera_position);
let view_matrix = transform.inverse();