render map image
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -3662,6 +3662,7 @@ dependencies = [
|
|||||||
"bevy_asset_loader",
|
"bevy_asset_loader",
|
||||||
"bevy_rapier3d",
|
"bevy_rapier3d",
|
||||||
"buildings",
|
"buildings",
|
||||||
|
"image 0.25.2",
|
||||||
"iyes_perf_ui",
|
"iyes_perf_ui",
|
||||||
"noise 0.8.2",
|
"noise 0.8.2",
|
||||||
"rayon",
|
"rayon",
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ impl BiomePainterAsset {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource)]
|
#[derive(Resource, Clone)]
|
||||||
pub struct BiomePainter {
|
pub struct BiomePainter {
|
||||||
pub biomes: Vec<BiomeAsset>,
|
pub biomes: Vec<BiomeAsset>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
|
|
||||||
use super::chunk::Chunk;
|
use super::chunk::Chunk;
|
||||||
|
|
||||||
#[derive(Resource, Reflect, Default)]
|
#[derive(Resource, Reflect, Default, Clone)]
|
||||||
#[reflect(Resource)]
|
#[reflect(Resource)]
|
||||||
pub struct GenerationConfig {
|
pub struct GenerationConfig {
|
||||||
pub sea_level: f64,
|
pub sea_level: f64,
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ pub fn render_image(
|
|||||||
data: &Vec<f32>,
|
data: &Vec<f32>,
|
||||||
color1: LinearRgba,
|
color1: LinearRgba,
|
||||||
color2: LinearRgba,
|
color2: LinearRgba,
|
||||||
) -> ImageBuffer<image::Rgb<u8>, Vec<u8>> {
|
) -> ImageBuffer<image::Rgba<u8>, Vec<u8>> {
|
||||||
let mut image = ImageBuffer::new(size.x * Chunk::SIZE as u32, size.y * Chunk::SIZE as u32);
|
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);
|
update_image(size, data, color1, color2, &mut image);
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ pub fn update_image(
|
|||||||
data: &Vec<f32>,
|
data: &Vec<f32>,
|
||||||
color1: LinearRgba,
|
color1: LinearRgba,
|
||||||
color2: LinearRgba,
|
color2: LinearRgba,
|
||||||
image: &mut ImageBuffer<image::Rgb<u8>, Vec<u8>>,
|
image: &mut ImageBuffer<image::Rgba<u8>, Vec<u8>>,
|
||||||
) {
|
) {
|
||||||
let min = *data.iter().min_by(|a, b| a.partial_cmp(b).unwrap()).unwrap_or(&0.0);
|
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);
|
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<u8> {
|
fn to_pixel(col: &LinearRgba) -> image::Rgba<u8> {
|
||||||
return image::Rgb([
|
return image::Rgba([
|
||||||
(col.red * 255.0) as u8,
|
(col.red * 255.0) as u8,
|
||||||
(col.green * 255.0) as u8,
|
(col.green * 255.0) as u8,
|
||||||
(col.blue * 255.0) as u8,
|
(col.blue * 255.0) as u8,
|
||||||
|
255,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
pub fn render_map(map: &Map, smooth: f32) -> ImageBuffer<image::Rgb<u8>, Vec<u8>> {
|
pub fn render_map(map: &Map, smooth: f32) -> ImageBuffer<image::Rgba<u8>, Vec<u8>> {
|
||||||
let mut image = ImageBuffer::new(
|
let mut image = ImageBuffer::new(
|
||||||
map.width as u32 * Chunk::SIZE as u32,
|
map.width as u32 * Chunk::SIZE as u32,
|
||||||
map.height 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<image::Rgb<u8>, Vec<u8>
|
|||||||
update_map(map, smooth, &mut image);
|
update_map(map, smooth, &mut image);
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
pub fn update_map(map: &Map, smooth: f32, image: &mut ImageBuffer<image::Rgb<u8>, Vec<u8>>) {
|
pub fn update_map(map: &Map, smooth: f32, image: &mut ImageBuffer<image::Rgba<u8>, Vec<u8>>) {
|
||||||
image.par_enumerate_pixels_mut().for_each(|(x, y, pixel)| {
|
image.par_enumerate_pixels_mut().for_each(|(x, y, pixel)| {
|
||||||
let coord = HexCoord::from_grid_pos(x as usize, y as usize);
|
let coord = HexCoord::from_grid_pos(x as usize, y as usize);
|
||||||
let right = coord.get_neighbor(1);
|
let right = coord.get_neighbor(1);
|
||||||
@@ -91,7 +92,7 @@ pub fn update_map(map: &Map, smooth: f32, image: &mut ImageBuffer<image::Rgb<u8>
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_biome_map(map: &Map) -> ImageBuffer<image::Rgb<u8>, Vec<u8>> {
|
pub fn render_biome_map(map: &Map) -> ImageBuffer<image::Rgba<u8>, Vec<u8>> {
|
||||||
let mut image = ImageBuffer::new(
|
let mut image = ImageBuffer::new(
|
||||||
map.width as u32 * Chunk::SIZE as u32,
|
map.width as u32 * Chunk::SIZE as u32,
|
||||||
map.height 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<image::Rgb<u8>, Vec<u8>> {
|
|||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_biome_map(map: &Map, image: &mut ImageBuffer<image::Rgb<u8>, Vec<u8>>) {
|
pub fn update_biome_map(map: &Map, image: &mut ImageBuffer<image::Rgba<u8>, Vec<u8>>) {
|
||||||
image.par_enumerate_pixels_mut().for_each(|(x, y, pixel)| {
|
image.par_enumerate_pixels_mut().for_each(|(x, y, pixel)| {
|
||||||
let coord = HexCoord::from_grid_pos(x as usize, y as usize);
|
let coord = HexCoord::from_grid_pos(x as usize, y as usize);
|
||||||
let tile = map.get_biome(&coord);
|
let tile = map.get_biome(&coord);
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ bevy_asset_loader = { version = "0.21.0", features = [
|
|||||||
"3d",
|
"3d",
|
||||||
] }
|
] }
|
||||||
ron = "0.8.1"
|
ron = "0.8.1"
|
||||||
|
image = "0.25.2"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
tracing = ["bevy/trace_tracy", "world_generation/tracing", "buildings/tracing"]
|
tracing = ["bevy/trace_tracy", "world_generation/tracing", "buildings/tracing"]
|
||||||
|
|||||||
@@ -3,10 +3,12 @@ use bevy::log::*;
|
|||||||
use bevy::{
|
use bevy::{
|
||||||
pbr::{ExtendedMaterial, NotShadowCaster},
|
pbr::{ExtendedMaterial, NotShadowCaster},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
|
render::texture::ImageFormat,
|
||||||
};
|
};
|
||||||
use bevy_asset_loader::prelude::*;
|
use bevy_asset_loader::prelude::*;
|
||||||
|
|
||||||
use bevy_inspector_egui::quick::ResourceInspectorPlugin;
|
use bevy_inspector_egui::quick::ResourceInspectorPlugin;
|
||||||
|
use image::DynamicImage;
|
||||||
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
||||||
use shared::states::{AssetLoadState, GameplayState, MenuState};
|
use shared::states::{AssetLoadState, GameplayState, MenuState};
|
||||||
use world_generation::{
|
use world_generation::{
|
||||||
@@ -213,11 +215,6 @@ fn create_heightmap(
|
|||||||
};
|
};
|
||||||
let heightmap = generate_heightmap(&config, 42069, &biome_painter);
|
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();
|
let (mut cam_t, cam_entity) = cam.single_mut();
|
||||||
cam_t.translation = heightmap.get_center();
|
cam_t.translation = heightmap.get_center();
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use crate::camera_system::components::PhosCamera;
|
use crate::camera_system::components::PhosCamera;
|
||||||
use crate::map_rendering::map_init::MapInitPlugin;
|
use crate::map_rendering::map_init::MapInitPlugin;
|
||||||
|
use crate::utlis::editor_plugin::EditorPlugin;
|
||||||
use crate::utlis::render_distance_system::RenderDistancePlugin;
|
use crate::utlis::render_distance_system::RenderDistancePlugin;
|
||||||
use crate::{camera_system::camera_plugin::PhosCameraPlugin, utlis::debug_plugin::DebugPlugin};
|
use crate::{camera_system::camera_plugin::PhosCameraPlugin, utlis::debug_plugin::DebugPlugin};
|
||||||
use bevy::{
|
use bevy::{
|
||||||
@@ -34,6 +35,8 @@ impl Plugin for PhosGamePlugin {
|
|||||||
BuildingPugin,
|
BuildingPugin,
|
||||||
DespawnPuglin,
|
DespawnPuglin,
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
|
EditorPlugin,
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
DebugPlugin,
|
DebugPlugin,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|||||||
49
game/main/src/utlis/editor_plugin.rs
Normal file
49
game/main/src/utlis/editor_plugin.rs
Normal file
@@ -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::<UIState>();
|
||||||
|
|
||||||
|
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<Image>);
|
||||||
|
|
||||||
|
pub fn prepare_image(mut images: ResMut<Assets<Image>>, heightmap: Res<Map>, 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<MapImage>, mut contexts: EguiContexts, mut state: ResMut<UIState>) {
|
||||||
|
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],
|
||||||
|
)));
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
pub mod chunk_utils;
|
pub mod chunk_utils;
|
||||||
pub mod render_distance_system;
|
pub mod render_distance_system;
|
||||||
pub mod debug_plugin;
|
pub mod debug_plugin;
|
||||||
|
pub mod editor_plugin;
|
||||||
|
|||||||
Reference in New Issue
Block a user