Skip to content

How to draw with Hatching for dashed/dotted lines? #232

@mattzque

Description

@mattzque

As far as I understand it, Lyon supports dashed lines via the hatching module in lyon_algorithms, but I couldn't get this to work in bevy with this crate. Algorithms is already a dependency (although not exported).

Does anyone know how to do that / has an example of how to accomplish this?

What I tried was:

[dependencies]
bevy = "0.12.1"
bevy_prototype_lyon = "0.10.0"
lyon_algorithms = "1"
use bevy::prelude::*;
use bevy_prototype_lyon::{
    draw::Stroke, entity::{Path, ShapeBundle}, geometry::GeometryBuilder, plugin::ShapePlugin, shapes::Line
};
use lyon_algorithms::{
    geom::LineSegment,
    hatching::{HatchSegment, Hatcher, HatchingOptions, RegularHatchingPattern},
    path::Path as LyonPath,
};

fn setup(mut commands: Commands) {
    commands.spawn(Camera2dBundle::default());

    // ***********************************************************************

    let shape = Line(Vec2::ZERO, Vec2::new(100.0, 0.0));

    commands.spawn((
        ShapeBundle {
            path: GeometryBuilder::build_as(&shape),
            ..default()
        },
        Stroke::new(Color::WHITE, 10.0),
    ));

    // ***********************************************************************
    // works:

    let shape = Line(Vec2::ZERO, Vec2::new(100.0, 0.0));

    let original_path = GeometryBuilder::build_as(&shape).0;

    commands.spawn((
        ShapeBundle {
            path: Path(original_path),
            spatial: SpatialBundle {
                transform: Transform::from_translation(Vec3::new(0.0, 100.0, 1.0)),
                ..default()
            },
            ..default()
        },
        Stroke::new(Color::BLUE, 10.0),
    ));

    // ***********************************************************************
    // this renders nothing:

    let shape = Line(Vec2::ZERO, Vec2::new(100.0, 0.0));

    let original_path = GeometryBuilder::build_as(&shape).0;

    let mut hatches = LyonPath::builder();
    let mut hatcher = Hatcher::new();
    hatcher.hatch_path(
        original_path.iter(),
        &HatchingOptions::DEFAULT,
        &mut RegularHatchingPattern {
            interval: 1.0,
            callback: &mut |segment: &HatchSegment| {
                hatches.add_line_segment(&LineSegment {
                    from: segment.a.position,
                    to: segment.b.position,
                });
            },
        },
    );
    let hatched_path = hatches.build();

    commands.spawn((
        ShapeBundle {
            path: Path(hatched_path),
            spatial: SpatialBundle {
                transform: Transform::from_translation(Vec3::new(0.0, 200.0, 1.0)),
                ..default()
            },
            ..default()
        },
        Stroke::new(Color::RED, 10.0),
    ));
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(ShapePlugin)
        .add_systems(Startup, setup)
        .run();
}

But the line doesn't render at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions