Crate lyon [−] [src]
2d rendering on the GPU in rust.
Crates
This meta-crate (lyon
) reexports the following sub-crates for convenience:
- lyon_tessellation - Path tessellation routines.
- lyon_path - Tools to build and iterate over paths.
- lyon_geom - 2d utilities for cubic and quadratic bézier curves, arcs and more.
- lyon_svg - Create paths using SVG's path syntax.
- lyon_extra - Additional testing and debugging tools.
- lyon_tess2 - Alternative fill tessellation implementation using libtess2.
Most lyon_<name>
crate is reexported as a <name>
module in lyon
. For example:
extern crate lyon_tessellation; use lyon_tessellation::FillTessellator;
Is equivalent to:
extern crate lyon; use lyon::tessellation::FillTessellator;
Additional documentation and links
- very basic gfx-rs example.
- advanced gfx-rs example.
- There is some useful documentaion on the project's wiki.
- The source code is available on the project's git repository.
- Interested in contributing? Pull requests are welcome. If you would like to help but don't know what to do specifically, have a look at the github issues, some of which are tagged as easy.
Examples
Tessellating a rounded rectangle
The lyon_tessellation
crate provides a collection of tessellation routines
for common shapes such as rectangles and circles. Let's have a look at how
to obtain the fill tessellation a rectangle with rounded corners:
extern crate lyon; use lyon::math::rect; use lyon::tessellation::{VertexBuffers, FillOptions}; use lyon::tessellation::basic_shapes::*; use lyon::tessellation::geometry_builder::simple_builder; fn main() { let mut geometry = VertexBuffers::new(); let options = FillOptions::tolerance(0.1); fill_rounded_rectangle( &rect(0.0, 0.0, 100.0, 50.0), &BorderRadii { top_left: 10.0, top_right: 5.0, bottom_left: 20.0, bottom_right: 25.0, }, &options, &mut simple_builder(&mut geometry), ); // The tessellated geometry is ready to be uploaded to the GPU. println!(" -- {} vertices {} indices", geometry.vertices.len(), geometry.indices.len() ); }
Building and tessellating an arbitrary path
extern crate lyon; use lyon::math::point; use lyon::path::default::Path; use lyon::path::builder::*; use lyon::path::iterator::PathIterator; use lyon::tessellation::{FillTessellator, FillOptions, VertexBuffers}; use lyon::tessellation::geometry_builder::simple_builder; fn main() { // Build a Path. let mut builder = Path::builder(); builder.move_to(point(0.0, 0.0)); builder.line_to(point(1.0, 0.0)); builder.quadratic_bezier_to(point(2.0, 0.0), point(2.0, 1.0)); builder.cubic_bezier_to(point(1.0, 1.0), point(0.0, 1.0), point(0.0, 0.0)); builder.close(); let path = builder.build(); // Will contain the result of the tessellation. let mut geometry = VertexBuffers::new(); let mut tessellator = FillTessellator::new(); { let mut geom_builder = simple_builder(&mut geometry); let tolerance = 0.1; // Compute the tessellation. tessellator.tessellate_path( path.path_iter(), &FillOptions::default(), &mut geom_builder ).unwrap(); } // The tessellated geometry is ready to be uploaded to the GPU. println!(" -- {} vertices {} indices", geometry.vertices.len(), geometry.indices.len() ); }
What is the tolerance variable in these examples?
The tessellator operates on flattened paths (that only contains line segments) so we have to approximate the curves segments with sequences of line segments. To do so we pick a tolerance threshold which is the maximum distance allowed between the curve and its approximation. The documentation of the lyon_geom crate provides more detailed explanations about this tolerance parameter.
Rendering the tessellated geometry
Lyon does not provide with any GPU abstraction or rendering backend (for now). It is up to the user of this crate to decide whether to use OpenGL, vulkan, gfx-rs, glium, or any low level graphics API and how to render it. The basic and advanced gfx-rs examples can be used to get an idea of how to render the geometry (in this case using gfx-rs).
Reexports
pub extern crate lyon_tessellation; |
pub extern crate lyon_extra; |
pub extern crate lyon_svg; |
Modules
extra | |
geom | |
math |
Basic types that are used everywhere. Most other lyon crates reexport them. |
path | |
svg | |
tessellation |