Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MaterialMesh2dBundle with ColorMaterial #224

Merged
merged 4 commits into from
Sep 10, 2023
Merged
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
23 changes: 15 additions & 8 deletions src/entity.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
//! Custom Bevy ECS bundle for shapes.

use bevy::{
ecs::{bundle::Bundle, component::Component},
prelude::{Handle, SpatialBundle},
sprite::Mesh2dHandle,
};
use bevy::{prelude::*, sprite::Mesh2dHandle};
use lyon_tessellation::{self as tess};

use crate::{prelude::Geometry, render::ShapeMaterial};
use crate::{geometry::Geometry, plugin::COLOR_MATERIAL_HANDLE};

/// A Bevy `Bundle` to represent a shape.
#[allow(missing_docs)]
#[derive(Bundle, Default)]
#[derive(Bundle)]
pub struct ShapeBundle {
pub path: Path,
pub mesh: Mesh2dHandle,
pub material: Handle<ShapeMaterial>,
pub material: Handle<ColorMaterial>,
pub spatial: SpatialBundle,
}

impl Default for ShapeBundle {
fn default() -> Self {
Self {
path: default(),
mesh: default(),
material: COLOR_MATERIAL_HANDLE,
spatial: default(),
}
}
}

#[allow(missing_docs)]
#[derive(Component, Default)]
pub struct Path(pub tess::path::Path);
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub mod entity;
pub mod geometry;
pub mod path;
pub mod plugin;
pub mod render;
pub mod shapes;

mod utils;
Expand Down
35 changes: 16 additions & 19 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,44 @@
//! `ShapeBundle`.

use bevy::{
app::{App, Plugin},
asset::Assets,
ecs::{
query::{Changed, Or},
system::{Query, ResMut, Resource},
},
log::error,
prelude::{
Color, Deref, DerefMut, IntoSystemConfigs, IntoSystemSetConfigs, PostUpdate, SystemSet,
},
render::{
mesh::{Indices, Mesh},
render_resource::PrimitiveTopology,
},
prelude::*,
render::{mesh::Indices, render_resource::PrimitiveTopology},
sprite::Mesh2dHandle,
};
use lyon_tessellation::{self as tess, BuffersBuilder};

use crate::{
draw::{Fill, Stroke},
entity::Path,
render::ShapeMaterialPlugin,
vertex::{VertexBuffers, VertexConstructor},
};

pub(crate) const COLOR_MATERIAL_HANDLE: Handle<ColorMaterial> =
Handle::weak_from_u128(0x7CC6_61A1_0CD6_C147_129A_2C01_882D_9580);

/// A plugin that provides resources and a system to draw shapes in Bevy with
/// less boilerplate.
pub struct ShapePlugin;

impl Plugin for ShapePlugin {
fn build(&self, app: &mut App) {
let fill_tess = lyon_tessellation::FillTessellator::new();
let stroke_tess = lyon_tessellation::StrokeTessellator::new();
let fill_tess = tess::FillTessellator::new();
let stroke_tess = tess::StrokeTessellator::new();
app.insert_resource(FillTessellator(fill_tess))
.insert_resource(StrokeTessellator(stroke_tess))
.configure_sets(
PostUpdate,
BuildShapes.after(bevy::transform::TransformSystem::TransformPropagate),
)
.add_systems(PostUpdate, mesh_shapes_system.in_set(BuildShapes))
.add_plugins(ShapeMaterialPlugin);
.add_systems(PostUpdate, mesh_shapes_system.in_set(BuildShapes));

app.world.resource_mut::<Assets<ColorMaterial>>().insert(
COLOR_MATERIAL_HANDLE,
ColorMaterial {
color: Color::WHITE,
..default()
},
);
}
}

Expand Down
46 changes: 0 additions & 46 deletions src/render/mod.rs

This file was deleted.

15 changes: 0 additions & 15 deletions src/render/shape_material.wgsl

This file was deleted.