use bevy::{ prelude::*, transform::TransformSystem, window::{CursorGrabMode, PrimaryWindow, Window, WindowMode}, }; use big_space::{ camera::{CameraController, CameraInput}, FloatingOrigin, GridCell, }; fn main() { App::new() .add_plugins(( DefaultPlugins.build().disable::(), big_space::FloatingOriginPlugin::::default(), big_space::debug::FloatingOriginDebugPlugin::::default(), big_space::camera::CameraControllerPlugin::::default(), bevy_framepace::FramepacePlugin, )) .insert_resource(ClearColor(Color::BLACK)) .add_systems(Startup, (setup, ui_setup)) .add_systems(Update, (cursor_grab_system, ui_text_system)) .add_systems( PostUpdate, highlight_nearest_sphere.after(TransformSystem::TransformPropagate), ) .run() } fn setup( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, ) { // camera commands.spawn(( Camera3dBundle { transform: Transform::from_xyz(0.0, 0.0, 8.0) .looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y), projection: Projection::Perspective(PerspectiveProjection { near: 1e-16, ..default() }), ..default() }, GridCell::::default(), // All spatial entities need this component FloatingOrigin, // Important: marks this as the entity to use as the floating origin CameraController::default() .with_max_speed(10e35) .with_smoothness(0.9, 0.8), // Built-in camera controller )); let mesh_handle = meshes.add( shape::Icosphere { radius: 0.5, subdivisions: 32, } .try_into() .unwrap(), ); let matl_handle = materials.add(StandardMaterial { base_color: Color::BLUE, perceptual_roughness: 0.8, reflectance: 1.0, ..default() }); let mut translation = Vec3::ZERO; for i in -12..=27 { let j = 10_f32.powf(i as f32); translation.x += j; commands.spawn(( PbrBundle { mesh: mesh_handle.clone(), material: matl_handle.clone(), transform: Transform::from_scale(Vec3::splat(j)).with_translation(translation), ..default() }, GridCell::::default(), )); } // light commands.spawn((DirectionalLightBundle { directional_light: DirectionalLight { illuminance: 100_000.0, ..default() }, ..default() },)); } #[derive(Component, Reflect)] pub struct BigSpaceDebugText; fn ui_setup(mut commands: Commands) { commands.spawn(( TextBundle::from_section( "", TextStyle { font_size: 28.0, color: Color::WHITE, ..default() }, ) .with_text_alignment(TextAlignment::Left) .with_style(Style { position_type: PositionType::Absolute, top: Val::Px(10.0), left: Val::Px(10.0), ..default() }), BigSpaceDebugText, )); } fn highlight_nearest_sphere( cameras: Query<&CameraController>, objects: Query<&GlobalTransform>, mut gizmos: Gizmos, ) { let Some((entity, _)) = cameras.single().nearest_object() else { return }; let Ok(transform) = objects.get(entity) else { return }; let (scale, rotation, translation) = transform.to_scale_rotation_translation(); gizmos .sphere(translation, rotation, scale.x * 0.505, Color::RED) .circle_segments(128); } fn ui_text_system( mut text: Query<&mut Text, With>, time: Res