For one of my personal projects i needed a way to load and display some simple SVG files/shapes in Bevy
,
so i took inspiration from bevy_prototype_lyon
and modified and extended it to...well...load and display
simple SVG files. SVGs can be used/displayed in 2D
as well as in 3D
.
I use usvg
to load, parse and simplify a SVG or SVGZ file and Lyon
to tessellate it into a vertex buffer,
which i then convert into a Bevy
mesh and draw with shaders.
Something else that i want to change, is how i load the SVG file. This would ideally use the Bevy asset manager, but i didn't have the time to take a deeper look at how it works or how i can integrate with it.
Bevy version |
bevy_svg version |
Branch |
---|---|---|
bevy-0.5 |
||
main |
Complex shapes | Multiple colors | Fonts |
---|---|---|
This crate is not yet on crates.io because it uses Bevy master. But i am planning to publish it as soon as Bevy 0.5 is released.
Until then, you need to copy this to your Cargo.toml
# Stable
bevy_svg = "0.3"
# Living on the edge (at your own risk 😅)
bevy_svg = { git = "https://github.com/Weasy666/bevy_svg", branch = "main" }
Then use it like this.
fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.insert_resource(WindowDescriptor {
title: "SVG Plugin".to_string(),
..Default::default()
})
.add_plugins(DefaultPlugins)
.add_plugin(bevy_svg::prelude::SvgPlugin)
.add_startup_system(setup.system());
.run();
}
fn setup(mut commands: Commands) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(
SvgBuilder::from_file("path/to/file.svg")
.origin(Origin::Center)
.position(Vec3::new(0.0, 0.0, 0.0))
.build()
.expect("File not found")
);
}
fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.insert_resource(WindowDescriptor {
title: "SVG Plugin".to_string(),
..Default::default()
})
.add_plugins(DefaultPlugins)
.add_plugin(bevy_svg::prelude::SvgPlugin)
.add_startup_system(setup.system());
.run();
}
fn setup(mut commands: Commands) {
commands.spawn_bundle(PerspectiveCameraBundle::new_3d());
commands.spawn_bundle(
SvgBuilder::from_file("path/to/file.svg")
.origin(Origin::Center)
.position(Vec3::new(0.0, 0.0, -1.0))
.scale(Vec2::new(0.01, 0.01))
.build()
.expect("File not found")
);
}