diff --git a/Cargo.lock b/Cargo.lock index caf296d..0c8b2d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3662,6 +3662,7 @@ dependencies = [ "bevy_asset_loader", "bevy_rapier3d", "buildings", + "image 0.25.2", "iyes_perf_ui", "noise 0.8.2", "rayon", diff --git a/engine/world_generation/src/biome_painter.rs b/engine/world_generation/src/biome_painter.rs index 802d3fc..0452d9d 100644 --- a/engine/world_generation/src/biome_painter.rs +++ b/engine/world_generation/src/biome_painter.rs @@ -38,7 +38,7 @@ impl BiomePainterAsset { } } -#[derive(Resource)] +#[derive(Resource, Clone)] pub struct BiomePainter { pub biomes: Vec, } diff --git a/engine/world_generation/src/map/config.rs b/engine/world_generation/src/map/config.rs index 86fe848..84c22b4 100644 --- a/engine/world_generation/src/map/config.rs +++ b/engine/world_generation/src/map/config.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; use super::chunk::Chunk; -#[derive(Resource, Reflect, Default)] +#[derive(Resource, Reflect, Default, Clone)] #[reflect(Resource)] pub struct GenerationConfig { pub sea_level: f64, diff --git a/engine/world_generation/src/map/map_utils.rs b/engine/world_generation/src/map/map_utils.rs index 6e6ebb6..3dc58e6 100644 --- a/engine/world_generation/src/map/map_utils.rs +++ b/engine/world_generation/src/map/map_utils.rs @@ -11,7 +11,7 @@ pub fn render_image( data: &Vec, color1: LinearRgba, color2: LinearRgba, -) -> ImageBuffer, Vec> { +) -> ImageBuffer, Vec> { let mut image = ImageBuffer::new(size.x * Chunk::SIZE as u32, size.y * Chunk::SIZE as u32); update_image(size, data, color1, color2, &mut image); @@ -23,7 +23,7 @@ pub fn update_image( data: &Vec, color1: LinearRgba, color2: LinearRgba, - image: &mut ImageBuffer, Vec>, + image: &mut ImageBuffer, Vec>, ) { let min = *data.iter().min_by(|a, b| a.partial_cmp(b).unwrap()).unwrap_or(&0.0); let max = *data.iter().min_by(|a, b| a.partial_cmp(b).unwrap()).unwrap_or(&1.0); @@ -39,14 +39,15 @@ pub fn update_image( }); } -fn to_pixel(col: &LinearRgba) -> image::Rgb { - return image::Rgb([ +fn to_pixel(col: &LinearRgba) -> image::Rgba { + return image::Rgba([ (col.red * 255.0) as u8, (col.green * 255.0) as u8, (col.blue * 255.0) as u8, + 255, ]); } -pub fn render_map(map: &Map, smooth: f32) -> ImageBuffer, Vec> { +pub fn render_map(map: &Map, smooth: f32) -> ImageBuffer, Vec> { let mut image = ImageBuffer::new( map.width as u32 * Chunk::SIZE as u32, map.height as u32 * Chunk::SIZE as u32, @@ -54,7 +55,7 @@ pub fn render_map(map: &Map, smooth: f32) -> ImageBuffer, Vec update_map(map, smooth, &mut image); return image; } -pub fn update_map(map: &Map, smooth: f32, image: &mut ImageBuffer, Vec>) { +pub fn update_map(map: &Map, smooth: f32, image: &mut ImageBuffer, Vec>) { image.par_enumerate_pixels_mut().for_each(|(x, y, pixel)| { let coord = HexCoord::from_grid_pos(x as usize, y as usize); let right = coord.get_neighbor(1); @@ -91,7 +92,7 @@ pub fn update_map(map: &Map, smooth: f32, image: &mut ImageBuffer }); } -pub fn render_biome_map(map: &Map) -> ImageBuffer, Vec> { +pub fn render_biome_map(map: &Map) -> ImageBuffer, Vec> { let mut image = ImageBuffer::new( map.width as u32 * Chunk::SIZE as u32, map.height as u32 * Chunk::SIZE as u32, @@ -100,7 +101,7 @@ pub fn render_biome_map(map: &Map) -> ImageBuffer, Vec> { return image; } -pub fn update_biome_map(map: &Map, image: &mut ImageBuffer, Vec>) { +pub fn update_biome_map(map: &Map, image: &mut ImageBuffer, Vec>) { image.par_enumerate_pixels_mut().for_each(|(x, y, pixel)| { let coord = HexCoord::from_grid_pos(x as usize, y as usize); let tile = map.get_biome(&coord); diff --git a/game/main/Cargo.toml b/game/main/Cargo.toml index 657cb9c..c7c66ae 100644 --- a/game/main/Cargo.toml +++ b/game/main/Cargo.toml @@ -21,6 +21,7 @@ bevy_asset_loader = { version = "0.21.0", features = [ "3d", ] } ron = "0.8.1" +image = "0.25.2" [features] tracing = ["bevy/trace_tracy", "world_generation/tracing", "buildings/tracing"] diff --git a/game/main/src/map_rendering/map_init.rs b/game/main/src/map_rendering/map_init.rs index b6b7429..0a7fcd8 100644 --- a/game/main/src/map_rendering/map_init.rs +++ b/game/main/src/map_rendering/map_init.rs @@ -3,10 +3,12 @@ use bevy::log::*; use bevy::{ pbr::{ExtendedMaterial, NotShadowCaster}, prelude::*, + render::texture::ImageFormat, }; use bevy_asset_loader::prelude::*; use bevy_inspector_egui::quick::ResourceInspectorPlugin; +use image::DynamicImage; use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use shared::states::{AssetLoadState, GameplayState, MenuState}; use world_generation::{ @@ -213,11 +215,6 @@ fn create_heightmap( }; let heightmap = generate_heightmap(&config, 42069, &biome_painter); - let game_map = render_map(&heightmap, 1.5); - let biome_map = render_biome_map(&heightmap); - _ = biome_map.save("Biomes.png"); - _ = game_map.save("Test.png"); - let (mut cam_t, cam_entity) = cam.single_mut(); cam_t.translation = heightmap.get_center(); diff --git a/game/main/src/phos.rs b/game/main/src/phos.rs index aba0cf5..7ed8296 100644 --- a/game/main/src/phos.rs +++ b/game/main/src/phos.rs @@ -1,5 +1,6 @@ use crate::camera_system::components::PhosCamera; use crate::map_rendering::map_init::MapInitPlugin; +use crate::utlis::editor_plugin::EditorPlugin; use crate::utlis::render_distance_system::RenderDistancePlugin; use crate::{camera_system::camera_plugin::PhosCameraPlugin, utlis::debug_plugin::DebugPlugin}; use bevy::{ @@ -34,6 +35,8 @@ impl Plugin for PhosGamePlugin { BuildingPugin, DespawnPuglin, #[cfg(debug_assertions)] + EditorPlugin, + #[cfg(debug_assertions)] DebugPlugin, )); diff --git a/game/main/src/utlis/editor_plugin.rs b/game/main/src/utlis/editor_plugin.rs new file mode 100644 index 0000000..2460c55 --- /dev/null +++ b/game/main/src/utlis/editor_plugin.rs @@ -0,0 +1,49 @@ +use bevy::{prelude::*, render::render_asset::RenderAssetUsages}; +use bevy_inspector_egui::bevy_egui::EguiContexts; +use bevy_inspector_egui::egui::{self}; +use world_generation::{map::map_utils::render_map, prelude::Map, states::GeneratorState}; + +pub struct EditorPlugin; + +impl Plugin for EditorPlugin { + fn build(&self, app: &mut App) { + app.init_resource::(); + + app.add_systems(PostUpdate, prepare_image.run_if(in_state(GeneratorState::SpawnMap))); + app.add_systems(Update, (render_map_ui).run_if(in_state(GeneratorState::Idle))); + } +} + +#[derive(Resource)] +struct MapImage(pub Handle); + +pub fn prepare_image(mut images: ResMut>, heightmap: Res, mut commands: Commands) { + let image = render_map(&heightmap, 0.1); + let handle = images.add(Image::from_dynamic(image.into(), true, RenderAssetUsages::RENDER_WORLD)); + + commands.insert_resource(MapImage(handle)); +} + +#[derive(Resource)] +struct UIState { + pub is_open: bool, +} + +impl Default for UIState { + fn default() -> Self { + Self { is_open: true } + } +} + +fn render_map_ui(image: Res, mut contexts: EguiContexts, mut state: ResMut) { + let id = contexts.add_image(image.0.clone_weak()); + + let ctx = contexts.ctx_mut(); + egui::Window::new("Map").open(&mut state.is_open).show(ctx, |ui| { + ui.label("Map Test"); + ui.add(egui::widgets::Image::new(egui::load::SizedTexture::new( + id, + [512.0, 512.0], + ))); + }); +} diff --git a/game/main/src/utlis/mod.rs b/game/main/src/utlis/mod.rs index b2154a8..c307916 100644 --- a/game/main/src/utlis/mod.rs +++ b/game/main/src/utlis/mod.rs @@ -1,3 +1,4 @@ pub mod chunk_utils; pub mod render_distance_system; pub mod debug_plugin; +pub mod editor_plugin;