Skip to content

Commit 0783556

Browse files
committed
Directional, Point, and Spot Lights
1 parent 55f0741 commit 0783556

File tree

4 files changed

+181
-5
lines changed

4 files changed

+181
-5
lines changed

crates/processing_render/src/lib.rs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod error;
33
pub mod geometry;
44
mod graphics;
55
pub mod image;
6+
pub mod light;
67
pub mod render;
78
mod surface;
89
pub mod transform;
@@ -25,7 +26,7 @@ use tracing::debug;
2526
use crate::geometry::{AttributeFormat, AttributeValue};
2627
use crate::graphics::flush;
2728
use crate::{
28-
graphics::GraphicsPlugin, image::ImagePlugin, render::command::DrawCommand,
29+
graphics::GraphicsPlugin, image::ImagePlugin, light::LightPlugin, render::command::DrawCommand,
2930
surface::SurfacePlugin,
3031
};
3132

@@ -248,6 +249,7 @@ fn create_app(config: Config) -> App {
248249
GraphicsPlugin,
249250
SurfacePlugin,
250251
geometry::GeometryPlugin,
252+
LightPlugin,
251253
));
252254
app.add_systems(First, (clear_transient_meshes, activate_cameras))
253255
.add_systems(Update, flush_draw_commands.before(AssetEventSystems));
@@ -774,6 +776,88 @@ pub fn image_destroy(entity: Entity) -> error::Result<()> {
774776
})
775777
}
776778

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> {
812+
app_mut(|app| {
813+
Ok(app
814+
.world_mut()
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+
)
857+
.unwrap())
858+
})
859+
}
860+
777861
pub fn geometry_layout_create() -> error::Result<Entity> {
778862
app_mut(|app| {
779863
Ok(app
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//! A light in Processing
2+
//!
3+
4+
use bevy::prelude::*;
5+
6+
pub struct LightPlugin;
7+
8+
impl Plugin for LightPlugin {
9+
fn build(&self, _app: &mut App) {}
10+
}
11+
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()
26+
}
27+
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()
55+
}
56+
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+
)>,
72+
mut commands: Commands,
73+
) -> Entity {
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()
88+
}

docs/api.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ rendering texture that the camera draws to (`ViewTarget`), which is not typicall
3434
For consistency, all image functions should accept a graphics object, although its internal image representation is
3535
not guaranteed to be the same as a user-created image.
3636

37+
### Light
38+
39+
[//]: # (TODO: Document Image API object)
40+
3741
### Image
3842

3943
Images are 2D or 3D arrays of pixels that can be drawn onto surfaces. They can be created from files, generated procedurally,
@@ -85,4 +89,4 @@ can also be used to implement 2D image processing effects, etc.
8589

8690
### Shader
8791

88-
[//]: # (TODO: Document Shader API object, do we even need this with a sufficiently robust Material API?)
92+
[//]: # (TODO: Document Shader API object, do we even need this with a sufficiently robust Material API?)

examples/lights.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ fn sketch() -> error::Result<()> {
2929
let graphics = graphics_create(surface, width, height)?;
3030
let box_geo = geometry_box(10.0, 10.0, 10.0)?;
3131

32-
let point_light = light_create()?;
32+
// We will only declare lights in `setup`
33+
// rather than calling some sort of `light()` method inside of `draw`
34+
let _dir_light = light_create_directional(0.0, 0.0, 0.0, 0.5, 0.43, 1.0, 1.0, 1000.0);
3335

3436
graphics_mode_3d(graphics)?;
3537
graphics_camera_position(graphics, 100.0, 100.0, 300.0)?;
@@ -45,8 +47,6 @@ fn sketch() -> error::Result<()> {
4547
DrawCommand::BackgroundColor(bevy::color::Color::srgb(0.18, 0.20, 0.15)),
4648
)?;
4749

48-
graphics_record_command(graphics, DrawCommand::Light(point_light))?;
49-
5050
graphics_record_command(graphics, DrawCommand::PushMatrix)?;
5151
graphics_record_command(graphics, DrawCommand::Translate { x: 25.0, y: 25.0 })?;
5252
graphics_record_command(graphics, DrawCommand::Rotate { angle })?;

0 commit comments

Comments
 (0)