mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
6a85eb3d7e
# Objective Add a convenient immediate mode drawing API for visual debugging. Fixes #5619 Alternative to #1625 Partial alternative to #5734 Based off https://github.com/Toqozz/bevy_debug_lines with some changes: * Simultaneous support for 2D and 3D. * Methods for basic shapes; circles, spheres, rectangles, boxes, etc. * 2D methods. * Removed durations. Seemed niche, and can be handled by users. <details> <summary>Performance</summary> Stress tested using Bevy's recommended optimization settings for the dev profile with the following command. ```bash cargo run --example many_debug_lines \ --config "profile.dev.package.\"*\".opt-level=3" \ --config "profile.dev.opt-level=1" ``` I dipped to 65-70 FPS at 300,000 lines CPU: 3700x RAM Speed: 3200 Mhz GPU: 2070 super - probably not very relevant, mostly cpu/memory bound </details> <details> <summary>Fancy bloom screenshot</summary> ![Screenshot_20230207_155033](https://user-images.githubusercontent.com/29694403/217291980-f1e0500e-7a14-4131-8c96-eaaaf52596ae.png) </details> ## Changelog * Added `GizmoPlugin` * Added `Gizmos` system parameter for drawing lines and wireshapes. ### TODO - [ ] Update changelog - [x] Update performance numbers - [x] Add credit to PR description ### Future work - Cache rendering primitives instead of constructing them out of line segments each frame. - Support for drawing solid meshes - Interactions. (See [bevy_mod_gizmos](https://github.com/LiamGallagher737/bevy_mod_gizmos)) - Fancier line drawing. (See [bevy_polyline](https://github.com/ForesightMiningSoftwareCorporation/bevy_polyline)) - Support for `RenderLayers` - Display gizmos for a certain duration. Currently everything displays for one frame (ie. immediate mode) - Changing settings per drawn item like drawing on top or drawing to different `RenderLayers` Co-Authored By: @lassade <felipe.jorge.pereira@gmail.com> Co-Authored By: @The5-1 <agaku@hotmail.de> Co-Authored By: @Toqozz <toqoz@hotmail.com> Co-Authored By: @nicopap <nico@nicopap.ch> --------- Co-authored-by: Robert Swain <robert.swain@gmail.com> Co-authored-by: IceSentry <c.giguere42@gmail.com> Co-authored-by: Carter Anderson <mcanders1@gmail.com>
108 lines
3.1 KiB
Rust
108 lines
3.1 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>>,
|
|
asset_server: Res<AssetServer>,
|
|
) {
|
|
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()
|
|
});
|
|
// text
|
|
commands.spawn(TextBundle::from_section(
|
|
"Press 't' to toggle drawing gizmos on top of everything else in the scene",
|
|
TextStyle {
|
|
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
|
|
font_size: 24.,
|
|
color: Color::WHITE,
|
|
},
|
|
));
|
|
}
|
|
|
|
fn system(mut gizmos: Gizmos, time: Res<Time>) {
|
|
gizmos.cuboid(
|
|
Vec3::Y * -0.5,
|
|
Quat::IDENTITY,
|
|
Vec3::new(5., 1., 2.),
|
|
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.with_a(0.5),
|
|
);
|
|
|
|
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);
|
|
}
|
|
|
|
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 gizmo_config: ResMut<GizmoConfig>, keyboard: Res<Input<KeyCode>>) {
|
|
if keyboard.just_pressed(KeyCode::T) {
|
|
gizmo_config.on_top = !gizmo_config.on_top;
|
|
}
|
|
}
|