Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mattzque opened this issue Feb 2, 2024 · 0 comments
Open

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

mattzque opened this issue Feb 2, 2024 · 0 comments

Comments

@mattzque
Copy link

mattzque commented Feb 2, 2024

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.

@mattzque mattzque changed the title Hatching for dashed/dotted lines How to draw with Hatching for dashed/dotted lines? Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant