Skip to content

Commit

Permalink
Merge pull request #968 from tychedelia/966-use-bevy-meshes-as-nannou…
Browse files Browse the repository at this point in the history
…s-rendering-backend

966 use bevy meshes as nannous rendering backend
  • Loading branch information
tychedelia authored Mar 25, 2024
2 parents 4a88a15 + c42474b commit d7b3ddb
Show file tree
Hide file tree
Showing 27 changed files with 1,220 additions and 2,142 deletions.
991 changes: 533 additions & 458 deletions Cargo.lock

Large diffs are not rendered by default.

70 changes: 61 additions & 9 deletions bevy_nannou/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use bevy::core_pipeline::bloom::{BloomCompositeMode, BloomPrefilterSettings, BloomSettings};
use bevy::core_pipeline::clear_color::ClearColorConfig;
use bevy::core_pipeline::tonemapping::Tonemapping;
use bevy::prelude::shape::Torus;
use bevy::prelude::*;
use bevy_nannou::NannouPlugin;
use bevy_nannou_draw::color::{RED, SALMON, SEAGREEN, SEASHELL, SKYBLUE};
use bevy_nannou_render::NannouMesh;

pub fn main() {
App::new()
Expand All @@ -21,6 +24,40 @@ fn startup(mut commands: Commands, assets: Res<AssetServer>, mut meshes: ResMut<
color: Color::WHITE,
brightness: 1.0,
});

commands.spawn(PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
intensity: 10_000_000_000.,
range: 100.0,
..default()
},
transform: Transform::from_xyz(0.0, 500.0, 100.0),
..default()
});
//
// commands.spawn(DirectionalLightBundle {
// directional_light: DirectionalLight {
// color: Color::rgb(1.0, 0.00, 0.00),
// shadows_enabled: true,
// ..default()
// },
// transform: Transform::from_xyz(100.0, 50.0, 100.0)
// .looking_at(Vec3::new(-0.15, -0.05, 0.25), Vec3::Y),
// ..default()
// });
//
// commands.spawn(DirectionalLightBundle {
// directional_light: DirectionalLight {
// color: Color::rgb(0.0, 0.00, 1.00),
// shadows_enabled: true,
// ..default()
// },
// transform: Transform::from_xyz(-100.0, -50.0, 100.0)
// .looking_at(Vec3::new(-0.15, -0.05, 0.25), Vec3::Y),
// ..default()
// });

commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(Torus {
radius: 100.0,
Expand All @@ -30,10 +67,10 @@ fn startup(mut commands: Commands, assets: Res<AssetServer>, mut meshes: ResMut<
..default()
});

commands.spawn(
(Camera3dBundle {
commands.spawn((
Camera3dBundle {
camera: Camera {
hdr: false,
hdr: true,
..Default::default()
},
camera_3d: Camera3d {
Expand All @@ -42,20 +79,32 @@ fn startup(mut commands: Commands, assets: Res<AssetServer>, mut meshes: ResMut<
..Default::default()
},
transform: Transform::from_xyz(0.0, 0.0, -10.0).looking_at(Vec3::ZERO, Vec3::Z),
tonemapping: Tonemapping::TonyMcMapface, // 2. Using a tonemapper that desaturates to white is recommended
projection: OrthographicProjection {
scale: 1.0,
..Default::default()
}
.into(),
..Default::default()
}),
);
},
BloomSettings {
intensity: 0.09,
low_frequency_boost: 0.7,
low_frequency_boost_curvature: 0.95,
high_pass_frequency: 1.0,
prefilter_settings: BloomPrefilterSettings {
threshold: 0.1,
threshold_softness: 0.4,
},
composite_mode: BloomCompositeMode::Additive,
},
));

let handle = assets.load("images/nannou.png");
commands.insert_resource(MyTexture(handle));
}

fn update_mesh(mut handles: Query<(&Handle<Mesh>, &mut Transform)>) {
fn update_mesh(mut handles: Query<(&Handle<Mesh>, &mut Transform), Without<NannouMesh>>) {
for (_, mut transform) in handles.iter_mut() {
transform.translation.x += 1.0;
transform.translation.y += 1.0;
Expand All @@ -79,10 +128,13 @@ fn update_draw(

// TODO: why is the texture rotated?
// draw.texture(texture_handle.clone(), texture.clone());
draw.ellipse().w_h(100.0, 100.0).color(SALMON);
draw.ellipse().w_h(100.0, 100.0).color(Color::SALMON);
draw.ellipse()
.x(100.0 + time.elapsed().as_millis() as f32 / 100.0)
.w_h(100.0, 100.0)
.color(SEASHELL);
draw.ellipse().x(-100.0).w_h(100.0, 100.0).color(SKYBLUE);
.color(Color::SEA_GREEN);
draw.ellipse()
.x(-100.0)
.w_h(100.0, 100.0)
.color(Color::BISQUE);
}
78 changes: 39 additions & 39 deletions bevy_nannou_draw/src/draw/background.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::draw::properties::ColorScalar;
use crate::draw::Draw;
use nannou_core::color::{self, IntoLinSrgba, Srgb, Srgba};
use bevy::prelude::Color;
use nannou_core::color::{self, IntoColor, Srgb, Srgba};

/// A type used to update the background colour.
pub struct Background<'a> {
Expand All @@ -20,22 +20,22 @@ impl<'a> Background<'a> {
/// Colors that have no alpha channel will be given an opaque alpha channel value `1.0`.
pub fn color<C>(self, color: C) -> Self
where
C: IntoLinSrgba<ColorScalar>,
C: Into<Color>,
{
if let Ok(mut state) = self.draw.state.try_write() {
state.background_color = Some(color.into_lin_srgba());
state.background_color = Some(color.into());
}
self
}

/// Specify the color via red, green and blue channels.
pub fn rgb(self, r: ColorScalar, g: ColorScalar, b: ColorScalar) -> Self {
self.color(Srgb::new(r, g, b))
pub fn rgb(self, r: f32, g: f32, b: f32) -> Self {
self.color(Color::rgb(r, g, b))
}

/// Specify the color via red, green, blue and alpha channels.
pub fn rgba(self, r: ColorScalar, g: ColorScalar, b: ColorScalar, a: ColorScalar) -> Self {
self.color(Srgba::new(r, g, b, a))
pub fn rgba(self, r: f32, g: f32, b: f32, a: f32) -> Self {
self.color(Color::rgba(r, g, b, a))
}

/// Specify the color via hue, saturation and luminance.
Expand All @@ -47,9 +47,9 @@ impl<'a> Background<'a> {
///
/// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
/// this color space.
pub fn hsl(self, h: ColorScalar, s: ColorScalar, l: ColorScalar) -> Self {
pub fn hsl(self, h: f32, s: f32, l: f32) -> Self {
let hue = h * 360.0;
self.color(color::Hsl::new(hue, s, l))
self.color(Color::hsl(hue, s, l))
}

/// Specify the color via hue, saturation, luminance and an alpha channel.
Expand All @@ -61,36 +61,36 @@ impl<'a> Background<'a> {
///
/// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
/// this color space.
pub fn hsla(self, h: ColorScalar, s: ColorScalar, l: ColorScalar, a: ColorScalar) -> Self {
pub fn hsla(self, h: f32, s: f32, l: f32, a: f32) -> Self {
let hue = h * 360.0;
self.color(color::Hsla::new(hue, s, l, a))
self.color(Color::hsla(hue, s, l, a))
}

/// Specify the color via hue, saturation and *value* (brightness).
///
/// This is sometimes also known as "hsb".
///
/// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
/// 360 degrees (or 2 PI radians).
///
/// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
/// this color space.
pub fn hsv(self, h: ColorScalar, s: ColorScalar, v: ColorScalar) -> Self {
let hue = h * 360.0;
self.color(color::Hsv::new(hue, s, v))
}

/// Specify the color via hue, saturation, *value* (brightness) and an alpha channel.
///
/// This is sometimes also known as "hsba".
///
/// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
/// 360 degrees (or 2 PI radians).
///
/// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
/// this color space.
pub fn hsva(self, h: ColorScalar, s: ColorScalar, v: ColorScalar, a: ColorScalar) -> Self {
let hue = h * 360.0;
self.color(color::Hsva::new(hue, s, v, a))
}
// /// Specify the color via hue, saturation and *value* (brightness).
// ///
// /// This is sometimes also known as "hsb".
// ///
// /// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
// /// 360 degrees (or 2 PI radians).
// ///
// /// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
// /// this color space.
// pub fn hsv(self, h: f32, s: f32, v: f32) -> Self {
// let hue = h * 360.0;
// self.color(color::Hsv::new(hue, s, v))
// }
//
// /// Specify the color via hue, saturation, *value* (brightness) and an alpha channel.
// ///
// /// This is sometimes also known as "hsba".
// ///
// /// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
// /// 360 degrees (or 2 PI radians).
// ///
// /// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
// /// this color space.
// pub fn hsva(self, h: f32, s: f32, v: f32, a: f32) -> Self {
// let hue = h * 360.0;
// self.color(color::Hsva::new(hue, s, v, a))
// }
}
73 changes: 36 additions & 37 deletions bevy_nannou_draw/src/draw/drawing.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::draw::mesh::vertex::{Color, TexCoords};
use crate::draw::primitive::Primitive;
use crate::draw::properties::{
ColorScalar, SetColor, SetDimensions, SetFill, SetOrientation, SetPosition, SetStroke,
SetColor, SetDimensions, SetFill, SetOrientation, SetPosition, SetStroke,
};
use crate::draw::{self, Draw};
use bevy::prelude::*;
use lyon::path::PathEvent;
use lyon::tessellation::{FillOptions, LineCap, LineJoin, StrokeOptions};
use nannou_core::color::IntoLinSrgba;
use nannou_core::color::IntoColor;
use std::marker::PhantomData;

/// A **Drawing** in progress.
Expand Down Expand Up @@ -37,13 +36,13 @@ pub struct Drawing<'a, T> {
/// This is particularly useful for paths and meshes.
pub struct DrawingContext<'a> {
/// The intermediary mesh for buffering yet-to-be-drawn paths and meshes.
pub mesh: &'a mut draw::Mesh,
pub mesh: &'a mut Mesh,
/// A re-usable buffer for collecting path events.
pub path_event_buffer: &'a mut Vec<PathEvent>,
/// A re-usable buffer for collecting colored polyline points.
pub path_points_colored_buffer: &'a mut Vec<(Vec2, Color)>,
/// A re-usable buffer for collecting textured polyline points.
pub path_points_textured_buffer: &'a mut Vec<(Vec2, TexCoords)>,
pub path_points_textured_buffer: &'a mut Vec<(Vec2, Vec2)>,
/// A re-usable buffer for collecting text.
pub text_buffer: &'a mut String,
}
Expand Down Expand Up @@ -205,7 +204,7 @@ impl<'a, T> Drawing<'a, T> {

impl<'a, T> Drawing<'a, T>
where
T: SetColor<ColorScalar> + Into<Primitive>,
T: SetColor + Into<Primitive>,
Primitive: Into<Option<T>>,
{
/// Specify a color.
Expand All @@ -215,13 +214,13 @@ where
/// Colors that have no alpha channel will be given an opaque alpha channel value `1.0`.
pub fn color<C>(self, color: C) -> Self
where
C: IntoLinSrgba<ColorScalar>,
C: Into<Color>,
{
self.map_ty(|ty| SetColor::color(ty, color))
}

/// Specify the color via red, green and blue channels.
pub fn rgb(self, r: ColorScalar, g: ColorScalar, b: ColorScalar) -> Self {
pub fn rgb(self, r: f32, g: f32, b: f32) -> Self {
self.map_ty(|ty| SetColor::rgb(ty, r, g, b))
}

Expand All @@ -231,7 +230,7 @@ where
}

/// Specify the color via red, green, blue and alpha channels.
pub fn rgba(self, r: ColorScalar, g: ColorScalar, b: ColorScalar, a: ColorScalar) -> Self {
pub fn rgba(self, r: f32, g: f32, b: f32, a: f32) -> Self {
self.map_ty(|ty| SetColor::rgba(ty, r, g, b, a))
}

Expand All @@ -249,7 +248,7 @@ where
///
/// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
/// this color space.
pub fn hsl(self, h: ColorScalar, s: ColorScalar, l: ColorScalar) -> Self {
pub fn hsl(self, h: f32, s: f32, l: f32) -> Self {
self.map_ty(|ty| SetColor::hsl(ty, h, s, l))
}

Expand All @@ -262,40 +261,40 @@ where
///
/// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
/// this color space.
pub fn hsla(self, h: ColorScalar, s: ColorScalar, l: ColorScalar, a: ColorScalar) -> Self {
pub fn hsla(self, h: f32, s: f32, l: f32, a: f32) -> Self {
self.map_ty(|ty| SetColor::hsla(ty, h, s, l, a))
}

/// Specify the color via hue, saturation and *value* (brightness).
///
/// This is sometimes also known as "hsb".
///
/// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
/// 360 degrees (or 2 PI radians).
///
/// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
/// this color space.
pub fn hsv(self, h: ColorScalar, s: ColorScalar, v: ColorScalar) -> Self {
self.map_ty(|ty| SetColor::hsv(ty, h, s, v))
}

/// Specify the color via hue, saturation, *value* (brightness) and an alpha channel.
///
/// This is sometimes also known as "hsba".
///
/// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
/// 360 degrees (or 2 PI radians).
///
/// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
/// this color space.
pub fn hsva(self, h: ColorScalar, s: ColorScalar, v: ColorScalar, a: ColorScalar) -> Self {
self.map_ty(|ty| SetColor::hsva(ty, h, s, v, a))
}
// /// Specify the color via hue, saturation and *value* (brightness).
// ///
// /// This is sometimes also known as "hsb".
// ///
// /// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
// /// 360 degrees (or 2 PI radians).
// ///
// /// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
// /// this color space.
// pub fn hsv(self, h: f32, s: f32, v: f32) -> Self {
// self.map_ty(|ty| SetColor::hsv(ty, h, s, v))
// }
//
// /// Specify the color via hue, saturation, *value* (brightness) and an alpha channel.
// ///
// /// This is sometimes also known as "hsba".
// ///
// /// The given hue expects a value between `0.0` and `1.0` where `0.0` is 0 degress and `1.0` is
// /// 360 degrees (or 2 PI radians).
// ///
// /// See the [wikipedia entry](https://en.wikipedia.org/wiki/HSL_and_HSV) for more details on
// /// this color space.
// pub fn hsva(self, h: f32, s: f32, v: f32, a: f32) -> Self {
// self.map_ty(|ty| SetColor::hsva(ty, h, s, v, a))
// }

/// Specify the color as gray scale
///
/// The given g expects a value between `0.0` and `1.0` where `0.0` is black and `1.0` is white
pub fn gray(self, g: ColorScalar) -> Self {
pub fn gray(self, g: f32) -> Self {
self.map_ty(|ty| SetColor::gray(ty, g))
}
}
Expand Down
Loading

0 comments on commit d7b3ddb

Please sign in to comment.