mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
35cec8bd3a
# Objective - Implement grid gizmos, suggestion of #9400 ## Solution - Added `gizmos.grid(...) ` and `gizmos.grid_2d(...)` - The grids may be configured using `.outer_edges(...)` to specify whether to draw the outer border/edges of the grid and `.skew(...)`to specify the skew of the grid along the x or y directions. --- ## Changelog - Added a `grid` module to `bevy_gizmos` containing `gizmos.grid(...) ` and `gizmos.grid_2d(...)` as well as assorted items. - Updated the `2d_gizmos` and `3d_gizmos` examples to use grids. ## Additional The 2D and 3D examples now look like this: <img width="1440" alt="Screenshot 2024-02-20 at 15 09 40" src="https://github.com/bevyengine/bevy/assets/62256001/ce04191e-d839-4faf-a6e3-49b6bb4b922b"> <img width="1440" alt="Screenshot 2024-02-20 at 15 10 07" src="https://github.com/bevyengine/bevy/assets/62256001/317459ba-d452-42eb-ae95-7c84cdbd569b">
130 lines
3.9 KiB
Rust
130 lines
3.9 KiB
Rust
//! This example demonstrates Bevy's immediate mode drawing API intended for visual debugging.
|
|
|
|
use std::f32::consts::{PI, TAU};
|
|
|
|
use bevy::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",
|
|
TextStyle {
|
|
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
|
|
font_size: 24.,
|
|
color: LegacyColor::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.), LegacyColor::RED);
|
|
gizmos.ray_2d(Vec2::Y * sin, Vec2::splat(80.), LegacyColor::GREEN);
|
|
|
|
gizmos
|
|
.grid_2d(
|
|
Vec2::ZERO,
|
|
0.0,
|
|
UVec2::new(16, 12),
|
|
Vec2::new(60., 60.),
|
|
// Light gray
|
|
LegacyColor::rgb(0.65, 0.65, 0.65),
|
|
)
|
|
.outer_edges(true);
|
|
|
|
// Triangle
|
|
gizmos.linestrip_gradient_2d([
|
|
(Vec2::Y * 300., LegacyColor::BLUE),
|
|
(Vec2::new(-255., -155.), LegacyColor::RED),
|
|
(Vec2::new(255., -155.), LegacyColor::GREEN),
|
|
(Vec2::Y * 300., LegacyColor::BLUE),
|
|
]);
|
|
|
|
gizmos.rect_2d(
|
|
Vec2::ZERO,
|
|
time.elapsed_seconds() / 3.,
|
|
Vec2::splat(300.),
|
|
LegacyColor::BLACK,
|
|
);
|
|
|
|
// The circles have 32 line-segments by default.
|
|
my_gizmos.circle_2d(Vec2::ZERO, 120., LegacyColor::BLACK);
|
|
my_gizmos.ellipse_2d(
|
|
Vec2::ZERO,
|
|
time.elapsed_seconds() % TAU,
|
|
Vec2::new(100., 200.),
|
|
LegacyColor::YELLOW_GREEN,
|
|
);
|
|
// You may want to increase this for larger circles.
|
|
my_gizmos
|
|
.circle_2d(Vec2::ZERO, 300., LegacyColor::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.,
|
|
LegacyColor::ORANGE_RED,
|
|
);
|
|
|
|
gizmos.arrow_2d(
|
|
Vec2::ZERO,
|
|
Vec2::from_angle(sin / -10. + PI / 2.) * 50.,
|
|
LegacyColor::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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|