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
17 changes: 16 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,10 @@ 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, LightType},
render::command::DrawCommand,
surface::SurfacePlugin,
};

Expand Down Expand Up @@ -247,6 +251,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 +707,16 @@ pub fn image_destroy(entity: Entity) -> error::Result<()> {
})
}

// pub fn geometry_box(width: f32, height: f32, depth: f32) -> error::Result<Entity> {
pub fn light_create(light_type: LightType, x: f32, y: f32, z: f32) -> error::Result<Entity> {
app_mut(|app| {
Ok(app
.world_mut()
.run_system_cached_with(light::create, (light_type, x, y, z))
.unwrap())
})
}

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

use bevy::prelude::*;

pub struct LightPlugin;

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

#[derive(Component)]
pub struct Light {
pub light_type: LightType,
pub pos: Vec3,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LightType {
Ambient,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ambient lights are a bit weird in that they are added to the camera rather than spawned independently in the world. In that sense, they're more like a global graphics setting. I think we should punt on ambient light for now or add it as a setting for Graphics.

Directional,
Point,
Spot,
}

pub fn create(
In((light_type, x, y, z)): In<(LightType, f32, f32, f32)>,
mut commands: Commands,
) -> Entity {
// let light = Light {
// light_type: light_type,
// pos: Vec3::new(x, y, z),
// };
// commands.spawn(light).id()

match light_type {
LightType::Directional => commands
.spawn((DirectionalLight::default(), Transform::from_xyz(x, y, z)))
.id(),
_ => commands.spawn(AmbientLight::default()).id(),
}
}
1 change: 1 addition & 0 deletions crates/processing_render/src/render/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub enum DrawCommand {
angle: f32,
},
Geometry(Entity),
// Light(Entity),
}

#[derive(Debug, Default, Component)]
Expand Down
21 changes: 20 additions & 1 deletion crates/processing_render/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub fn flush_draw_commands(
>,
p_images: Query<&Image>,
p_geometries: Query<&Geometry>,
// p_lights: Query<&Light>,
) {
for (graphics_entity, mut cmd_buffer, mut state, render_layers, projection, camera_transform) in
graphics.iter_mut()
Expand Down Expand Up @@ -234,7 +235,25 @@ pub fn flush_draw_commands(
));

batch.draw_index += 1;
}
} // DrawCommand::Light(entity) => {
// let Some(light) = p_lights.get(entity).ok() else {
// warn!("Could not find Light for entity {:?}", entity);
// continue;
// };

// flush_batch(&mut res, &mut batch);

// let pos = light.pos;
// res.commands.spawn((
// PointLight {
// color: Color::srgba(1.0, 0.25, 0.44, 1.0),
// ..default()
// },
// Transform::from_xyz(pos.x, pos.y, pos.z),
// ));

// batch.draw_index += 1;
// }
}
}

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?)
69 changes: 69 additions & 0 deletions examples/lights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
mod glfw;

use glfw::GlfwContext;
use processing::prelude::*;
use processing_render::light::LightType;
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 _point_light = light_create(LightType::Point, 0.0, 0.0, 0.0)?;
Comment on lines +33 to +35
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally sure, but I think we may want to consider including color as a required constructor arg since all three types share that.


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(())
}