Code Monkey home page Code Monkey logo

bevy_polyline's Introduction

Bevy Polyline

crates.io docs.rs CI Bevy tracking

Bevy Polyline is a plugin for Bevy Engine that adds instanced rendering of Polylines. The plugin comes courtesy of Foresight Mining Software Corporation who sponsor its creation and maintenance.

nbody demo

Implementation

Bevy Polyline closely mimics the way Meshes are rendered in Bevy. It works internally by passing a minimal Instance Buffer to the GPU, containing only the line segment endpoints and then completely determines all vertex positions within the vertex shader, such that the triangles form a line that is rotated around it's longitudinal axis to face towards the camera. The shader code is based on this great tutorial by Rye Terrell.

Examples

There are two examples, linestrip demonstrates how to make a very basic static Polyline. nbody (shown in the above demo) demonstrates how to do updateable Polylines, by changing the vertices of a Polyline.

Usage

Usage of Bevy Polyline is quite simple. First add it to your Cargo.toml:

[dependencies]
bevy_polyline = "0.1"

You add it as a plugin to your app:

app.add_plugin(PolylinePlugin);

And then you can add some Polylines through PolylineBundle

fn my_system(
    mut commands: Commands,
    mut polylines: ResMut<Assets<Polyline>>,
    mut polyline_materials: ResMut<Assets<PolylineMaterial>>,
) {
    commands.spawn_bundle(PolylineBundle {
        polyline: polylines.add(Polyline {
            vertices: vec![
                Vec3::new(-0.5, -0.5, -0.5),
                Vec3::new(0.5, -0.5, -0.5),
                Vec3::new(0.5, 0.5, -0.5),
                Vec3::new(-0.5, 0.5, -0.5),
                Vec3::new(-0.5, 0.5, 0.5),
                Vec3::new(0.5, 0.5, 0.5),
                Vec3::new(0.5, -0.5, 0.5),
                Vec3::new(-0.5, -0.5, 0.5),
            ],
            ..Default::default()
        }),
        material: polyline_materials.add(PolylineMaterial {
            width: 3.0,
            color: Color::RED,
            perspective: false,
            ..Default::default()
        }),
        ..Default::default()
    });
}

Transform

Polylines respect positioning through GlobalTransform, so you can position them directly, or through the use of a Transform hierarchy.

PolylineMaterial

Currently the main way of customizing a Polyline is by changing the PolylineMaterial, which, as can be seen above, has fields for width, color and perspective. width directly correlates to screen pixels in non-perspective mode. In perspective mode width gets divided by the w component of the homogeneous coordinate, meaning it corresponds to screen pixels at the near plane and becomes progressively smaller further away.

Shaders

For more significant customization, you have to make a custom shader, although it's likely we'll add more shaders in the future. The current version only implements line strips (i.e. PolyLines rendered as connected line segments) with no caps.

Due to the nature of its instanced rendering, Bevy Polyline comes with fairly specific shaders. You can still replace these with custom ones, but you will have to keep a good chunk of the shader in tact if you want to use Bevy Polyline's way of creating the line triangles.

Aliasing/shimmering

Bevy Polyline does some work to reduce aliasing, by implementing the line thinness fade from https://acegikmo.com/shapes/docs/#anti-aliasing. But if your line segments are very short, you will still see shimmering, caused by triangles < 1 pixel in size. This can be reduced by only adding segments of a minimum length.

Performance

Due to instancing, Bevy Polyline only makes one drawcall per PolyLine, one for the line segments and one for the miter joins (not currently enabled). We've tested the nbody demo at some 500 lines with 4096 segments being updated every frame (in addition to a 4th order Yoshida integrator for the nbody simulation) running at 60fps. There is still some room for performance optimization, particularly reducing to one drawcall per Polyline (depending on join and cap types) and more efficient updates of the instance buffer for updated lines.

Bevy Version Support

We intend to track the main branch of Bevy. PRs supporting this are welcome!

bevy bevy_polyline
0.6 0.1

Community Support

If you want some help using this plugin, you can ask in the Bevy Discord at https://discord.gg/bevy.

Special Thanks

Credit goes to mtsr for the initial implementation of this plugin.

License

bevy_polyline is free and open source! All code in this repository is dual-licensed under either:

at your option. This means you can select the license you prefer! This dual-licensing approach is the de-facto standard in the Rust ecosystem and there are very good reasons to include both.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Sponsors

The creation and maintenance of Bevy Polyline is sponsored by Foresight Mining Software Corporation.

Foresight Mining Software Corporation

bevy_polyline's People

Contributors

aevyrie avatar mtsr avatar julianruiseco avatar vmihdal avatar wandalen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.