diff --git a/assets/shaders/render_params.wgsl b/assets/shaders/render_params.wgsl index f51dfa0c..b56a3085 100644 --- a/assets/shaders/render_params.wgsl +++ b/assets/shaders/render_params.wgsl @@ -11,7 +11,9 @@ struct RenderParams { sand_col: vec4, sea_col: vec4, viewport: vec2, + unproj_pos: vec2, time: f32, time_always: f32, shadow_mapping_resolution: i32, + terraforming_mode_radius: f32, } \ No newline at end of file diff --git a/assets/shaders/terrain/terrain.frag.wgsl b/assets/shaders/terrain/terrain.frag.wgsl index ab1fba92..14e3ee5d 100644 --- a/assets/shaders/terrain/terrain.frag.wgsl +++ b/assets/shaders/terrain/terrain.frag.wgsl @@ -143,7 +143,7 @@ fn frag(@builtin(position) position: vec4, let V_denorm: vec3 = params.cam_pos.xyz - in_wpos; let depth: f32 = length(V_denorm); let V: vec3 = V_denorm / depth; - let F0: vec3 = vec3(0.00); + let F0: vec3 = vec3(0.01); let roughness: f32 = 1.3; // avoid specular highlights which look weird on terrain let normal: vec3 = normalize(in_normal); let F_spec: vec3 = F0; // simplified with constant folding: fresnelSchlickRoughness(max(dot(normal, V), 0.0), F0, roughness); @@ -152,6 +152,21 @@ fn frag(@builtin(position) position: vec4, 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 = render(params.sun, V, position.xy, diff --git a/engine/src/gfx.rs b/engine/src/gfx.rs index c9ce6222..14832d4e 100644 --- a/engine/src/gfx.rs +++ b/engine/src/gfx.rs @@ -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, @@ -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::() + ); + assert!(std::mem::size_of::() < 1024); } impl Default for RenderParams { @@ -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(), } } } diff --git a/native_app/src/game_loop.rs b/native_app/src/game_loop.rs index 6311bcf8..6ef5cfd4 100644 --- a/native_app/src/game_loop.rs +++ b/native_app/src/game_loop.rs @@ -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}; @@ -262,6 +263,15 @@ impl State { ) .try_into() .unwrap(); + params.unproj_pos = self + .uiw + .read::() + .unprojected + .unwrap_or_default() + .xy(); + params.terraforming_mode_radius = matches!(*self.uiw.read::(), Tool::Terraforming) + .then(|| self.uiw.read::().radius) + .unwrap_or_default(); drop(camera); let c = simulation::config(); params.grass_col = c.grass_col.into(); diff --git a/native_app/src/gui/terraforming.rs b/native_app/src/gui/terraforming.rs index 498e5913..ca9470aa 100644 --- a/native_app/src/gui/terraforming.rs +++ b/native_app/src/gui/terraforming.rs @@ -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; @@ -26,7 +26,7 @@ pub fn terraforming(sim: &Simulation, uiworld: &mut UiWorld) { let res = uiworld.write::(); let tool = *uiworld.read::(); let inp = uiworld.read::(); - let mut draw = uiworld.write::(); + let _draw = uiworld.write::(); let _map = sim.map(); let commands = &mut *uiworld.commands(); @@ -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 {