#camera #bevy #smoothing #exponential #controller #transform #look-transform

dev smooth-bevy-cameras

具有奶油般、指数平滑的 Bevy 相机控制器

11 个重大版本发布

0.12.0 2024 年 7 月 10 日
0.11.0 2024 年 2 月 19 日
0.10.0 2023 年 11 月 4 日
0.8.0 2023 年 3 月 11 日
0.2.0 2022 年 1 月 10 日

#105游戏开发

Download history 205/week @ 2024-05-04 172/week @ 2024-05-11 242/week @ 2024-05-18 330/week @ 2024-05-25 272/week @ 2024-06-01 161/week @ 2024-06-08 223/week @ 2024-06-15 231/week @ 2024-06-22 106/week @ 2024-06-29 254/week @ 2024-07-06 154/week @ 2024-07-13 87/week @ 2024-07-20 231/week @ 2024-07-27 238/week @ 2024-08-03 332/week @ 2024-08-10 285/week @ 2024-08-17

1,094 每月下载量
用于 13 crates

MIT 许可证

41KB
779

smooth-bevy-cameras

crates.io docs.rs

为 Bevy 引擎收集的一系列指数平滑相机控制器。

外观变换

所有控制器都基于一个 LookTransform 组件,它只是一个指向 target 点的 eye 点。通过修改此组件,场景图 Transform 将自动同步。

具有所有 Transform、外观变换和 Smoother 组件的实体将自动平滑其 Transform。平滑对 LookTransform 无效,仅对场景图中的最终 Transform 有影响。

use bevy::prelude::*;
use smooth_bevy_cameras::{LookTransform, LookTransformBundle, LookTransformPlugin, Smoother};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        // Enables the system that synchronizes your `Transform`s and `LookTransform`s.
        .add_plugins(LookTransformPlugin)
        .add_startup_system(setup)
        .add_system(move_camera_system);
}

fn setup(mut commands: Commands) {
    let eye = Vec3::default();
    let target = Vec3::default();

    commands
        .spawn(LookTransformBundle {
            transform: LookTransform::new(eye, target),
            smoother: Smoother::new(0.9), // Value between 0.0 and 1.0, higher is smoother.
        })
        .insert_bundle(Camera3dBundle::default());

}

fn move_camera_system(mut cameras: Query<&mut LookTransform>) {
    // Later, another system will update the `Transform` and apply smoothing automatically.
    for mut c in cameras.iter_mut() { c.target += Vec3::new(1.0, 1.0, 1.0); }
}

外观角度

实现相机控制器时,通常需要直接处理外观方向的角度(俯仰和偏航)。您可以使用 LookAngles 类型来做到这一点

use bevy::prelude::*;
use smooth_bevy_cameras::{
    LookAngles,
    LookTransform
};

fn look_angles(mut transform: LookTransform, delta: Vec2) {
    let mut angles = LookAngles::from_vector(transform.look_direction().unwrap());
    angles.add_pitch(delta.y);
    angles.add_yaw(delta.x);
    // Third-person.
    transform.eye = transform.target + 1.0 * transform.radius() * angles.unit_vector();
    // First-person.
    // transform.target = transform.eye + 1.0 * transform.radius() * angles.unit_vector();
}

内置控制器就是这样实现旋转控制的。

内置控制器

这些插件依赖于 LookTransformPlugin

  • FpsCameraPlugin + FpsCameraBundle

    • WASD:在 XZ 平面上平移
    • Shift/Space:沿 Y 轴平移
    • 鼠标:旋转相机
  • OrbitCameraPlugin + OrbitCameraBundle

    • CTRL + 鼠标拖动:旋转相机
    • 右键鼠标拖动:平移相机
    • 鼠标滚轮:缩放
  • UnrealCameraPlugin + UnrealCameraBundle

    最佳用法:按住右鼠标按钮在视图中环绕查看,同时使用 WASD 在场景中导航,使用滚轮加速/减速。

    • 左键鼠标拖动:移动
    • 右键鼠标拖动:旋转相机
    • 左右或中键拖动:平移相机
    • 按住任何鼠标键,使用A/D键左右平移,Q/E键上下平移
    • 按住任何鼠标键,使用W/S键前后移动
    • 按住任何鼠标键,使用滚轮增加/减少移动和平移速度
    • 不按任何鼠标键,使用滚轮前后移动

许可证:MIT

依赖项

~23MB
~420K SLoC