Skip to content

Commit 046951f

Browse files
authored
Merge pull request #3 from tychedelia/lights
Add render layers for lights
2 parents a5b80fd + f6f7a9a commit 046951f

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

crates/processing_render/src/lib.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -705,21 +705,25 @@ pub fn image_destroy(entity: Entity) -> error::Result<()> {
705705
}
706706

707707
pub fn light_create_directional(
708+
graphics_entity: Entity,
708709
x: f32,
709710
y: f32,
710711
z: f32,
711712
color: Color,
712713
illuminance: f32,
713714
) -> error::Result<Entity> {
714715
app_mut(|app| {
715-
Ok(app
716-
.world_mut()
717-
.run_system_cached_with(light::create_directional, (x, y, z, color, illuminance))
718-
.unwrap())
716+
app.world_mut()
717+
.run_system_cached_with(
718+
light::create_directional,
719+
(graphics_entity, x, y, z, color, illuminance),
720+
)
721+
.unwrap()
719722
})
720723
}
721724

722725
pub fn light_create_point(
726+
graphics_entity: Entity,
723727
x: f32,
724728
y: f32,
725729
z: f32,
@@ -729,17 +733,17 @@ pub fn light_create_point(
729733
radius: f32,
730734
) -> error::Result<Entity> {
731735
app_mut(|app| {
732-
Ok(app
733-
.world_mut()
736+
app.world_mut()
734737
.run_system_cached_with(
735738
light::create_point,
736-
(x, y, z, color, intensity, range, radius),
739+
(graphics_entity, x, y, z, color, intensity, range, radius),
737740
)
738-
.unwrap())
741+
.unwrap()
739742
})
740743
}
741744

742745
pub fn light_create_spot(
746+
graphics_entity: Entity,
743747
x: f32,
744748
y: f32,
745749
z: f32,
@@ -751,11 +755,11 @@ pub fn light_create_spot(
751755
outer_angle: f32,
752756
) -> error::Result<Entity> {
753757
app_mut(|app| {
754-
Ok(app
755-
.world_mut()
758+
app.world_mut()
756759
.run_system_cached_with(
757760
light::create_spot,
758761
(
762+
graphics_entity,
759763
x,
760764
y,
761765
z,
@@ -767,7 +771,7 @@ pub fn light_create_spot(
767771
outer_angle,
768772
),
769773
)
770-
.unwrap())
774+
.unwrap()
771775
})
772776
}
773777

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! A light in Processing
22
//!
33
4-
use bevy::prelude::*;
4+
use bevy::{camera::visibility::RenderLayers, prelude::*};
5+
6+
use crate::{error::ProcessingError, graphics::Graphics};
57

68
pub struct LightPlugin;
79

@@ -10,26 +12,44 @@ impl Plugin for LightPlugin {
1012
}
1113

1214
pub fn create_directional(
13-
In((px, py, pz, color, illuminance)): In<(f32, f32, f32, Color, f32)>,
15+
In((entity, px, py, pz, color, illuminance)): In<(Entity, f32, f32, f32, Color, f32)>,
1416
mut commands: Commands,
15-
) -> Entity {
16-
commands
17+
graphics: Query<&RenderLayers, With<Graphics>>,
18+
) -> Result<Entity, ProcessingError> {
19+
let layer = graphics
20+
.get(entity)
21+
.map_err(|_| ProcessingError::GraphicsNotFound)?;
22+
Ok(commands
1723
.spawn((
1824
DirectionalLight {
1925
illuminance,
2026
color,
2127
..default()
2228
},
2329
Transform::from_xyz(px, py, pz),
30+
layer.clone(),
2431
))
25-
.id()
32+
.id())
2633
}
2734

2835
pub fn create_point(
29-
In((px, py, pz, color, intensity, range, radius)): In<(f32, f32, f32, Color, f32, f32, f32)>,
36+
In((entity, px, py, pz, color, intensity, range, radius)): In<(
37+
Entity,
38+
f32,
39+
f32,
40+
f32,
41+
Color,
42+
f32,
43+
f32,
44+
f32,
45+
)>,
3046
mut commands: Commands,
31-
) -> Entity {
32-
commands
47+
graphics: Query<&RenderLayers, With<Graphics>>,
48+
) -> Result<Entity, ProcessingError> {
49+
let layer = graphics
50+
.get(entity)
51+
.map_err(|_| ProcessingError::GraphicsNotFound)?;
52+
Ok(commands
3353
.spawn((
3454
PointLight {
3555
intensity,
@@ -39,12 +59,14 @@ pub fn create_point(
3959
..default()
4060
},
4161
Transform::from_xyz(px, py, pz),
62+
layer.clone(),
4263
))
43-
.id()
64+
.id())
4465
}
4566

4667
pub fn create_spot(
47-
In((px, py, pz, color, intensity, range, radius, inner_angle, outer_angle)): In<(
68+
In((entity, px, py, pz, color, intensity, range, radius, inner_angle, outer_angle)): In<(
69+
Entity,
4870
f32,
4971
f32,
5072
f32,
@@ -56,8 +78,12 @@ pub fn create_spot(
5678
f32,
5779
)>,
5880
mut commands: Commands,
59-
) -> Entity {
60-
commands
81+
graphics: Query<&RenderLayers, With<Graphics>>,
82+
) -> Result<Entity, ProcessingError> {
83+
let layer = graphics
84+
.get(entity)
85+
.map_err(|_| ProcessingError::GraphicsNotFound)?;
86+
Ok(commands
6187
.spawn((
6288
SpotLight {
6389
color,
@@ -69,6 +95,7 @@ pub fn create_spot(
6995
..default()
7096
},
7197
Transform::from_xyz(px, py, pz),
98+
layer.clone(),
7299
))
73-
.id()
100+
.id())
74101
}

examples/lights.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ fn sketch() -> error::Result<()> {
3232
// We will only declare lights in `setup`
3333
// rather than calling some sort of `light()` method inside of `draw`
3434
let _dir_light = light_create_directional(
35+
graphics,
3536
0.0,
3637
0.0,
3738
0.0,
3839
bevy::color::Color::srgb(1.0, 0.0, 0.0),
3940
1000.0,
40-
);
41+
)?;
4142

4243
graphics_mode_3d(graphics)?;
4344
graphics_camera_position(graphics, 100.0, 100.0, 300.0)?;

0 commit comments

Comments
 (0)