Skip to content

Commit

Permalink
feat: add terrain sea level and height exponent
Browse files Browse the repository at this point in the history
  • Loading branch information
manankarnik committed Dec 24, 2023
1 parent 386ea78 commit 0979da0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title></title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link data-trunk rel="rust" data-bin="noise_map" />
<link data-trunk rel="rust" data-bin="bevy_generative" />
</head>
<body>
<canvas id="bevy-canvas" oncontextmenu="return false;"></canvas>
Expand Down
33 changes: 9 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -45,18 +41,14 @@ fn main() {
.run();
}

fn setup(
mut commands: Commands,
_meshes: ResMut<Assets<Mesh>>,
_materials: ResMut<Assets<StandardMaterial>>,
) {
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((
Expand All @@ -68,15 +60,6 @@ fn setup(
),
..default()
},
camera: Camera {
hdr: true,
..default()
},
tonemapping: Tonemapping::TonyMcMapface,
..default()
},
BloomSettings {
intensity: 0.4,
..default()
},
PanOrbitCamera::default(),
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion src/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -21,6 +23,8 @@ impl Default for Terrain {
Self {
noise: Noise::default(),
wireframe: false,
height_exponent: 1.0,
sea_level: 10.0,
export: false,
}
}
Expand Down Expand Up @@ -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]);
Expand Down

0 comments on commit 0979da0

Please sign in to comment.