From e0f78b6f695866445960c1b58df136f36c520eb9 Mon Sep 17 00:00:00 2001 From: Nicola Papale Date: Wed, 12 Apr 2023 10:23:48 +0200 Subject: [PATCH] Add max_step controls + clamp to 0 depth --- examples/3d/parallax_mapping.rs | 56 +++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/examples/3d/parallax_mapping.rs b/examples/3d/parallax_mapping.rs index bf0dcbed95d7b..f7bf65c1b3660 100644 --- a/examples/3d/parallax_mapping.rs +++ b/examples/3d/parallax_mapping.rs @@ -1,6 +1,8 @@ //! A simple 3D scene with a spinning cube with a normal map and depth map to demonstrate parallax mapping. //! Press left mouse button to cycle through different views. +use std::fmt; + use bevy::{prelude::*, render::render_resource::TextureFormat, window::close_on_esc}; fn main() { @@ -48,6 +50,34 @@ impl Default for TargetLayers { TargetLayers(5.0) } } +struct CurrentMethod(ParallaxMappingMethod); +impl Default for CurrentMethod { + fn default() -> Self { + CurrentMethod(ParallaxMappingMethod::Relief { max_steps: 4 }) + } +} +impl fmt::Display for CurrentMethod { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.0 { + ParallaxMappingMethod::Occlusion => write!(f, "Parallax Occlusion Mapping"), + ParallaxMappingMethod::Relief { max_steps } => { + write!(f, "Relief Mapping with {max_steps} steps") + } + } + } +} +impl CurrentMethod { + fn next_method(&mut self) { + use ParallaxMappingMethod::*; + self.0 = match self.0 { + Occlusion => Relief { max_steps: 2 }, + Relief { max_steps } if max_steps < 3 => Relief { max_steps: 4 }, + Relief { max_steps } if max_steps < 5 => Relief { max_steps: 8 }, + Relief { .. } => Occlusion, + } + } +} + fn update_parallax_depth_scale( input: Res>, mut materials: ResMut>, @@ -57,7 +87,7 @@ fn update_parallax_depth_scale( ) { if input.just_pressed(KeyCode::Key1) { target_depth.0 -= DEPTH_UPDATE_STEP; - target_depth.0 = target_depth.0.max(-MAX_DEPTH); + target_depth.0 = target_depth.0.max(0.0); *depth_update = true; } if input.just_pressed(KeyCode::Key2) { @@ -84,25 +114,18 @@ fn switch_method( input: Res>, mut materials: ResMut>, mut text: Query<&mut Text>, - mut current: Local, + mut current: Local, ) { if input.just_pressed(KeyCode::Space) { - *current = match *current { - ParallaxMappingMethod::Relief { .. } => ParallaxMappingMethod::Occlusion, - ParallaxMappingMethod::Occlusion => ParallaxMappingMethod::Relief { max_steps: 5 }, - } + current.next_method(); } else { return; } - let method_name = match *current { - ParallaxMappingMethod::Relief { .. } => "Relief Mapping", - ParallaxMappingMethod::Occlusion => "Parallax Occlusion Mapping", - }; let mut text = text.single_mut(); - text.sections[2].value = format!("Method: {method_name}\n"); + text.sections[2].value = format!("Method: {}\n", *current); for (_, mat) in materials.iter_mut() { - mat.parallax_mapping_method = *current; + mat.parallax_mapping_method = current.0; } } @@ -133,7 +156,7 @@ fn spin(time: Res