bevy 0.13 (#14)

Update to bevy 0.13
This commit is contained in:
Gord Lea 2024-03-07 00:55:52 -08:00 committed by GitHub
parent 6c3cf18816
commit a2e3f05a70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 39 additions and 58 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "big_space" name = "big_space"
version = "0.4.0" version = "0.5.0"
edition = "2021" edition = "2021"
description = "A floating origin plugin for bevy" description = "A floating origin plugin for bevy"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
@ -11,10 +11,10 @@ documentation = "https://docs.rs/crate/big_space/latest"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bevy = { version = "0.12", default_features = false } bevy = { version = "0.13", default_features = false }
[dev-dependencies] [dev-dependencies]
bevy = { version = "0.12", default-features = false, features = [ bevy = { version = "0.13", default-features = false, features = [
"bevy_winit", "bevy_winit",
"default_font", "default_font",
"bevy_ui", "bevy_ui",
@ -22,7 +22,7 @@ bevy = { version = "0.12", default-features = false, features = [
"x11", "x11",
"tonemapping_luts", "tonemapping_luts",
] } ] }
bevy_framepace = { version = "0.14", default-features = false } bevy_framepace = { version = "0.15", default-features = false }
[features] [features]
default = ["debug", "camera", "bevy_render"] default = ["debug", "camera", "bevy_render"]

View File

@ -36,6 +36,7 @@ I intend to track the `main` branch of Bevy. PRs supporting this are welcome!
| bevy | big_space | | bevy | big_space |
| ---- | --------- | | ---- | --------- |
| 0.13 | 0.5 |
| 0.12 | 0.4 | | 0.12 | 0.4 |
| 0.11 | 0.3 | | 0.11 | 0.3 |
| 0.10 | 0.2 | | 0.10 | 0.2 |

View File

@ -57,14 +57,7 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
let mesh_handle = meshes.add( let mesh_handle = meshes.add(Sphere::new(0.1).mesh().ico(16).unwrap());
shape::Icosphere {
radius: 0.1,
..default()
}
.try_into()
.unwrap(),
);
let matl_handle = materials.add(StandardMaterial { let matl_handle = materials.add(StandardMaterial {
base_color: Color::YELLOW, base_color: Color::YELLOW,
..default() ..default()
@ -115,10 +108,6 @@ fn setup(
commands.spawn(( commands.spawn((
PointLightBundle { PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0), transform: Transform::from_xyz(4.0, 8.0, 4.0),
point_light: PointLight {
intensity: 10_000f32,
..default()
},
..default() ..default()
}, },
GridCell::<i64>::default(), GridCell::<i64>::default(),

View File

@ -51,14 +51,7 @@ fn setup(
.with_speed(1.0), .with_speed(1.0),
)); ));
let mesh_handle = meshes.add( let mesh_handle = meshes.add(Sphere::new(0.5).mesh().ico(32).unwrap());
shape::Icosphere {
radius: 0.5,
subdivisions: 32,
}
.try_into()
.unwrap(),
);
let matl_handle = materials.add(StandardMaterial { let matl_handle = materials.add(StandardMaterial {
base_color: Color::BLUE, base_color: Color::BLUE,
perceptual_roughness: 0.8, perceptual_roughness: 0.8,
@ -107,7 +100,7 @@ fn ui_setup(mut commands: Commands) {
..default() ..default()
}, },
) )
.with_text_alignment(TextAlignment::Left) .with_text_justify(JustifyText::Left)
.with_style(Style { .with_style(Style {
position_type: PositionType::Absolute, position_type: PositionType::Absolute,
top: Val::Px(10.0), top: Val::Px(10.0),
@ -133,7 +126,7 @@ fn ui_setup(mut commands: Commands) {
left: Val::Px(10.0), left: Val::Px(10.0),
..default() ..default()
}) })
.with_text_alignment(TextAlignment::Center), .with_text_justify(JustifyText::Center),
FunFactText, FunFactText,
)); ));
} }
@ -149,9 +142,10 @@ fn highlight_nearest_sphere(
let Ok(transform) = objects.get(entity) else { let Ok(transform) = objects.get(entity) else {
return; return;
}; };
let (scale, rotation, translation) = transform.to_scale_rotation_translation(); // Ignore rotation due to panicking in gizmos, as of bevy 0.13
let (scale, _, translation) = transform.to_scale_rotation_translation();
gizmos gizmos
.sphere(translation, rotation, scale.x * 0.505, Color::RED) .sphere(translation, Quat::IDENTITY, scale.x * 0.505, Color::RED)
.circle_segments(128); .circle_segments(128);
} }
@ -247,8 +241,8 @@ fn closest<'a>(diameter: f32) -> (f32, &'a str) {
fn cursor_grab_system( fn cursor_grab_system(
mut windows: Query<&mut Window, With<PrimaryWindow>>, mut windows: Query<&mut Window, With<PrimaryWindow>>,
mut cam: ResMut<CameraInput>, mut cam: ResMut<CameraInput>,
btn: Res<Input<MouseButton>>, btn: Res<ButtonInput<MouseButton>>,
key: Res<Input<KeyCode>>, key: Res<ButtonInput<KeyCode>>,
) { ) {
let Some(mut window) = windows.get_single_mut().ok() else { let Some(mut window) = windows.get_single_mut().ok() else {
return; return;

View File

@ -5,7 +5,7 @@
//! independently from the camera, which is equivalent to what would happen when moving far from the //! independently from the camera, which is equivalent to what would happen when moving far from the
//! origin when not using this plugin. //! origin when not using this plugin.
use bevy::prelude::{shape::UVSphere, *}; use bevy::prelude::*;
use big_space::{FloatingOrigin, FloatingOriginSettings, GridCell}; use big_space::{FloatingOrigin, FloatingOriginSettings, GridCell};
fn main() { fn main() {
@ -20,19 +20,19 @@ fn main() {
} }
/// You can put things really, really far away from the origin. The distance we use here is actually /// You can put things really, really far away from the origin. The distance we use here is actually
/// quite small, because we want the cubes to still be visible when the floating origin is far from /// quite small, because we want the mesh to still be visible when the floating origin is far from
/// the camera. If you go much further than this, the cubes will simply disappear in a *POOF* of /// the camera. If you go much further than this, the mesh will simply disappear in a *POOF* of
/// floating point error. /// floating point error.
/// ///
/// This plugin can function much further from the origin without any issues. Try setting this to: /// This plugin can function much further from the origin without any issues. Try setting this to:
/// 10_000_000_000_000_000_000_000_000_000_000_000_000 /// 10_000_000_000_000_000_000_000_000_000_000_000_000
const DISTANCE: i128 = 20_000_000; const DISTANCE: i128 = 21_000_000;
/// Move the floating origin back to the "true" origin when the user presses the spacebar to emulate /// Move the floating origin back to the "true" origin when the user presses the spacebar to emulate
/// disabling the plugin. Normally you would make your active camera the floating origin to avoid /// disabling the plugin. Normally you would make your active camera the floating origin to avoid
/// this issue. /// this issue.
fn toggle_plugin( fn toggle_plugin(
input: Res<Input<KeyCode>>, input: Res<ButtonInput<KeyCode>>,
settings: Res<big_space::FloatingOriginSettings>, settings: Res<big_space::FloatingOriginSettings>,
mut text: Query<&mut Text>, mut text: Query<&mut Text>,
mut disabled: Local<bool>, mut disabled: Local<bool>,
@ -74,7 +74,7 @@ fn toggle_plugin(
}; };
text.single_mut().sections[0].value = text.single_mut().sections[0].value =
format!("Press Spacebar to toggle: {msg}\nCamera distance to floating origin: {}\nCubes distance from origin: {}", thousands(dist), thousands(DISTANCE)) format!("Press Spacebar to toggle: {msg}\nCamera distance to floating origin: {}\nMesh distance from origin: {}", thousands(dist), thousands(DISTANCE))
} }
#[derive(Component)] #[derive(Component)]
@ -108,15 +108,14 @@ fn setup_ui(mut commands: Commands) {
}); });
} }
/// set up a simple scene with a "parent" cube and a "child" cube
fn setup_scene( fn setup_scene(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
settings: Res<FloatingOriginSettings>, settings: Res<FloatingOriginSettings>,
) { ) {
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 2.0 })); let mesh_handle = meshes.add(Sphere::new(1.5).mesh());
let cube_material_handle = materials.add(StandardMaterial { let matl_handle = materials.add(StandardMaterial {
base_color: Color::rgb(0.8, 0.7, 0.6), base_color: Color::rgb(0.8, 0.7, 0.6),
..default() ..default()
}); });
@ -129,8 +128,8 @@ fn setup_scene(
// plugin isn't used. // plugin isn't used.
commands.spawn(( commands.spawn((
PbrBundle { PbrBundle {
mesh: meshes.add(UVSphere::default().into()), mesh: meshes.add(Sphere::default().mesh()),
material: materials.add(Color::RED.into()), material: materials.add(StandardMaterial::from(Color::RED)),
transform: Transform::from_scale(Vec3::splat(10000.0)), transform: Transform::from_scale(Vec3::splat(10000.0)),
..default() ..default()
}, },
@ -138,23 +137,21 @@ fn setup_scene(
FloatingOrigin, FloatingOrigin,
)); ));
// parent cube
commands commands
.spawn(( .spawn((
PbrBundle { PbrBundle {
mesh: cube_handle.clone(), mesh: mesh_handle.clone(),
material: cube_material_handle.clone(), material: matl_handle.clone(),
..default() ..default()
}, },
distant_grid_cell, distant_grid_cell,
Rotator, Rotator,
)) ))
.with_children(|parent| { .with_children(|parent| {
// child cube
parent.spawn(PbrBundle { parent.spawn(PbrBundle {
mesh: cube_handle, mesh: mesh_handle,
material: cube_material_handle, material: matl_handle,
transform: Transform::from_xyz(0.0, 0.0, 3.0), transform: Transform::from_xyz(0.0, 0.0, 4.0),
..default() ..default()
}); });
}); });

View File

@ -99,7 +99,7 @@ impl Default for CameraController {
} }
} }
/// Input state used to command camera motion. Reset every time the values are read to update the /// ButtonInput state used to command camera motion. Reset every time the values are read to update the
/// camera. Allows you to map any input to camera motions. Uses aircraft principle axes conventions. /// camera. Allows you to map any input to camera motions. Uses aircraft principle axes conventions.
#[derive(Clone, Debug, Default, Reflect, Resource)] #[derive(Clone, Debug, Default, Reflect, Resource)]
pub struct CameraInput { pub struct CameraInput {
@ -147,20 +147,20 @@ impl CameraInput {
/// Provides sensible keyboard and mouse input defaults /// Provides sensible keyboard and mouse input defaults
pub fn default_camera_inputs( pub fn default_camera_inputs(
keyboard: Res<Input<KeyCode>>, keyboard: Res<ButtonInput<KeyCode>>,
mut mouse_move: EventReader<MouseMotion>, mut mouse_move: EventReader<MouseMotion>,
mut cam: ResMut<CameraInput>, mut cam: ResMut<CameraInput>,
) { ) {
keyboard.pressed(KeyCode::W).then(|| cam.forward -= 1.0); keyboard.pressed(KeyCode::KeyW).then(|| cam.forward -= 1.0);
keyboard.pressed(KeyCode::S).then(|| cam.forward += 1.0); keyboard.pressed(KeyCode::KeyS).then(|| cam.forward += 1.0);
keyboard.pressed(KeyCode::A).then(|| cam.right -= 1.0); keyboard.pressed(KeyCode::KeyA).then(|| cam.right -= 1.0);
keyboard.pressed(KeyCode::D).then(|| cam.right += 1.0); keyboard.pressed(KeyCode::KeyD).then(|| cam.right += 1.0);
keyboard.pressed(KeyCode::Space).then(|| cam.up += 1.0); keyboard.pressed(KeyCode::Space).then(|| cam.up += 1.0);
keyboard keyboard
.pressed(KeyCode::ControlLeft) .pressed(KeyCode::ControlLeft)
.then(|| cam.up -= 1.0); .then(|| cam.up -= 1.0);
keyboard.pressed(KeyCode::Q).then(|| cam.roll += 1.0); keyboard.pressed(KeyCode::KeyQ).then(|| cam.roll += 1.0);
keyboard.pressed(KeyCode::E).then(|| cam.roll -= 1.0); keyboard.pressed(KeyCode::KeyE).then(|| cam.roll -= 1.0);
keyboard keyboard
.pressed(KeyCode::ShiftLeft) .pressed(KeyCode::ShiftLeft)
.then(|| cam.boost = true); .then(|| cam.boost = true);
@ -215,7 +215,7 @@ pub fn camera_controller<P: GridPrecision>(
let (vel_t_current, vel_r_current) = (controller.vel_translation, controller.vel_rotation); let (vel_t_current, vel_r_current) = (controller.vel_translation, controller.vel_rotation);
let (vel_t_target, vel_r_target) = input.target_velocity(speed, time.delta_seconds_f64()); let (vel_t_target, vel_r_target) = input.target_velocity(speed, time.delta_seconds_f64());
let cam_rot = cam_transform.rotation.as_f64(); let cam_rot = cam_transform.rotation.as_dquat();
let vel_t_next = cam_rot * vel_t_target; // Orients the translation to match the camera let vel_t_next = cam_rot * vel_t_target; // Orients the translation to match the camera
let vel_t_next = vel_t_current.lerp(vel_t_next, lerp_translation); let vel_t_next = vel_t_current.lerp(vel_t_next, lerp_translation);
// Convert the high precision translation to a grid cell and low precision translation // Convert the high precision translation to a grid cell and low precision translation
@ -224,7 +224,7 @@ pub fn camera_controller<P: GridPrecision>(
cam_transform.translation += new_translation; cam_transform.translation += new_translation;
let new_rotation = vel_r_current.slerp(vel_r_target, lerp_rotation); let new_rotation = vel_r_current.slerp(vel_r_target, lerp_rotation);
cam_transform.rotation *= new_rotation.as_f32(); cam_transform.rotation *= new_rotation.as_quat();
// Store the new velocity to be used in the next frame // Store the new velocity to be used in the next frame
controller.vel_translation = vel_t_next; controller.vel_translation = vel_t_next;