Skip to content

Commit

Permalink
water settings
Browse files Browse the repository at this point in the history
  • Loading branch information
rfuzzo committed Jun 7, 2024
1 parent ef96777 commit 3eb77a7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ impl TemplateApp {
let dimensions = &mut self.dimensions;

if let Some(i) = compute_landscape_image(
&self.ui_data.landscape_settings,
dimensions,
&self.land_records,
&self.ltex_records,
Expand Down
38 changes: 23 additions & 15 deletions src/background/landscape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use log::info;
use tes3::esp::{Landscape, LandscapeFlags, LandscapeTexture};

use crate::{
height_from_screen_space, overlay_colors_with_alpha, CellKey, Dimensions, DEFAULT_COLOR,
GRID_SIZE, VERTEX_CNT,
height_from_screen_space, overlay_colors_with_alpha, CellKey, Dimensions, LandscapeSettings,
DEFAULT_COLOR, GRID_SIZE, VERTEX_CNT,
};

/// Compute a landscape image from the given landscape records and texture map.
pub fn compute_landscape_image(
settings: &LandscapeSettings,
dimensions: &Dimensions,
landscape_records: &HashMap<CellKey, Landscape>,
ltex_records: &HashMap<u32, LandscapeTexture>,
Expand Down Expand Up @@ -58,27 +59,34 @@ pub fn compute_landscape_image(
let index = (y * d.texture_size) + x;
let mut color = texture.pixels[index];

// blend color when under water
let tx = d.tranform_to_canvas_x(cx) * d.cell_size()
+ gx * d.texture_size
+ x;
let ty = d.tranform_to_canvas_y(cy) * d.cell_size()
+ (GRID_SIZE - 1 - gy) * d.texture_size
+ y;
let screenx = tx * VERTEX_CNT / d.cell_size();
let screeny = ty * VERTEX_CNT / d.cell_size();

if let Some(height) = height_from_screen_space(
heights, dimensions, screenx, screeny,
) {
if height < 0_f32 {
let a = 0.5;
// blend color when under water
if settings.show_water {
let screenx = tx * VERTEX_CNT / d.cell_size();
let screeny = ty * VERTEX_CNT / d.cell_size();

if let Some(height) = height_from_screen_space(
heights, dimensions, screenx, screeny,
) {
if height < 0_f32 {
let a = 0.5;

color = overlay_colors_with_alpha(
color,
Color32::BLUE,
a,
);
if settings.remove_water {
color = Color32::TRANSPARENT;
} else {
color = overlay_colors_with_alpha(
color,
Color32::BLUE,
a,
);
}
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ pub enum EBackground {
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct LandscapeSettings {
pub texture_size: usize, // landscape
pub show_water: bool,
pub remove_water: bool,
}

impl Default for LandscapeSettings {
fn default() -> Self {
Self {
// landscape
texture_size: 16,
show_water: true,
remove_water: false,
}
}
}
Expand Down
29 changes: 26 additions & 3 deletions src/views/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,39 @@ impl TemplateApp {
}

fn landscape_settings_ui(&mut self, ui: &mut Ui, ctx: &egui::Context) {
let settings = &mut self.ui_data.landscape_settings;
ui.label("Landscape settings");

ui.checkbox(&mut self.ui_data.realtime_update, "Realtime update");

let max_texture_side = ctx.input(|i| i.max_texture_side);
let max_texture_resolution = self.dimensions.get_max_texture_resolution(max_texture_side);

ui.add(
egui::Slider::new(&mut settings.texture_size, 2..=max_texture_resolution)
.text("Texture Resolution"),
egui::Slider::new(
&mut self.ui_data.landscape_settings.texture_size,
2..=max_texture_resolution,
)
.text("Texture Resolution"),
);

if ui
.checkbox(
&mut self.ui_data.landscape_settings.show_water,
"Render water",
)
.changed()
{
self.reload_background(ctx, None, false, false);
}
if ui
.checkbox(
&mut self.ui_data.landscape_settings.remove_water,
"Clip water",
)
.changed()
{
self.reload_background(ctx, None, false, false);
}
}

fn heightmap_settings_ui(&mut self, ui: &mut Ui, ctx: &egui::Context) {
Expand Down

0 comments on commit 3eb77a7

Please sign in to comment.