Skip to content

Commit

Permalink
Allow shadows to be toggled on a per light basis
Browse files Browse the repository at this point in the history
  • Loading branch information
jgayfer committed Aug 5, 2024
1 parent 7d9339e commit 4b64edd
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bevy = { version = "0.14", default-features = false, features = [
"bevy_winit",
"bevy_sprite",
"png",
"x11",
"x11"
] }

[lints.clippy]
Expand Down
1 change: 1 addition & 0 deletions examples/dungeon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ fn spawn_candles(mut commands: Commands, spritesheet: Res<CandleSpritesheet>) {
color: Color::Srgba(YELLOW),
intensity: 25.0,
falloff: 4.0,
..default()
},
..default()
},))
Expand Down
1 change: 1 addition & 0 deletions examples/multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn setup(mut commands: Commands) {
radius: 50.,
intensity: 5.0,
falloff: 5.0,
..default()
},
transform: Transform::from_xyz(25., 50., 0.),
..default()
Expand Down
1 change: 1 addition & 0 deletions examples/occlusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn setup(mut commands: Commands) {
intensity: 20.0,
radius: 1000.0,
falloff: 10.0,
cast_shadows: true,
..default()
},
transform: Transform {
Expand Down
3 changes: 3 additions & 0 deletions src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct PointLight2d {
/// How quickly illumination from the light should deteriorate over distance.
/// A higher falloff value will result in less illumination at the light's maximum radius.
pub falloff: f32,
/// Whether the light should cast shadows.
pub cast_shadows: bool,
}

impl Default for PointLight2d {
Expand All @@ -42,6 +44,7 @@ impl Default for PointLight2d {
intensity: 1.0,
radius: 0.5,
falloff: 0.0,
cast_shadows: false,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/render/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct ExtractedPointLight2d {
pub color: LinearRgba,
pub intensity: f32,
pub falloff: f32,
pub cast_shadows: u32,
}

#[derive(Component, Default, Clone, ShaderType)]
Expand Down Expand Up @@ -42,6 +43,7 @@ pub fn extract_point_lights(
radius: point_light.radius,
intensity: point_light.intensity,
falloff: point_light.falloff,
cast_shadows: if point_light.cast_shadows { 1 } else { 0 },
});
}

Expand All @@ -53,6 +55,7 @@ pub fn extract_point_lights(
radius: 0.0,
falloff: 0.0,
color: LinearRgba::BLACK,
cast_shadows: 0,
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/render/light_map/light_map.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn fragment(in: FullscreenVertexOutput) -> @location(0) vec4<f32> {
if dist < light.radius {
let raymarch = raymarch(pos, light.center);

if raymarch > 0.0 {
if raymarch > 0.0 || light.cast_shadows == 0 {
lighting_color += light.color.rgb * attenuation(light, dist);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/render/types.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ struct PointLight2d {
radius: f32,
color: vec4<f32>,
intensity: f32,
falloff: f32
falloff: f32,
cast_shadows: u32
}

0 comments on commit 4b64edd

Please sign in to comment.