Skip to content

Commit

Permalink
contour lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Dec 15, 2023
1 parent ae4280e commit 005d32e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
2 changes: 2 additions & 0 deletions assets/shaders/render_params.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ struct RenderParams {
sand_col: vec4<f32>,
sea_col: vec4<f32>,
viewport: vec2<f32>,
unproj_pos: vec2<f32>,
time: f32,
time_always: f32,
shadow_mapping_resolution: i32,
terraforming_mode_radius: f32,
}
17 changes: 16 additions & 1 deletion assets/shaders/terrain/terrain.frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn frag(@builtin(position) position: vec4<f32>,
let V_denorm: vec3<f32> = params.cam_pos.xyz - in_wpos;
let depth: f32 = length(V_denorm);
let V: vec3<f32> = V_denorm / depth;
let F0: vec3<f32> = vec3(0.00);
let F0: vec3<f32> = vec3(0.01);
let roughness: f32 = 1.3; // avoid specular highlights which look weird on terrain
let normal: vec3<f32> = normalize(in_normal);
let F_spec: vec3<f32> = F0; // simplified with constant folding: fresnelSchlickRoughness(max(dot(normal, V), 0.0), F0, roughness);
Expand All @@ -152,6 +152,21 @@ fn frag(@builtin(position) position: vec4<f32>,
c = 0.05 * vec3(0.0, 0.0, debug);
#endif

if (params.terraforming_mode_radius > 0.0) {
let dist = length(params.unproj_pos - in_wpos.xy);
let alpha = smoothstep(params.terraforming_mode_radius, 0.0, dist) * 0.1;
let alpha2 = smoothstep(params.terraforming_mode_radius * 0.5, 0.0, dist) * 0.3;
let alpha3 = smoothstep(params.terraforming_mode_radius * 0.25, 0.0, dist) * 0.3;
var fw = fwidth(in_wpos.z) * 2.5;
let alpha4 = smoothstep(fw, 0.0, abs((in_wpos.z % 10.0) - 5.0)) * 0.1;
let alpha5 = smoothstep(fw, 0.0, abs((in_wpos.z % 50.0) - 25.0)) * 0.1;

c = mix(c, vec3(1.0, 0.5, 0.5), alpha);
c = mix(c, vec3(0.5, 1.0, 0.5), alpha2);
c = mix(c, vec3(0.5, 0.5, 1.0), alpha3);
c = mix(c, vec3(0.0, 0.0, 0.0), alpha4 + alpha5);
}

let final_rgb: vec3<f32> = render(params.sun,
V,
position.xy,
Expand Down
18 changes: 15 additions & 3 deletions engine/src/gfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub struct RenderParams {
pub sun_shadow_proj: [Matrix4; N_CASCADES],
pub cam_pos: Vec3,
pub _pad: f32,
pub cam_dir: Vec3,
pub cam_dir: Vec3, // Vec3s need to be 16 aligned
pub _pad4: f32,
pub sun: Vec3,
pub _pad2: f32,
Expand All @@ -168,10 +168,21 @@ pub struct RenderParams {
pub sand_col: LinearColor,
pub sea_col: LinearColor,
pub viewport: Vec2,
pub unproj_pos: Vec2,
pub time: f32,
pub time_always: f32,
pub shadow_mapping_resolution: i32,
pub _pad5: [f32; 3],
pub terraforming_mode_radius: f32,
}

#[cfg(test)]
#[test]
fn test_renderparam_size() {
println!(
"size of RenderParams: {}",
std::mem::size_of::<RenderParams>()
);
assert!(std::mem::size_of::<RenderParams>() < 1024);
}

impl Default for RenderParams {
Expand All @@ -187,13 +198,14 @@ impl Default for RenderParams {
cam_dir: Default::default(),
sun: Default::default(),
viewport: vec2(1000.0, 1000.0),
unproj_pos: Default::default(),
time: 0.0,
time_always: 0.0,
shadow_mapping_resolution: 2048,
terraforming_mode_radius: 0.0,
_pad: 0.0,
_pad2: 0.0,
_pad4: 0.0,
_pad5: Default::default(),
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions native_app/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use simulation::utils::time::GameTime;
use simulation::Simulation;

use crate::audio::GameAudio;
use crate::gui::terraforming::TerraformingResource;
use crate::gui::windows::debug::DebugObjs;
use crate::gui::windows::settings::{manage_settings, Settings};
use crate::gui::{ExitState, FollowEntity, Gui, Tool, UiTextures};
Expand Down Expand Up @@ -262,6 +263,15 @@ impl State {
)
.try_into()
.unwrap();
params.unproj_pos = self
.uiw
.read::<InputMap>()
.unprojected
.unwrap_or_default()
.xy();
params.terraforming_mode_radius = matches!(*self.uiw.read::<Tool>(), Tool::Terraforming)
.then(|| self.uiw.read::<TerraformingResource>().radius)
.unwrap_or_default();
drop(camera);
let c = simulation::config();
params.grass_col = c.grass_col.into();
Expand Down
6 changes: 2 additions & 4 deletions native_app/src/gui/terraforming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::inputmap::{InputAction, InputMap};
use crate::rendering::immediate::ImmediateDraw;
use crate::uiworld::UiWorld;
use egui_inspect::Inspect;
use geom::{LinearColor, Vec3};
use geom::Vec3;
use simulation::map::TerraformKind;
use simulation::world_command::WorldCommand;
use simulation::Simulation;
Expand All @@ -26,7 +26,7 @@ pub fn terraforming(sim: &Simulation, uiworld: &mut UiWorld) {
let res = uiworld.write::<TerraformingResource>();
let tool = *uiworld.read::<Tool>();
let inp = uiworld.read::<InputMap>();
let mut draw = uiworld.write::<ImmediateDraw>();
let _draw = uiworld.write::<ImmediateDraw>();
let _map = sim.map();
let commands = &mut *uiworld.commands();

Expand All @@ -35,8 +35,6 @@ pub fn terraforming(sim: &Simulation, uiworld: &mut UiWorld) {
}

let mpos = unwrap_ret!(inp.unprojected);
draw.circle(mpos.up(0.8), res.radius)
.color(LinearColor::GREEN.a(0.1));

if inp.act.contains(&InputAction::Select) {
commands.push(WorldCommand::Terraform {
Expand Down

0 comments on commit 005d32e

Please sign in to comment.