From 0979da0806bea5e2e9b95ddc049c8adb8dd698e6 Mon Sep 17 00:00:00 2001 From: Manan Karnik Date: Sun, 24 Dec 2023 20:10:27 +0530 Subject: [PATCH] feat: add terrain sea level and height exponent --- index.html | 2 +- src/main.rs | 33 +++++++++------------------------ src/terrain.rs | 11 ++++++++++- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/index.html b/index.html index e1a6135..741bf90 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,7 @@ - + diff --git a/src/main.rs b/src/main.rs index 5b045e4..e68e9f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,4 @@ -use bevy::{ - core_pipeline::{bloom::BloomSettings, tonemapping::Tonemapping}, - pbr::wireframe::WireframePlugin, - prelude::*, -}; +use bevy::{pbr::wireframe::WireframePlugin, prelude::*}; use bevy_egui::{ egui::{self, RichText}, EguiContexts, EguiPlugin, @@ -45,18 +41,14 @@ fn main() { .run(); } -fn setup( - mut commands: Commands, - _meshes: ResMut>, - _materials: ResMut>, -) { - commands.spawn(PointLightBundle { - point_light: PointLight { - intensity: 1500.0, - shadows_enabled: false, +fn setup(mut commands: Commands) { + commands.spawn(DirectionalLightBundle { + directional_light: DirectionalLight { + color: Color::WHITE, + illuminance: 10000.0, ..default() }, - transform: Transform::from_xyz(0.0, 10.0, 0.0), + transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), ..default() }); commands.spawn(( @@ -68,15 +60,6 @@ fn setup( ), ..default() }, - camera: Camera { - hdr: true, - ..default() - }, - tonemapping: Tonemapping::TonyMcMapface, - ..default() - }, - BloomSettings { - intensity: 0.4, ..default() }, PanOrbitCamera::default(), @@ -242,6 +225,8 @@ fn gui(mut contexts: EguiContexts, mut query: Query<&mut Terrain>) { ui.label("Base Color"); ui.color_edit_button_srgba_unmultiplied(&mut terrain.noise.base_color); }); + ui.add(Slider::new(&mut terrain.height_exponent, 0.1..=2.5).text("Height Exponent")); + ui.add(Slider::new(&mut terrain.sea_level, 0.0..=100.0).text("Sea Level")); ui.separator(); if ui.button("Add Region").clicked() { let index = terrain.noise.regions.len() + 1; diff --git a/src/terrain.rs b/src/terrain.rs index cf5c2bd..ee11f2a 100644 --- a/src/terrain.rs +++ b/src/terrain.rs @@ -13,6 +13,8 @@ use crate::{noise::generate_noise_map, noise::Noise, util::export_terrain}; pub struct Terrain { pub noise: Noise, pub wireframe: bool, + pub height_exponent: f32, + pub sea_level: f32, pub export: bool, } @@ -21,6 +23,8 @@ impl Default for Terrain { Self { noise: Noise::default(), wireframe: false, + height_exponent: 1.0, + sea_level: 10.0, export: false, } } @@ -117,7 +121,12 @@ fn generate_terrain( let width = rows as f32; let depth = cols as f32; let x = i / grid_size - width / (grid_size * 2.0); - let y = (noise_values[i as usize][j as usize] / 100.0) as f32; + let y = if noise_values[i as usize][j as usize] > terrain.sea_level.into() { + (noise_values[i as usize][j as usize] * 5.0 / 100.0) + .powf(terrain.height_exponent.into()) as f32 + } else { + (terrain.sea_level * 5.0 / 100.0).powf(terrain.height_exponent) + }; let z = j / grid_size - depth / (grid_size * 2.0); let color = grad.at(noise_values[i as usize][j as usize]);