-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
275 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,15 @@ | ||
use bevy::{ | ||
app::{App, Plugin}, | ||
asset::{Assets, HandleUntyped}, | ||
reflect::TypeUuid, | ||
render::{ | ||
render_phase::AddRenderCommand, | ||
render_resource::{Shader, SpecializedRenderPipelines}, | ||
RenderApp, RenderStage, | ||
}, | ||
}; | ||
#[cfg(feature = "2d")] | ||
use bevy::core_pipeline::Transparent2d; | ||
#[cfg(feature = "3d")] | ||
use bevy::core_pipeline::Transparent3d; | ||
use lyon_tessellation::{FillTessellator, StrokeTessellator}; | ||
|
||
#[cfg(feature = "2d")] | ||
mod pipeline_2d; | ||
#[cfg(feature = "3d")] | ||
mod pipeline_3d; | ||
mod plugin; | ||
pub(crate) mod tessellation; | ||
mod vertex_buffer; | ||
|
||
#[cfg(feature = "2d")] | ||
mod svg2d; | ||
#[cfg(feature = "3d")] | ||
mod svg3d; | ||
|
||
/// Plugin that renders [`Svg`](crate::svg::Svg)s in 2D | ||
pub struct SvgPlugin; | ||
|
||
/// Handle to the custom shader with a unique random ID | ||
#[cfg(feature = "2d")] | ||
pub const SVG_2D_SHADER_HANDLE: HandleUntyped = HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 8514826620251853414); | ||
pub use svg2d::Svg2dBundle; | ||
#[cfg(feature = "3d")] | ||
pub const SVG_3D_SHADER_HANDLE: HandleUntyped = HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 8514826640451853414); | ||
pub use svg3d::Svg3dBundle; | ||
|
||
impl Plugin for SvgPlugin { | ||
fn build(&self, app: &mut App) { | ||
let fill_tess = FillTessellator::new(); | ||
let stroke_tess = StrokeTessellator::new(); | ||
// Load SVG shader | ||
let mut shaders = app.world.get_resource_mut::<Assets<Shader>>().unwrap(); | ||
#[cfg(feature = "2d")] | ||
shaders.set_untracked( | ||
SVG_2D_SHADER_HANDLE, | ||
Shader::from_wgsl(include_str!("svg_2d.wgsl")), | ||
); | ||
#[cfg(feature = "3d")] | ||
shaders.set_untracked( | ||
SVG_3D_SHADER_HANDLE, | ||
Shader::from_wgsl(include_str!("svg_3d.wgsl")), | ||
); | ||
app | ||
.insert_resource(fill_tess) | ||
.insert_resource(stroke_tess); | ||
// Register our custom draw function and pipeline, and add our render systems | ||
let render_app = app.get_sub_app_mut(RenderApp).unwrap(); | ||
#[cfg(feature = "2d")] | ||
render_app | ||
.add_render_command::<Transparent2d, pipeline_2d::DrawSvg2d>() | ||
.init_resource::<pipeline_2d::Svg2dPipeline>() | ||
.init_resource::<SpecializedRenderPipelines<pipeline_2d::Svg2dPipeline>>() | ||
.init_resource::<pipeline_2d::ExtractedSvgs2d>() | ||
.add_system_to_stage(RenderStage::Extract, pipeline_2d::extract_svg_2d) | ||
.add_system_to_stage(RenderStage::Prepare, pipeline_2d::prepare_svg_2d) | ||
.add_system_to_stage(RenderStage::Queue, pipeline_2d::queue_svg_2d); | ||
#[cfg(feature = "3d")] | ||
render_app | ||
.add_render_command::<Transparent3d, pipeline_3d::DrawSvg3d>() | ||
.init_resource::<pipeline_3d::Svg3dPipeline>() | ||
.init_resource::<SpecializedRenderPipelines<pipeline_3d::Svg3dPipeline>>() | ||
.init_resource::<pipeline_3d::ExtractedSvgs3d>() | ||
.add_system_to_stage(RenderStage::Extract, pipeline_3d::extract_svg_3d) | ||
.add_system_to_stage(RenderStage::Prepare, pipeline_3d::prepare_svg_3d) | ||
.add_system_to_stage(RenderStage::Queue, pipeline_3d::queue_svg_3d); | ||
} | ||
} | ||
pub use plugin::SvgPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use bevy::{ | ||
app::{App, Plugin}, | ||
asset::Assets, | ||
render::RenderApp, | ||
render::render_resource::Shader, | ||
}; | ||
use lyon_tessellation::{FillTessellator, StrokeTessellator}; | ||
|
||
#[cfg(feature = "2d")] | ||
use crate::render::svg2d; | ||
#[cfg(feature = "3d")] | ||
use crate::render::svg3d; | ||
|
||
|
||
/// Plugin that renders [`Svg`](crate::svg::Svg)s in 2D | ||
pub struct SvgPlugin; | ||
|
||
impl Plugin for SvgPlugin { | ||
fn build(&self, app: &mut App) { | ||
// Load SVG shader | ||
let mut shaders = app.world.resource_mut::<Assets<Shader>>(); | ||
#[cfg(feature = "2d")] | ||
shaders.set_untracked( | ||
svg2d::SVG_2D_SHADER_HANDLE, | ||
Shader::from_wgsl(include_str!("svg2d/svg_2d.wgsl")), | ||
); | ||
#[cfg(feature = "3d")] | ||
shaders.set_untracked( | ||
svg3d::SVG_3D_SHADER_HANDLE, | ||
Shader::from_wgsl(include_str!("svg3d/svg_3d.wgsl")), | ||
); | ||
let fill_tess = FillTessellator::new(); | ||
let stroke_tess = StrokeTessellator::new(); | ||
app | ||
.insert_resource(fill_tess) | ||
.insert_resource(stroke_tess); | ||
// Register our custom draw function and pipeline, and add our render systems | ||
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { | ||
#[cfg(feature = "2d")] | ||
render_app.add_plugin(svg2d::RenderPlugin); | ||
#[cfg(feature = "3d")] | ||
render_app.add_plugin(svg3d::RenderPlugin); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! Bevy [`Bundle`] representing an SVG entity. | ||
use bevy::{ | ||
asset::Handle, | ||
ecs::bundle::Bundle, | ||
render::view::{ComputedVisibility, Visibility}, | ||
sprite::Mesh2dHandle, | ||
transform::components::{GlobalTransform, Transform}, | ||
}; | ||
|
||
use crate::svg::{Origin, Svg}; | ||
|
||
|
||
/// A Bevy [`Bundle`] representing an SVG entity. | ||
#[allow(missing_docs)] | ||
#[derive(Bundle)] | ||
pub struct Svg2dBundle { | ||
pub svg: Handle<Svg>, | ||
pub mesh_2d: Mesh2dHandle, | ||
/// [`Origin`] of the coordinate system and as such the origin for the Bevy position. | ||
pub origin: Origin, | ||
pub transform: Transform, | ||
pub global_transform: GlobalTransform, | ||
pub visibility: Visibility, | ||
pub computed_visibility: ComputedVisibility, | ||
} | ||
|
||
impl Default for Svg2dBundle { | ||
/// Creates a default [`Svg2dBundle`]. | ||
fn default() -> Self { | ||
Self { | ||
svg: Default::default(), | ||
mesh_2d: Default::default(), | ||
origin: Default::default(), | ||
transform: Transform::default(), | ||
global_transform: GlobalTransform::default(), | ||
visibility: Visibility::default(), | ||
computed_visibility: ComputedVisibility::default(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use bevy::{ | ||
asset::HandleUntyped, | ||
reflect::TypeUuid, | ||
render::render_resource::Shader, | ||
}; | ||
|
||
mod bundle; | ||
mod pipeline_2d; | ||
mod plugin; | ||
|
||
/// Handle to the custom shader with a unique random ID | ||
pub const SVG_2D_SHADER_HANDLE: HandleUntyped = HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 8514826620251853414); | ||
|
||
pub use bundle::Svg2dBundle; | ||
pub use plugin::RenderPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.