Implement Shadows #24

Merged
SeanOMik merged 28 commits from feat/shadow-maps into main 2024-08-10 03:10:30 +00:00
4 changed files with 23 additions and 29 deletions
Showing only changes of commit 6d57b40629 - Show all commits

View File

@ -136,6 +136,12 @@ fn setup_scene_plugin(game: &mut Game) {
Transform::from_xyz(0.0, -2.0, -5.0),
));
world.spawn((
cube_mesh.clone(),
WorldTransform::default(),
Transform::from_xyz(3.0, -3.75, -5.0),
));
world.spawn((
platform_mesh.clone(),
WorldTransform::default(),

View File

@ -80,7 +80,7 @@ impl MeshPass {
impl Node for MeshPass {
fn desc(
&mut self,
graph: &mut crate::render::graph::RenderGraph,
_: &mut crate::render::graph::RenderGraph,
) -> crate::render::graph::NodeDesc {
// load the default texture
//let bytes = include_bytes!("../../default_texture.png");
@ -244,7 +244,10 @@ impl Node for MeshPass {
stencil: wgpu::StencilState::default(), // TODO: stencil buffer
bias: wgpu::DepthBiasState::default(),
}),
primitive: wgpu::PrimitiveState::default(),
primitive: wgpu::PrimitiveState {
cull_mode: Some(wgpu::Face::Back),
..Default::default()
},
multisample: wgpu::MultisampleState::default(),
multiview: None,
},

View File

@ -1,8 +1,8 @@
use std::{mem, num::NonZeroU64, rc::Rc, sync::Arc};
use lyra_ecs::{query::Entities, AtomicRef, Entity, ResourceData};
use lyra_ecs::{query::{filter::Has, Entities}, AtomicRef, Entity, ResourceData};
use lyra_game_derive::RenderGraphLabel;
use lyra_math::{Transform, OPENGL_TO_WGPU_MATRIX};
use lyra_math::Transform;
use rustc_hash::FxHashMap;
use tracing::{debug, warn};
use wgpu::util::DeviceExt;
@ -11,7 +11,7 @@ use crate::render::{
graph::{Node, NodeDesc, NodeType, SlotAttribute, SlotValue},
light::directional::DirectionalLight,
resource::{
FragmentState, PipelineDescriptor, RenderPipeline, RenderPipelineDescriptor, Shader,
RenderPipeline, RenderPipelineDescriptor, Shader,
VertexState,
},
transform_buffer_storage::TransformBuffers,
@ -181,7 +181,7 @@ impl ShadowMapsPass {
impl Node for ShadowMapsPass {
fn desc(
&mut self,
graph: &mut crate::render::graph::RenderGraph,
_: &mut crate::render::graph::RenderGraph,
) -> crate::render::graph::NodeDesc {
let mut node = NodeDesc::new(NodeType::Render, None, vec![]);
@ -216,13 +216,13 @@ impl Node for ShadowMapsPass {
&mut self,
graph: &mut crate::render::graph::RenderGraph,
world: &mut lyra_ecs::World,
context: &mut crate::render::graph::RenderGraphContext,
_: &mut crate::render::graph::RenderGraphContext,
) {
self.render_meshes = world.try_get_resource_data::<RenderMeshes>();
self.transform_buffers = world.try_get_resource_data::<TransformBuffers>();
self.mesh_buffers = world.try_get_resource_data::<RenderAssets<MeshBufferStorage>>();
for (entity, pos, light) in world.view_iter::<(Entities, &Transform, &DirectionalLight)>() {
for (entity, pos, _) in world.view_iter::<(Entities, &Transform, Has<DirectionalLight>)>() {
if !self.depth_maps.contains_key(&entity) {
self.create_depth_map(graph.device(), entity, *pos);
debug!("Created depth map for {:?} light entity", entity);
@ -267,7 +267,10 @@ impl Node for ShadowMapsPass {
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
}),
primitive: wgpu::PrimitiveState::default(),
primitive: wgpu::PrimitiveState {
cull_mode: Some(wgpu::Face::Back),
..Default::default()
},
multisample: wgpu::MultisampleState::default(),
multiview: None,
},
@ -278,8 +281,8 @@ impl Node for ShadowMapsPass {
fn execute(
&mut self,
graph: &mut crate::render::graph::RenderGraph,
desc: &crate::render::graph::NodeDesc,
_: &mut crate::render::graph::RenderGraph,
_: &crate::render::graph::NodeDesc,
context: &mut crate::render::graph::RenderGraphContext,
) {
let encoder = context.encoder.as_mut().unwrap();

View File

@ -149,25 +149,7 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let light: Light = u_lights.data[light_index];
if (light.light_ty == LIGHT_TY_DIRECTIONAL) {
/*let shadow = calc_shadow(in.frag_pos_light_space);
return vec4<f32>(vec3<f32>(shadow), 1.0);*/
/*var proj_coords = in.frag_pos_light_space / in.frag_pos_light_space.w;
proj_coords = proj_coords * 0.5 + 0.5;
let closest_depth = textureSampleLevel(t_shadow_maps_atlas, s_shadow_maps_atlas, proj_coords.xy, 0.0);
let current_depth = proj_coords.z;
if current_depth > closest_depth {
return vec4<f32>(vec3<f32>(current_depth), 1.0);
} else {
return vec4<f32>(vec3<f32>(closest_depth), 1.0);
}*/
//return vec4<f32>(vec3<f32>(closest_depth), 1.0);
//let shadow = select(0.0, 1.0, current_depth > closest_depth);
let light_dir = normalize(-light.direction);
let shadow = calc_shadow(in.world_normal, light_dir, in.frag_pos_light_space);
light_res += blinn_phong_dir_light(in.world_position, in.world_normal, light, u_material, specular_color, shadow);
} else if (light.light_ty == LIGHT_TY_POINT) {