Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ path = "examples/animated_mesh.rs"
name = "custom_attribute"
path = "examples/custom_attribute.rs"

[[example]]
name = "lights"
path = "examples/lights.rs"

[profile.wasm-release]
inherits = "release"
opt-level = "z"
Expand Down
75 changes: 74 additions & 1 deletion crates/processing_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod error;
pub mod geometry;
mod graphics;
pub mod image;
pub mod light;
pub mod render;
mod surface;

Expand All @@ -24,7 +25,7 @@ use tracing::debug;
use crate::geometry::{AttributeFormat, AttributeValue};
use crate::graphics::flush;
use crate::{
graphics::GraphicsPlugin, image::ImagePlugin, render::command::DrawCommand,
graphics::GraphicsPlugin, image::ImagePlugin, light::LightPlugin, render::command::DrawCommand,
surface::SurfacePlugin,
};

Expand Down Expand Up @@ -247,6 +248,7 @@ fn create_app(config: Config) -> App {
GraphicsPlugin,
SurfacePlugin,
geometry::GeometryPlugin,
LightPlugin,
));
app.add_systems(First, (clear_transient_meshes, activate_cameras))
.add_systems(Update, flush_draw_commands.before(AssetEventSystems));
Expand Down Expand Up @@ -702,6 +704,77 @@ pub fn image_destroy(entity: Entity) -> error::Result<()> {
})
}

pub fn light_create_directional(
graphics_entity: Entity,
x: f32,
y: f32,
z: f32,
color: Color,
illuminance: f32,
) -> error::Result<Entity> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(
light::create_directional,
(graphics_entity, x, y, z, color, illuminance),
)
.unwrap()
})
}

pub fn light_create_point(
graphics_entity: Entity,
x: f32,
y: f32,
z: f32,
color: Color,
intensity: f32,
range: f32,
radius: f32,
) -> error::Result<Entity> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(
light::create_point,
(graphics_entity, x, y, z, color, intensity, range, radius),
)
.unwrap()
})
}

pub fn light_create_spot(
graphics_entity: Entity,
x: f32,
y: f32,
z: f32,
color: Color,
intensity: f32,
range: f32,
radius: f32,
inner_angle: f32,
outer_angle: f32,
) -> error::Result<Entity> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(
light::create_spot,
(
graphics_entity,
x,
y,
z,
color,
intensity,
range,
radius,
inner_angle,
outer_angle,
),
)
.unwrap()
})
}

pub fn geometry_layout_create() -> error::Result<Entity> {
app_mut(|app| {
Ok(app
Expand Down
101 changes: 101 additions & 0 deletions crates/processing_render/src/light.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//! A light in Processing
//!

use bevy::{camera::visibility::RenderLayers, prelude::*};

use crate::{error::ProcessingError, graphics::Graphics};

pub struct LightPlugin;

impl Plugin for LightPlugin {
fn build(&self, _app: &mut App) {}
}

pub fn create_directional(
In((entity, px, py, pz, color, illuminance)): In<(Entity, f32, f32, f32, Color, f32)>,
mut commands: Commands,
graphics: Query<&RenderLayers, With<Graphics>>,
) -> Result<Entity, ProcessingError> {
let layer = graphics
.get(entity)
.map_err(|_| ProcessingError::GraphicsNotFound)?;
Ok(commands
.spawn((
DirectionalLight {
illuminance,
color,
..default()
},
Transform::from_xyz(px, py, pz),
layer.clone(),
))
.id())
}

pub fn create_point(
In((entity, px, py, pz, color, intensity, range, radius)): In<(
Entity,
f32,
f32,
f32,
Color,
f32,
f32,
f32,
)>,
mut commands: Commands,
graphics: Query<&RenderLayers, With<Graphics>>,
) -> Result<Entity, ProcessingError> {
let layer = graphics
.get(entity)
.map_err(|_| ProcessingError::GraphicsNotFound)?;
Ok(commands
.spawn((
PointLight {
intensity,
color,
range,
radius,
..default()
},
Transform::from_xyz(px, py, pz),
layer.clone(),
))
.id())
}

pub fn create_spot(
In((entity, px, py, pz, color, intensity, range, radius, inner_angle, outer_angle)): In<(
Entity,
f32,
f32,
f32,
Color,
f32,
f32,
f32,
f32,
f32,
)>,
mut commands: Commands,
graphics: Query<&RenderLayers, With<Graphics>>,
) -> Result<Entity, ProcessingError> {
let layer = graphics
.get(entity)
.map_err(|_| ProcessingError::GraphicsNotFound)?;
Ok(commands
.spawn((
SpotLight {
color,
intensity,
range,
radius,
inner_angle,
outer_angle,
..default()
},
Transform::from_xyz(px, py, pz),
layer.clone(),
))
.id())
}
2 changes: 1 addition & 1 deletion crates/processing_render/src/render/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl MaterialKey {
pub fn to_material(&self) -> StandardMaterial {
StandardMaterial {
base_color: Color::WHITE,
unlit: true,
unlit: false,
cull_mode: None,
base_color_texture: self.background_image.clone(),
alpha_mode: if self.transparent {
Expand Down
6 changes: 5 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ rendering texture that the camera draws to (`ViewTarget`), which is not typicall
For consistency, all image functions should accept a graphics object, although its internal image representation is
not guaranteed to be the same as a user-created image.

### Light

[//]: # (TODO: Document Image API object)

### Image

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

### Shader

[//]: # (TODO: Document Shader API object, do we even need this with a sufficiently robust Material API?)
[//]: # (TODO: Document Shader API object, do we even need this with a sufficiently robust Material API?)
75 changes: 75 additions & 0 deletions examples/lights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
mod glfw;

use glfw::GlfwContext;
use processing::prelude::*;
use processing_render::render::command::DrawCommand;

fn main() {
match sketch() {
Ok(_) => {
eprintln!("Sketch completed successfully");
exit(0).unwrap();
}
Err(e) => {
eprintln!("Sketch error: {:?}", e);
exit(1).unwrap();
}
};
}

fn sketch() -> error::Result<()> {
let mut glfw_ctx = GlfwContext::new(400, 400)?;
init(Config::default())?;

let width = 400;
let height = 400;
let scale_factor = 1.0;

let surface = glfw_ctx.create_surface(width, height, scale_factor)?;
let graphics = graphics_create(surface, width, height)?;
let box_geo = geometry_box(10.0, 10.0, 10.0)?;

// We will only declare lights in `setup`
// rather than calling some sort of `light()` method inside of `draw`
let _dir_light = light_create_directional(
graphics,
0.0,
0.0,
0.0,
bevy::color::Color::srgb(1.0, 0.0, 0.0),
1000.0,
)?;

graphics_mode_3d(graphics)?;
graphics_camera_position(graphics, 100.0, 100.0, 300.0)?;
graphics_camera_look_at(graphics, 0.0, 0.0, 0.0)?;

let mut angle = 0.0;

while glfw_ctx.poll_events() {
graphics_begin_draw(graphics)?;

graphics_record_command(
graphics,
DrawCommand::BackgroundColor(bevy::color::Color::srgb(0.18, 0.20, 0.15)),
)?;

graphics_record_command(graphics, DrawCommand::PushMatrix)?;
graphics_record_command(graphics, DrawCommand::Translate { x: 25.0, y: 25.0 })?;
graphics_record_command(graphics, DrawCommand::Rotate { angle })?;
graphics_record_command(graphics, DrawCommand::Geometry(box_geo))?;
graphics_record_command(graphics, DrawCommand::PopMatrix)?;

graphics_record_command(graphics, DrawCommand::PushMatrix)?;
graphics_record_command(graphics, DrawCommand::Translate { x: -25.0, y: 20.0 })?;
graphics_record_command(graphics, DrawCommand::Scale { x: 1.5, y: 2.0 })?;
graphics_record_command(graphics, DrawCommand::Rotate { angle })?;
graphics_record_command(graphics, DrawCommand::Geometry(box_geo))?;
graphics_record_command(graphics, DrawCommand::PopMatrix)?;

graphics_end_draw(graphics)?;

angle += 0.02;
}
Ok(())
}