From 7e5a3efd02e1ba87a7a145bd73164e5b3175b700 Mon Sep 17 00:00:00 2001 From: Imbris Date: Sat, 22 Jan 2022 01:29:47 -0500 Subject: [PATCH] Add egui window to toggle experimental shaders. --- voxygen/egui/src/admin.rs | 6 ++-- voxygen/egui/src/experimental_shaders.rs | 29 ++++++++++++++++ voxygen/egui/src/lib.rs | 21 ++++++++++-- voxygen/src/render/mod.rs | 13 +++++++- voxygen/src/session/mod.rs | 7 +++- voxygen/src/ui/egui/mod.rs | 42 ++++++++++++++++++++++-- 6 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 voxygen/egui/src/experimental_shaders.rs diff --git a/voxygen/egui/src/admin.rs b/voxygen/egui/src/admin.rs index 73b365321b..b5a16bc49b 100644 --- a/voxygen/egui/src/admin.rs +++ b/voxygen/egui/src/admin.rs @@ -1,4 +1,4 @@ -use crate::{AdminCommandState, EguiAction, EguiActions, EguiWindows}; +use crate::{AdminCommandState, EguiAction, EguiActions}; use common::cmd::ChatCommand; use egui::{CollapsingHeader, CtxRef, Resize, Slider, Ui, Vec2, Window}; use lazy_static::lazy_static; @@ -17,11 +17,11 @@ lazy_static! { pub fn draw_admin_commands_window( ctx: &CtxRef, state: &mut AdminCommandState, - windows: &mut EguiWindows, + open: &mut bool, egui_actions: &mut EguiActions, ) { Window::new("Admin Commands") - .open(&mut windows.admin_commands) + .open(open) .default_width(400.0) .default_height(600.0) .show(ctx, |ui| { diff --git a/voxygen/egui/src/experimental_shaders.rs b/voxygen/egui/src/experimental_shaders.rs new file mode 100644 index 0000000000..6b3d9a8730 --- /dev/null +++ b/voxygen/egui/src/experimental_shaders.rs @@ -0,0 +1,29 @@ +use crate::{EguiAction, EguiActions}; +use egui::{CtxRef, Vec2, Window}; + +pub fn draw_experimental_shaders_window( + ctx: &CtxRef, + open: &mut bool, + egui_actions: &mut EguiActions, + experimental_shaders: &[(String, bool)], +) { + Window::new("Experimental Shaders") + .open(open) + .default_width(250.0) + .default_height(600.0) + .show(ctx, |ui| { + ui.spacing_mut().item_spacing = Vec2::new(10.0, 10.0); + experimental_shaders.iter().for_each(|(shader, enabled)| { + let mut enabled_mut = *enabled; + + ui.checkbox(&mut enabled_mut, shader); + + if enabled_mut != *enabled { + egui_actions.actions.push(EguiAction::SetExperimentalShader( + shader.into(), + enabled_mut, + )); + } + }) + }); +} diff --git a/voxygen/egui/src/lib.rs b/voxygen/egui/src/lib.rs index d1233d462c..58c92fb168 100644 --- a/voxygen/egui/src/lib.rs +++ b/voxygen/egui/src/lib.rs @@ -5,6 +5,7 @@ compile_error!("Can't use both \"be-dyn-lib\" and \"use-dyn-lib\" features at on mod admin; mod character_states; +mod experimental_shaders; mod widgets; use client::{Client, Join, World, WorldExt}; @@ -21,7 +22,7 @@ use egui::{ use crate::{ admin::draw_admin_commands_window, character_states::draw_char_state_group, - widgets::two_col_row, + experimental_shaders::draw_experimental_shaders_window, widgets::two_col_row, }; use common::{ cmd::ChatCommand, @@ -100,6 +101,7 @@ pub struct EguiWindows { egui_memory: bool, frame_time: bool, ecs_entities: bool, + experimental_shaders: bool, } impl Default for EguiInnerState { @@ -131,6 +133,7 @@ pub enum EguiDebugShapeAction { pub enum EguiAction { ChatCommand { cmd: ChatCommand, args: Vec }, DebugShape(EguiDebugShapeAction), + SetExperimentalShader(String, bool), } #[derive(Default)] @@ -147,6 +150,7 @@ pub fn maintain( client: &Client, debug_info: Option, added_cylinder_shape_id: Option, + experimental_shaders: Vec<(String, bool)>, ) -> EguiActions { #[cfg(not(feature = "use-dyn-lib"))] { @@ -156,6 +160,7 @@ pub fn maintain( client, debug_info, added_cylinder_shape_id, + experimental_shaders, ) } @@ -171,6 +176,7 @@ pub fn maintain( &Client, Option, Option, + Vec<(String, bool)>, ) -> EguiActions, > = unsafe { lib.get(MAINTAIN_EGUI_FN) }.unwrap_or_else(|e| { panic!( @@ -189,6 +195,7 @@ pub fn maintain( client, debug_info, added_cylinder_shape_id, + experimental_shaders, ) } } @@ -200,6 +207,7 @@ pub fn maintain_egui_inner( client: &Client, debug_info: Option, added_cylinder_shape_id: Option, + experimental_shaders: Vec<(String, bool)>, ) -> EguiActions { platform.begin_frame(); let ctx = &platform.context(); @@ -244,6 +252,7 @@ pub fn maintain_egui_inner( ui.checkbox(&mut windows.admin_commands, "Admin Commands"); ui.checkbox(&mut windows.ecs_entities, "ECS Entities"); ui.checkbox(&mut windows.frame_time, "Frame Time"); + ui.checkbox(&mut windows.experimental_shaders, "Experimental Shaders"); }); }); @@ -412,7 +421,6 @@ pub fn maintain_egui_inner( ui.end_row(); } }); - let margin = ui.visuals().clip_rect_margin; let current_scroll = ui.clip_rect().top() - ui.min_rect().top() + margin; @@ -434,10 +442,17 @@ pub fn maintain_egui_inner( draw_admin_commands_window( ctx, &mut egui_state.admin_command_state, - &mut windows, + &mut windows.admin_commands, &mut egui_actions, ); + draw_experimental_shaders_window( + ctx, + &mut windows.experimental_shaders, + &mut egui_actions, + &experimental_shaders, + ); + if let Some(previous) = previous_selected_entity { if let Some(debug_shape_id) = previous.debug_shape_id { egui_actions diff --git a/voxygen/src/render/mod.rs b/voxygen/src/render/mod.rs index 4612ba967c..e5066f374f 100644 --- a/voxygen/src/render/mod.rs +++ b/voxygen/src/render/mod.rs @@ -417,7 +417,18 @@ struct OtherModes { /// /// You can enable these using Voxygen's `settings.ron`. See /// [here](https://book.veloren.net/players/voxygen.html#experimental-shaders) for more information. -#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive( + Clone, + Debug, + PartialEq, + Eq, + Hash, + Serialize, + Deserialize, + strum::EnumIter, + strum::Display, + strum::EnumString, +)] pub enum ExperimentalShader { /// Add brick-like normal mapping to the world. Brickloren, diff --git a/voxygen/src/session/mod.rs b/voxygen/src/session/mod.rs index 143946cdfc..29129659d9 100644 --- a/voxygen/src/session/mod.rs +++ b/voxygen/src/session/mod.rs @@ -1103,14 +1103,19 @@ impl PlayState for SessionState { // Maintain egui (debug interface) #[cfg(feature = "egui-ui")] if global_state.settings.interface.egui_enabled() { - global_state.egui_state.maintain( + let settings_change = global_state.egui_state.maintain( &mut self.client.borrow_mut(), &mut self.scene, debug_info.map(|debug_info| EguiDebugInfo { frame_time: debug_info.frame_time, ping_ms: debug_info.ping_ms, }), + &global_state.settings, ); + + if let Some(settings_change) = settings_change { + settings_change.process(global_state, self); + } } // Look for changes in the localization files diff --git a/voxygen/src/ui/egui/mod.rs b/voxygen/src/ui/egui/mod.rs index e98db48f5b..29c6e7d420 100644 --- a/voxygen/src/ui/egui/mod.rs +++ b/voxygen/src/ui/egui/mod.rs @@ -1,5 +1,7 @@ use crate::{ scene::{DebugShape, DebugShapeId, Scene}, + session::settings_change::{Graphics, SettingsChange}, + settings::Settings, window::Window, }; use client::Client; @@ -35,15 +37,34 @@ impl EguiState { client: &mut Client, scene: &mut Scene, debug_info: Option, - ) { + settings: &Settings, + ) -> Option { + use crate::render::ExperimentalShader; + use strum::IntoEnumIterator; + let experimental_shaders = ExperimentalShader::iter() + .map(|s| { + ( + s.to_string(), + settings + .graphics + .render_mode + .experimental_shaders + .contains(&s), + ) + }) + .collect(); + let egui_actions = voxygen_egui::maintain( &mut self.platform, &mut self.egui_inner_state, client, debug_info, self.new_debug_shape_id.take(), + experimental_shaders, ); + let mut new_render_mode = None; + egui_actions .actions .into_iter() @@ -68,6 +89,23 @@ impl EguiState { .set_context(DebugShapeId(id), pos, color, identity_ori); }, }, - }) + EguiAction::SetExperimentalShader(shader, enabled) => { + // TODO Rust 2021 + use core::convert::TryFrom; + if let Ok(shader) = ExperimentalShader::try_from(shader.as_str()) { + let shaders = &mut new_render_mode + .get_or_insert_with(|| settings.graphics.render_mode.clone()) + .experimental_shaders; + + if enabled { + shaders.insert(shader); + } else { + shaders.remove(&shader); + } + } + }, + }); + + new_render_mode.map(|rm| SettingsChange::Graphics(Graphics::ChangeRenderMode(Box::new(rm)))) } }