Add render feature

This commit is contained in:
Tobias Berger 2022-09-05 11:43:50 +02:00
parent 99f4caab54
commit 133798146a
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
8 changed files with 122 additions and 2177 deletions

40
Cargo.lock generated
View file

@ -4,9 +4,9 @@ version = 3
[[package]] [[package]]
name = "ab_glyph" name = "ab_glyph"
version = "0.2.16" version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "846ffacb9d0c8b879ef9e565b59e18fb76d6a61013e5bd24ecc659864e6b1a1f" checksum = "04a9283dace1c41c265496614998d5b9c4a97b3eb770e804f007c5144bf03f2b"
dependencies = [ dependencies = [
"ab_glyph_rasterizer", "ab_glyph_rasterizer",
"owned_ttf_parser", "owned_ttf_parser",
@ -14,9 +14,9 @@ dependencies = [
[[package]] [[package]]
name = "ab_glyph_rasterizer" name = "ab_glyph_rasterizer"
version = "0.1.5" version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a13739d7177fbd22bb0ed28badfff9f372f8bef46c863db4e1c6248f6b223b6e" checksum = "363b9b88fad3af3be80bc8f762c9a3f9dfe906fd0327b8e92f1c12e5ae1b8bbb"
[[package]] [[package]]
name = "ahash" name = "ahash"
@ -31,9 +31,9 @@ dependencies = [
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.7.18" version = "0.7.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" checksum = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
@ -58,9 +58,9 @@ dependencies = [
[[package]] [[package]]
name = "android_system_properties" name = "android_system_properties"
version = "0.1.4" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7ed72e1635e121ca3e79420540282af22da58be50de153d36f81ddc6b83aa9e" checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [ dependencies = [
"libc", "libc",
] ]
@ -76,9 +76,9 @@ dependencies = [
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.62" version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1485d4d2cc45e7b201ee3767015c96faa5904387c9d87c6efdd0fb511f12d305" checksum = "b9a8f622bcf6ff3df478e9deba3e03e4e04b300f8e6a139e192c05fa3490afc7"
[[package]] [[package]]
name = "approx" name = "approx"
@ -1390,9 +1390,9 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]] [[package]]
name = "hexasphere" name = "hexasphere"
version = "7.2.0" version = "7.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9652f2ed7ee9c6374a061039f60fc6e25d7adac7fa10f83365669af3b24b0bf0" checksum = "aaadafd1beb6ad34cff5521987017ece5848f9ad5401fdb039bff896a643add4"
dependencies = [ dependencies = [
"glam", "glam",
"once_cell", "once_cell",
@ -1736,15 +1736,15 @@ dependencies = [
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.13.1" version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0"
[[package]] [[package]]
name = "owned_ttf_parser" name = "owned_ttf_parser"
version = "0.15.1" version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07ef1a404ae479dd6906f4fa2c88b3c94028f1284beb42a47c183a7c27ee9a3e" checksum = "05e6affeb1632d6ff6a23d2cd40ffed138e82f1532571a26f527c8a284bb2fbb"
dependencies = [ dependencies = [
"ttf-parser", "ttf-parser",
] ]
@ -2191,18 +2191,18 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.32" version = "1.0.34"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5f6586b7f764adc0231f4c79be7b920e766bb2f3e51b3661cdb263828f19994" checksum = "8c1b05ca9d106ba7d2e31a9dab4a64e7be2cce415321966ea3132c49a656e252"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.32" version = "1.0.34"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12bafc5b54507e0149cdf1b145a5d80ab80a90bcd9275df43d4fff68460f6c21" checksum = "e8f2591983642de85c921015f3f070c665a197ed69e417af436115e3a1407487"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View file

@ -3,7 +3,10 @@ name = "worlds-sim-rust"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features]
debug = ["save/debug"]
render = ["bevy/bevy_asset", "bevy/bevy_winit", "bevy/bevy_scene", "bevy/render", "save/render"]
default = ["render"]
[dependencies.save] [dependencies.save]
path = "save" path = "save"
@ -11,4 +14,3 @@ path = "save"
[dependencies.bevy] [dependencies.bevy]
version = "0.8" version = "0.8"
default-features = false default-features = false
features = ["bevy_asset", "bevy_winit", "bevy_scene", "render"]

2131
save/Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -3,6 +3,10 @@ name = "save"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[features]
debug = []
render = ["bevy/render"]
[dependencies.noise] [dependencies.noise]
version = "0.7.0" version = "0.7.0"
default-features = false default-features = false

View file

@ -316,6 +316,7 @@ impl World {
Ok(()) Ok(())
} }
/*
fn generate_rainfall(&mut self) -> Result<(), CartesianError> { fn generate_rainfall(&mut self) -> Result<(), CartesianError> {
let offset = Self::random_offset_vector(); let offset = Self::random_offset_vector();
const RADIUS: f32 = 2.0; const RADIUS: f32 = 2.0;
@ -346,4 +347,5 @@ impl World {
((raw_rainfall * Self::RAINFALL_ALTITUDE_FACTOR) + Self::MIN_RAINFALL) ((raw_rainfall * Self::RAINFALL_ALTITUDE_FACTOR) + Self::MIN_RAINFALL)
.clamp(0.0, Self::MAX_RAINFALL) .clamp(0.0, Self::MAX_RAINFALL)
} }
*/
} }

View file

@ -1,4 +1,7 @@
use crate::{TerrainCell, World, WorldGenError}; #[cfg(feature = "render")]
use crate::TerrainCell;
use crate::{World, WorldGenError};
#[cfg(feature = "render")]
use bevy::render::color::Color; use bevy::render::color::Color;
use rand::random; use rand::random;
@ -23,6 +26,7 @@ impl WorldManager {
Ok(self.get_world().unwrap()) Ok(self.get_world().unwrap())
} }
#[cfg(feature = "render")]
fn generate_color(cell: &TerrainCell) -> Color { fn generate_color(cell: &TerrainCell) -> Color {
let altitude_color = Self::altitude_contour_color(cell.altitude); let altitude_color = Self::altitude_contour_color(cell.altitude);
let rainfall_color = Self::rainfall_color(cell.rainfall); let rainfall_color = Self::rainfall_color(cell.rainfall);
@ -39,6 +43,8 @@ impl WorldManager {
Color::rgb_linear(r, g, b) Color::rgb_linear(r, g, b)
} }
/*
#[cfg(feature = "render")]
fn altitude_color(altitude: f32) -> Color { fn altitude_color(altitude: f32) -> Color {
if altitude < 0.0 { if altitude < 0.0 {
Color::rgb(0.0, 0.0, (2.0 - altitude / World::MIN_ALTITUDE) / 2.0) Color::rgb(0.0, 0.0, (2.0 - altitude / World::MIN_ALTITUDE) / 2.0)
@ -48,7 +54,9 @@ impl WorldManager {
Color::rgb(0.58 * mult, 0.29 * mult, 0.0) Color::rgb(0.58 * mult, 0.29 * mult, 0.0)
} }
} }
*/
#[cfg(feature = "render")]
fn altitude_contour_color(altitude: f32) -> Color { fn altitude_contour_color(altitude: f32) -> Color {
if altitude < 0.0 { if altitude < 0.0 {
Color::rgb(0.0, 0.0, (2.0 - altitude / World::MIN_ALTITUDE) / 2.0) Color::rgb(0.0, 0.0, (2.0 - altitude / World::MIN_ALTITUDE) / 2.0)
@ -63,6 +71,7 @@ impl WorldManager {
} }
} }
#[cfg(feature = "render")]
fn rainfall_color(rainfall: f32) -> Color { fn rainfall_color(rainfall: f32) -> Color {
if rainfall <= 0.0 { if rainfall <= 0.0 {
Color::BLACK Color::BLACK
@ -72,6 +81,7 @@ impl WorldManager {
} }
} }
#[cfg(feature = "render")]
fn normalize_rainfall(rainfall: f32) -> f32 { fn normalize_rainfall(rainfall: f32) -> f32 {
if rainfall <= 0.0 { if rainfall <= 0.0 {
rainfall rainfall
@ -80,6 +90,7 @@ impl WorldManager {
} }
} }
#[cfg(feature = "render")]
pub fn world_colors(&self) -> Vec<Color> { pub fn world_colors(&self) -> Vec<Color> {
match self.get_world() { match self.get_world() {
None => panic!("Called world_colors before generating world"), None => panic!("Called world_colors before generating world"),
@ -94,6 +105,7 @@ impl WorldManager {
} }
} }
#[cfg(feature = "render")]
pub fn world_color_bytes(&self) -> Vec<u8> { pub fn world_color_bytes(&self) -> Vec<u8> {
self.world_colors() self.world_colors()
.iter() .iter()

View file

@ -32,11 +32,13 @@
#![warn(unused_results)] #![warn(unused_results)]
#![warn(variant_size_differences)] #![warn(variant_size_differences)]
mod plugins;
use bevy::app::App;
#[cfg(feature = "render")]
use bevy::{ use bevy::{
app::App,
asset::Assets, asset::Assets,
core_pipeline::core_2d::Camera2dBundle, core_pipeline::core_2d::Camera2dBundle,
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
ecs::{ ecs::{
change_detection::ResMut, change_detection::ResMut,
system::{Commands, Res}, system::{Commands, Res},
@ -51,10 +53,11 @@ use bevy::{
utils::default, utils::default,
window::WindowDescriptor, window::WindowDescriptor,
winit::WinitSettings, winit::WinitSettings,
DefaultPlugins,
}; };
use plugins::WorldPlugins;
use save::*; use save::*;
#[cfg(feature = "render")]
fn generate_texture( fn generate_texture(
mut commands: Commands<'_, '_>, mut commands: Commands<'_, '_>,
mut images: ResMut<'_, Assets<Image>>, mut images: ResMut<'_, Assets<Image>>,
@ -92,10 +95,12 @@ fn generate_texture(
} }
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut app = App::new();
let mut manager = WorldManager::new(); let mut manager = WorldManager::new();
#[cfg(feature = "render")]
{
let world = manager.new_world()?; let world = manager.new_world()?;
_ = app
App::new()
// Only run the app when there is user input. This will significantly reduce CPU/GPU use. // Only run the app when there is user input. This will significantly reduce CPU/GPU use.
.insert_resource(WinitSettings::desktop_app()) .insert_resource(WinitSettings::desktop_app())
// Use nearest-neighbor rendering for cripsier pixels // Use nearest-neighbor rendering for cripsier pixels
@ -107,12 +112,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
resizable: true, resizable: true,
..default() ..default()
}) })
.insert_resource(manager) .add_startup_system(generate_texture);
.add_startup_system(generate_texture) }
.add_plugins(DefaultPlugins) #[cfg(not(feature = "render"))]
.add_plugin(LogDiagnosticsPlugin::default()) {
.add_plugin(FrameTimeDiagnosticsPlugin::default()) _ = manager.new_world()?
.run(); }
app.insert_resource(manager).add_plugins(WorldPlugins).run();
Ok(()) Ok(())
} }

50
src/plugins.rs Normal file
View file

@ -0,0 +1,50 @@
pub(crate) struct WorldPlugins;
use bevy::{
app::{PluginGroup, PluginGroupBuilder},
core::CorePlugin,
diagnostic::{DiagnosticsPlugin, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
log::LogPlugin,
time::TimePlugin,
};
impl PluginGroup for WorldPlugins {
fn build(&mut self, group: &mut PluginGroupBuilder) {
_ = group
.add(LogPlugin::default())
.add(CorePlugin::default())
.add(TimePlugin::default());
#[cfg(feature = "render")]
{
use bevy::{
asset::AssetPlugin, core_pipeline::CorePipelinePlugin, input::InputPlugin,
render::RenderPlugin, sprite::SpritePlugin, text::TextPlugin,
transform::TransformPlugin, ui::UiPlugin, window::WindowPlugin, winit::WinitPlugin,
};
_ = group
.add(TransformPlugin::default())
// hierarchy
.add(InputPlugin::default())
.add(WindowPlugin::default())
.add(AssetPlugin::default())
// scene
.add(WinitPlugin::default())
.add(RenderPlugin::default())
.add(CorePipelinePlugin::default())
.add(SpritePlugin::default())
.add(TextPlugin::default())
.add(UiPlugin::default());
}
#[cfg(not(feature = "render"))]
{
use bevy::app::ScheduleRunnerPlugin;
_ = group.add(ScheduleRunnerPlugin::default());
}
_ = group
.add(DiagnosticsPlugin::default())
.add(FrameTimeDiagnosticsPlugin::default())
.add(LogDiagnosticsPlugin::default());
}
}