3个不稳定版本

0.2.0 2023年6月24日
0.1.1 2023年4月14日
0.1.0 2023年4月14日

#1018 in 游戏开发

26 每月下载量

MIT/Apache

12KB
150

buttery

使用简洁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();

以下示例

此示例使摄像机跟踪目标。

在这种情况下,我们无法完全使用 .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();

依赖项

~3MB
~89K SLoC