From be690d90d5e67e18f6ab2f3add7b0969c19a52a3 Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Fri, 5 Jul 2024 00:21:09 -0700 Subject: [PATCH 1/4] Bevy 0.14 (#26) --- Cargo.toml | 35 ++++++++++---------- README.md | 1 + examples/debug.rs | 5 +-- examples/demo.rs | 16 ++++++--- examples/error.rs | 2 +- examples/error_child.rs | 27 ++++++++-------- examples/planets.rs | 15 +++++---- examples/split_screen.rs | 21 ++++++------ src/camera.rs | 50 ++++++++++++++++++++++++----- src/debug.rs | 13 +++++--- src/plugin.rs | 6 ++-- src/reference_frame/local_origin.rs | 46 +++++++++++++------------- src/reference_frame/mod.rs | 2 +- src/tests.rs | 26 +++++++-------- 14 files changed, 158 insertions(+), 107 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dfeafe0..d127a51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "big_space" -version = "0.6.0" +version = "0.7.0-rc.0" edition = "2021" description = "A floating origin plugin for bevy" license = "MIT OR Apache-2.0" @@ -9,22 +9,23 @@ repository = "https://github.com/aevyrie/big_space" documentation = "https://docs.rs/crate/big_space/latest" [dependencies] -bevy_app = { version = "0.13", default_features = false } -bevy_ecs = { version = "0.13", default_features = false } -bevy_hierarchy = { version = "0.13", default_features = false } -bevy_log = { version = "0.13", default_features = false } -bevy_math = { version = "0.13", default_features = false } -bevy_reflect = { version = "0.13", default_features = false } -bevy_transform = { version = "0.13", default_features = false } -bevy_utils = { version = "0.13", default_features = false } +bevy_app = { version = "0.14.0-rc.4", default_features = false } +bevy_ecs = { version = "0.14.0-rc.4", default_features = false } +bevy_hierarchy = { version = "0.14.0-rc.4", default_features = false } +bevy_log = { version = "0.14.0-rc.4", default_features = false } +bevy_math = { version = "0.14.0-rc.4", default_features = false } +bevy_reflect = { version = "0.14.0-rc.4", default_features = false } +bevy_transform = { version = "0.14.0-rc.4", default_features = false } +bevy_utils = { version = "0.14.0-rc.4", default_features = false } # Optional -bevy_gizmos = { version = "0.13", default_features = false, optional = true } -bevy_render = { version = "0.13", default_features = false, optional = true } -bevy_input = { version = "0.13", default_features = false, optional = true } -bevy_time = { version = "0.13", default_features = false, optional = true } +bevy_color = { version = "0.14.0-rc.4", default_features = false, optional = true } +bevy_gizmos = { version = "0.14.0-rc.4", default_features = false, optional = true } +bevy_render = { version = "0.14.0-rc.4", default_features = false, optional = true } +bevy_input = { version = "0.14.0-rc.4", default_features = false, optional = true } +bevy_time = { version = "0.14.0-rc.4", default_features = false, optional = true } [dev-dependencies] -bevy = { version = "0.13", default-features = false, features = [ +bevy = { version = "0.14.0-rc.4", default-features = false, features = [ "bevy_scene", "bevy_gltf", "bevy_winit", @@ -34,14 +35,14 @@ bevy = { version = "0.13", default-features = false, features = [ "bevy_gizmos", "x11", "tonemapping_luts", - "multi-threaded", + "multi_threaded", ] } -bevy-inspector-egui = "0.24" +# bevy-inspector-egui = "0.24" rand = "0.8.5" [features] default = ["debug", "camera", "bevy_render"] -debug = ["bevy_gizmos", "bevy_render"] +debug = ["bevy_gizmos", "bevy_color"] camera = ["bevy_render", "bevy_time", "bevy_input"] [[example]] diff --git a/README.md b/README.md index 1ce0c23..298e307 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ From the docs: https://docs.rs/big_space/latest/big_space/precision/trait.GridPr | bevy | big_space | | ---- | --------- | +| 0.14 | 0.7 | | 0.13 | 0.5, 0.6 | | 0.12 | 0.4 | | 0.11 | 0.3 | diff --git a/examples/debug.rs b/examples/debug.rs index 65a5aa9..58b9263 100644 --- a/examples/debug.rs +++ b/examples/debug.rs @@ -1,6 +1,7 @@ #![allow(clippy::type_complexity)] use bevy::prelude::*; +use bevy_color::palettes; use big_space::{commands::BigSpaceCommands, reference_frame::ReferenceFrame, FloatingOrigin}; fn main() { @@ -13,7 +14,7 @@ fn main() { .insert_resource(ClearColor(Color::BLACK)) .add_systems(Startup, setup) .add_systems(Update, (movement, rotation)) - .run() + .run(); } #[derive(Component)] @@ -61,7 +62,7 @@ fn setup( ) { let mesh_handle = meshes.add(Sphere::new(0.1).mesh().ico(16).unwrap()); let matl_handle = materials.add(StandardMaterial { - base_color: Color::YELLOW, + base_color: Color::Srgba(palettes::basic::YELLOW), ..default() }); diff --git a/examples/demo.rs b/examples/demo.rs index 9c4b620..9c57c44 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -3,6 +3,7 @@ use bevy::{ transform::TransformSystem, window::{CursorGrabMode, PrimaryWindow}, }; +use bevy_color::palettes; use big_space::{ camera::{CameraController, CameraInput}, commands::BigSpaceCommands, @@ -26,7 +27,7 @@ fn main() { PostUpdate, highlight_nearest_sphere.after(TransformSystem::TransformPropagate), ) - .run() + .run(); } fn setup( @@ -54,7 +55,7 @@ fn setup( let mesh_handle = meshes.add(Sphere::new(0.5).mesh().ico(32).unwrap()); let matl_handle = materials.add(StandardMaterial { - base_color: Color::BLUE, + base_color: Color::Srgba(palettes::basic::BLUE), perceptual_roughness: 0.8, reflectance: 1.0, ..default() @@ -78,7 +79,7 @@ fn setup( // light root.spawn_spatial(DirectionalLightBundle { directional_light: DirectionalLight { - illuminance: 100_000.0, + illuminance: 10_000.0, ..default() }, ..default() @@ -147,8 +148,13 @@ fn highlight_nearest_sphere( // Ignore rotation due to panicking in gizmos, as of bevy 0.13 let (scale, rotation, translation) = transform.to_scale_rotation_translation(); gizmos - .sphere(translation, rotation, scale.x * 0.505, Color::RED) - .circle_segments(128); + .sphere( + translation, + rotation, + scale.x * 0.505, + Color::Srgba(palettes::basic::RED), + ) + .resolution(128); } #[allow(clippy::type_complexity)] diff --git a/examples/error.rs b/examples/error.rs index cb5d7e0..4e4e9fb 100644 --- a/examples/error.rs +++ b/examples/error.rs @@ -20,7 +20,7 @@ fn main() { )) .add_systems(Startup, (setup_scene, setup_ui)) .add_systems(Update, (rotator_system, toggle_plugin)) - .run() + .run(); } /// You can put things really, really far away from the origin. The distance we use here is actually diff --git a/examples/error_child.rs b/examples/error_child.rs index 3fd19d7..1e9298b 100644 --- a/examples/error_child.rs +++ b/examples/error_child.rs @@ -1,18 +1,19 @@ //! This example demonstrates error accumulating from parent to children in nested reference frames. use bevy::{math::DVec3, prelude::*}; +use bevy_color::palettes; use big_space::{commands::BigSpaceCommands, reference_frame::ReferenceFrame, FloatingOrigin}; fn main() { App::new() .add_plugins(( DefaultPlugins.build().disable::(), - bevy_inspector_egui::quick::WorldInspectorPlugin::new(), + // bevy_inspector_egui::quick::WorldInspectorPlugin::new(), big_space::BigSpacePlugin::::default(), big_space::camera::CameraControllerPlugin::::default(), big_space::debug::FloatingOriginDebugPlugin::::default(), )) .add_systems(Startup, setup_scene) - .run() + .run(); } // The nearby object is NEARBY meters away from us. The distance object is DISTANT meters away from @@ -28,7 +29,7 @@ fn setup_scene( ) { let mesh_handle = meshes.add(Sphere::new(SPHERE_RADIUS).mesh()); let matl_handle = materials.add(StandardMaterial { - base_color: Color::rgb(0.8, 0.7, 0.6), + base_color: Color::srgb(0.8, 0.7, 0.6), ..default() }); @@ -37,7 +38,7 @@ fn setup_scene( |root_frame| { root_frame.spawn_spatial(PbrBundle { mesh: mesh_handle.clone(), - material: materials.add(Color::BLUE), + material: materials.add(Color::from(palettes::css::BLUE)), transform: Transform::from_translation(NEARBY), ..default() }); @@ -46,8 +47,8 @@ fn setup_scene( root_frame.with_frame( ReferenceFrame::new(SPHERE_RADIUS * 100.0, 0.0), |parent_frame| { - // This function introduces a small amount of error, because it can only work up to - // double precision floats. (f64). + // This function introduces a small amount of error, because it can only work up + // to double precision floats. (f64). let child = parent_frame .frame() .translation_to_grid(-DISTANT + NEARBY.as_dvec3()); @@ -60,16 +61,16 @@ fn setup_scene( parent_frame.insert(parent.0); parent_frame.with_children(|child_builder| { - // A green sphere that is a child of the sphere very far from the origin. This - // child is very far from its parent, and should be located exactly at the - // origin (if there was no floating point error). The distance from the green - // sphere to the red sphere is the error caused by float imprecision. Note that - // the sphere does not have any rendering artifacts, its position just has a - // fixed error. + // A green sphere that is a child of the sphere very far from the origin. + // This child is very far from its parent, and should be located exactly at + // the origin (if there was no floating point error). The distance from the + // green sphere to the red sphere is the error caused by float imprecision. + // Note that the sphere does not have any rendering artifacts, its position + // just has a fixed error. child_builder.spawn(( PbrBundle { mesh: mesh_handle, - material: materials.add(Color::GREEN), + material: materials.add(Color::from(palettes::css::GREEN)), transform: Transform::from_translation(child.1), ..default() }, diff --git a/examples/planets.rs b/examples/planets.rs index d12907a..946e8e4 100644 --- a/examples/planets.rs +++ b/examples/planets.rs @@ -11,6 +11,7 @@ use bevy::{ render::camera::Exposure, transform::TransformSystem, }; +use bevy_color::palettes; use big_space::{ camera::{CameraController, CameraInput}, commands::BigSpaceCommands, @@ -51,7 +52,7 @@ fn main() { ) .register_type::() .register_type::() - .run() + .run(); } const EARTH_ORBIT_RADIUS_M: f64 = 149.60e9; @@ -129,7 +130,7 @@ fn spawn_solar_system( let earth_mesh_handle = meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()); let moon_mesh_handle = meshes.add(Sphere::new(MOON_RADIUS_M as f32).mesh().ico(15).unwrap()); let ball_mesh_handle = meshes.add(Sphere::new(5.0).mesh().ico(5).unwrap()); - let plane_mesh_handle = meshes.add(Plane3d::new(Vec3::X)); + let plane_mesh_handle = meshes.add(Plane3d::new(Vec3::X, Vec2::splat(0.5))); commands.spawn(( PrimaryLight, @@ -160,7 +161,7 @@ fn spawn_solar_system( mesh: sun_mesh_handle, material: materials.add(StandardMaterial { base_color: Color::WHITE, - emissive: Color::rgb_linear(100000000., 100000000., 100000000.), + emissive: LinearRgba::rgb(100000., 100000., 100000.), ..default() }), ..default() @@ -177,7 +178,7 @@ fn spawn_solar_system( PbrBundle { mesh: earth_mesh_handle, material: materials.add(StandardMaterial { - base_color: Color::BLUE, + base_color: Color::Srgba(palettes::css::BLUE), perceptual_roughness: 0.8, reflectance: 1.0, ..default() @@ -197,7 +198,7 @@ fn spawn_solar_system( PbrBundle { mesh: moon_mesh_handle, material: materials.add(StandardMaterial { - base_color: Color::GRAY, + base_color: Color::Srgba(palettes::css::GRAY), perceptual_roughness: 1.0, reflectance: 0.0, ..default() @@ -226,7 +227,7 @@ fn spawn_solar_system( children.spawn((PbrBundle { mesh: plane_mesh_handle, material: materials.add(StandardMaterial { - base_color: Color::DARK_GREEN, + base_color: Color::Srgba(palettes::css::DARK_GREEN), perceptual_roughness: 1.0, reflectance: 0.0, ..default() @@ -282,7 +283,7 @@ fn spawn_solar_system( let star_mat = materials.add(StandardMaterial { base_color: Color::WHITE, - emissive: Color::rgb_linear(100000., 100000., 100000.), + emissive: LinearRgba::rgb(2., 2., 2.), ..default() }); let star_mesh_handle = meshes.add(Sphere::new(1e10).mesh().ico(5).unwrap()); diff --git a/examples/split_screen.rs b/examples/split_screen.rs index fee73df..7090bce 100644 --- a/examples/split_screen.rs +++ b/examples/split_screen.rs @@ -9,6 +9,7 @@ use bevy::{ render::{camera::Viewport, view::RenderLayers}, transform::TransformSystem, }; +use bevy_color::palettes; use big_space::{ camera::{CameraController, CameraControllerPlugin}, commands::BigSpaceCommands, @@ -33,7 +34,7 @@ fn main() { .after(big_space::camera::camera_controller::) .before(TransformSystem::TransformPropagate), ) - .run() + .run(); } #[derive(Component)] @@ -58,7 +59,7 @@ fn setup( transform: Transform::default().looking_to(Vec3::NEG_ONE, Vec3::Y), ..default() }, - RenderLayers::all(), + RenderLayers::from_layers(&[1, 2]), )); // Big Space 1 @@ -80,7 +81,7 @@ fn setup( PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 2.0, 1.0)), material: materials.add(StandardMaterial { - base_color: Color::YELLOW, + base_color: Color::Srgba(palettes::css::YELLOW), ..default() }), ..default() @@ -94,7 +95,7 @@ fn setup( PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 2.0, 1.0)), material: materials.add(StandardMaterial { - base_color: Color::PINK, + base_color: Color::Srgba(palettes::css::FUCHSIA), ..default() }), ..default() @@ -106,7 +107,7 @@ fn setup( PbrBundle { mesh: meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()), material: materials.add(StandardMaterial { - base_color: Color::BLUE, + base_color: Color::Srgba(palettes::css::BLUE), ..default() }), transform: Transform::from_xyz(1_000_000.0, 0.0, 0.0) @@ -120,7 +121,7 @@ fn setup( PbrBundle { mesh: meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()), material: materials.add(StandardMaterial { - base_color: Color::GREEN, + base_color: Color::Srgba(palettes::css::GREEN), ..default() }), transform: Transform::from_xyz(-1_000_000.0, 0.0, 0.0) @@ -154,7 +155,7 @@ fn setup( PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 2.0, 1.0)), material: materials.add(StandardMaterial { - base_color: Color::PINK, + base_color: Color::Srgba(palettes::css::PINK), ..default() }), ..default() @@ -168,7 +169,7 @@ fn setup( PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 2.0, 1.0)), material: materials.add(StandardMaterial { - base_color: Color::YELLOW, + base_color: Color::Srgba(palettes::css::YELLOW), ..default() }), ..default() @@ -180,7 +181,7 @@ fn setup( PbrBundle { mesh: meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()), material: materials.add(StandardMaterial { - base_color: Color::BLUE, + base_color: Color::Srgba(palettes::css::BLUE), ..default() }), transform: Transform::from_xyz(1_000_000.0, 0.0, 0.0) @@ -194,7 +195,7 @@ fn setup( PbrBundle { mesh: meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()), material: materials.add(StandardMaterial { - base_color: Color::GREEN, + base_color: Color::Srgba(palettes::css::GREEN), ..default() }), transform: Transform::from_xyz(-1_000_000.0, 0.0, 0.0) diff --git a/src/camera.rs b/src/camera.rs index 78051e3..56aeafa 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -45,6 +45,12 @@ pub struct CameraController { pub rotational_smoothness: f64, /// Base speed. pub speed: f64, + /// Rotational yaw speed multiplier. + pub speed_yaw: f64, + /// Rotational pitch speed multiplier. + pub speed_pitch: f64, + /// Rotational roll speed multiplier. + pub speed_roll: f64, /// Minimum and maximum speed. pub speed_bounds: [f64; 2], /// Whether the camera should slow down when approaching an entity's [`Aabb`]. @@ -73,6 +79,25 @@ impl CameraController { self.speed = speed; self } + + /// Sets the yaw angular velocity of the controller, and returns the modified result. + pub fn with_speed_yaw(mut self, speed: f64) -> Self { + self.speed_yaw = speed; + self + } + + /// Sets the pitch angular velocity of the controller, and returns the modified result. + pub fn with_speed_pitch(mut self, speed: f64) -> Self { + self.speed_pitch = speed; + self + } + + /// Sets the pitch angular velocity of the controller, and returns the modified result. + pub fn with_speed_roll(mut self, speed: f64) -> Self { + self.speed_roll = speed; + self + } + /// Sets the speed of the controller, and returns the modified result. pub fn with_speed_bounds(mut self, speed_limits: [f64; 2]) -> Self { self.speed_bounds = speed_limits; @@ -96,6 +121,9 @@ impl Default for CameraController { smoothness: 0.8, rotational_smoothness: 0.5, speed: 1.0, + speed_pitch: 1.0, + speed_yaw: 1.0, + speed_roll: 1.0, speed_bounds: [1e-17, 1e30], slow_near_objects: true, nearest_object: None, @@ -137,12 +165,17 @@ impl CameraInput { } /// Returns the desired velocity transform. - pub fn target_velocity(&self, speed: f64, dt: f64) -> (DVec3, DQuat) { + pub fn target_velocity( + &self, + controller: &CameraController, + speed: f64, + dt: f64, + ) -> (DVec3, DQuat) { let rotation = DQuat::from_euler( EulerRot::XYZ, - self.pitch * dt, - self.yaw * dt, - self.roll * dt, + self.pitch * dt * controller.speed_pitch, + self.yaw * dt * controller.speed_yaw, + self.roll * dt * controller.speed_roll, ); let translation = DVec3::new(self.right, self.up, self.forward) * speed * dt; @@ -196,15 +229,15 @@ pub fn nearest_objects_in_frame( let Ok((cam_entity, mut camera, cam_pos, cam_layer)) = camera.get_single_mut() else { return; }; - let cam_layer = cam_layer.copied().unwrap_or(RenderLayers::all()); + let cam_layer = cam_layer.to_owned().unwrap_or_default(); let cam_children: HashSet = children.iter_descendants(cam_entity).collect(); let nearest_object = objects .iter() .filter(|(entity, ..)| !cam_children.contains(entity)) .filter(|(.., obj_layer)| { - let obj_layer = obj_layer.copied().unwrap_or(RenderLayers::layer(0)); - cam_layer.intersects(&obj_layer) + let obj_layer = obj_layer.unwrap_or_default(); + cam_layer.intersects(obj_layer) }) .map(|(entity, object_local, obj_pos, aabb, _)| { let center_distance = @@ -243,7 +276,8 @@ pub fn camera_controller( let lerp_rotation = 1.0 - controller.rotational_smoothness.clamp(0.0, 0.999); 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(&controller, speed, time.delta_seconds_f64()); let cam_rot = position.transform.rotation.as_dquat(); let vel_t_next = cam_rot * vel_t_target; // Orients the translation to match the camera diff --git a/src/debug.rs b/src/debug.rs index 852c8d2..907fdd7 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -3,10 +3,13 @@ use std::marker::PhantomData; use bevy_app::prelude::*; +use bevy_color::{ + palettes::css::{BLUE, GREEN, RED}, + prelude::*, +}; use bevy_ecs::prelude::*; use bevy_gizmos::prelude::*; use bevy_math::prelude::*; -use bevy_render::prelude::*; use bevy_transform::prelude::*; use crate::{ @@ -44,7 +47,7 @@ pub fn update_debug_bounds( &Transform::from_scale(Vec3::splat(frame.cell_edge_length() * 0.999)), ); if origin.is_none() { - gizmos.cuboid(transform, Color::GREEN) + gizmos.cuboid(transform, Color::Srgba(GREEN)) } else { // gizmos.cuboid(transform, Color::rgba(0.0, 0.0, 1.0, 0.5)) } @@ -59,8 +62,8 @@ pub fn update_reference_frame_axes( for (transform, frame) in frames.iter() { let start = transform.translation(); let len = frame.cell_edge_length() * 2.0; - gizmos.ray(start, transform.right() * len, Color::RED); - gizmos.ray(start, transform.up() * len, Color::GREEN); - gizmos.ray(start, transform.back() * len, Color::BLUE); + gizmos.ray(start, transform.right() * len, Color::Srgba(RED)); + gizmos.ray(start, transform.up() * len, Color::Srgba(GREEN)); + gizmos.ray(start, transform.back() * len, Color::Srgba(BLUE)); } } diff --git a/src/plugin.rs b/src/plugin.rs index 832e7fe..1eaf5b1 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -2,7 +2,7 @@ use bevy_app::prelude::*; use bevy_ecs::prelude::*; -use bevy_reflect::prelude::*; +use bevy_reflect::{prelude::*, GetTypeRegistration}; use bevy_transform::{prelude::*, TransformSystem}; use std::marker::PhantomData; @@ -52,7 +52,9 @@ pub enum FloatingOriginSet { PropagateLowPrecision, } -impl Plugin for BigSpacePlugin

{ +impl Plugin + for BigSpacePlugin

+{ fn build(&self, app: &mut App) { let system_set_config = || { ( diff --git a/src/reference_frame/local_origin.rs b/src/reference_frame/local_origin.rs index 01f38aa..0decd54 100644 --- a/src/reference_frame/local_origin.rs +++ b/src/reference_frame/local_origin.rs @@ -506,18 +506,18 @@ mod tests { ReferenceFrame::::default(), ); - let child_1 = app.world.spawn(frame_bundle.clone()).id(); - let child_2 = app.world.spawn(frame_bundle.clone()).id(); - let parent = app.world.spawn(frame_bundle.clone()).id(); - let root = app.world.spawn(frame_bundle.clone()).id(); + let child_1 = app.world_mut().spawn(frame_bundle.clone()).id(); + let child_2 = app.world_mut().spawn(frame_bundle.clone()).id(); + let parent = app.world_mut().spawn(frame_bundle.clone()).id(); + let root = app.world_mut().spawn(frame_bundle.clone()).id(); - app.world.entity_mut(root).push_children(&[parent]); - app.world + app.world_mut().entity_mut(root).push_children(&[parent]); + app.world_mut() .entity_mut(parent) .push_children(&[child_1, child_2]); - let mut state = SystemState::>::new(&mut app.world); - let mut ref_frames = state.get_mut(&mut app.world); + let mut state = SystemState::>::new(app.world_mut()); + let mut ref_frames = state.get_mut(app.world_mut()); // Children let result = ref_frames.child_frames(root); @@ -559,12 +559,12 @@ mod tests { ..default() }; let root = app - .world + .world_mut() .spawn((Transform::default(), GridCell::::default(), root_frame)) .id(); let child = app - .world + .world_mut() .spawn(( Transform::from_rotation(Quat::from_rotation_z(std::f32::consts::FRAC_PI_2)) .with_translation(Vec3::new(1.0, 1.0, 0.0)), @@ -573,10 +573,10 @@ mod tests { )) .id(); - app.world.entity_mut(root).push_children(&[child]); + app.world_mut().entity_mut(root).push_children(&[child]); - let mut state = SystemState::>::new(&mut app.world); - let mut reference_frames = state.get_mut(&mut app.world); + let mut state = SystemState::>::new(app.world_mut()); + let mut reference_frames = state.get_mut(app.world_mut()); // The function we are testing propagate_origin_to_child(root, &mut reference_frames, child); @@ -615,10 +615,10 @@ mod tests { GridCell::::default(), ReferenceFrame::::default(), ); - let root = app.world.spawn(frame_bundle.clone()).id(); + let root = app.world_mut().spawn(frame_bundle.clone()).id(); let child = app - .world + .world_mut() .spawn(( Transform::from_rotation(Quat::from_rotation_z(std::f32::consts::FRAC_PI_2)) .with_translation(Vec3::new(1.0, 1.0, 0.0)), @@ -634,10 +634,10 @@ mod tests { )) .id(); - app.world.entity_mut(root).push_children(&[child]); + app.world_mut().entity_mut(root).push_children(&[child]); - let mut state = SystemState::>::new(&mut app.world); - let mut reference_frames = state.get_mut(&mut app.world); + let mut state = SystemState::>::new(app.world_mut()); + let mut reference_frames = state.get_mut(app.world_mut()); // The function we are testing propagate_origin_to_parent(child, &mut reference_frames, root); @@ -672,7 +672,7 @@ mod tests { app.add_plugins(BigSpacePlugin::::default()); let root = app - .world + .world_mut() .spawn(( Transform::default(), GridCell::::default(), @@ -688,7 +688,7 @@ mod tests { .id(); let child = app - .world + .world_mut() .spawn(( Transform::default() .with_rotation(Quat::from_rotation_z(-std::f32::consts::FRAC_PI_2)) @@ -698,10 +698,10 @@ mod tests { )) .id(); - app.world.entity_mut(root).push_children(&[child]); + app.world_mut().entity_mut(root).push_children(&[child]); - let mut state = SystemState::>::new(&mut app.world); - let mut reference_frames = state.get_mut(&mut app.world); + let mut state = SystemState::>::new(app.world_mut()); + let mut reference_frames = state.get_mut(app.world_mut()); propagate_origin_to_child(root, &mut reference_frames, child); diff --git a/src/reference_frame/mod.rs b/src/reference_frame/mod.rs index 16b0d72..3d90b42 100644 --- a/src/reference_frame/mod.rs +++ b/src/reference_frame/mod.rs @@ -34,7 +34,7 @@ pub mod propagation; /// the same rotating reference frame, instead of moving rapidly through space around a star, or /// worse, around the center of the galaxy. #[derive(Debug, Clone, Reflect, Component)] -pub struct ReferenceFrame { +pub struct ReferenceFrame { /// The high-precision position of the floating origin's current grid cell local to this /// reference frame. local_floating_origin: LocalFloatingOrigin

, diff --git a/src/tests.rs b/src/tests.rs index b1c9a04..509ea3f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -8,7 +8,7 @@ fn changing_floating_origin_updates_global_transform() { app.add_plugins(BigSpacePlugin::::default()); let first = app - .world + .world_mut() .spawn(( TransformBundle::from_transform(Transform::from_translation(Vec3::new( 150.0, 0.0, 0.0, @@ -19,7 +19,7 @@ fn changing_floating_origin_updates_global_transform() { .id(); let second = app - .world + .world_mut() .spawn(( TransformBundle::from_transform(Transform::from_translation(Vec3::new( 0.0, 0.0, 300.0, @@ -28,18 +28,18 @@ fn changing_floating_origin_updates_global_transform() { )) .id(); - app.world + app.world_mut() .spawn(BigSpaceRootBundle::::default()) .push_children(&[first, second]); app.update(); - app.world.entity_mut(first).remove::(); - app.world.entity_mut(second).insert(FloatingOrigin); + app.world_mut().entity_mut(first).remove::(); + app.world_mut().entity_mut(second).insert(FloatingOrigin); app.update(); - let second_global_transform = app.world.get::(second).unwrap(); + let second_global_transform = app.world_mut().get::(second).unwrap(); assert_eq!( second_global_transform.translation(), @@ -53,7 +53,7 @@ fn child_global_transforms_are_updated_when_floating_origin_changes() { app.add_plugins(BigSpacePlugin::::default()); let first = app - .world + .world_mut() .spawn(( TransformBundle::from_transform(Transform::from_translation(Vec3::new( 150.0, 0.0, 0.0, @@ -64,7 +64,7 @@ fn child_global_transforms_are_updated_when_floating_origin_changes() { .id(); let second = app - .world + .world_mut() .spawn(( TransformBundle::from_transform(Transform::from_translation(Vec3::new( 0.0, 0.0, 300.0, @@ -78,19 +78,19 @@ fn child_global_transforms_are_updated_when_floating_origin_changes() { }) .id(); - app.world + app.world_mut() .spawn(BigSpaceRootBundle::::default()) .push_children(&[first, second]); app.update(); - app.world.entity_mut(first).remove::(); - app.world.entity_mut(second).insert(FloatingOrigin); + app.world_mut().entity_mut(first).remove::(); + app.world_mut().entity_mut(second).insert(FloatingOrigin); app.update(); - let child = app.world.get::(second).unwrap()[0]; - let child_transform = app.world.get::(child).unwrap(); + let child = app.world_mut().get::(second).unwrap()[0]; + let child_transform = app.world_mut().get::(child).unwrap(); assert_eq!(child_transform.translation(), Vec3::new(0.0, 0.0, 600.0)); } From 6fa7d31a0e0ab5d188ce5517a590782e752b1ca0 Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Fri, 5 Jul 2024 00:21:41 -0700 Subject: [PATCH 2/4] Revert "Bevy 0.14" (#30) Reverts aevyrie/big_space#26 --- Cargo.toml | 35 ++++++++++---------- README.md | 1 - examples/debug.rs | 5 ++- examples/demo.rs | 16 +++------ examples/error.rs | 2 +- examples/error_child.rs | 27 ++++++++-------- examples/planets.rs | 15 ++++----- examples/split_screen.rs | 21 ++++++------ src/camera.rs | 50 +++++------------------------ src/debug.rs | 13 +++----- src/plugin.rs | 6 ++-- src/reference_frame/local_origin.rs | 46 +++++++++++++------------- src/reference_frame/mod.rs | 2 +- src/tests.rs | 26 +++++++-------- 14 files changed, 107 insertions(+), 158 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d127a51..dfeafe0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "big_space" -version = "0.7.0-rc.0" +version = "0.6.0" edition = "2021" description = "A floating origin plugin for bevy" license = "MIT OR Apache-2.0" @@ -9,23 +9,22 @@ repository = "https://github.com/aevyrie/big_space" documentation = "https://docs.rs/crate/big_space/latest" [dependencies] -bevy_app = { version = "0.14.0-rc.4", default_features = false } -bevy_ecs = { version = "0.14.0-rc.4", default_features = false } -bevy_hierarchy = { version = "0.14.0-rc.4", default_features = false } -bevy_log = { version = "0.14.0-rc.4", default_features = false } -bevy_math = { version = "0.14.0-rc.4", default_features = false } -bevy_reflect = { version = "0.14.0-rc.4", default_features = false } -bevy_transform = { version = "0.14.0-rc.4", default_features = false } -bevy_utils = { version = "0.14.0-rc.4", default_features = false } +bevy_app = { version = "0.13", default_features = false } +bevy_ecs = { version = "0.13", default_features = false } +bevy_hierarchy = { version = "0.13", default_features = false } +bevy_log = { version = "0.13", default_features = false } +bevy_math = { version = "0.13", default_features = false } +bevy_reflect = { version = "0.13", default_features = false } +bevy_transform = { version = "0.13", default_features = false } +bevy_utils = { version = "0.13", default_features = false } # Optional -bevy_color = { version = "0.14.0-rc.4", default_features = false, optional = true } -bevy_gizmos = { version = "0.14.0-rc.4", default_features = false, optional = true } -bevy_render = { version = "0.14.0-rc.4", default_features = false, optional = true } -bevy_input = { version = "0.14.0-rc.4", default_features = false, optional = true } -bevy_time = { version = "0.14.0-rc.4", default_features = false, optional = true } +bevy_gizmos = { version = "0.13", default_features = false, optional = true } +bevy_render = { version = "0.13", default_features = false, optional = true } +bevy_input = { version = "0.13", default_features = false, optional = true } +bevy_time = { version = "0.13", default_features = false, optional = true } [dev-dependencies] -bevy = { version = "0.14.0-rc.4", default-features = false, features = [ +bevy = { version = "0.13", default-features = false, features = [ "bevy_scene", "bevy_gltf", "bevy_winit", @@ -35,14 +34,14 @@ bevy = { version = "0.14.0-rc.4", default-features = false, features = [ "bevy_gizmos", "x11", "tonemapping_luts", - "multi_threaded", + "multi-threaded", ] } -# bevy-inspector-egui = "0.24" +bevy-inspector-egui = "0.24" rand = "0.8.5" [features] default = ["debug", "camera", "bevy_render"] -debug = ["bevy_gizmos", "bevy_color"] +debug = ["bevy_gizmos", "bevy_render"] camera = ["bevy_render", "bevy_time", "bevy_input"] [[example]] diff --git a/README.md b/README.md index 298e307..1ce0c23 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,6 @@ From the docs: https://docs.rs/big_space/latest/big_space/precision/trait.GridPr | bevy | big_space | | ---- | --------- | -| 0.14 | 0.7 | | 0.13 | 0.5, 0.6 | | 0.12 | 0.4 | | 0.11 | 0.3 | diff --git a/examples/debug.rs b/examples/debug.rs index 58b9263..65a5aa9 100644 --- a/examples/debug.rs +++ b/examples/debug.rs @@ -1,7 +1,6 @@ #![allow(clippy::type_complexity)] use bevy::prelude::*; -use bevy_color::palettes; use big_space::{commands::BigSpaceCommands, reference_frame::ReferenceFrame, FloatingOrigin}; fn main() { @@ -14,7 +13,7 @@ fn main() { .insert_resource(ClearColor(Color::BLACK)) .add_systems(Startup, setup) .add_systems(Update, (movement, rotation)) - .run(); + .run() } #[derive(Component)] @@ -62,7 +61,7 @@ fn setup( ) { let mesh_handle = meshes.add(Sphere::new(0.1).mesh().ico(16).unwrap()); let matl_handle = materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::basic::YELLOW), + base_color: Color::YELLOW, ..default() }); diff --git a/examples/demo.rs b/examples/demo.rs index 9c57c44..9c4b620 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -3,7 +3,6 @@ use bevy::{ transform::TransformSystem, window::{CursorGrabMode, PrimaryWindow}, }; -use bevy_color::palettes; use big_space::{ camera::{CameraController, CameraInput}, commands::BigSpaceCommands, @@ -27,7 +26,7 @@ fn main() { PostUpdate, highlight_nearest_sphere.after(TransformSystem::TransformPropagate), ) - .run(); + .run() } fn setup( @@ -55,7 +54,7 @@ fn setup( let mesh_handle = meshes.add(Sphere::new(0.5).mesh().ico(32).unwrap()); let matl_handle = materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::basic::BLUE), + base_color: Color::BLUE, perceptual_roughness: 0.8, reflectance: 1.0, ..default() @@ -79,7 +78,7 @@ fn setup( // light root.spawn_spatial(DirectionalLightBundle { directional_light: DirectionalLight { - illuminance: 10_000.0, + illuminance: 100_000.0, ..default() }, ..default() @@ -148,13 +147,8 @@ fn highlight_nearest_sphere( // Ignore rotation due to panicking in gizmos, as of bevy 0.13 let (scale, rotation, translation) = transform.to_scale_rotation_translation(); gizmos - .sphere( - translation, - rotation, - scale.x * 0.505, - Color::Srgba(palettes::basic::RED), - ) - .resolution(128); + .sphere(translation, rotation, scale.x * 0.505, Color::RED) + .circle_segments(128); } #[allow(clippy::type_complexity)] diff --git a/examples/error.rs b/examples/error.rs index 4e4e9fb..cb5d7e0 100644 --- a/examples/error.rs +++ b/examples/error.rs @@ -20,7 +20,7 @@ fn main() { )) .add_systems(Startup, (setup_scene, setup_ui)) .add_systems(Update, (rotator_system, toggle_plugin)) - .run(); + .run() } /// You can put things really, really far away from the origin. The distance we use here is actually diff --git a/examples/error_child.rs b/examples/error_child.rs index 1e9298b..3fd19d7 100644 --- a/examples/error_child.rs +++ b/examples/error_child.rs @@ -1,19 +1,18 @@ //! This example demonstrates error accumulating from parent to children in nested reference frames. use bevy::{math::DVec3, prelude::*}; -use bevy_color::palettes; use big_space::{commands::BigSpaceCommands, reference_frame::ReferenceFrame, FloatingOrigin}; fn main() { App::new() .add_plugins(( DefaultPlugins.build().disable::(), - // bevy_inspector_egui::quick::WorldInspectorPlugin::new(), + bevy_inspector_egui::quick::WorldInspectorPlugin::new(), big_space::BigSpacePlugin::::default(), big_space::camera::CameraControllerPlugin::::default(), big_space::debug::FloatingOriginDebugPlugin::::default(), )) .add_systems(Startup, setup_scene) - .run(); + .run() } // The nearby object is NEARBY meters away from us. The distance object is DISTANT meters away from @@ -29,7 +28,7 @@ fn setup_scene( ) { let mesh_handle = meshes.add(Sphere::new(SPHERE_RADIUS).mesh()); let matl_handle = materials.add(StandardMaterial { - base_color: Color::srgb(0.8, 0.7, 0.6), + base_color: Color::rgb(0.8, 0.7, 0.6), ..default() }); @@ -38,7 +37,7 @@ fn setup_scene( |root_frame| { root_frame.spawn_spatial(PbrBundle { mesh: mesh_handle.clone(), - material: materials.add(Color::from(palettes::css::BLUE)), + material: materials.add(Color::BLUE), transform: Transform::from_translation(NEARBY), ..default() }); @@ -47,8 +46,8 @@ fn setup_scene( root_frame.with_frame( ReferenceFrame::new(SPHERE_RADIUS * 100.0, 0.0), |parent_frame| { - // This function introduces a small amount of error, because it can only work up - // to double precision floats. (f64). + // This function introduces a small amount of error, because it can only work up to + // double precision floats. (f64). let child = parent_frame .frame() .translation_to_grid(-DISTANT + NEARBY.as_dvec3()); @@ -61,16 +60,16 @@ fn setup_scene( parent_frame.insert(parent.0); parent_frame.with_children(|child_builder| { - // A green sphere that is a child of the sphere very far from the origin. - // This child is very far from its parent, and should be located exactly at - // the origin (if there was no floating point error). The distance from the - // green sphere to the red sphere is the error caused by float imprecision. - // Note that the sphere does not have any rendering artifacts, its position - // just has a fixed error. + // A green sphere that is a child of the sphere very far from the origin. This + // child is very far from its parent, and should be located exactly at the + // origin (if there was no floating point error). The distance from the green + // sphere to the red sphere is the error caused by float imprecision. Note that + // the sphere does not have any rendering artifacts, its position just has a + // fixed error. child_builder.spawn(( PbrBundle { mesh: mesh_handle, - material: materials.add(Color::from(palettes::css::GREEN)), + material: materials.add(Color::GREEN), transform: Transform::from_translation(child.1), ..default() }, diff --git a/examples/planets.rs b/examples/planets.rs index 946e8e4..d12907a 100644 --- a/examples/planets.rs +++ b/examples/planets.rs @@ -11,7 +11,6 @@ use bevy::{ render::camera::Exposure, transform::TransformSystem, }; -use bevy_color::palettes; use big_space::{ camera::{CameraController, CameraInput}, commands::BigSpaceCommands, @@ -52,7 +51,7 @@ fn main() { ) .register_type::() .register_type::() - .run(); + .run() } const EARTH_ORBIT_RADIUS_M: f64 = 149.60e9; @@ -130,7 +129,7 @@ fn spawn_solar_system( let earth_mesh_handle = meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()); let moon_mesh_handle = meshes.add(Sphere::new(MOON_RADIUS_M as f32).mesh().ico(15).unwrap()); let ball_mesh_handle = meshes.add(Sphere::new(5.0).mesh().ico(5).unwrap()); - let plane_mesh_handle = meshes.add(Plane3d::new(Vec3::X, Vec2::splat(0.5))); + let plane_mesh_handle = meshes.add(Plane3d::new(Vec3::X)); commands.spawn(( PrimaryLight, @@ -161,7 +160,7 @@ fn spawn_solar_system( mesh: sun_mesh_handle, material: materials.add(StandardMaterial { base_color: Color::WHITE, - emissive: LinearRgba::rgb(100000., 100000., 100000.), + emissive: Color::rgb_linear(100000000., 100000000., 100000000.), ..default() }), ..default() @@ -178,7 +177,7 @@ fn spawn_solar_system( PbrBundle { mesh: earth_mesh_handle, material: materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::css::BLUE), + base_color: Color::BLUE, perceptual_roughness: 0.8, reflectance: 1.0, ..default() @@ -198,7 +197,7 @@ fn spawn_solar_system( PbrBundle { mesh: moon_mesh_handle, material: materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::css::GRAY), + base_color: Color::GRAY, perceptual_roughness: 1.0, reflectance: 0.0, ..default() @@ -227,7 +226,7 @@ fn spawn_solar_system( children.spawn((PbrBundle { mesh: plane_mesh_handle, material: materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::css::DARK_GREEN), + base_color: Color::DARK_GREEN, perceptual_roughness: 1.0, reflectance: 0.0, ..default() @@ -283,7 +282,7 @@ fn spawn_solar_system( let star_mat = materials.add(StandardMaterial { base_color: Color::WHITE, - emissive: LinearRgba::rgb(2., 2., 2.), + emissive: Color::rgb_linear(100000., 100000., 100000.), ..default() }); let star_mesh_handle = meshes.add(Sphere::new(1e10).mesh().ico(5).unwrap()); diff --git a/examples/split_screen.rs b/examples/split_screen.rs index 7090bce..fee73df 100644 --- a/examples/split_screen.rs +++ b/examples/split_screen.rs @@ -9,7 +9,6 @@ use bevy::{ render::{camera::Viewport, view::RenderLayers}, transform::TransformSystem, }; -use bevy_color::palettes; use big_space::{ camera::{CameraController, CameraControllerPlugin}, commands::BigSpaceCommands, @@ -34,7 +33,7 @@ fn main() { .after(big_space::camera::camera_controller::) .before(TransformSystem::TransformPropagate), ) - .run(); + .run() } #[derive(Component)] @@ -59,7 +58,7 @@ fn setup( transform: Transform::default().looking_to(Vec3::NEG_ONE, Vec3::Y), ..default() }, - RenderLayers::from_layers(&[1, 2]), + RenderLayers::all(), )); // Big Space 1 @@ -81,7 +80,7 @@ fn setup( PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 2.0, 1.0)), material: materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::css::YELLOW), + base_color: Color::YELLOW, ..default() }), ..default() @@ -95,7 +94,7 @@ fn setup( PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 2.0, 1.0)), material: materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::css::FUCHSIA), + base_color: Color::PINK, ..default() }), ..default() @@ -107,7 +106,7 @@ fn setup( PbrBundle { mesh: meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()), material: materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::css::BLUE), + base_color: Color::BLUE, ..default() }), transform: Transform::from_xyz(1_000_000.0, 0.0, 0.0) @@ -121,7 +120,7 @@ fn setup( PbrBundle { mesh: meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()), material: materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::css::GREEN), + base_color: Color::GREEN, ..default() }), transform: Transform::from_xyz(-1_000_000.0, 0.0, 0.0) @@ -155,7 +154,7 @@ fn setup( PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 2.0, 1.0)), material: materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::css::PINK), + base_color: Color::PINK, ..default() }), ..default() @@ -169,7 +168,7 @@ fn setup( PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 2.0, 1.0)), material: materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::css::YELLOW), + base_color: Color::YELLOW, ..default() }), ..default() @@ -181,7 +180,7 @@ fn setup( PbrBundle { mesh: meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()), material: materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::css::BLUE), + base_color: Color::BLUE, ..default() }), transform: Transform::from_xyz(1_000_000.0, 0.0, 0.0) @@ -195,7 +194,7 @@ fn setup( PbrBundle { mesh: meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()), material: materials.add(StandardMaterial { - base_color: Color::Srgba(palettes::css::GREEN), + base_color: Color::GREEN, ..default() }), transform: Transform::from_xyz(-1_000_000.0, 0.0, 0.0) diff --git a/src/camera.rs b/src/camera.rs index 56aeafa..78051e3 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -45,12 +45,6 @@ pub struct CameraController { pub rotational_smoothness: f64, /// Base speed. pub speed: f64, - /// Rotational yaw speed multiplier. - pub speed_yaw: f64, - /// Rotational pitch speed multiplier. - pub speed_pitch: f64, - /// Rotational roll speed multiplier. - pub speed_roll: f64, /// Minimum and maximum speed. pub speed_bounds: [f64; 2], /// Whether the camera should slow down when approaching an entity's [`Aabb`]. @@ -79,25 +73,6 @@ impl CameraController { self.speed = speed; self } - - /// Sets the yaw angular velocity of the controller, and returns the modified result. - pub fn with_speed_yaw(mut self, speed: f64) -> Self { - self.speed_yaw = speed; - self - } - - /// Sets the pitch angular velocity of the controller, and returns the modified result. - pub fn with_speed_pitch(mut self, speed: f64) -> Self { - self.speed_pitch = speed; - self - } - - /// Sets the pitch angular velocity of the controller, and returns the modified result. - pub fn with_speed_roll(mut self, speed: f64) -> Self { - self.speed_roll = speed; - self - } - /// Sets the speed of the controller, and returns the modified result. pub fn with_speed_bounds(mut self, speed_limits: [f64; 2]) -> Self { self.speed_bounds = speed_limits; @@ -121,9 +96,6 @@ impl Default for CameraController { smoothness: 0.8, rotational_smoothness: 0.5, speed: 1.0, - speed_pitch: 1.0, - speed_yaw: 1.0, - speed_roll: 1.0, speed_bounds: [1e-17, 1e30], slow_near_objects: true, nearest_object: None, @@ -165,17 +137,12 @@ impl CameraInput { } /// Returns the desired velocity transform. - pub fn target_velocity( - &self, - controller: &CameraController, - speed: f64, - dt: f64, - ) -> (DVec3, DQuat) { + pub fn target_velocity(&self, speed: f64, dt: f64) -> (DVec3, DQuat) { let rotation = DQuat::from_euler( EulerRot::XYZ, - self.pitch * dt * controller.speed_pitch, - self.yaw * dt * controller.speed_yaw, - self.roll * dt * controller.speed_roll, + self.pitch * dt, + self.yaw * dt, + self.roll * dt, ); let translation = DVec3::new(self.right, self.up, self.forward) * speed * dt; @@ -229,15 +196,15 @@ pub fn nearest_objects_in_frame( let Ok((cam_entity, mut camera, cam_pos, cam_layer)) = camera.get_single_mut() else { return; }; - let cam_layer = cam_layer.to_owned().unwrap_or_default(); + let cam_layer = cam_layer.copied().unwrap_or(RenderLayers::all()); let cam_children: HashSet = children.iter_descendants(cam_entity).collect(); let nearest_object = objects .iter() .filter(|(entity, ..)| !cam_children.contains(entity)) .filter(|(.., obj_layer)| { - let obj_layer = obj_layer.unwrap_or_default(); - cam_layer.intersects(obj_layer) + let obj_layer = obj_layer.copied().unwrap_or(RenderLayers::layer(0)); + cam_layer.intersects(&obj_layer) }) .map(|(entity, object_local, obj_pos, aabb, _)| { let center_distance = @@ -276,8 +243,7 @@ pub fn camera_controller( let lerp_rotation = 1.0 - controller.rotational_smoothness.clamp(0.0, 0.999); let (vel_t_current, vel_r_current) = (controller.vel_translation, controller.vel_rotation); - let (vel_t_target, vel_r_target) = - input.target_velocity(&controller, speed, time.delta_seconds_f64()); + let (vel_t_target, vel_r_target) = input.target_velocity(speed, time.delta_seconds_f64()); let cam_rot = position.transform.rotation.as_dquat(); let vel_t_next = cam_rot * vel_t_target; // Orients the translation to match the camera diff --git a/src/debug.rs b/src/debug.rs index 907fdd7..852c8d2 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -3,13 +3,10 @@ use std::marker::PhantomData; use bevy_app::prelude::*; -use bevy_color::{ - palettes::css::{BLUE, GREEN, RED}, - prelude::*, -}; use bevy_ecs::prelude::*; use bevy_gizmos::prelude::*; use bevy_math::prelude::*; +use bevy_render::prelude::*; use bevy_transform::prelude::*; use crate::{ @@ -47,7 +44,7 @@ pub fn update_debug_bounds( &Transform::from_scale(Vec3::splat(frame.cell_edge_length() * 0.999)), ); if origin.is_none() { - gizmos.cuboid(transform, Color::Srgba(GREEN)) + gizmos.cuboid(transform, Color::GREEN) } else { // gizmos.cuboid(transform, Color::rgba(0.0, 0.0, 1.0, 0.5)) } @@ -62,8 +59,8 @@ pub fn update_reference_frame_axes( for (transform, frame) in frames.iter() { let start = transform.translation(); let len = frame.cell_edge_length() * 2.0; - gizmos.ray(start, transform.right() * len, Color::Srgba(RED)); - gizmos.ray(start, transform.up() * len, Color::Srgba(GREEN)); - gizmos.ray(start, transform.back() * len, Color::Srgba(BLUE)); + gizmos.ray(start, transform.right() * len, Color::RED); + gizmos.ray(start, transform.up() * len, Color::GREEN); + gizmos.ray(start, transform.back() * len, Color::BLUE); } } diff --git a/src/plugin.rs b/src/plugin.rs index 1eaf5b1..832e7fe 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -2,7 +2,7 @@ use bevy_app::prelude::*; use bevy_ecs::prelude::*; -use bevy_reflect::{prelude::*, GetTypeRegistration}; +use bevy_reflect::prelude::*; use bevy_transform::{prelude::*, TransformSystem}; use std::marker::PhantomData; @@ -52,9 +52,7 @@ pub enum FloatingOriginSet { PropagateLowPrecision, } -impl Plugin - for BigSpacePlugin

-{ +impl Plugin for BigSpacePlugin

{ fn build(&self, app: &mut App) { let system_set_config = || { ( diff --git a/src/reference_frame/local_origin.rs b/src/reference_frame/local_origin.rs index 0decd54..01f38aa 100644 --- a/src/reference_frame/local_origin.rs +++ b/src/reference_frame/local_origin.rs @@ -506,18 +506,18 @@ mod tests { ReferenceFrame::::default(), ); - let child_1 = app.world_mut().spawn(frame_bundle.clone()).id(); - let child_2 = app.world_mut().spawn(frame_bundle.clone()).id(); - let parent = app.world_mut().spawn(frame_bundle.clone()).id(); - let root = app.world_mut().spawn(frame_bundle.clone()).id(); + let child_1 = app.world.spawn(frame_bundle.clone()).id(); + let child_2 = app.world.spawn(frame_bundle.clone()).id(); + let parent = app.world.spawn(frame_bundle.clone()).id(); + let root = app.world.spawn(frame_bundle.clone()).id(); - app.world_mut().entity_mut(root).push_children(&[parent]); - app.world_mut() + app.world.entity_mut(root).push_children(&[parent]); + app.world .entity_mut(parent) .push_children(&[child_1, child_2]); - let mut state = SystemState::>::new(app.world_mut()); - let mut ref_frames = state.get_mut(app.world_mut()); + let mut state = SystemState::>::new(&mut app.world); + let mut ref_frames = state.get_mut(&mut app.world); // Children let result = ref_frames.child_frames(root); @@ -559,12 +559,12 @@ mod tests { ..default() }; let root = app - .world_mut() + .world .spawn((Transform::default(), GridCell::::default(), root_frame)) .id(); let child = app - .world_mut() + .world .spawn(( Transform::from_rotation(Quat::from_rotation_z(std::f32::consts::FRAC_PI_2)) .with_translation(Vec3::new(1.0, 1.0, 0.0)), @@ -573,10 +573,10 @@ mod tests { )) .id(); - app.world_mut().entity_mut(root).push_children(&[child]); + app.world.entity_mut(root).push_children(&[child]); - let mut state = SystemState::>::new(app.world_mut()); - let mut reference_frames = state.get_mut(app.world_mut()); + let mut state = SystemState::>::new(&mut app.world); + let mut reference_frames = state.get_mut(&mut app.world); // The function we are testing propagate_origin_to_child(root, &mut reference_frames, child); @@ -615,10 +615,10 @@ mod tests { GridCell::::default(), ReferenceFrame::::default(), ); - let root = app.world_mut().spawn(frame_bundle.clone()).id(); + let root = app.world.spawn(frame_bundle.clone()).id(); let child = app - .world_mut() + .world .spawn(( Transform::from_rotation(Quat::from_rotation_z(std::f32::consts::FRAC_PI_2)) .with_translation(Vec3::new(1.0, 1.0, 0.0)), @@ -634,10 +634,10 @@ mod tests { )) .id(); - app.world_mut().entity_mut(root).push_children(&[child]); + app.world.entity_mut(root).push_children(&[child]); - let mut state = SystemState::>::new(app.world_mut()); - let mut reference_frames = state.get_mut(app.world_mut()); + let mut state = SystemState::>::new(&mut app.world); + let mut reference_frames = state.get_mut(&mut app.world); // The function we are testing propagate_origin_to_parent(child, &mut reference_frames, root); @@ -672,7 +672,7 @@ mod tests { app.add_plugins(BigSpacePlugin::::default()); let root = app - .world_mut() + .world .spawn(( Transform::default(), GridCell::::default(), @@ -688,7 +688,7 @@ mod tests { .id(); let child = app - .world_mut() + .world .spawn(( Transform::default() .with_rotation(Quat::from_rotation_z(-std::f32::consts::FRAC_PI_2)) @@ -698,10 +698,10 @@ mod tests { )) .id(); - app.world_mut().entity_mut(root).push_children(&[child]); + app.world.entity_mut(root).push_children(&[child]); - let mut state = SystemState::>::new(app.world_mut()); - let mut reference_frames = state.get_mut(app.world_mut()); + let mut state = SystemState::>::new(&mut app.world); + let mut reference_frames = state.get_mut(&mut app.world); propagate_origin_to_child(root, &mut reference_frames, child); diff --git a/src/reference_frame/mod.rs b/src/reference_frame/mod.rs index 3d90b42..16b0d72 100644 --- a/src/reference_frame/mod.rs +++ b/src/reference_frame/mod.rs @@ -34,7 +34,7 @@ pub mod propagation; /// the same rotating reference frame, instead of moving rapidly through space around a star, or /// worse, around the center of the galaxy. #[derive(Debug, Clone, Reflect, Component)] -pub struct ReferenceFrame { +pub struct ReferenceFrame { /// The high-precision position of the floating origin's current grid cell local to this /// reference frame. local_floating_origin: LocalFloatingOrigin

, diff --git a/src/tests.rs b/src/tests.rs index 509ea3f..b1c9a04 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -8,7 +8,7 @@ fn changing_floating_origin_updates_global_transform() { app.add_plugins(BigSpacePlugin::::default()); let first = app - .world_mut() + .world .spawn(( TransformBundle::from_transform(Transform::from_translation(Vec3::new( 150.0, 0.0, 0.0, @@ -19,7 +19,7 @@ fn changing_floating_origin_updates_global_transform() { .id(); let second = app - .world_mut() + .world .spawn(( TransformBundle::from_transform(Transform::from_translation(Vec3::new( 0.0, 0.0, 300.0, @@ -28,18 +28,18 @@ fn changing_floating_origin_updates_global_transform() { )) .id(); - app.world_mut() + app.world .spawn(BigSpaceRootBundle::::default()) .push_children(&[first, second]); app.update(); - app.world_mut().entity_mut(first).remove::(); - app.world_mut().entity_mut(second).insert(FloatingOrigin); + app.world.entity_mut(first).remove::(); + app.world.entity_mut(second).insert(FloatingOrigin); app.update(); - let second_global_transform = app.world_mut().get::(second).unwrap(); + let second_global_transform = app.world.get::(second).unwrap(); assert_eq!( second_global_transform.translation(), @@ -53,7 +53,7 @@ fn child_global_transforms_are_updated_when_floating_origin_changes() { app.add_plugins(BigSpacePlugin::::default()); let first = app - .world_mut() + .world .spawn(( TransformBundle::from_transform(Transform::from_translation(Vec3::new( 150.0, 0.0, 0.0, @@ -64,7 +64,7 @@ fn child_global_transforms_are_updated_when_floating_origin_changes() { .id(); let second = app - .world_mut() + .world .spawn(( TransformBundle::from_transform(Transform::from_translation(Vec3::new( 0.0, 0.0, 300.0, @@ -78,19 +78,19 @@ fn child_global_transforms_are_updated_when_floating_origin_changes() { }) .id(); - app.world_mut() + app.world .spawn(BigSpaceRootBundle::::default()) .push_children(&[first, second]); app.update(); - app.world_mut().entity_mut(first).remove::(); - app.world_mut().entity_mut(second).insert(FloatingOrigin); + app.world.entity_mut(first).remove::(); + app.world.entity_mut(second).insert(FloatingOrigin); app.update(); - let child = app.world_mut().get::(second).unwrap()[0]; - let child_transform = app.world_mut().get::(child).unwrap(); + let child = app.world.get::(second).unwrap()[0]; + let child_transform = app.world.get::(child).unwrap(); assert_eq!(child_transform.translation(), Vec3::new(0.0, 0.0, 600.0)); } From 87e96a700ddecf131463f68b988d7d0f9971b21e Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Fri, 5 Jul 2024 00:28:46 -0700 Subject: [PATCH 3/4] v 0.7.0 (#29) --- Cargo.toml | 35 ++++++++++---------- README.md | 1 + examples/debug.rs | 5 +-- examples/demo.rs | 18 +++++++---- examples/error.rs | 2 +- examples/error_child.rs | 27 ++++++++-------- examples/planets.rs | 15 +++++---- examples/split_screen.rs | 21 ++++++------ src/camera.rs | 50 ++++++++++++++++++++++++----- src/debug.rs | 13 +++++--- src/plugin.rs | 6 ++-- src/reference_frame/local_origin.rs | 46 +++++++++++++------------- src/reference_frame/mod.rs | 2 +- src/tests.rs | 26 +++++++-------- 14 files changed, 159 insertions(+), 108 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dfeafe0..aa9d8a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "big_space" -version = "0.6.0" +version = "0.7.0" edition = "2021" description = "A floating origin plugin for bevy" license = "MIT OR Apache-2.0" @@ -9,22 +9,23 @@ repository = "https://github.com/aevyrie/big_space" documentation = "https://docs.rs/crate/big_space/latest" [dependencies] -bevy_app = { version = "0.13", default_features = false } -bevy_ecs = { version = "0.13", default_features = false } -bevy_hierarchy = { version = "0.13", default_features = false } -bevy_log = { version = "0.13", default_features = false } -bevy_math = { version = "0.13", default_features = false } -bevy_reflect = { version = "0.13", default_features = false } -bevy_transform = { version = "0.13", default_features = false } -bevy_utils = { version = "0.13", default_features = false } +bevy_app = { version = "0.14.0", default-features = false } +bevy_ecs = { version = "0.14.0", default-features = false } +bevy_hierarchy = { version = "0.14.0", default-features = false } +bevy_log = { version = "0.14.0", default-features = false } +bevy_math = { version = "0.14.0", default-features = false } +bevy_reflect = { version = "0.14.0", default-features = false } +bevy_transform = { version = "0.14.0", default-features = false } +bevy_utils = { version = "0.14.0", default-features = false } # Optional -bevy_gizmos = { version = "0.13", default_features = false, optional = true } -bevy_render = { version = "0.13", default_features = false, optional = true } -bevy_input = { version = "0.13", default_features = false, optional = true } -bevy_time = { version = "0.13", default_features = false, optional = true } +bevy_color = { version = "0.14.0", default-features = false, optional = true } +bevy_gizmos = { version = "0.14.0", default-features = false, optional = true } +bevy_render = { version = "0.14.0", default-features = false, optional = true } +bevy_input = { version = "0.14.0", default-features = false, optional = true } +bevy_time = { version = "0.14.0", default-features = false, optional = true } [dev-dependencies] -bevy = { version = "0.13", default-features = false, features = [ +bevy = { version = "0.14.0", default-features = false, features = [ "bevy_scene", "bevy_gltf", "bevy_winit", @@ -34,14 +35,14 @@ bevy = { version = "0.13", default-features = false, features = [ "bevy_gizmos", "x11", "tonemapping_luts", - "multi-threaded", + "multi_threaded", ] } -bevy-inspector-egui = "0.24" +# bevy-inspector-egui = "0.24" rand = "0.8.5" [features] default = ["debug", "camera", "bevy_render"] -debug = ["bevy_gizmos", "bevy_render"] +debug = ["bevy_gizmos", "bevy_color"] camera = ["bevy_render", "bevy_time", "bevy_input"] [[example]] diff --git a/README.md b/README.md index 1ce0c23..298e307 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ From the docs: https://docs.rs/big_space/latest/big_space/precision/trait.GridPr | bevy | big_space | | ---- | --------- | +| 0.14 | 0.7 | | 0.13 | 0.5, 0.6 | | 0.12 | 0.4 | | 0.11 | 0.3 | diff --git a/examples/debug.rs b/examples/debug.rs index 65a5aa9..58b9263 100644 --- a/examples/debug.rs +++ b/examples/debug.rs @@ -1,6 +1,7 @@ #![allow(clippy::type_complexity)] use bevy::prelude::*; +use bevy_color::palettes; use big_space::{commands::BigSpaceCommands, reference_frame::ReferenceFrame, FloatingOrigin}; fn main() { @@ -13,7 +14,7 @@ fn main() { .insert_resource(ClearColor(Color::BLACK)) .add_systems(Startup, setup) .add_systems(Update, (movement, rotation)) - .run() + .run(); } #[derive(Component)] @@ -61,7 +62,7 @@ fn setup( ) { let mesh_handle = meshes.add(Sphere::new(0.1).mesh().ico(16).unwrap()); let matl_handle = materials.add(StandardMaterial { - base_color: Color::YELLOW, + base_color: Color::Srgba(palettes::basic::YELLOW), ..default() }); diff --git a/examples/demo.rs b/examples/demo.rs index 9c4b620..abf0e8f 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -3,6 +3,7 @@ use bevy::{ transform::TransformSystem, window::{CursorGrabMode, PrimaryWindow}, }; +use bevy_color::palettes; use big_space::{ camera::{CameraController, CameraInput}, commands::BigSpaceCommands, @@ -26,7 +27,7 @@ fn main() { PostUpdate, highlight_nearest_sphere.after(TransformSystem::TransformPropagate), ) - .run() + .run(); } fn setup( @@ -54,7 +55,7 @@ fn setup( let mesh_handle = meshes.add(Sphere::new(0.5).mesh().ico(32).unwrap()); let matl_handle = materials.add(StandardMaterial { - base_color: Color::BLUE, + base_color: Color::Srgba(palettes::basic::BLUE), perceptual_roughness: 0.8, reflectance: 1.0, ..default() @@ -78,7 +79,7 @@ fn setup( // light root.spawn_spatial(DirectionalLightBundle { directional_light: DirectionalLight { - illuminance: 100_000.0, + illuminance: 10_000.0, ..default() }, ..default() @@ -145,10 +146,15 @@ fn highlight_nearest_sphere( return; }; // Ignore rotation due to panicking in gizmos, as of bevy 0.13 - let (scale, rotation, translation) = transform.to_scale_rotation_translation(); + let (scale, _, translation) = transform.to_scale_rotation_translation(); gizmos - .sphere(translation, rotation, scale.x * 0.505, Color::RED) - .circle_segments(128); + .sphere( + translation, + Quat::IDENTITY, // Bevy likes to explode on non-normalized quats in gizmos, + scale.x * 0.505, + Color::Srgba(palettes::basic::RED), + ) + .resolution(128); } #[allow(clippy::type_complexity)] diff --git a/examples/error.rs b/examples/error.rs index cb5d7e0..4e4e9fb 100644 --- a/examples/error.rs +++ b/examples/error.rs @@ -20,7 +20,7 @@ fn main() { )) .add_systems(Startup, (setup_scene, setup_ui)) .add_systems(Update, (rotator_system, toggle_plugin)) - .run() + .run(); } /// You can put things really, really far away from the origin. The distance we use here is actually diff --git a/examples/error_child.rs b/examples/error_child.rs index 3fd19d7..1e9298b 100644 --- a/examples/error_child.rs +++ b/examples/error_child.rs @@ -1,18 +1,19 @@ //! This example demonstrates error accumulating from parent to children in nested reference frames. use bevy::{math::DVec3, prelude::*}; +use bevy_color::palettes; use big_space::{commands::BigSpaceCommands, reference_frame::ReferenceFrame, FloatingOrigin}; fn main() { App::new() .add_plugins(( DefaultPlugins.build().disable::(), - bevy_inspector_egui::quick::WorldInspectorPlugin::new(), + // bevy_inspector_egui::quick::WorldInspectorPlugin::new(), big_space::BigSpacePlugin::::default(), big_space::camera::CameraControllerPlugin::::default(), big_space::debug::FloatingOriginDebugPlugin::::default(), )) .add_systems(Startup, setup_scene) - .run() + .run(); } // The nearby object is NEARBY meters away from us. The distance object is DISTANT meters away from @@ -28,7 +29,7 @@ fn setup_scene( ) { let mesh_handle = meshes.add(Sphere::new(SPHERE_RADIUS).mesh()); let matl_handle = materials.add(StandardMaterial { - base_color: Color::rgb(0.8, 0.7, 0.6), + base_color: Color::srgb(0.8, 0.7, 0.6), ..default() }); @@ -37,7 +38,7 @@ fn setup_scene( |root_frame| { root_frame.spawn_spatial(PbrBundle { mesh: mesh_handle.clone(), - material: materials.add(Color::BLUE), + material: materials.add(Color::from(palettes::css::BLUE)), transform: Transform::from_translation(NEARBY), ..default() }); @@ -46,8 +47,8 @@ fn setup_scene( root_frame.with_frame( ReferenceFrame::new(SPHERE_RADIUS * 100.0, 0.0), |parent_frame| { - // This function introduces a small amount of error, because it can only work up to - // double precision floats. (f64). + // This function introduces a small amount of error, because it can only work up + // to double precision floats. (f64). let child = parent_frame .frame() .translation_to_grid(-DISTANT + NEARBY.as_dvec3()); @@ -60,16 +61,16 @@ fn setup_scene( parent_frame.insert(parent.0); parent_frame.with_children(|child_builder| { - // A green sphere that is a child of the sphere very far from the origin. This - // child is very far from its parent, and should be located exactly at the - // origin (if there was no floating point error). The distance from the green - // sphere to the red sphere is the error caused by float imprecision. Note that - // the sphere does not have any rendering artifacts, its position just has a - // fixed error. + // A green sphere that is a child of the sphere very far from the origin. + // This child is very far from its parent, and should be located exactly at + // the origin (if there was no floating point error). The distance from the + // green sphere to the red sphere is the error caused by float imprecision. + // Note that the sphere does not have any rendering artifacts, its position + // just has a fixed error. child_builder.spawn(( PbrBundle { mesh: mesh_handle, - material: materials.add(Color::GREEN), + material: materials.add(Color::from(palettes::css::GREEN)), transform: Transform::from_translation(child.1), ..default() }, diff --git a/examples/planets.rs b/examples/planets.rs index d12907a..946e8e4 100644 --- a/examples/planets.rs +++ b/examples/planets.rs @@ -11,6 +11,7 @@ use bevy::{ render::camera::Exposure, transform::TransformSystem, }; +use bevy_color::palettes; use big_space::{ camera::{CameraController, CameraInput}, commands::BigSpaceCommands, @@ -51,7 +52,7 @@ fn main() { ) .register_type::() .register_type::() - .run() + .run(); } const EARTH_ORBIT_RADIUS_M: f64 = 149.60e9; @@ -129,7 +130,7 @@ fn spawn_solar_system( let earth_mesh_handle = meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()); let moon_mesh_handle = meshes.add(Sphere::new(MOON_RADIUS_M as f32).mesh().ico(15).unwrap()); let ball_mesh_handle = meshes.add(Sphere::new(5.0).mesh().ico(5).unwrap()); - let plane_mesh_handle = meshes.add(Plane3d::new(Vec3::X)); + let plane_mesh_handle = meshes.add(Plane3d::new(Vec3::X, Vec2::splat(0.5))); commands.spawn(( PrimaryLight, @@ -160,7 +161,7 @@ fn spawn_solar_system( mesh: sun_mesh_handle, material: materials.add(StandardMaterial { base_color: Color::WHITE, - emissive: Color::rgb_linear(100000000., 100000000., 100000000.), + emissive: LinearRgba::rgb(100000., 100000., 100000.), ..default() }), ..default() @@ -177,7 +178,7 @@ fn spawn_solar_system( PbrBundle { mesh: earth_mesh_handle, material: materials.add(StandardMaterial { - base_color: Color::BLUE, + base_color: Color::Srgba(palettes::css::BLUE), perceptual_roughness: 0.8, reflectance: 1.0, ..default() @@ -197,7 +198,7 @@ fn spawn_solar_system( PbrBundle { mesh: moon_mesh_handle, material: materials.add(StandardMaterial { - base_color: Color::GRAY, + base_color: Color::Srgba(palettes::css::GRAY), perceptual_roughness: 1.0, reflectance: 0.0, ..default() @@ -226,7 +227,7 @@ fn spawn_solar_system( children.spawn((PbrBundle { mesh: plane_mesh_handle, material: materials.add(StandardMaterial { - base_color: Color::DARK_GREEN, + base_color: Color::Srgba(palettes::css::DARK_GREEN), perceptual_roughness: 1.0, reflectance: 0.0, ..default() @@ -282,7 +283,7 @@ fn spawn_solar_system( let star_mat = materials.add(StandardMaterial { base_color: Color::WHITE, - emissive: Color::rgb_linear(100000., 100000., 100000.), + emissive: LinearRgba::rgb(2., 2., 2.), ..default() }); let star_mesh_handle = meshes.add(Sphere::new(1e10).mesh().ico(5).unwrap()); diff --git a/examples/split_screen.rs b/examples/split_screen.rs index fee73df..7090bce 100644 --- a/examples/split_screen.rs +++ b/examples/split_screen.rs @@ -9,6 +9,7 @@ use bevy::{ render::{camera::Viewport, view::RenderLayers}, transform::TransformSystem, }; +use bevy_color::palettes; use big_space::{ camera::{CameraController, CameraControllerPlugin}, commands::BigSpaceCommands, @@ -33,7 +34,7 @@ fn main() { .after(big_space::camera::camera_controller::) .before(TransformSystem::TransformPropagate), ) - .run() + .run(); } #[derive(Component)] @@ -58,7 +59,7 @@ fn setup( transform: Transform::default().looking_to(Vec3::NEG_ONE, Vec3::Y), ..default() }, - RenderLayers::all(), + RenderLayers::from_layers(&[1, 2]), )); // Big Space 1 @@ -80,7 +81,7 @@ fn setup( PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 2.0, 1.0)), material: materials.add(StandardMaterial { - base_color: Color::YELLOW, + base_color: Color::Srgba(palettes::css::YELLOW), ..default() }), ..default() @@ -94,7 +95,7 @@ fn setup( PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 2.0, 1.0)), material: materials.add(StandardMaterial { - base_color: Color::PINK, + base_color: Color::Srgba(palettes::css::FUCHSIA), ..default() }), ..default() @@ -106,7 +107,7 @@ fn setup( PbrBundle { mesh: meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()), material: materials.add(StandardMaterial { - base_color: Color::BLUE, + base_color: Color::Srgba(palettes::css::BLUE), ..default() }), transform: Transform::from_xyz(1_000_000.0, 0.0, 0.0) @@ -120,7 +121,7 @@ fn setup( PbrBundle { mesh: meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()), material: materials.add(StandardMaterial { - base_color: Color::GREEN, + base_color: Color::Srgba(palettes::css::GREEN), ..default() }), transform: Transform::from_xyz(-1_000_000.0, 0.0, 0.0) @@ -154,7 +155,7 @@ fn setup( PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 2.0, 1.0)), material: materials.add(StandardMaterial { - base_color: Color::PINK, + base_color: Color::Srgba(palettes::css::PINK), ..default() }), ..default() @@ -168,7 +169,7 @@ fn setup( PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 2.0, 1.0)), material: materials.add(StandardMaterial { - base_color: Color::YELLOW, + base_color: Color::Srgba(palettes::css::YELLOW), ..default() }), ..default() @@ -180,7 +181,7 @@ fn setup( PbrBundle { mesh: meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()), material: materials.add(StandardMaterial { - base_color: Color::BLUE, + base_color: Color::Srgba(palettes::css::BLUE), ..default() }), transform: Transform::from_xyz(1_000_000.0, 0.0, 0.0) @@ -194,7 +195,7 @@ fn setup( PbrBundle { mesh: meshes.add(Sphere::new(1.0).mesh().ico(35).unwrap()), material: materials.add(StandardMaterial { - base_color: Color::GREEN, + base_color: Color::Srgba(palettes::css::GREEN), ..default() }), transform: Transform::from_xyz(-1_000_000.0, 0.0, 0.0) diff --git a/src/camera.rs b/src/camera.rs index 78051e3..56aeafa 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -45,6 +45,12 @@ pub struct CameraController { pub rotational_smoothness: f64, /// Base speed. pub speed: f64, + /// Rotational yaw speed multiplier. + pub speed_yaw: f64, + /// Rotational pitch speed multiplier. + pub speed_pitch: f64, + /// Rotational roll speed multiplier. + pub speed_roll: f64, /// Minimum and maximum speed. pub speed_bounds: [f64; 2], /// Whether the camera should slow down when approaching an entity's [`Aabb`]. @@ -73,6 +79,25 @@ impl CameraController { self.speed = speed; self } + + /// Sets the yaw angular velocity of the controller, and returns the modified result. + pub fn with_speed_yaw(mut self, speed: f64) -> Self { + self.speed_yaw = speed; + self + } + + /// Sets the pitch angular velocity of the controller, and returns the modified result. + pub fn with_speed_pitch(mut self, speed: f64) -> Self { + self.speed_pitch = speed; + self + } + + /// Sets the pitch angular velocity of the controller, and returns the modified result. + pub fn with_speed_roll(mut self, speed: f64) -> Self { + self.speed_roll = speed; + self + } + /// Sets the speed of the controller, and returns the modified result. pub fn with_speed_bounds(mut self, speed_limits: [f64; 2]) -> Self { self.speed_bounds = speed_limits; @@ -96,6 +121,9 @@ impl Default for CameraController { smoothness: 0.8, rotational_smoothness: 0.5, speed: 1.0, + speed_pitch: 1.0, + speed_yaw: 1.0, + speed_roll: 1.0, speed_bounds: [1e-17, 1e30], slow_near_objects: true, nearest_object: None, @@ -137,12 +165,17 @@ impl CameraInput { } /// Returns the desired velocity transform. - pub fn target_velocity(&self, speed: f64, dt: f64) -> (DVec3, DQuat) { + pub fn target_velocity( + &self, + controller: &CameraController, + speed: f64, + dt: f64, + ) -> (DVec3, DQuat) { let rotation = DQuat::from_euler( EulerRot::XYZ, - self.pitch * dt, - self.yaw * dt, - self.roll * dt, + self.pitch * dt * controller.speed_pitch, + self.yaw * dt * controller.speed_yaw, + self.roll * dt * controller.speed_roll, ); let translation = DVec3::new(self.right, self.up, self.forward) * speed * dt; @@ -196,15 +229,15 @@ pub fn nearest_objects_in_frame( let Ok((cam_entity, mut camera, cam_pos, cam_layer)) = camera.get_single_mut() else { return; }; - let cam_layer = cam_layer.copied().unwrap_or(RenderLayers::all()); + let cam_layer = cam_layer.to_owned().unwrap_or_default(); let cam_children: HashSet = children.iter_descendants(cam_entity).collect(); let nearest_object = objects .iter() .filter(|(entity, ..)| !cam_children.contains(entity)) .filter(|(.., obj_layer)| { - let obj_layer = obj_layer.copied().unwrap_or(RenderLayers::layer(0)); - cam_layer.intersects(&obj_layer) + let obj_layer = obj_layer.unwrap_or_default(); + cam_layer.intersects(obj_layer) }) .map(|(entity, object_local, obj_pos, aabb, _)| { let center_distance = @@ -243,7 +276,8 @@ pub fn camera_controller( let lerp_rotation = 1.0 - controller.rotational_smoothness.clamp(0.0, 0.999); 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(&controller, speed, time.delta_seconds_f64()); let cam_rot = position.transform.rotation.as_dquat(); let vel_t_next = cam_rot * vel_t_target; // Orients the translation to match the camera diff --git a/src/debug.rs b/src/debug.rs index 852c8d2..907fdd7 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -3,10 +3,13 @@ use std::marker::PhantomData; use bevy_app::prelude::*; +use bevy_color::{ + palettes::css::{BLUE, GREEN, RED}, + prelude::*, +}; use bevy_ecs::prelude::*; use bevy_gizmos::prelude::*; use bevy_math::prelude::*; -use bevy_render::prelude::*; use bevy_transform::prelude::*; use crate::{ @@ -44,7 +47,7 @@ pub fn update_debug_bounds( &Transform::from_scale(Vec3::splat(frame.cell_edge_length() * 0.999)), ); if origin.is_none() { - gizmos.cuboid(transform, Color::GREEN) + gizmos.cuboid(transform, Color::Srgba(GREEN)) } else { // gizmos.cuboid(transform, Color::rgba(0.0, 0.0, 1.0, 0.5)) } @@ -59,8 +62,8 @@ pub fn update_reference_frame_axes( for (transform, frame) in frames.iter() { let start = transform.translation(); let len = frame.cell_edge_length() * 2.0; - gizmos.ray(start, transform.right() * len, Color::RED); - gizmos.ray(start, transform.up() * len, Color::GREEN); - gizmos.ray(start, transform.back() * len, Color::BLUE); + gizmos.ray(start, transform.right() * len, Color::Srgba(RED)); + gizmos.ray(start, transform.up() * len, Color::Srgba(GREEN)); + gizmos.ray(start, transform.back() * len, Color::Srgba(BLUE)); } } diff --git a/src/plugin.rs b/src/plugin.rs index 832e7fe..1eaf5b1 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -2,7 +2,7 @@ use bevy_app::prelude::*; use bevy_ecs::prelude::*; -use bevy_reflect::prelude::*; +use bevy_reflect::{prelude::*, GetTypeRegistration}; use bevy_transform::{prelude::*, TransformSystem}; use std::marker::PhantomData; @@ -52,7 +52,9 @@ pub enum FloatingOriginSet { PropagateLowPrecision, } -impl Plugin for BigSpacePlugin

{ +impl Plugin + for BigSpacePlugin

+{ fn build(&self, app: &mut App) { let system_set_config = || { ( diff --git a/src/reference_frame/local_origin.rs b/src/reference_frame/local_origin.rs index 01f38aa..0decd54 100644 --- a/src/reference_frame/local_origin.rs +++ b/src/reference_frame/local_origin.rs @@ -506,18 +506,18 @@ mod tests { ReferenceFrame::::default(), ); - let child_1 = app.world.spawn(frame_bundle.clone()).id(); - let child_2 = app.world.spawn(frame_bundle.clone()).id(); - let parent = app.world.spawn(frame_bundle.clone()).id(); - let root = app.world.spawn(frame_bundle.clone()).id(); + let child_1 = app.world_mut().spawn(frame_bundle.clone()).id(); + let child_2 = app.world_mut().spawn(frame_bundle.clone()).id(); + let parent = app.world_mut().spawn(frame_bundle.clone()).id(); + let root = app.world_mut().spawn(frame_bundle.clone()).id(); - app.world.entity_mut(root).push_children(&[parent]); - app.world + app.world_mut().entity_mut(root).push_children(&[parent]); + app.world_mut() .entity_mut(parent) .push_children(&[child_1, child_2]); - let mut state = SystemState::>::new(&mut app.world); - let mut ref_frames = state.get_mut(&mut app.world); + let mut state = SystemState::>::new(app.world_mut()); + let mut ref_frames = state.get_mut(app.world_mut()); // Children let result = ref_frames.child_frames(root); @@ -559,12 +559,12 @@ mod tests { ..default() }; let root = app - .world + .world_mut() .spawn((Transform::default(), GridCell::::default(), root_frame)) .id(); let child = app - .world + .world_mut() .spawn(( Transform::from_rotation(Quat::from_rotation_z(std::f32::consts::FRAC_PI_2)) .with_translation(Vec3::new(1.0, 1.0, 0.0)), @@ -573,10 +573,10 @@ mod tests { )) .id(); - app.world.entity_mut(root).push_children(&[child]); + app.world_mut().entity_mut(root).push_children(&[child]); - let mut state = SystemState::>::new(&mut app.world); - let mut reference_frames = state.get_mut(&mut app.world); + let mut state = SystemState::>::new(app.world_mut()); + let mut reference_frames = state.get_mut(app.world_mut()); // The function we are testing propagate_origin_to_child(root, &mut reference_frames, child); @@ -615,10 +615,10 @@ mod tests { GridCell::::default(), ReferenceFrame::::default(), ); - let root = app.world.spawn(frame_bundle.clone()).id(); + let root = app.world_mut().spawn(frame_bundle.clone()).id(); let child = app - .world + .world_mut() .spawn(( Transform::from_rotation(Quat::from_rotation_z(std::f32::consts::FRAC_PI_2)) .with_translation(Vec3::new(1.0, 1.0, 0.0)), @@ -634,10 +634,10 @@ mod tests { )) .id(); - app.world.entity_mut(root).push_children(&[child]); + app.world_mut().entity_mut(root).push_children(&[child]); - let mut state = SystemState::>::new(&mut app.world); - let mut reference_frames = state.get_mut(&mut app.world); + let mut state = SystemState::>::new(app.world_mut()); + let mut reference_frames = state.get_mut(app.world_mut()); // The function we are testing propagate_origin_to_parent(child, &mut reference_frames, root); @@ -672,7 +672,7 @@ mod tests { app.add_plugins(BigSpacePlugin::::default()); let root = app - .world + .world_mut() .spawn(( Transform::default(), GridCell::::default(), @@ -688,7 +688,7 @@ mod tests { .id(); let child = app - .world + .world_mut() .spawn(( Transform::default() .with_rotation(Quat::from_rotation_z(-std::f32::consts::FRAC_PI_2)) @@ -698,10 +698,10 @@ mod tests { )) .id(); - app.world.entity_mut(root).push_children(&[child]); + app.world_mut().entity_mut(root).push_children(&[child]); - let mut state = SystemState::>::new(&mut app.world); - let mut reference_frames = state.get_mut(&mut app.world); + let mut state = SystemState::>::new(app.world_mut()); + let mut reference_frames = state.get_mut(app.world_mut()); propagate_origin_to_child(root, &mut reference_frames, child); diff --git a/src/reference_frame/mod.rs b/src/reference_frame/mod.rs index 16b0d72..3d90b42 100644 --- a/src/reference_frame/mod.rs +++ b/src/reference_frame/mod.rs @@ -34,7 +34,7 @@ pub mod propagation; /// the same rotating reference frame, instead of moving rapidly through space around a star, or /// worse, around the center of the galaxy. #[derive(Debug, Clone, Reflect, Component)] -pub struct ReferenceFrame { +pub struct ReferenceFrame { /// The high-precision position of the floating origin's current grid cell local to this /// reference frame. local_floating_origin: LocalFloatingOrigin

, diff --git a/src/tests.rs b/src/tests.rs index b1c9a04..509ea3f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -8,7 +8,7 @@ fn changing_floating_origin_updates_global_transform() { app.add_plugins(BigSpacePlugin::::default()); let first = app - .world + .world_mut() .spawn(( TransformBundle::from_transform(Transform::from_translation(Vec3::new( 150.0, 0.0, 0.0, @@ -19,7 +19,7 @@ fn changing_floating_origin_updates_global_transform() { .id(); let second = app - .world + .world_mut() .spawn(( TransformBundle::from_transform(Transform::from_translation(Vec3::new( 0.0, 0.0, 300.0, @@ -28,18 +28,18 @@ fn changing_floating_origin_updates_global_transform() { )) .id(); - app.world + app.world_mut() .spawn(BigSpaceRootBundle::::default()) .push_children(&[first, second]); app.update(); - app.world.entity_mut(first).remove::(); - app.world.entity_mut(second).insert(FloatingOrigin); + app.world_mut().entity_mut(first).remove::(); + app.world_mut().entity_mut(second).insert(FloatingOrigin); app.update(); - let second_global_transform = app.world.get::(second).unwrap(); + let second_global_transform = app.world_mut().get::(second).unwrap(); assert_eq!( second_global_transform.translation(), @@ -53,7 +53,7 @@ fn child_global_transforms_are_updated_when_floating_origin_changes() { app.add_plugins(BigSpacePlugin::::default()); let first = app - .world + .world_mut() .spawn(( TransformBundle::from_transform(Transform::from_translation(Vec3::new( 150.0, 0.0, 0.0, @@ -64,7 +64,7 @@ fn child_global_transforms_are_updated_when_floating_origin_changes() { .id(); let second = app - .world + .world_mut() .spawn(( TransformBundle::from_transform(Transform::from_translation(Vec3::new( 0.0, 0.0, 300.0, @@ -78,19 +78,19 @@ fn child_global_transforms_are_updated_when_floating_origin_changes() { }) .id(); - app.world + app.world_mut() .spawn(BigSpaceRootBundle::::default()) .push_children(&[first, second]); app.update(); - app.world.entity_mut(first).remove::(); - app.world.entity_mut(second).insert(FloatingOrigin); + app.world_mut().entity_mut(first).remove::(); + app.world_mut().entity_mut(second).insert(FloatingOrigin); app.update(); - let child = app.world.get::(second).unwrap()[0]; - let child_transform = app.world.get::(child).unwrap(); + let child = app.world_mut().get::(second).unwrap()[0]; + let child_transform = app.world_mut().get::(child).unwrap(); assert_eq!(child_transform.translation(), Vec3::new(0.0, 0.0, 600.0)); } From b2ebb025e7410adaaec73e6db497f1ea48eb0c4e Mon Sep 17 00:00:00 2001 From: Lemonzy Date: Tue, 16 Jul 2024 03:03:50 +0200 Subject: [PATCH 4/4] Use object visibility to determine the closest object to the camera (#28) Fix #27 --- src/camera.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 56aeafa..869af8b 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -8,7 +8,10 @@ use bevy_hierarchy::prelude::*; use bevy_input::{mouse::MouseMotion, prelude::*}; use bevy_math::{prelude::*, DQuat, DVec3}; use bevy_reflect::prelude::*; -use bevy_render::{primitives::Aabb, view::RenderLayers}; +use bevy_render::{ + primitives::Aabb, + view::{InheritedVisibility, RenderLayers}, +}; use bevy_time::prelude::*; use bevy_transform::{prelude::*, TransformSystem}; use bevy_utils::HashSet; @@ -217,6 +220,7 @@ pub fn nearest_objects_in_frame( &GlobalTransform, &Aabb, Option<&RenderLayers>, + Option<&InheritedVisibility>, )>, mut camera: Query<( Entity, @@ -235,11 +239,15 @@ pub fn nearest_objects_in_frame( let nearest_object = objects .iter() .filter(|(entity, ..)| !cam_children.contains(entity)) - .filter(|(.., obj_layer)| { + .filter(|(.., obj_layer, _)| { let obj_layer = obj_layer.unwrap_or_default(); cam_layer.intersects(obj_layer) }) - .map(|(entity, object_local, obj_pos, aabb, _)| { + .filter(|(.., visibility)| { + let visibility = visibility.copied().unwrap_or(InheritedVisibility::VISIBLE); + visibility.get() + }) + .map(|(entity, object_local, obj_pos, aabb, ..)| { let center_distance = obj_pos.translation().as_dvec3() - cam_pos.translation().as_dvec3(); let nearest_distance = center_distance.length()