bevy_polyline

repository·main·Indexed 42 days ago

https://github.com/fslabs/bevy_polyline

A high-performance plugin for the Bevy Engine providing instanced rendering for polylines. It uses a specialized vertex shader to expand line segments into camera-facing geometry. Version 0.14.0 supports Bevy 0.18. Key features include the PolylineMaterial for controlling thickness, color, and perspective scaling, and the PolylineBundle for spawning polylines using standard Bevy transforms.

Tokens
2.1K
Snippets
7
Records
13
Agent score
52%

What's inside bevy_polyline

  1. Position Polylines using Transform

    main
    Polylines respect Bevy's standard positioning system. You can position them using a Transform component directly on the entity, or by placing them within a Transform hierarchy (parent-child relationships). The plugin uses GlobalTransform to determine the final world-space position.
  2. Use AlphaMode to control polyline transparency

    main

    The PolylineMaterial automatically determines its transparency behavior based on the alpha channel of the color property. While the AlphaMode enum is used internally by the renderer to select the correct pipeline, you control this behavior by setting the alpha value of your LinearRgba color.

    Supported modes (determined by the renderer):

    • Opaque: Used when color.alpha() >= 1.0.
    • Blend: Used when color.alpha() < 1.0. This uses standard alpha-blending.

    Note: The renderer also supports internal modes like Mask, Premultiplied, Add, and Multiply, but the primary way to trigger transparency in the current implementation is via the color's alpha channel.

  3. Create a Polyline using PolylineBundle

    main

    To spawn a polyline in your world, use the PolylineBundle. This bundle includes the PolylineHandle (which points to the actual vertex data), a PolylineMaterialHandle, and standard Bevy transform and visibility components.

    use bevy::prelude::*;
    use bevy_polyline::{Polyline, Polyline, PolylineBundle, PolylineHandle};
    
    fn spawn_polyline(mut commands: Commands, mut polylines: ResMut<Assets<Polyline>>) {
        let polyline_asset = Polylines::add(Polyline {
            vertices: vec![Vec3::ZERO, Vec3::X, Vec3::Y],
        });
    
        commands.spawn(PolylineBundle {
            polyline: PolylineHandle(polyline_asset),
            // material: PolylineMaterialHandle(your_material_handle),
            ..default()
        });
    }
  4. Add PolylinePlugin to your Bevy application

    main

    To enable polyline rendering in your Bevy application, add the PolylinePlugin to your app. This plugin initializes the necessary internal assets (like the polyline shader) and registers the core polyline plugins: PolylineBasePlugin, PolylineRenderPlugin, and PolylineMaterialPlugin.

    use bevy::prelude::*;
    use bevy_polyline::PolylinePlugin;
    
    fn main() {
        App::new()
            .add_plugins(DefaultPlugins)
            .add_plugins(PolylinePlugin)
            .run();
    }
  5. Setup the PolylineMaterialPlugin

    main

    To enable rendering of entities using PolylineMaterial, you must add the PolylineMaterialPlugin to your Bevy App. This plugin initializes the necessary assets, extraction logic, and render pipelines for both opaque and transparent polylines.

    use bevy_polyline::material::PolylineMaterialPlugin;
    
    fn main() {
        App::new()
            .add_plugins(DefaultPlugins)
            .add_plugins(PolylineMaterialPlugin)
            .run();
    }
  6. Initialize Bevy Polyline with PolylineBasePlugin

    main

    To use the core polyline functionality, add PolylineBasePlugin to your Bevy App. This plugin initializes the Polyline asset and sets up the necessary rendering infrastructure for GPU polylines.

    use bevy_polyline::PolylineBasePlugin;
    
    fn main() {
        App::new()
            .add_plugins(DefaultPlugins)
            .add_plugins(PolylineBasePlugin)
            .run();
    }
  7. Reduce aliasing and shimmering in Polylines

    main

    While Bevy Polyline implements line thinness fade to reduce aliasing, you may still encounter shimmering if line segments are extremely short (triangles smaller than 1 pixel).

    Best Practice: To minimize shimmering, ensure that you only add line segments that meet a minimum length threshold.

  8. Customize Polyline appearance with PolylineMaterial

    main

    You can customize the visual properties of a Polyline using the PolylineMaterial. It provides control over the line's thickness, color, and how width behaves in 3D space.

    Key fields:

    • width: Determines the thickness. In non-perspective mode, this correlates directly to screen pixels. In perspective mode, the width is divided by the w component of the homogeneous coordinate, meaning the line appears to have a constant size at the near plane and becomes smaller as it moves further away.
    • color: Sets the line color.
    • perspective: A boolean determining if the width should scale with distance.
  9. Check Bevy version compatibility

    main

    Bevy Polyline tracks the main branch of Bevy. Ensure your version of bevy_polyline matches your bevy version according to the compatibility table below:

    bevybevy_polyline
    0.180.14
    0.170.13
    0.160.12
    0.150.11
    0.140.10
    0.130.9
    0.120.8
    0.110.7
    0.100.5, 0.6
    0.90.4
    0.80.3
    0.70.2
    0.60.1
  10. Use the bevy_polyline prelude

    main

    The bevy_polyline::prelude module provides the primary types needed to work with polylines and their materials. Use this prelude to access:

    • Polyline: The core component representing a polyline.
    • PolylineBundle: A bundle containing all necessary components for spawning a polyline.
    • PolylineHandle: A handle to a polyline.
    • PolylineMaterial: The material used for rendering polylines.
    • PolylineMaterialHandle: A handle to a PolylineMaterial.
    use bevy_polyline::prelude::*;
  11. Configure PolylineMaterial for visual appearance

    main

    The PolylineMaterial asset allows you to control the visual properties of a polyline. You can create and manage these via Handle<PolylineMaterial> attached to an entity using the PolylineMaterialHandle component.

    Key properties include:

    • width: The line width in screen pixels when positioned nearest the camera.
    • color: The LinearRgba color of the line.
    • depth_bias: A value between -1 and 1 used to prevent z-fighting. A negative value (close to 0.0) will render the line in front of other geometry, which is useful for wireframes.
    • perspective: A boolean that determines if the line width should reduce with perspective. When true, the width is divided by the w component of the homogeneous coordinate, making lines smaller as they move away. If the width drops below 1, the color is faded to prevent flickering.
    // Example of creating a material
    let material = PolylineMaterial {
        width: 5.0,
        color: Color::RED.to_linear(),
        depth_bias: -0.1,
        perspective: true,
    };
    let material_handle = assets.add(material);
    
    // To apply it to an entity, use PolylineMaterialHandle
    commands.entity(polyline_entity).
        insert(PolylineMaterialHandle(material_handle))
        .insert(polyline_uniform);
  12. Reference the Polyline components and assets

    main

    The following types are the primary interfaces for managing polylines:

    • Polyline: The source asset containing a Vec<Vec3> of vertices.
    • PolylineHandle: A component wrapping a Handle<Polyline>, used to associate an entity with specific vertex data.
    • PolylineBundle: A bundle containing PolylineHandle, PolylineMaterialHandle, Transform, GlobalTransform, Visibility, InheritedVisibility, and ViewVisibility for easy spawning.