Skip to content

Commit

Permalink
Split up shape from light occluder
Browse files Browse the repository at this point in the history
This will allow us to encode more information into an occluder, such as
information on how shadows should be cast.
  • Loading branch information
jgayfer committed Aug 5, 2024
1 parent 2d9507c commit 7d9339e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
30 changes: 20 additions & 10 deletions examples/occlusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,50 @@ fn setup(mut commands: Commands) {
});

commands.spawn(LightOccluder2dBundle {
light_occluder: LightOccluder2d::Rectangle {
half_size: Vec2::splat(25.0),
light_occluder: LightOccluder2d {
shape: LightOccluder2dShape::Rectangle {
half_size: Vec2::splat(25.0),
},
},
transform: Transform::from_xyz(-400.0, -50.0, 0.0),
..default()
});

commands.spawn(LightOccluder2dBundle {
light_occluder: LightOccluder2d::Rectangle {
half_size: Vec2::splat(25.0),
light_occluder: LightOccluder2d {
shape: LightOccluder2dShape::Rectangle {
half_size: Vec2::splat(25.0),
},
},
transform: Transform::from_xyz(-200.0, -50.0, 0.0),
..default()
});

commands.spawn(LightOccluder2dBundle {
light_occluder: LightOccluder2d::Rectangle {
half_size: Vec2::splat(25.0),
light_occluder: LightOccluder2d {
shape: LightOccluder2dShape::Rectangle {
half_size: Vec2::splat(25.0),
},
},
transform: Transform::from_xyz(0.0, -50.0, 0.0),
..default()
});

commands.spawn(LightOccluder2dBundle {
light_occluder: LightOccluder2d::Rectangle {
half_size: Vec2::splat(25.0),
light_occluder: LightOccluder2d {
shape: LightOccluder2dShape::Rectangle {
half_size: Vec2::splat(25.0),
},
},
transform: Transform::from_xyz(200.0, -50.0, 0.0),
..default()
});

commands.spawn(LightOccluder2dBundle {
light_occluder: LightOccluder2d::Rectangle {
half_size: Vec2::splat(25.0),
light_occluder: LightOccluder2d {
shape: LightOccluder2dShape::Rectangle {
half_size: Vec2::splat(25.0),
},
},
transform: Transform::from_xyz(400.0, -50.0, 0.0),
..default()
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ mod render;
/// A module which exports commonly used dependencies.
pub mod prelude {
pub use crate::light::{AmbientLight2d, PointLight2d, PointLight2dBundle};
pub use crate::occluder::{LightOccluder2d, LightOccluder2dBundle};
pub use crate::occluder::{LightOccluder2d, LightOccluder2dBundle, LightOccluder2dShape};
pub use crate::plugin::Light2dPlugin;
}
12 changes: 9 additions & 3 deletions src/occluder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ use bevy::{
/// A light occluder that prevents light passing through it, casting shadows.
///
/// This is commonly used as a component within [`LightOcluder2dBundle`].
#[derive(Component)]
pub enum LightOccluder2d {
#[derive(Default, Component)]
pub struct LightOccluder2d {
/// The shape of the light occluder.
pub shape: LightOccluder2dShape,
}

/// Shape data for a light occluder.
pub enum LightOccluder2dShape {
/// A rectangular light occluder.
Rectangle {
/// Half of the width and height of the rectangle.
half_size: Vec2,
},
}

impl Default for LightOccluder2d {
impl Default for LightOccluder2dShape {
fn default() -> Self {
Self::Rectangle {
half_size: Vec2::splat(0.0),
Expand Down
8 changes: 4 additions & 4 deletions src/render/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::{

use crate::{
light::{AmbientLight2d, PointLight2d},
occluder::LightOccluder2d,
occluder::{LightOccluder2d, LightOccluder2dShape},
};

#[derive(Component, Default, Clone, ShaderType)]
Expand Down Expand Up @@ -67,9 +67,9 @@ pub fn extract_light_occluders(
continue;
}

let extracted_occluder = match light_occluder {
LightOccluder2d::Rectangle { half_size } => ExtractedLightOccluder2d {
half_size: *half_size,
let extracted_occluder = match light_occluder.shape {
LightOccluder2dShape::Rectangle { half_size } => ExtractedLightOccluder2d {
half_size,
center: global_transform.translation().xy(),
},
};
Expand Down

0 comments on commit 7d9339e

Please sign in to comment.