//! This example demonstrates what floating point error in rendering looks like. You can press //! spacebar to smoothly switch between enabling and disabling the floating origin. //! //! Instead of disabling the plugin outright, this example simply moves the floating origin //! independently from the camera, which is equivalent to what would happen when moving far from the //! origin when not using this plugin. use bevy::prelude::*; use big_space::{FloatingOrigin, FloatingSpatialBundle, GridCell}; fn main() { App::new() .add_plugins(( DefaultPlugins.build().disable::(), big_space::FloatingOriginPlugin::::default(), )) .add_systems(Startup, (setup_scene, setup_ui)) .add_systems(Update, (rotator_system, toggle_plugin)) .run() } /// 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 /// the camera. If you go much further than this, the cubes will simply disappear in a *POOF* of /// floating point error. /// /// 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 const DISTANCE: f32 = 10_000_000.0; /// 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 /// this issue. fn toggle_plugin( input: Res>, mut text: Query<&mut Text>, mut state: Local, mut floating_origin: Query<&mut GridCell, With>, ) { if input.just_pressed(KeyCode::Space) { *state = !*state; } let mut cell = floating_origin.single_mut(); let cell_max = (DISTANCE / 10_000f32) as i64; let i = cell_max / 200; let msg = if *state { if 0 <= cell.x - i { cell.x = 0.max(cell.x - i); cell.y = 0.max(cell.y - i); cell.z = 0.max(cell.z - i); "Disabling..." } else { "Floating Origin Disabled" } } else if cell_max >= cell.x + i { cell.x = i64::min(cell_max, cell.x + i); cell.y = i64::min(cell_max, cell.y + i); cell.z = i64::min(cell_max, cell.z + i); "Enabling..." } else { "Floating Origin Enabled" }; let dist = (cell_max - cell.x) * 10_000; text.single_mut().sections[0].value = format!("Press Spacebar to toggle: {msg}\nCamera distance to floating origin: {dist}") } #[derive(Component)] struct Rotator; fn rotator_system(time: Res