mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 06:30:19 +00:00
ab300d0ed9
## Objective - Add an arrow gizmo as suggested by #9400 ## Solution (excuse my Protomen music) https://github.com/bevyengine/bevy/assets/14184826/192adf24-079f-4a4b-a17b-091e892974ec Wasn't horribly hard when i remembered i can change coordinate systems whenever I want. Gave them four tips (as suggested by @alice-i-cecile in discord) instead of trying to decide what direction the tips should point. Made the tip length default to 1/10 of the arrow's length, which looked good enough to me. Hard-coded the angle from the body to the tips to 45 degrees. ## Still TODO - [x] actual doc comments - [x] doctests - [x] `ArrowBuilder.with_tip_length()` --- ## Changelog - Added `gizmos.arrow()` and `gizmos.arrow_2d()` - Added arrows to `2d_gizmos` and `3d_gizmos` examples ## Migration Guide N/A --------- Co-authored-by: Nicola Papale <nicopap@users.noreply.github.com>
126 lines
3.8 KiB
Rust
126 lines
3.8 KiB
Rust
//! This example demonstrates Bevy's immediate mode drawing API intended for visual debugging.
|
|
|
|
use std::f32::consts::PI;
|
|
|
|
use bevy::prelude::*;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, (system, rotate_camera, update_config))
|
|
.run();
|
|
}
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
) {
|
|
commands.spawn(Camera3dBundle {
|
|
transform: Transform::from_xyz(0., 1.5, 6.).looking_at(Vec3::ZERO, Vec3::Y),
|
|
..default()
|
|
});
|
|
// plane
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))),
|
|
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
|
|
..default()
|
|
});
|
|
// cube
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
|
|
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
|
|
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
|
..default()
|
|
});
|
|
// light
|
|
commands.spawn(PointLightBundle {
|
|
point_light: PointLight {
|
|
intensity: 1500.0,
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
transform: Transform::from_xyz(4.0, 8.0, 4.0),
|
|
..default()
|
|
});
|
|
|
|
// example instructions
|
|
commands.spawn(
|
|
TextBundle::from_section(
|
|
"Press 'D' to toggle drawing gizmos on top of everything else in the scene\n\
|
|
Press 'P' to toggle perspective for line gizmos\n\
|
|
Hold 'Left' or 'Right' to change the line width",
|
|
TextStyle {
|
|
font_size: 20.,
|
|
..default()
|
|
},
|
|
)
|
|
.with_style(Style {
|
|
position_type: PositionType::Absolute,
|
|
top: Val::Px(12.0),
|
|
left: Val::Px(12.0),
|
|
..default()
|
|
}),
|
|
);
|
|
}
|
|
|
|
fn system(mut gizmos: Gizmos, time: Res<Time>) {
|
|
gizmos.cuboid(
|
|
Transform::from_translation(Vec3::Y * 0.5).with_scale(Vec3::splat(1.)),
|
|
Color::BLACK,
|
|
);
|
|
gizmos.rect(
|
|
Vec3::new(time.elapsed_seconds().cos() * 2.5, 1., 0.),
|
|
Quat::from_rotation_y(PI / 2.),
|
|
Vec2::splat(2.),
|
|
Color::GREEN,
|
|
);
|
|
|
|
gizmos.sphere(Vec3::new(1., 0.5, 0.), Quat::IDENTITY, 0.5, Color::RED);
|
|
|
|
for y in [0., 0.5, 1.] {
|
|
gizmos.ray(
|
|
Vec3::new(1., y, 0.),
|
|
Vec3::new(-3., (time.elapsed_seconds() * 3.).sin(), 0.),
|
|
Color::BLUE,
|
|
);
|
|
}
|
|
|
|
// Circles have 32 line-segments by default.
|
|
gizmos.circle(Vec3::ZERO, Vec3::Y, 3., Color::BLACK);
|
|
// You may want to increase this for larger circles or spheres.
|
|
gizmos
|
|
.circle(Vec3::ZERO, Vec3::Y, 3.1, Color::NAVY)
|
|
.segments(64);
|
|
gizmos
|
|
.sphere(Vec3::ZERO, Quat::IDENTITY, 3.2, Color::BLACK)
|
|
.circle_segments(64);
|
|
|
|
gizmos.arrow(Vec3::ZERO, Vec3::ONE * 1.5, Color::YELLOW);
|
|
}
|
|
|
|
fn rotate_camera(mut query: Query<&mut Transform, With<Camera>>, time: Res<Time>) {
|
|
let mut transform = query.single_mut();
|
|
|
|
transform.rotate_around(Vec3::ZERO, Quat::from_rotation_y(time.delta_seconds() / 2.));
|
|
}
|
|
|
|
fn update_config(mut config: ResMut<GizmoConfig>, keyboard: Res<Input<KeyCode>>, time: Res<Time>) {
|
|
if keyboard.just_pressed(KeyCode::D) {
|
|
config.depth_bias = if config.depth_bias == 0. { -1. } else { 0. };
|
|
}
|
|
if keyboard.just_pressed(KeyCode::P) {
|
|
// Toggle line_perspective
|
|
config.line_perspective ^= true;
|
|
// Increase the line width when line_perspective is on
|
|
config.line_width *= if config.line_perspective { 5. } else { 1. / 5. };
|
|
}
|
|
|
|
if keyboard.pressed(KeyCode::Right) {
|
|
config.line_width += 5. * time.delta_seconds();
|
|
}
|
|
if keyboard.pressed(KeyCode::Left) {
|
|
config.line_width -= 5. * time.delta_seconds();
|
|
}
|
|
}
|