mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
97a5059535
# Objective - Adds line styles to bevy gizmos, suggestion of #9400 - Currently solid and dotted lines are implemented but this can easily be extended to support dashed lines as well if that is wanted. ## Solution - Adds the enum `GizmoLineStyle` and uses it in each `GizmoConfig` to configure the style of the line. - Each "dot" in a dotted line has the same width and height as the `line_width` of the corresponding line. --- ## Changelog - Added `GizmoLineStyle` to `bevy_gizmos` - Added a `line_style: GizmoLineStyle ` attribute to `GizmoConfig` - Updated the `lines.wgsl` shader and the pipelines accordingly. ## Migration Guide - Any manually created `GizmoConfig` must now include the `line_style` attribute ## Additional information Some pretty pictures :) This is the 3d_gizmos example with/without `line_perspective`: <img width="1440" alt="Screenshot 2024-03-09 at 23 25 53" src="https://github.com/bevyengine/bevy/assets/62256001/b1b97311-e78d-4de3-8dfe-9e48a35bb27d"> <img width="1440" alt="Screenshot 2024-03-09 at 23 25 39" src="https://github.com/bevyengine/bevy/assets/62256001/50ee8ecb-5290-484d-ba36-7fd028374f7f"> And the 2d example: <img width="1440" alt="Screenshot 2024-03-09 at 23 25 06" src="https://github.com/bevyengine/bevy/assets/62256001/4452168f-d605-4333-bfa5-5461d268b132"> --------- Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com>
152 lines
5 KiB
Rust
152 lines
5 KiB
Rust
//! This example demonstrates Bevy's immediate mode drawing API intended for visual debugging.
|
|
|
|
use std::f32::consts::{PI, TAU};
|
|
|
|
use bevy::{color::palettes::css::*, prelude::*};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.init_gizmo_group::<MyRoundGizmos>()
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, (draw_example_collection, update_config))
|
|
.run();
|
|
}
|
|
|
|
// We can create our own gizmo config group!
|
|
#[derive(Default, Reflect, GizmoConfigGroup)]
|
|
struct MyRoundGizmos {}
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
commands.spawn(Camera2dBundle::default());
|
|
// text
|
|
commands.spawn(TextBundle::from_section(
|
|
"Hold 'Left' or 'Right' to change the line width of straight gizmos\n\
|
|
Hold 'Up' or 'Down' to change the line width of round gizmos\n\
|
|
Press '1' or '2' to toggle the visibility of straight gizmos or round gizmos\n\
|
|
Press 'U' or 'I' to cycle through line styles for straight or round gizmos\n\
|
|
Press 'J' or 'K' to cycle through line joins for straight or round gizmos",
|
|
TextStyle {
|
|
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
|
|
font_size: 24.,
|
|
color: Color::WHITE,
|
|
},
|
|
));
|
|
}
|
|
|
|
fn draw_example_collection(
|
|
mut gizmos: Gizmos,
|
|
mut my_gizmos: Gizmos<MyRoundGizmos>,
|
|
time: Res<Time>,
|
|
) {
|
|
let sin = time.elapsed_seconds().sin() * 50.;
|
|
gizmos.line_2d(Vec2::Y * -sin, Vec2::splat(-80.), RED);
|
|
gizmos.ray_2d(Vec2::Y * sin, Vec2::splat(80.), LIME);
|
|
|
|
gizmos
|
|
.grid_2d(
|
|
Vec2::ZERO,
|
|
0.0,
|
|
UVec2::new(16, 12),
|
|
Vec2::new(60., 60.),
|
|
// Light gray
|
|
LinearRgba::gray(0.65),
|
|
)
|
|
.outer_edges();
|
|
|
|
// Triangle
|
|
gizmos.linestrip_gradient_2d([
|
|
(Vec2::Y * 300., BLUE),
|
|
(Vec2::new(-255., -155.), RED),
|
|
(Vec2::new(255., -155.), LIME),
|
|
(Vec2::Y * 300., BLUE),
|
|
]);
|
|
|
|
gizmos.rect_2d(
|
|
Vec2::ZERO,
|
|
time.elapsed_seconds() / 3.,
|
|
Vec2::splat(300.),
|
|
BLACK,
|
|
);
|
|
|
|
// The circles have 32 line-segments by default.
|
|
my_gizmos.circle_2d(Vec2::ZERO, 120., BLACK);
|
|
my_gizmos.ellipse_2d(
|
|
Vec2::ZERO,
|
|
time.elapsed_seconds() % TAU,
|
|
Vec2::new(100., 200.),
|
|
YELLOW_GREEN,
|
|
);
|
|
// You may want to increase this for larger circles.
|
|
my_gizmos.circle_2d(Vec2::ZERO, 300., NAVY).segments(64);
|
|
|
|
// Arcs default amount of segments is linearly interpolated between
|
|
// 1 and 32, using the arc length as scalar.
|
|
my_gizmos.arc_2d(Vec2::ZERO, sin / 10., PI / 2., 350., ORANGE_RED);
|
|
|
|
gizmos.arrow_2d(
|
|
Vec2::ZERO,
|
|
Vec2::from_angle(sin / -10. + PI / 2.) * 50.,
|
|
YELLOW,
|
|
);
|
|
}
|
|
|
|
fn update_config(
|
|
mut config_store: ResMut<GizmoConfigStore>,
|
|
keyboard: Res<ButtonInput<KeyCode>>,
|
|
time: Res<Time>,
|
|
) {
|
|
let (config, _) = config_store.config_mut::<DefaultGizmoConfigGroup>();
|
|
if keyboard.pressed(KeyCode::ArrowRight) {
|
|
config.line_width += 5. * time.delta_seconds();
|
|
config.line_width = config.line_width.clamp(0., 50.);
|
|
}
|
|
if keyboard.pressed(KeyCode::ArrowLeft) {
|
|
config.line_width -= 5. * time.delta_seconds();
|
|
config.line_width = config.line_width.clamp(0., 50.);
|
|
}
|
|
if keyboard.just_pressed(KeyCode::Digit1) {
|
|
config.enabled ^= true;
|
|
}
|
|
if keyboard.just_pressed(KeyCode::KeyU) {
|
|
config.line_style = match config.line_style {
|
|
GizmoLineStyle::Solid => GizmoLineStyle::Dotted,
|
|
_ => GizmoLineStyle::Solid,
|
|
};
|
|
}
|
|
if keyboard.just_pressed(KeyCode::KeyJ) {
|
|
config.line_joints = match config.line_joints {
|
|
GizmoLineJoint::Bevel => GizmoLineJoint::Miter,
|
|
GizmoLineJoint::Miter => GizmoLineJoint::Round(4),
|
|
GizmoLineJoint::Round(_) => GizmoLineJoint::None,
|
|
GizmoLineJoint::None => GizmoLineJoint::Bevel,
|
|
};
|
|
}
|
|
|
|
let (my_config, _) = config_store.config_mut::<MyRoundGizmos>();
|
|
if keyboard.pressed(KeyCode::ArrowUp) {
|
|
my_config.line_width += 5. * time.delta_seconds();
|
|
my_config.line_width = my_config.line_width.clamp(0., 50.);
|
|
}
|
|
if keyboard.pressed(KeyCode::ArrowDown) {
|
|
my_config.line_width -= 5. * time.delta_seconds();
|
|
my_config.line_width = my_config.line_width.clamp(0., 50.);
|
|
}
|
|
if keyboard.just_pressed(KeyCode::Digit2) {
|
|
my_config.enabled ^= true;
|
|
}
|
|
if keyboard.just_pressed(KeyCode::KeyI) {
|
|
my_config.line_style = match my_config.line_style {
|
|
GizmoLineStyle::Solid => GizmoLineStyle::Dotted,
|
|
_ => GizmoLineStyle::Solid,
|
|
};
|
|
}
|
|
if keyboard.just_pressed(KeyCode::KeyK) {
|
|
my_config.line_joints = match my_config.line_joints {
|
|
GizmoLineJoint::Bevel => GizmoLineJoint::Miter,
|
|
GizmoLineJoint::Miter => GizmoLineJoint::Round(4),
|
|
GizmoLineJoint::Round(_) => GizmoLineJoint::None,
|
|
GizmoLineJoint::None => GizmoLineJoint::Bevel,
|
|
};
|
|
}
|
|
}
|