diff --git a/Cargo.toml b/Cargo.toml index e668ad0..736c74b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,6 @@ bevy-inspector-egui = "0.28.0" bevy_reflect = "0.15.0" bevy_render = "0.15.0" bevy_window = "0.15.0" -egui_dock = "0.14.0" \ No newline at end of file +egui_dock = "0.14.0" +bytemuck = "1.13" +bevy_mod_debugdump = "0.12.1" \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index ee36acc..bcb3ef2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,8 +1,8 @@ use bevy::prelude::*; use bevy_egui::EguiSet; +use bevy_render::extract_resource::ExtractResourcePlugin; use crate::helper::debug_gizmos::debug_gizmos; use crate::helper::egui_dock::{reset_camera_viewport, set_camera_viewport, set_gizmo_mode, show_ui_system, UiState}; -use crate::helper::large_transform::DoubleTransform; pub struct AppPlugin; @@ -26,7 +26,6 @@ impl Plugin for AppPlugin { app.add_plugins(crate::plugins::ui_plugin::UiPlugin); app.add_plugins(crate::plugins::environment_plugin::EnvironmentPlugin); - app.add_plugins(crate::plugins::large_transform_plugin::LargeTransformPlugin); @@ -50,7 +49,6 @@ impl Plugin for AppPlugin { app.add_systems(Update, set_gizmo_mode); app.register_type::>>(); app.register_type::(); - app.register_type::(); } diff --git a/src/helper/large_transform.rs b/src/helper/large_transform.rs deleted file mode 100644 index 40adb81..0000000 --- a/src/helper/large_transform.rs +++ /dev/null @@ -1,103 +0,0 @@ -use bevy::math::{DQuat, DVec3}; -use bevy::prelude::{Commands, Component, GlobalTransform, Query, Reflect, Res, ResMut, Resource, Transform, With, Without}; -use bevy_render::prelude::Camera; - - -#[derive(Resource, Reflect,Default)] -pub struct WorldOffset(pub DVec3); - -#[derive(Component, Default,Reflect)] -pub struct DoubleTransform { - pub translation: DVec3, - pub rotation: DQuat, - pub scale: DVec3, -} - -impl DoubleTransform { - pub fn new(translation: DVec3, rotation: DQuat, scale: DVec3) -> Self { - Self { - translation, - rotation, - scale, - } - } - - /// Returns a unit vector pointing "forward" (negative-Z) based on the rotation - pub fn forward(&self) -> DVec3 { - self.rotation * DVec3::new(0.0, 0.0, -1.0) - } - - /// Returns a unit vector pointing "right" (positive-X) - pub fn right(&self) -> DVec3 { - self.rotation * DVec3::new(1.0, 0.0, 0.0) - } - - /// Returns a unit vector pointing "up" (positive-Y) - pub fn up(&self) -> DVec3 { - self.rotation * DVec3::new(0.0, 1.0, 0.0) - } - pub fn down(&self) -> DVec3 { - self.rotation * DVec3::new(0.0, -1.0, 0.0) - } - -} - -pub(crate) fn get_true_world_position( - offset: &WorldOffset, - transform: &DoubleTransform, -) -> DVec3 { - transform.translation + offset.0 -} - -pub fn setup(mut commands: Commands) { - commands - .spawn(( - DoubleTransform { - translation: DVec3::new(100_000.0, 0.0, 0.0), - rotation: DQuat::IDENTITY, - scale: DVec3::ONE, - }, - // The standard Bevy Transform (will be updated each frame) - Transform::default(), - GlobalTransform::default(), - // Add your mesh/visibility components, etc. - )); - -} - - -pub fn update_render_transform_system( - camera_query: Query<&DoubleTransform, With>, - mut query: Query<(&DoubleTransform, &mut Transform), Without>, -) { - let camera_double_tf = camera_query.single(); - // The camera offset in double-precision - let camera_pos = camera_double_tf.translation; - - for (double_tf, mut transform) in query.iter_mut() { - // relative position (double precision) - let relative_pos = double_tf.translation - camera_pos; - transform.translation = relative_pos.as_vec3(); // convert f64 -> f32 - transform.rotation = double_tf.rotation.as_quat(); // f64 -> f32 - transform.scale = double_tf.scale.as_vec3(); // f64 -> f32 - } -} - -pub fn floating_origin_system( - mut query: Query<&mut DoubleTransform, Without>, - mut camera_query: Query<&mut DoubleTransform, With>, - mut offset: ResMut, -) { - let mut camera_tf = camera_query.single_mut(); - let camera_pos = camera_tf.translation; - - // If the camera moves any distance, recenter it - if camera_pos.length() > 0.001 { - offset.0 += camera_pos; - // Shift everything so camera ends up back at zero - for mut dtf in query.iter_mut() { - dtf.translation -= camera_pos; - } - camera_tf.translation = DVec3::ZERO; - } -} \ No newline at end of file diff --git a/src/helper/mod.rs b/src/helper/mod.rs index 66818b0..ec80961 100644 --- a/src/helper/mod.rs +++ b/src/helper/mod.rs @@ -1,3 +1,2 @@ pub mod egui_dock; -pub mod debug_gizmos; -pub mod large_transform; \ No newline at end of file +pub mod debug_gizmos; \ No newline at end of file diff --git a/src/plugins/environment_plugin.rs b/src/plugins/environment_plugin.rs index ad8c4cc..cc9a222 100644 --- a/src/plugins/environment_plugin.rs +++ b/src/plugins/environment_plugin.rs @@ -1,22 +1,19 @@ -use std::fs::create_dir; -use bevy::app::{App, Plugin, PreUpdate, Startup}; -use bevy::color::palettes::css::{GRAY, RED}; -use bevy::prelude::{default, Color, Commands, GlobalTransform, IntoSystemConfigs, Query, Res, Update}; -use bevy_render::prelude::ClearColor; -use crate::app::InspectorVisible; + +use bevy::app::{App, Plugin, Startup}; +use bevy::color::palettes::basic::{GREEN, YELLOW}; +use bevy::color::palettes::css::RED; +use bevy::prelude::*; use crate::systems::environment_system::*; -use crate::systems::voxels::structure::{ChunkEntities, SparseVoxelOctree, Voxel}; +use crate::systems::voxels::structure::{OctreeNode, SparseVoxelOctree}; pub struct EnvironmentPlugin; impl Plugin for EnvironmentPlugin { fn build(&self, app: &mut App) { - /*app.insert_resource(ClearColor(Color::from(GRAY)));*/ - app.init_resource::(); + app.add_systems(Startup, (setup).chain()); - app.add_systems(Update, (crate::systems::voxels::rendering::render,crate::systems::voxels::debug::visualize_octree.run_if(should_visualize_octree), crate::systems::voxels::debug::draw_grid.run_if(should_draw_grid), crate::systems::voxels::debug::debug_draw_chunks_system.run_if(should_visualize_chunks)).chain()); + app.add_systems(Update, (crate::systems::voxels::rendering::render,crate::systems::voxels::debug::visualize_octree.run_if(should_visualize_octree), crate::systems::voxels::debug::draw_grid.run_if(should_draw_grid)).chain()); app.register_type::(); - app.register_type::(); } @@ -34,4 +31,5 @@ fn should_draw_grid(octree_query: Query<&SparseVoxelOctree>,) -> bool { fn should_visualize_chunks(octree_query: Query<&SparseVoxelOctree>,) -> bool { octree_query.single().show_chunks -} \ No newline at end of file +} + diff --git a/src/plugins/large_transform_plugin.rs b/src/plugins/large_transform_plugin.rs deleted file mode 100644 index cbeb99d..0000000 --- a/src/plugins/large_transform_plugin.rs +++ /dev/null @@ -1,17 +0,0 @@ - -use bevy::app::{App, Plugin, PreUpdate, Startup, Update}; -use bevy::prelude::IntoSystemConfigs; -use crate::helper::large_transform::*; - -pub struct LargeTransformPlugin; -impl Plugin for LargeTransformPlugin { - fn build(&self, app: &mut App) { - - app.insert_resource(WorldOffset::default()); - app.add_systems(Startup, setup); - app.add_systems(Update, floating_origin_system.after(crate::systems::camera_system::camera_controller_system)); - app.add_systems(Update, update_render_transform_system.after(floating_origin_system)); - } - - -} diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index 6f23a92..8626e4b 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -1,5 +1,4 @@ -pub mod large_transform_plugin; pub mod camera_plugin; pub mod ui_plugin; pub mod environment_plugin; \ No newline at end of file diff --git a/src/systems/camera_system.rs b/src/systems/camera_system.rs index 6c9c872..131922e 100644 --- a/src/systems/camera_system.rs +++ b/src/systems/camera_system.rs @@ -5,7 +5,6 @@ use bevy::prelude::*; use bevy_render::camera::{OrthographicProjection, Projection, ScalingMode}; use bevy_window::CursorGrabMode; use crate::helper::egui_dock::MainCamera; -use crate::helper::large_transform::{DoubleTransform, WorldOffset}; use crate::InspectorVisible; use crate::systems::voxels::structure::{Ray, SparseVoxelOctree, Voxel}; @@ -33,12 +32,7 @@ pub fn setup(mut commands: Commands,){ commands.spawn(( - DoubleTransform { - translation: DVec3::new(0.0, 0.0, 10.0), - rotation: DQuat::IDENTITY, - scale: DVec3::ONE, - }, - Transform::from_xyz(0.0, 5.0, 10.0), // initial f32 + Transform::from_xyz(0.0, 0.0, 10.0), // initial f32 GlobalTransform::default(), Camera3d::default(), Projection::from(PerspectiveProjection{ @@ -64,13 +58,12 @@ pub fn camera_controller_system( // Here we query for BOTH DoubleTransform (f64) and Transform (f32). // We'll update DoubleTransform for the "true" position // and keep Transform in sync for rendering.a - mut query: Query<(&mut DoubleTransform, &mut Transform, &mut CameraController)>, + mut query: Query<(&mut Transform, &mut CameraController)>, mut octree_query: Query<&mut SparseVoxelOctree>, mut app_exit_events: EventWriter, - world_offset: Res, ) { let mut window = windows.single_mut(); - let (mut double_tf, mut render_tf, mut controller) = query.single_mut(); + let (mut transform, mut controller) = query.single_mut(); // ==================== // 1) Handle Mouse Look @@ -87,10 +80,10 @@ pub fn camera_controller_system( let pitch_radians = controller.pitch.to_radians(); // Build a double-precision quaternion from those angles - let rot_yaw = DQuat::from_axis_angle(DVec3::Y, yaw_radians as f64); - let rot_pitch = DQuat::from_axis_angle(DVec3::X, -pitch_radians as f64); + let rot_yaw = Quat::from_axis_angle(Vec3::Y, yaw_radians); + let rot_pitch = Quat::from_axis_angle(Vec3::X, -pitch_radians); - double_tf.rotation = rot_yaw * rot_pitch; + transform.rotation = rot_yaw * rot_pitch; } } @@ -109,30 +102,30 @@ pub fn camera_controller_system( // ==================== // 3) Handle Keyboard Movement (WASD, Space, Shift) // ==================== - let mut direction = DVec3::ZERO; + let mut direction = Vec3::ZERO; // Forward/Back if keyboard_input.pressed(KeyCode::KeyW) { - direction += double_tf.forward(); + direction += transform.forward().as_vec3(); } if keyboard_input.pressed(KeyCode::KeyS) { - direction -= double_tf.forward(); + direction -= transform.forward().as_vec3(); } // Left/Right if keyboard_input.pressed(KeyCode::KeyA) { - direction -= double_tf.right(); + direction -= transform.right().as_vec3(); } if keyboard_input.pressed(KeyCode::KeyD) { - direction += double_tf.right(); + direction += transform.right().as_vec3(); } // Up/Down if keyboard_input.pressed(KeyCode::Space) { - direction += double_tf.up(); + direction += transform.up().as_vec3(); } if keyboard_input.pressed(KeyCode::ShiftLeft) || keyboard_input.pressed(KeyCode::ShiftRight) { - direction -= double_tf.up(); + direction -= transform.up().as_vec3(); } // Normalize direction if needed @@ -143,7 +136,7 @@ pub fn camera_controller_system( // Apply movement in double-precision let delta_seconds = time.delta_secs_f64(); let distance = controller.speed as f64 * delta_seconds; - double_tf.translation += direction * distance; + transform.translation += direction * distance as f32; @@ -185,7 +178,7 @@ pub fn camera_controller_system( } if keyboard_input.just_pressed(KeyCode::KeyQ) && window.cursor_options.visible == false{ for mut octree in octree_query.iter_mut() { - octree.insert(double_tf.translation.x as f64, double_tf.translation.y as f64, double_tf.translation.z as f64, Voxel::new(Color::srgb(1.0, 0.0, 0.0))); + octree.insert(transform.translation.x, transform.translation.y, transform.translation.z, Voxel::new(Color::srgb(1.0, 0.0, 0.0))); } } @@ -198,12 +191,12 @@ pub fn camera_controller_system( // Get the mouse position in normalized device coordinates (-1 to 1) if let Some(_) = window.cursor_position() { // Set the ray direction to the camera's forward vector - let ray_origin = world_offset.0 + double_tf.translation; - let ray_direction = double_tf.forward().normalize(); + let ray_origin = transform.translation; + let ray_direction = transform.forward().normalize(); let ray = Ray { - origin: ray_origin.as_vec3(), - direction: ray_direction.as_vec3(), + origin: ray_origin, + direction: ray_direction, }; @@ -229,17 +222,6 @@ pub fn camera_controller_system( BLUE, );*/ - let chunk = octree.compute_chunk_coords(hit_x, hit_y, hit_z); - - info!("Chunk Hit: {},{},{}", chunk.0, chunk.1, chunk.2); - - if let Some(chunk_node) = octree.get_chunk_node(hit_x,hit_y,hit_z) { - let has_volume = octree.has_volume(chunk_node); - - info!("Chunk Has Volume: {}", has_volume); - } - - @@ -258,9 +240,9 @@ pub fn camera_controller_system( // Align the offset position to the center of the nearest voxel let (new_voxel_x, new_voxel_y, new_voxel_z) = octree.normalize_to_voxel_at_depth( - offset_position.x as f64, - offset_position.y as f64, - offset_position.z as f64, + offset_position.x, + offset_position.y, + offset_position.z, depth, ); @@ -278,9 +260,9 @@ pub fn camera_controller_system( // Align the offset position to the center of the nearest voxel let (new_voxel_x, new_voxel_y, new_voxel_z) = octree.normalize_to_voxel_at_depth( - offset_position.x as f64, - offset_position.y as f64, - offset_position.z as f64, + offset_position.x, + offset_position.y, + offset_position.z, depth, ); @@ -305,14 +287,6 @@ pub fn camera_controller_system( app_exit_events.send(Default::default()); } - // ============================================= - // 8) Convert DoubleTransform -> Bevy Transform - // ============================================= - // The final step is to update the f32 `Transform` that Bevy uses for rendering. - // This ensures the camera is visually placed at the correct position. - render_tf.translation = double_tf.translation.as_vec3(); - render_tf.rotation = double_tf.rotation.as_quat(); - render_tf.scale = double_tf.scale.as_vec3(); } \ No newline at end of file diff --git a/src/systems/environment_system.rs b/src/systems/environment_system.rs index b9b5936..3e75eb5 100644 --- a/src/systems/environment_system.rs +++ b/src/systems/environment_system.rs @@ -2,7 +2,6 @@ use bevy::color::palettes::basic::*; use bevy::color::palettes::css::{BEIGE, MIDNIGHT_BLUE, ORANGE, ORANGE_RED, SEA_GREEN}; use bevy::math::*; use bevy::prelude::*; -use crate::helper::large_transform::DoubleTransform; use crate::systems::voxels::structure::{SparseVoxelOctree, Voxel}; /*pub fn setup( mut commands: Commands, @@ -15,7 +14,7 @@ use crate::systems::voxels::structure::{SparseVoxelOctree, Voxel}; DoubleTransform { translation: DVec3::new(0.0, 0.0, 10.0), // rotate -90 degrees around X so the circle is on the XY plane - rotation: DQuat::from_euler(EulerRot::XYZ, -std::f64::consts::FRAC_PI_2, 0.0, 0.0), + rotation: DQuat::from_euler(EulerRot::XYZ, -std::f32::consts::FRAC_PI_2, 0.0, 0.0), scale: DVec3::ONE, }, // Bevy's transform components @@ -65,7 +64,7 @@ pub fn setup(mut commands: Commands,) { let voxels_per_unit = 16; let unit_size = 1.0; // 1 unit in your coordinate space - let voxel_size = unit_size / voxels_per_unit as f64; + let voxel_size = unit_size / voxels_per_unit as f32; /*//Octree let octree_base_size = 64.0; @@ -76,7 +75,7 @@ pub fn setup(mut commands: Commands,) { let octree_depth = 10; - let mut octree = SparseVoxelOctree::new(octree_depth, octree_base_size, false, false, false); + let mut octree = SparseVoxelOctree::new(octree_depth, octree_base_size as f32, false, false, false); let color = Color::rgb(0.2, 0.8, 0.2); @@ -90,11 +89,6 @@ pub fn setup(mut commands: Commands,) { commands.spawn( ( - DoubleTransform { - translation: DVec3::new(0.0, 0.0, 0.0), - rotation: DQuat::IDENTITY, - scale: DVec3::ONE, - }, Transform::default(), octree ) @@ -104,11 +98,6 @@ pub fn setup(mut commands: Commands,) { commands.spawn(( Transform::default(), GlobalTransform::default(), - DoubleTransform { - translation: DVec3::new(0.0, 0.0, 0.0), - rotation: DQuat::IDENTITY, - scale: DVec3::ONE, - }, PointLight { shadows_enabled: true, ..default() @@ -125,30 +114,30 @@ pub fn setup(mut commands: Commands,) { /// - `voxel_step`: how finely to sample the sphere in the x/y/z loops fn generate_voxel_sphere( octree: &mut SparseVoxelOctree, - planet_radius: f64, + planet_radius: i32, voxel_color: Color, ) { // For simplicity, we center the sphere around (0,0,0). // We'll loop over a cubic region [-planet_radius, +planet_radius] in x, y, z - let min = -(planet_radius as i64); - let max = planet_radius as i64; + let min = -planet_radius; + let max = planet_radius; let step = octree.get_spacing_at_depth(octree.max_depth); for ix in min..=max { - let x = ix as f64; + let x = ix; for iy in min..=max { - let y = iy as f64; + let y = iy; for iz in min..=max { - let z = iz as f64; + let z = iz; // Check if within sphere of radius `planet_radius` let dist2 = x * x + y * y + z * z; if dist2 <= planet_radius * planet_radius { // Convert (x,y,z) to world space, stepping by `voxel_step`. - let wx = x * step; - let wy = y * step; - let wz = z * step; + let wx = x as f32 * step; + let wy = y as f32 * step; + let wz = z as f32 * step; // Insert the voxel let voxel = Voxel { @@ -180,11 +169,11 @@ fn generate_voxel_rect( // Triple-nested loop for each voxel in [0..16, 0..256, 0..16] for ix in 0..size_x { - let x = ix as f64; + let x = ix as f32; for iy in 0..size_y { - let y = iy as f64; + let y = iy as f32; for iz in 0..size_z { - let z = iz as f64; + let z = iz as f32; // Convert (x,y,z) to world coordinates let wx = x * step; @@ -216,9 +205,9 @@ fn generate_large_plane( // Double-nested loop for each voxel in [0..width, 0..depth], // with y=0. for ix in 0..width { - let x = ix as f64; + let x = ix as f32; for iz in 0..depth { - let z = iz as f64; + let z = iz as f32; // y is always 0. let y = 0.0; diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 3f17c35..5f40e3a 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -2,4 +2,4 @@ pub mod camera_system; pub mod ui_system; pub mod environment_system; -pub mod voxels; \ No newline at end of file +pub mod voxels; diff --git a/src/systems/ui_system.rs b/src/systems/ui_system.rs index dd1cacc..08ad48e 100644 --- a/src/systems/ui_system.rs +++ b/src/systems/ui_system.rs @@ -1,6 +1,5 @@ use bevy::asset::AssetServer; use bevy::prelude::*; -use crate::helper::large_transform::{DoubleTransform, WorldOffset}; use crate::systems::camera_system::CameraController; use crate::systems::voxels::structure::{SparseVoxelOctree}; @@ -44,47 +43,23 @@ pub fn update( // Query the camera controller so we can see its speed query_camera_controller: Query<&CameraController>, // We also query for the camera's f32 `Transform` and the double `DoubleTransform` - camera_query: Query<(&Transform, &DoubleTransform, &Camera)>, - // The global offset resource, if you have one - world_offset: Res, - // The chunk-size logic from the octree, so we can compute chunk coords - octree_query: Query<&SparseVoxelOctree>, // or get_single if there's only one octree + camera_query: Query<(&Transform, &Camera)>, // The UI text entity mut query_text: Query<&mut Text, With>, ) { let camera_controller = query_camera_controller.single(); - let (transform, double_tf, _camera) = camera_query.single(); + let (transform, _camera) = camera_query.single(); let mut text = query_text.single_mut(); - // The global double position: offset + camera's double translation - let global_pos = world_offset.0 + double_tf.translation; - // We'll attempt to get the octree so we can compute chunk coords - // If there's no octree, we just show "N/A". - /*let (chunk_cx, chunk_cy, chunk_cz) = if let Ok(octree) = octree_query.get_single() { - // 1) get voxel step - let step = octree.get_spacing_at_depth(octree.max_depth); - // 2) chunk world size - let chunk_world_size = CHUNK_SIZE as f64 * step; - // 3) compute chunk coords using global_pos - let cx = ((global_pos.x) / chunk_world_size).floor() as i32; - let cy = ((global_pos.y) / chunk_world_size).floor() as i32; - let cz = ((global_pos.z) / chunk_world_size).floor() as i32; - (cx, cy, cz) - } else { - (0, 0, 0) // or default - };*/ // Format the string to show speed, positions, and chunk coords text.0 = format!( - "\n Speed: {:.3}\n Position(f32): ({:.2},{:.2},{:.2})\n Position(f64): ({:.2},{:.2},{:.2})", + "\n Speed: {:.3}\n Position(f32): ({:.2},{:.2},{:.2})", camera_controller.speed, transform.translation.x, transform.translation.y, transform.translation.z, - global_pos.x, - global_pos.y, - global_pos.z, ); } \ No newline at end of file diff --git a/src/systems/voxels/debug.rs b/src/systems/voxels/debug.rs index cdcc0e6..950332c 100644 --- a/src/systems/voxels/debug.rs +++ b/src/systems/voxels/debug.rs @@ -1,29 +1,26 @@ use bevy::color::palettes::basic::{BLACK, RED, YELLOW}; use bevy::color::palettes::css::GREEN; -use bevy::math::{DQuat, DVec3, Vec3}; +use bevy::math::{DQuat, Vec3}; use bevy::pbr::wireframe::Wireframe; use bevy::prelude::*; use bevy::render::mesh::{Indices, PrimitiveTopology}; use bevy::render::render_asset::RenderAssetUsages; -use bevy_egui::egui::emath::Numeric; -use bevy_render::prelude::*; -use crate::helper::large_transform::DoubleTransform; -use crate::systems::voxels::structure::{ChunkEntities, OctreeNode, SparseVoxelOctree}; +use bevy_egui::egui::emath::Numeric; +use crate::systems::voxels::structure::{ OctreeNode, SparseVoxelOctree}; pub fn visualize_octree( mut gizmos: Gizmos, - camera_query: Query<&DoubleTransform, With>, - octree_query: Query<(&SparseVoxelOctree, &DoubleTransform)>, + camera_query: Query<&Transform, With>, + octree_query: Query<(&SparseVoxelOctree, &Transform)>, ) { let camera_tf = camera_query.single(); // your "real" camera position in double precision let camera_pos = camera_tf.translation; // DVec3 for (octree, octree_tf) in octree_query.iter() { - let octree_world_pos = octree_tf.translation; visualize_recursive( &mut gizmos, &octree.root, - octree_world_pos, // octree’s root center + octree_tf.translation, // octree’s root center octree.size, octree.max_depth, camera_pos, @@ -34,10 +31,10 @@ pub fn visualize_octree( fn visualize_recursive( gizmos: &mut Gizmos, node: &OctreeNode, - node_center: DVec3, - node_size: f64, + node_center: Vec3, + node_size: f32, depth: u32, - camera_pos: DVec3, + camera_pos: Vec3, ) { if depth == 0 { return; @@ -46,26 +43,30 @@ fn visualize_recursive( // If you want to draw the bounding box of this node: /*let half = node_size as f32 * 0.5;*/ // Convert double center -> local f32 position - let center_f32 = (node_center - camera_pos).as_vec3(); + let center_f32 = (node_center - camera_pos); // A quick approach: draw a wireframe cube by drawing lines for each edge // Or use "cuboid gizmo" methods in future bevy versions that might exist. /*draw_wire_cube(gizmos, center_f32, half, Color::YELLOW);*/ + + gizmos.cuboid( - Transform::from_translation(center_f32).with_scale(Vec3::splat(node_size as f32)), + Transform::from_translation(center_f32).with_scale(Vec3::splat(node_size)), BLACK, ); // Recurse children if let Some(children) = &node.children { let child_size = node_size / 2.0; + + for (i, child) in children.iter().enumerate() { let offset_x = if (i & 1) == 1 { child_size / 2.0 } else { -child_size / 2.0 }; let offset_y = if (i & 2) == 2 { child_size / 2.0 } else { -child_size / 2.0 }; let offset_z = if (i & 4) == 4 { child_size / 2.0 } else { -child_size / 2.0 }; - let child_center = DVec3::new( + let child_center = Vec3::new( node_center.x + offset_x, node_center.y + offset_y, node_center.z + offset_z, @@ -87,8 +88,8 @@ fn visualize_recursive( #[allow(dead_code)] pub fn draw_grid( mut gizmos: Gizmos, - camera_query: Query<&DoubleTransform, With>, - octree_query: Query<(&SparseVoxelOctree, &DoubleTransform)>, + camera_query: Query<&Transform, With>, + octree_query: Query<(&SparseVoxelOctree, &Transform)>, ) { // 1) Get the camera’s double transform for offset let camera_tf = camera_query.single(); @@ -100,19 +101,19 @@ pub fn draw_grid( // 2) Octree’s double position let octree_pos = octree_dtf.translation; // e.g. [100_000, 0, 0] in double space - // 3) Compute spacing in f64 - let grid_spacing = octree.get_spacing_at_depth(octree.max_depth) as f64; + // 3) Compute spacing in f32 + let grid_spacing = octree.get_spacing_at_depth(octree.max_depth) as f32; let grid_size = (octree.size / grid_spacing) as i32; // 4) Start position in local "octree space" // We'll define the bounding region from [-size/2, +size/2] let half_size = octree.size * 0.5; - let start_position = -half_size; // f64 + let start_position = -half_size; // f32 // 5) Loop over lines for i in 0..=grid_size { // i-th line offset - let offset = i as f64 * grid_spacing; + let offset = i as f32 * grid_spacing; // a) Lines along Z // from (start_position + offset, 0, start_position) @@ -120,15 +121,15 @@ pub fn draw_grid( { let x = start_position + offset; let z1 = start_position; - let z2 = start_position + (grid_size as f64 * grid_spacing); + let z2 = start_position + (grid_size as f32 * grid_spacing); // Convert these points to "world double" by adding octree_pos - let p1_d = DVec3::new(x, 0.0, z1) + octree_pos; - let p2_d = DVec3::new(x, 0.0, z2) + octree_pos; + let p1_d = Vec3::new(x, 0.0, z1) + octree_pos; + let p2_d = Vec3::new(x, 0.0, z2) + octree_pos; // Then offset by camera_pos, convert to f32 - let p1_f32 = (p1_d - camera_pos).as_vec3(); - let p2_f32 = (p2_d - camera_pos).as_vec3(); + let p1_f32 = (p1_d - camera_pos); + let p2_f32 = (p2_d - camera_pos); // Draw the line gizmos.line(p1_f32, p2_f32, Color::WHITE); @@ -140,221 +141,16 @@ pub fn draw_grid( { let z = start_position + offset; let x1 = start_position; - let x2 = start_position + (grid_size as f64 * grid_spacing); + let x2 = start_position + (grid_size as f32 * grid_spacing); - let p1_d = DVec3::new(x1, 0.0, z) + octree_pos; - let p2_d = DVec3::new(x2, 0.0, z) + octree_pos; + let p1_d = Vec3::new(x1, 0.0, z) + octree_pos; + let p2_d = Vec3::new(x2, 0.0, z) + octree_pos; - let p1_f32 = (p1_d - camera_pos).as_vec3(); - let p2_f32 = (p2_d - camera_pos).as_vec3(); + let p1_f32 = (p1_d - camera_pos); + let p2_f32 = (p2_d - camera_pos); gizmos.line(p1_f32, p2_f32, Color::WHITE); } } } } - -/*#[derive(Component)] -pub struct GridMarker; - -pub fn draw_grid( - mut commands: Commands, - mut meshes: ResMut>, - mut materials: ResMut>, - query: Query<(Entity, &SparseVoxelOctree)>, // Query to access the octree - grid_query: Query>, // Query to find existing grid entities -) { - for (_, octree) in query.iter() { - if octree.show_world_grid { - // If grid should be shown, check if it already exists - if grid_query.iter().next().is_none() { - // Grid doesn't exist, so create it - let grid_spacing = octree.get_spacing_at_depth(octree.max_depth) as f32; // Get spacing at the specified depth - let grid_size = (octree.size / grid_spacing as f64) as i32; // Determine the number of lines needed - - let mut positions = Vec::new(); - let mut indices = Vec::new(); - - // Calculate the start position to center the grid - let start_position = -(octree.size as f32 / 2.0); - - // Create lines along the X and Z axes based on calculated spacing - for i in 0..=grid_size { - // Lines along the Z-axis - positions.push([start_position + i as f32 * grid_spacing, 0.0, start_position]); - positions.push([start_position + i as f32 * grid_spacing, 0.0, start_position + grid_size as f32 * grid_spacing]); - - // Indices for the Z-axis lines - let base_index = (i * 2) as u32; - indices.push(base_index); - indices.push(base_index + 1); - - // Lines along the X-axis - positions.push([start_position, 0.0, start_position + i as f32 * grid_spacing]); - positions.push([start_position + grid_size as f32 * grid_spacing, 0.0, start_position + i as f32 * grid_spacing]); - - // Indices for the X-axis lines - let base_index_x = ((grid_size + 1 + i) * 2) as u32; - indices.push(base_index_x); - indices.push(base_index_x + 1); - } - - // Create the line mesh - let mut mesh = Mesh::new(PrimitiveTopology::LineList, RenderAssetUsages::default()); - mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions); - mesh.insert_indices(Indices::U32(indices)); - - - let color = bevy::color::Color::srgba(204.0 / 255.0, 0.0, 218.0 / 255.0, 15.0 / 255.0); - - - // Spawn the entity with the line mesh - commands.spawn(PbrBundle { - mesh: meshes.add(mesh).into(), - material: materials.add(StandardMaterial { - base_color: Color::WHITE, - unlit: true, // Makes the lines visible without lighting - ..Default::default() - }).into(), - transform: Transform::default(), - ..Default::default() - }) - .insert(GridMarker); // Add a marker component to identify the grid - } - } else { - // If grid should not be shown, remove any existing grid - for grid_entity in grid_query.iter() { - commands.entity(grid_entity).despawn(); - } - } - } -} -*/ - -/*#[derive(Component)] -pub struct BuildVisualization; - -#[derive(Debug)] -pub struct EphemeralLine { - pub start: Vec3, - pub end: Vec3, - pub color: Color, - pub time_left: f32, // in seconds -} - -#[derive(Resource, Default)] -pub struct EphemeralLines { - pub lines: Vec, -} - -pub fn ephemeral_lines_system( - mut lines: ResMut, - mut gizmos: Gizmos, - time: Res