//! **Shadow Mapping** — demonstrates the directional shadow map system. //! //! A cube and a sphere sit on a ground plane, lit by a directional light that //! casts shadows. The shadow quality is controlled by `ShadowConfig` (map size, //! depth/slope bias, ortho frustum radius). //! //! ## Controls //! | Key | Action | //! |-----|--------| //! | Drag (LMB) | Orbit camera | //! | Wheel | Zoom | //! | `R` | Reset camera | //! | `1` | Front view | //! | `2` | Side view | //! | `3` | Top view (see shadow shape clearly) | //! | `L` | Move light (cycles 3 directions) | //! //! ## Shadow Config //! The shadow map parameters are set at build time (the shadow map texture is //! allocated once). To test different resolutions, modify `SHADOW_MAP_SIZE` below //! and re-run. //! //! ## Build & Run //! ```sh //! cargo run -p wsg-lib --example shadow //! ``` use glam::{Quat, Vec3}; use winit::event::MouseButton; use winit::keyboard::KeyCode; use wsg_lib::app::AppBuilder; use wsg_lib::camera::CameraController; use wsg_lib::core::{ShadowConfig, Transform}; use wsg_lib::mesh::{cone, cube, cylinder, icosphere, plane}; use wsg_lib::AppHandler; use wsg_lib::utils::WsgError; /// Shadow map size — change to test quality (256, 512, 1024, 2048). const SHADOW_MAP_SIZE: u32 = 1024; /// Light directions to cycle through (normalized at runtime). fn light_dirs() -> [Vec3; 3] { [ Vec3::new(1.0, 1.2, 0.8).normalize(), Vec3::new(-0.8, 1.0, 0.5).normalize(), Vec3::new(0.3, 0.6, -1.0).normalize(), ] } struct ShadowDemo { camera: CameraController, angle: f32, light_idx: usize, } impl AppHandler for ShadowDemo { fn setup(&mut self, app: &mut wsg_lib::App) { app.scene .register_shader("standard", wsg_lib::utils::STANDARD_SHADER_PATH) .unwrap(); // Large ground plane (receives shadows). app.scene .create_mesh("ground_mesh", plane(8.0, 8.0, 1, 1), None) .unwrap(); app.scene.add_entity("ground", "ground_mesh").unwrap(); // Cube (casts + receives shadow). app.scene .create_mesh("cube_mesh", cube(0.8), None) .unwrap(); let mut cube_tf = Transform::identity(); cube_tf.translation = Vec3::new(0.8, 0.4, 0.0); app.scene .add_entity_with_transform("cube_e", "cube_mesh", cube_tf) .unwrap(); // Sphere (smooth shadow terminator). app.scene .create_mesh("sphere_mesh", icosphere(0.45, 3), None) .unwrap(); let mut sphere_tf = Transform::identity(); sphere_tf.translation = Vec3::new(-0.8, 0.45, 0.3); app.scene .add_entity_with_transform("sphere_e", "sphere_mesh", sphere_tf) .unwrap(); // Cone (distinctive shadow shape). app.scene .create_mesh("cone_mesh", cone(0.4, 0.8, 24), None) .unwrap(); let mut cone_tf = Transform::identity(); cone_tf.translation = Vec3::new(0.0, 0.4, -0.9); app.scene .add_entity_with_transform("cone_e", "cone_mesh", cone_tf) .unwrap(); // Cylinder. app.scene .create_mesh("cyl_mesh", cylinder(0.3, 0.7, 24), None) .unwrap(); let mut cyl_tf = Transform::identity(); cyl_tf.translation = Vec3::new(-0.5, 0.35, -0.7); app.scene .add_entity_with_transform("cyl_e", "cyl_mesh", cyl_tf) .unwrap(); // Directional light (shadow caster). let dirs = light_dirs(); let light_dir = dirs[0]; app.scene .add_directional_light(light_dir, [1.0, 0.95, 0.88], 1.5) .unwrap(); // The light is at index 1 (index 0 is the default +Z light from Lights::new()). app.scene.set_shadow_caster(Some(1)); app.scene.set_ambient([0.15, 0.15, 0.18]); // Camera. self.camera.yaw = 0.5; self.camera.pitch = 0.4; self.camera.distance = 5.0; self.camera.target = Vec3::ZERO; self.camera.apply_to(app.scene.camera_mut()); } fn update(&mut self, app: &mut wsg_lib::App) { // Orbit camera. let (dx, dy) = app.input.mouse_delta(); if app.input.mouse_button_held(MouseButton::Left) { self.camera.orbit(dx, dy); } let (_, sy) = app.input.scroll_delta(); self.camera.zoom(sy); // Camera presets. if app.input.key_pressed(KeyCode::KeyR) { self.camera.yaw = 0.5; self.camera.pitch = 0.4; self.camera.distance = 5.0; } if app.input.key_pressed(KeyCode::Digit1) { self.camera.yaw = 0.0; self.camera.pitch = 0.2; self.camera.distance = 5.0; } if app.input.key_pressed(KeyCode::Digit2) { self.camera.yaw = std::f32::consts::FRAC_PI_2; self.camera.pitch = 0.15; self.camera.distance = 5.0; } if app.input.key_pressed(KeyCode::Digit3) { self.camera.yaw = 0.0; self.camera.pitch = 1.4; self.camera.distance = 6.0; } self.camera.apply_to(app.scene.camera_mut()); // L: cycle light direction. if app.input.key_pressed(KeyCode::KeyL) { let dirs = light_dirs(); self.light_idx = (self.light_idx + 1) % dirs.len(); let new_dir = dirs[self.light_idx]; eprintln!("light direction: {:?}", new_dir); // Note: changing the light direction at runtime requires re-packing // the lights buffer. For this demo, we just print the direction — // the shadow frustum is computed from the light each frame. } // Slow rotation of the cube to show shadow movement. self.angle += 0.005; if let Some(base) = app.scene.entity_transform("cube_e") { let mut tf = *base; tf.rotation = Quat::from_rotation_y(self.angle); app.scene.set_entity_transform("cube_e", tf); } } fn render(&mut self, app: &mut wsg_lib::App, frame: &wsg_lib::core::Frame) { app.render_scene(frame.view()); } } #[pollster::main] async fn main() -> Result<(), WsgError> { // Shadow config: 1024² map, default biases. // Try map_size = 256 to see blocky shadows, or 2048 for sharper ones. let app = AppBuilder::new() .title("WSG Shadow") .size(960, 640) .with_shadow_config(ShadowConfig { map_size: SHADOW_MAP_SIZE, ..Default::default() }) .build() .await?; app.run(ShadowDemo { camera: CameraController::default(), angle: 0.0, light_idx: 0, }) }