Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed May 3, 2024
1 parent 35661f6 commit c936252
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
35 changes: 33 additions & 2 deletions bevy_nannou_draw/src/draw/drawing.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::marker::PhantomData;
use std::sync::{Arc, RwLock};
use bevy::asset::AsyncWriteExt;

use bevy::pbr::{ExtendedMaterial, MaterialExtension};
use bevy::prelude::*;
use bevy::render::render_resource::BlendComponent;
use lyon::path::PathEvent;
use lyon::tessellation::{FillOptions, LineCap, LineJoin, StrokeOptions};
use crate::changed::Cd;

use crate::draw::{Draw, DrawContext, DrawRef};
use crate::draw::mesh::MeshExt;
Expand Down Expand Up @@ -114,6 +117,8 @@ where
Err(err) => eprintln!("drawing failed to borrow state and finish: {}", err),
Ok(mut state) => {
match &self.draw {
// If we are "Owned", that means we mutated our material and so need to
// spawn a new entity just for this primitive.
DrawRef::Owned(draw) => {
let material = draw.material.read().unwrap().clone();
state.draw_commands.push(Some(Box::new(move |world: &mut World| {
Expand All @@ -137,6 +142,9 @@ where
});
render.mesh = mesh;
render.entity = entity;

// TODO: reset parent?? How does change detection work here?
// We should probably keep rendering into the same parent material.
})));
},
DrawRef::Borrowed(_) => (),
Expand Down Expand Up @@ -170,6 +178,27 @@ where
}
}

fn map_material<F>(mut self, map: F) -> Drawing<'a, T, M>
where
F: FnOnce(M) -> M,
{
self.finish_on_drop = false;
let Drawing { ref draw, index, .. } = self;
let material = map(self.draw.material.read().unwrap().clone());
let draw = Draw {
state: draw.state.clone(),
context: draw.context.clone(),
material: Arc::new(RwLock::new(Cd::new(material))),
window: draw.window,
};
Drawing {
draw: DrawRef::Owned(draw),
index,
finish_on_drop: true,
_ty: PhantomData,
}
}

// Map the given function onto the primitive stored within **Draw** at `index`.
//
// The functionn is only applied if the node has not yet been **Drawn**.
Expand Down Expand Up @@ -799,8 +828,10 @@ where
}

pub fn base_color(mut self, color: Color) -> Self {
// self.draw.material.base.base_color = color;
self
self.map_material(|mut material| {
material.base.base_color = color;
material
})
}

pub fn emissive(mut self, color: Color) -> Self {
Expand Down
9 changes: 9 additions & 0 deletions bevy_nannou_draw/src/draw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,15 @@ where
render.mesh = mesh;
render.entity = entity;
})));

// Reset the material changed flag so that we will re-use the same mesh
// until the material is changed again.
self.material.write().unwrap().reset();
} else {
// Our material wasn't changed, but we need to check our mesh to see if it was
// changed, as this would indicate that a primitive spawned by this draw instance
// has been drawn with a different material.
// TODO :(
}

// If drawing with a different context, insert the necessary command to update it.
Expand Down
1 change: 1 addition & 0 deletions generative_design/color/p_1_0_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn view(app: &App) {
draw.background().hsl(norm_mouse_y, 1.0, 0.5);

draw.rect()
.base_color(Color::WHITE)
.w_h(app.mouse().x * 2.0, app.mouse().x * 2.0)
.hsv(1.0 - (norm_mouse_y), 1.0, 0.5);

Expand Down
2 changes: 1 addition & 1 deletion generative_design/oscillation_figures/m_2_5_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn model(app: &App) -> Model {
let line_alpha = 0.2;

let mut model = Model {
point_count: 500,
point_count: 1000,
lissajous_points,
freq_x: 4.0,
freq_y: 7.0,
Expand Down

0 comments on commit c936252

Please sign in to comment.