Skip to content

Commit 8eae638

Browse files
committed
Create creation methods for directional, point, and spot lights
1 parent f18b73c commit 8eae638

File tree

3 files changed

+147
-26
lines changed

3 files changed

+147
-26
lines changed

crates/processing_render/src/lib.rs

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ use tracing::debug;
2525
use crate::geometry::{AttributeFormat, AttributeValue};
2626
use crate::graphics::flush;
2727
use crate::{
28-
graphics::GraphicsPlugin,
29-
image::ImagePlugin,
30-
light::{LightPlugin, LightType},
31-
render::command::DrawCommand,
28+
graphics::GraphicsPlugin, image::ImagePlugin, light::LightPlugin, render::command::DrawCommand,
3229
surface::SurfacePlugin,
3330
};
3431

@@ -707,12 +704,84 @@ pub fn image_destroy(entity: Entity) -> error::Result<()> {
707704
})
708705
}
709706

710-
// pub fn geometry_box(width: f32, height: f32, depth: f32) -> error::Result<Entity> {
711-
pub fn light_create(light_type: LightType, x: f32, y: f32, z: f32) -> error::Result<Entity> {
707+
pub fn light_create_directional(
708+
x: f32,
709+
y: f32,
710+
z: f32,
711+
r: f32,
712+
g: f32,
713+
b: f32,
714+
a: f32,
715+
illuminance: f32,
716+
) -> error::Result<Entity> {
717+
app_mut(|app| {
718+
Ok(app
719+
.world_mut()
720+
.run_system_cached_with(
721+
light::create_directional,
722+
(x, y, z, r, g, b, a, illuminance),
723+
)
724+
.unwrap())
725+
})
726+
}
727+
728+
pub fn light_create_point(
729+
x: f32,
730+
y: f32,
731+
z: f32,
732+
r: f32,
733+
g: f32,
734+
b: f32,
735+
a: f32,
736+
intensity: f32,
737+
range: f32,
738+
radius: f32,
739+
) -> error::Result<Entity> {
712740
app_mut(|app| {
713741
Ok(app
714742
.world_mut()
715-
.run_system_cached_with(light::create, (light_type, x, y, z))
743+
.run_system_cached_with(
744+
light::create_point,
745+
(x, y, z, r, g, b, a, intensity, range, radius),
746+
)
747+
.unwrap())
748+
})
749+
}
750+
751+
pub fn light_create_spot(
752+
x: f32,
753+
y: f32,
754+
z: f32,
755+
r: f32,
756+
g: f32,
757+
b: f32,
758+
a: f32,
759+
intensity: f32,
760+
range: f32,
761+
radius: f32,
762+
inner_angle: f32,
763+
outer_angle: f32,
764+
) -> error::Result<Entity> {
765+
app_mut(|app| {
766+
Ok(app
767+
.world_mut()
768+
.run_system_cached_with(
769+
light::create_spot,
770+
(
771+
x,
772+
y,
773+
z,
774+
r,
775+
g,
776+
b,
777+
a,
778+
intensity,
779+
range,
780+
radius,
781+
inner_angle,
782+
outer_angle,
783+
),
784+
)
716785
.unwrap())
717786
})
718787
}

crates/processing_render/src/light.rs

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,80 @@ impl Plugin for LightPlugin {
99
fn build(&self, _app: &mut App) {}
1010
}
1111

12-
#[derive(Component)]
13-
pub struct Light {
14-
pub light_type: LightType,
15-
pub pos: Vec3,
12+
pub fn create_directional(
13+
In((px, py, pz, r, g, b, a, illuminance)): In<(f32, f32, f32, f32, f32, f32, f32, f32)>,
14+
mut commands: Commands,
15+
) -> Entity {
16+
commands
17+
.spawn((
18+
DirectionalLight {
19+
illuminance,
20+
color: Color::srgba(r, g, b, a),
21+
..default()
22+
},
23+
Transform::from_xyz(px, py, pz),
24+
))
25+
.id()
1626
}
1727

18-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19-
pub enum LightType {
20-
Directional,
21-
Point,
22-
Spot,
28+
pub fn create_point(
29+
In((px, py, pz, r, g, b, a, intensity, range, radius)): In<(
30+
f32,
31+
f32,
32+
f32,
33+
f32,
34+
f32,
35+
f32,
36+
f32,
37+
f32,
38+
f32,
39+
f32,
40+
)>,
41+
mut commands: Commands,
42+
) -> Entity {
43+
commands
44+
.spawn((
45+
PointLight {
46+
intensity,
47+
color: Color::srgba(r, g, b, a),
48+
range,
49+
radius,
50+
..default()
51+
},
52+
Transform::from_xyz(px, py, pz),
53+
))
54+
.id()
2355
}
2456

25-
pub fn create(
26-
In((light_type, x, y, z)): In<(LightType, f32, f32, f32)>,
57+
pub fn create_spot(
58+
In((px, py, pz, r, g, b, a, intensity, range, radius, inner_angle, outer_angle)): In<(
59+
f32,
60+
f32,
61+
f32,
62+
f32,
63+
f32,
64+
f32,
65+
f32,
66+
f32,
67+
f32,
68+
f32,
69+
f32,
70+
f32,
71+
)>,
2772
mut commands: Commands,
2873
) -> Entity {
29-
match light_type {
30-
LightType::Directional => commands
31-
.spawn((DirectionalLight::default(), Transform::from_xyz(x, y, z)))
32-
.id(),
33-
_ => commands.spawn(DirectionalLight::default()).id(),
34-
}
74+
commands
75+
.spawn((
76+
SpotLight {
77+
color: Color::srgba(r, g, b, a),
78+
intensity,
79+
range,
80+
radius,
81+
inner_angle,
82+
outer_angle,
83+
..default()
84+
},
85+
Transform::from_xyz(px, py, pz),
86+
))
87+
.id()
3588
}

examples/lights.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ mod glfw;
22

33
use glfw::GlfwContext;
44
use processing::prelude::*;
5-
use processing_render::light::LightType;
65
use processing_render::render::command::DrawCommand;
76

87
fn main() {
@@ -32,7 +31,7 @@ fn sketch() -> error::Result<()> {
3231

3332
// We will only declare lights in `setup`
3433
// rather than calling some sort of `light()` method inside of `draw`
35-
let _point_light = light_create(LightType::Point, 0.0, 0.0, 0.0)?;
34+
let _dir_light = light_create_directional(0.0, 0.0, 0.0, 0.5, 0.43, 1.0, 1.0, 1000.0);
3635

3736
graphics_mode_3d(graphics)?;
3837
graphics_camera_position(graphics, 100.0, 100.0, 300.0)?;

0 commit comments

Comments
 (0)