WebGPU Voxel Game
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Shadowmap tweaking

+42 -16
+19 -8
src/gfx.rs
··· 41 41 pub struct InteractState { 42 42 pub clear_color: wgpu::Color, 43 43 pub wireframe: bool, 44 + pub shadows: bool, 44 45 } 45 46 pub struct ObjectState { 46 47 pub model: model::Model, ··· 292 293 surface.configure(&device, &surface_config); 293 294 294 295 let depth_texture = 295 - texture::Texture::create_depth_texture(&device, &surface_config, "depth_texture"); 296 + texture::Texture::create_depth_texture(&device, &surface_config, None, "depth_texture"); 296 297 297 298 let texture_bind_group_layout = 298 299 device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { ··· 368 369 369 370 log::info!("Light setup!"); 370 371 let light = camera::Camera { 371 - eye: vec3(90., 40., 90.), 372 + eye: vec3(0., 300., 0.), 372 373 target: Vec3::ZERO, 373 374 up: Vec3::Y, 374 375 aspect: surface_config.width as f32 / surface_config.height as f32, ··· 396 397 397 398 log::info!("asdfasdgf"); 398 399 399 - let shadow_map = 400 - texture::Texture::create_depth_texture(&device, &surface_config, "Shadow Map"); 400 + let shadow_map = texture::Texture::create_depth_texture( 401 + &device, 402 + &surface_config, 403 + Some((1000, 1000)), 404 + "Shadow Map", 405 + ); 401 406 // let shadow_texture = device.create_texture(&wgpu::TextureDescriptor { 402 407 // size: wgpu::Extent3d { 403 408 // width: 1024, ··· 581 586 topology: wgpu::PrimitiveTopology::TriangleList, 582 587 strip_index_format: None, 583 588 front_face: wgpu::FrontFace::Ccw, 584 - cull_mode: Some(wgpu::Face::Back), 589 + cull_mode: Some(wgpu::Face::Front), 585 590 // Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE 586 591 polygon_mode: wgpu::PolygonMode::Fill, 587 592 // Requires Features::DEPTH_CLIP_CONTROL ··· 730 735 b: 0.3, 731 736 a: 1.0, 732 737 }, 738 + shadows: true, 733 739 }, 734 740 object: ObjectState { 735 741 model: obj_model, ··· 759 765 self.depth_texture = texture::Texture::create_depth_texture( 760 766 &self.device, 761 767 &self.surface_config, 768 + None, 762 769 "depth_texture", 763 770 ); 764 771 } ··· 837 844 use crate::gfx::model::DrawModel; 838 845 839 846 // Shadow-map Pass 840 - { 847 + if self.interact.shadows { 841 848 let Pass { 842 849 pipeline, 843 850 bind_group, ··· 920 927 921 928 render_pass.set_vertex_buffer(1, self.object.instance_buffer.slice(..)); 922 929 923 - // use crate::gfx::model::DrawLight; 930 + use crate::gfx::model::DrawLight; 924 931 // render_pass.set_pipeline(pipeline.two().1); 925 932 // render_pass.draw_light_model( 926 933 // &self.object.model, ··· 944 951 .unwrap_or(&self.render_pipelines.camera), 945 952 );*/ 946 953 954 + render_pass.set_pipeline(pipeline.two().1); 955 + 956 + render_pass.draw_light_model(&self.object.model, &[bind_group]); 957 + 947 958 render_pass.set_pipeline(pipeline.two().0); 948 959 949 960 render_pass.draw_model_instanced( ··· 1007 1018 ); 1008 1019 1009 1020 self.light.object.eye = 1010 - Quat::from_axis_angle(vec3(0.0, 1.0, 0.0), 0.01) * self.light.object.eye; 1021 + Quat::from_axis_angle(vec3(0.0, 0.0, 1.0), 0.01) * self.light.object.eye; 1011 1022 self.light.uniform.update_view_proj(&self.light.object); 1012 1023 1013 1024 // let old_position: Vec3 = self.light.uniform.view_pos;
+7 -2
src/gfx/texture.rs
··· 13 13 pub fn create_depth_texture( 14 14 device: &wgpu::Device, 15 15 config: &wgpu::SurfaceConfiguration, 16 + size_override: Option<(u32, u32)>, 16 17 label: &str, 17 18 ) -> Self { 18 19 let size = wgpu::Extent3d { 19 - width: config.width.max(1), 20 - height: config.height.max(1), 20 + width: size_override 21 + .map(|(w, _h)| w) 22 + .unwrap_or(config.width.max(1)), 23 + height: size_override 24 + .map(|(_w, h)| h) 25 + .unwrap_or(config.height.max(1)), 21 26 depth_or_array_layers: 1, 22 27 }; 23 28
+6
src/gui.rs
··· 191 191 ui.add( 192 192 egui::Slider::new(&mut gfx.camera.object.eye.z, -500.0..=500.0).text("Cam Z"), 193 193 ); 194 + ui.separator(); 195 + 196 + ui.checkbox( 197 + &mut gfx.interact.shadows, 198 + "Light shadowing (via shadow-maps)", 199 + ); 194 200 195 201 ui.separator(); 196 202 ui.horizontal(|ui| {
+1 -1
src/light.wgsl
··· 21 21 fn vs_main( 22 22 model: VertexInput, 23 23 ) -> VertexOutput { 24 - let scale = 0.75; 24 + let scale = 10.0; 25 25 let light_color = vec3<f32>(1.0, 1.0, 1.0); 26 26 var out: VertexOutput; 27 27 out.clip_position = camera.view_proj * vec4<f32>(model.position * scale + light.view_pos.xyz, 1.0);
+9 -5
src/shader.wgsl
··· 75 75 @group(1) @binding(3) 76 76 var s_shadow: sampler_comparison; 77 77 78 - 78 + // TODO CITE: wgpu/examples/shadow 79 79 fn fetch_shadow(homogeneous_coords: vec4<f32>) -> f32 { 80 80 if (homogeneous_coords.w <= 0.0) { 81 81 return 1.0; ··· 90 90 } 91 91 92 92 @fragment 93 - fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> { 93 + fn fs_main(in: VertexOutput, @builtin(front_facing) face: bool) -> @location(0) vec4<f32> { 94 94 let light_color = vec3<f32>(1.0, 1.0, 1.0); 95 95 96 96 let object_color: vec4<f32> = textureSample(t_diffuse, s_diffuse, in.tex_coords); ··· 104 104 105 105 let view_dir = normalize(camera.view_pos.xyz - in.world_position.xyz); 106 106 let half_dir = normalize(view_dir + light_dir); 107 + 108 + let shadow = fetch_shadow(light.view_proj * vec4<f32>(in.world_position, 1.0)); 109 + 107 110 108 111 let specular_strength = pow(max(dot(in.world_normal, half_dir), 0.0), 32.0); 109 - let specular_color = specular_strength * light_color; 110 112 111 - let shadow = fetch_shadow(light.view_proj * vec4<f32>(in.world_position, 1.0)); 113 + // Disable specular effects in shadow 114 + let specular_color = specular_strength * light_color * shadow; 112 115 113 - let result = (ambient_color + diffuse_color + specular_color) * shadow * object_color.xyz; 116 + // let result = (ambient_color + diffuse_color + specular_color) * shadow * object_color.xyz; 117 + let result = (ambient_color + diffuse_color + specular_color) * (shadow * 0.5 + 0.5); 114 118 115 119 return vec4<f32>(result, object_color.a); 116 120 }