Skip to content

Commit 656a87c

Browse files
committed
Create creation methods for directional, point, and spot lights
1 parent cae5247 commit 656a87c

3 files changed

Lines changed: 147 additions & 26 deletions

File tree

crates/processing_render/src/lib.rs

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

@@ -779,12 +776,84 @@ pub fn image_destroy(entity: Entity) -> error::Result<()> {
779776
})
780777
}
781778

782-
// pub fn geometry_box(width: f32, height: f32, depth: f32) -> error::Result<Entity> {
783-
pub fn light_create(light_type: LightType, x: f32, y: f32, z: f32) -> error::Result<Entity> {
779+
pub fn light_create_directional(
780+
x: f32,
781+
y: f32,
782+
z: f32,
783+
r: f32,
784+
g: f32,
785+
b: f32,
786+
a: f32,
787+
illuminance: f32,
788+
) -> error::Result<Entity> {
789+
app_mut(|app| {
790+
Ok(app
791+
.world_mut()
792+
.run_system_cached_with(
793+
light::create_directional,
794+
(x, y, z, r, g, b, a, illuminance),
795+
)
796+
.unwrap())
797+
})
798+
}
799+
800+
pub fn light_create_point(
801+
x: f32,
802+
y: f32,
803+
z: f32,
804+
r: f32,
805+
g: f32,
806+
b: f32,
807+
a: f32,
808+
intensity: f32,
809+
range: f32,
810+
radius: f32,
811+
) -> error::Result<Entity> {
784812
app_mut(|app| {
785813
Ok(app
786814
.world_mut()
787-
.run_system_cached_with(light::create, (light_type, x, y, z))
815+
.run_system_cached_with(
816+
light::create_point,
817+
(x, y, z, r, g, b, a, intensity, range, radius),
818+
)
819+
.unwrap())
820+
})
821+
}
822+
823+
pub fn light_create_spot(
824+
x: f32,
825+
y: f32,
826+
z: f32,
827+
r: f32,
828+
g: f32,
829+
b: f32,
830+
a: f32,
831+
intensity: f32,
832+
range: f32,
833+
radius: f32,
834+
inner_angle: f32,
835+
outer_angle: f32,
836+
) -> error::Result<Entity> {
837+
app_mut(|app| {
838+
Ok(app
839+
.world_mut()
840+
.run_system_cached_with(
841+
light::create_spot,
842+
(
843+
x,
844+
y,
845+
z,
846+
r,
847+
g,
848+
b,
849+
a,
850+
intensity,
851+
range,
852+
radius,
853+
inner_angle,
854+
outer_angle,
855+
),
856+
)
788857
.unwrap())
789858
})
790859
}

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)