Skip to content

Commit

Permalink
Add emmissive example.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Jul 2, 2024
1 parent c7ee0b1 commit 4bae681
Show file tree
Hide file tree
Showing 15 changed files with 450 additions and 77 deletions.
1 change: 1 addition & 0 deletions bevy_nannou/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod prelude {
pub use bevy::render::render_asset::*;
pub use bevy::render::render_resource::*;
pub use bevy::winit::UpdateMode;
pub use bevy::core_pipeline::bloom::*;

pub use bevy_nannou_draw::color::*;
pub use bevy_nannou_draw::draw::*;
Expand Down
4 changes: 2 additions & 2 deletions bevy_nannou_draw/src/draw/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ where
self.color(Color::oklcha(l, c, h, alpha))
}

pub fn xyz(self, x: f32, y: f32, z: f32) -> Self {
pub fn cie_xyz(self, x: f32, y: f32, z: f32) -> Self {
self.color(Color::xyz(x, y, z))
}

pub fn xyza(self, x: f32, y: f32, z: f32, w: f32) -> Self {
pub fn cie_xyza(self, x: f32, y: f32, z: f32, w: f32) -> Self {
self.color(Color::xyza(x, y, z, w))
}
}
8 changes: 4 additions & 4 deletions bevy_nannou_draw/src/draw/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,12 +478,12 @@ where
self.map_ty(|ty| SetColor::oklcha(ty, l, c, h, alpha))
}

pub fn xyz(self, x: f32, y: f32, z: f32) -> Self {
self.map_ty(|ty| SetColor::xyz(ty, x, y, z))
pub fn cie_xyz(self, x: f32, y: f32, z: f32) -> Self {
self.map_ty(|ty| SetColor::cie_xyz(ty, x, y, z))
}

pub fn xyza(self, x: f32, y: f32, z: f32, alpha: f32) -> Self {
self.map_ty(|ty| SetColor::xyza(ty, x, y, z, alpha))
pub fn cie_xyza(self, x: f32, y: f32, z: f32, alpha: f32) -> Self {
self.map_ty(|ty| SetColor::cie_xyza(ty, x, y, z, alpha))
}

/// Specify the color as gray scale
Expand Down
4 changes: 2 additions & 2 deletions bevy_nannou_draw/src/draw/properties/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ pub trait SetColor: Sized {
self.color(Color::oklcha(l, c, h, alpha))
}

fn xyz(self, x: f32, y: f32, z: f32) -> Self {
fn cie_xyz(self, x: f32, y: f32, z: f32) -> Self {
self.color(Color::xyz(x, y, z))
}

fn xyza(self, x: f32, y: f32, z: f32, alpha: f32) -> Self {
fn cie_xyza(self, x: f32, y: f32, z: f32, alpha: f32) -> Self {
self.color(Color::xyza(x, y, z, alpha))
}

Expand Down
20 changes: 10 additions & 10 deletions bevy_nannou_draw/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,18 @@ fn update_draw_mesh(
mut meshes: ResMut<Assets<Mesh>>,
) {
for draw in draw_q.iter() {
let (mut window_camera, window_layers) = cameras_q
.iter_mut()
.find(|(camera, _)| {
if let RenderTarget::Window(WindowRef::Entity(window)) = camera.target {
if window == draw.window {
return true;
}
let Some((mut window_camera, window_layers)) = cameras_q.iter_mut().find(|(camera, _)| {
if let RenderTarget::Window(WindowRef::Entity(window)) = camera.target {
if window == draw.window {
return true;
}
}

false
})
.expect("No camera found for window");
false
}) else {
warn!("No camera found for window {:?}", draw.window);
continue;
};

// Reset the clear color each frame.
window_camera.clear_color = ClearColorConfig::None;
Expand Down
3 changes: 3 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ path = "draw/draw_textured_polygon.rs"
[[example]]
name = "draw_transform"
path = "draw/draw_transform.rs"
[[example]]
name = "draw_material_bloom"
path = "draw/draw_material_bloom.rs"

# Laser
[[example]]
Expand Down
35 changes: 34 additions & 1 deletion examples/draw/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,38 @@ fn view(app: &App) {
// Begin drawing
let draw = app.draw();

draw.tri().width(100.0).color(RED);
// Clear the background to blue.
draw.background().color(CORNFLOWER_BLUE);

// Draw a purple triangle in the top left half of the window.
let win = app.window_rect();
draw.tri()
.points(win.bottom_left(), win.top_left(), win.top_right())
.color(VIOLET);

// Draw an ellipse to follow the mouse.
let t = app.elapsed_seconds();
draw.ellipse()
.x_y(app.mouse().x * t.cos(), app.mouse().y)
.radius(win.w() * 0.125 * t.sin())
.color(RED);

// Draw a line!
draw.line()
.weight(10.0 + (t.sin() * 0.5 + 0.5) * 90.0)
.caps_round()
.color(PALE_GOLDENROD)
.points(win.top_left() * t.sin(), win.bottom_right() * t.cos());

// Draw a quad that follows the inverse of the ellipse.
draw.quad()
.x_y(-app.mouse().x, app.mouse().y)
.color(DARK_GREEN)
.rotate(t);

// Draw a rect that follows a different inverse of the ellipse.
draw.rect()
.x_y(app.mouse().y, app.mouse().x)
.w(app.mouse().x * 0.25)
.hsv(t, 1.0, 1.0);
}
58 changes: 58 additions & 0 deletions examples/draw/draw_material_bloom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use nannou::prelude::*;

fn main() {
nannou::app(model).update(update).run();
}

struct Model {
window: Entity,
camera: Entity,
}

fn model(app: &App) -> Model {
let camera = app
.new_camera()
// HDR is required for bloom to work.
.hdr(true)
// Pick a default bloom settings. This also can be configured manually.
.bloom_settings(BloomSettings::OLD_SCHOOL)
.build();

let window = app
.new_window()
.camera(camera)
.size_pixels(1024, 1024)
.view(view)
.build();

Model { camera, window }
}

fn update(app: &App, model: &mut Model) {
let camera = app.camera(model.camera);
let window_rect = app.window_rect();
let norm_mouse_y = (app.mouse().y / window_rect.w()) + 0.5;

camera.bloom_intensity(norm_mouse_y.clamp(0.0, 0.8));
}

fn view(app: &App, model: &Model) {
// Begin drawing
let draw = app.draw();
let window_rect = app.window_rect();
let norm_mouse_x = (app.mouse().x / window_rect.w()) + 0.5;

// Use the normalized mouse coordinate to create an initial color.
let color_hsl = Color::hsl((1.0 - norm_mouse_x) * 360.0, 1.0, 0.5);

// Convert the color to linear RGBA and multiply (for emissives, values outside of 1.0 are used).
let mut color_linear_rgb: LinearRgba = color_hsl.into();
color_linear_rgb = color_linear_rgb * 5.0;

let t = app.elapsed_seconds();

draw.tri()
.width(100.0)
.emissive(color_linear_rgb.into())
.color(WHITE);
}
2 changes: 1 addition & 1 deletion generative_design/color/p_1_0_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn view(app: &App) {
// Prepare to draw.
let draw = app.draw();

let norm_mouse_y = (app.mouse().x / app.window_rect().h()) + 0.5;
let norm_mouse_y = (app.mouse().y / app.window_rect().h()) + 0.5;
draw.background().hsl(norm_mouse_y, 1.0, 0.5);

draw.rect()
Expand Down
11 changes: 10 additions & 1 deletion nannou/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use crate::prelude::bevy_ecs::system::SystemState;
use crate::prelude::render::{NannouMesh, NannouPersistentMesh};
use crate::prelude::NannouMaterialPlugin;
use crate::window::WindowUserFunctions;
use crate::{geom, window};
use crate::{camera, geom, window};

/// The user function type for initialising their model.
pub type ModelFn<Model> = fn(&App) -> Model;
Expand Down Expand Up @@ -654,6 +654,11 @@ impl<'w> App<'w> {
todo!()
}

/// Begin building a new camera.
pub fn new_camera<'a>(&'a self) -> camera::Builder<'a, 'w> {
camera::Builder::new(self)
}

/// Begin building a new window.
pub fn new_window<'a, M>(&'a self) -> window::Builder<'a, 'w, M>
where
Expand Down Expand Up @@ -683,6 +688,10 @@ impl<'w> App<'w> {
window::Window::new(self, id)
}

pub fn camera<'a>(&'a self, id: Entity) -> camera::Camera<'a, 'w> {
camera::Camera::new(self, id)
}

/// Return the [Entity] of the currently focused window.
///
/// **Panics** if there are no windows or if no window is in focus.
Expand Down
Loading

0 comments on commit 4bae681

Please sign in to comment.