8 releases (5 breaking)

0.9.0 Jan 17, 2023
0.8.0 Sep 23, 2022
0.7.0 Apr 21, 2022
0.6.0 Jan 9, 2022
0.3.0 Apr 7, 2021

#20 in Rendering

Download history 26/week @ 2022-10-09 14/week @ 2022-10-16 21/week @ 2022-10-23 31/week @ 2022-10-30 37/week @ 2022-11-06 18/week @ 2022-11-13 17/week @ 2022-11-20 11/week @ 2022-11-27 11/week @ 2022-12-04 17/week @ 2022-12-11 22/week @ 2022-12-18 15/week @ 2022-12-25 7/week @ 2023-01-01 85/week @ 2023-01-08 43/week @ 2023-01-15 23/week @ 2023-01-22

160 downloads per month

MIT/Apache

60KB
1K SLoC

bevy_svg

Crates.io license

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.

Files are loaded through AssetLoader, then parsed and simplified with usvg and then tessellated with Lyon into a vertex buffer, which lastly is convert into a Bevy mesh and drawn with custom shaders.

Note: The SVG support is currently rather basic, i'd like to expand that in the future.

Compatibility

Bevy version bevy_svg version Branch
Crates.io Crates.io bevy-0.9
Crates.io Crates.io bevy-0.8
Crates.io Crates.io bevy-0.7
Crates.io Crates.io bevy-0.6
Crates.io Crates.io bevy-0.5
Crates.io Crates.io main

Examples

Complex shapes Multiple colors Fonts
complex_one_color two_colors twinkle

Usage

Copy this to your Cargo.toml

# Stable
bevy_svg = "0.9.0"

# 2D and 3D are available on default, if you only want/need one, use the following
bevy_svg = { version = "0.9.0", default-features = false, features = "2d" }
# or
bevy_svg = { version = "0.9.0", default-features = false, features = "3d" }

# Living on the edge (at your own risk 😅)
bevy_svg = { git = "https://github.com/Weasy666/bevy_svg", branch = "main" }

Then use it like this.

2D

use bevy_svg::prelude::*;

fn main() {
    App::new()
        .insert_resource(Msaa { samples: 4 })
        .add_plugins(DefaultPlugins.set(WindowPlugin {
            window: WindowDescriptor {
                title: "SVG Plugin".to_string(),
                ..Default::default()
            },
            ..Default::default()
        }))
        .add_plugin(bevy_svg::prelude::SvgPlugin)
        .add_startup_system(setup)
        .run();
}

fn setup(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
) {
    let svg = asset_server.load("path/to/file.svg");
    commands.spawn(Camera2dBundle::default());
    commands.spawn(Svg2dBundle {
        svg,
        origin: Origin::Center, // Origin::TopLeft is the default
        ..Default::default()
    });
}

3D

use bevy_svg::prelude::*;

fn main() {
    App::new()
        .insert_resource(Msaa { samples: 4 })
        .add_plugins(DefaultPlugins.set(WindowPlugin {
            window: WindowDescriptor {
                title: "SVG Plugin".to_string(),
                ..Default::default()
            },
            ..Default::default()
        }))
        .add_plugin(bevy_svg::prelude::SvgPlugin)
        .add_startup_system(setup)
        .run();
}

fn setup(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
) {
    let svg = asset_server.load("path/to/file.svg");
    commands.spawn(Camera3dBundle::default());
    commands.spawn(Svg3dBundle {
        svg,
        origin: Origin::Center, // Origin::TopLeft is the default
        transform: Transform {
            translation: Vec3::new(0.0, 0.0, -600.0),
            // The scale you need depends a lot on your SVG and camera distance it
            // is currently rather unpredictable, so you need to try it out a bit
            scale: Vec3::new(1.0, 1.0, 1.0),
            rotation: Quat::from_rotation_x(-std::f32::consts::PI / 5.0),
        },
        ..Default::default()
    });
}

License

bevy_svg is licensed under either of the following, at your option:

Dependencies

~22–32MB
~640K SLoC