//! This example demonstrates Bevy's immediate mode drawing API intended for visual debugging. use std::f32::consts::PI; use bevy::math::primitives::{ Capsule3d, Cone, ConicalFrustum, Cuboid, Cylinder, Line3d, Plane3d, Segment3d, Sphere, Torus, }; use bevy::prelude::*; fn main() { App::new() .insert_state(PrimitiveState::Nothing) .init_resource::() .add_plugins(DefaultPlugins) .init_gizmo_group::() .add_systems(Startup, setup) .add_systems(Update, rotate_camera) .add_systems(Update, (draw_example_collection, update_config)) .add_systems(Update, (draw_primitives, update_primitives)) .run(); } // We can create our own gizmo config group! #[derive(Default, Reflect, GizmoConfigGroup)] struct MyRoundGizmos {} #[derive(Debug, Clone, Resource)] struct PrimitiveSegments(usize); impl Default for PrimitiveSegments { fn default() -> Self { Self(10) } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States)] enum PrimitiveState { Nothing, Sphere, Plane, Line, LineSegment, Cuboid, Cylinder, Capsule, Cone, ConicalFrustum, Torus, } impl PrimitiveState { const ALL: [Self; 11] = [ Self::Sphere, Self::Plane, Self::Line, Self::LineSegment, Self::Cuboid, Self::Cylinder, Self::Capsule, Self::Cone, Self::ConicalFrustum, Self::Torus, Self::Nothing, ]; fn next(self) -> Self { Self::ALL .into_iter() .cycle() .skip_while(|&x| x != self) .nth(1) .unwrap() } fn last(self) -> Self { Self::ALL .into_iter() .rev() .cycle() .skip_while(|&x| x != self) .nth(1) .unwrap() } } fn setup( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, ) { 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(Plane3d::default().mesh().size(5.0, 5.0)), material: materials.add(Color::rgb(0.3, 0.5, 0.3)), ..default() }); // cube commands.spawn(PbrBundle { mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)), material: materials.add(Color::rgb(0.8, 0.7, 0.6)), transform: Transform::from_xyz(0.0, 0.5, 0.0), ..default() }); // light commands.spawn(PointLightBundle { point_light: PointLight { intensity: 250000.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 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 'A' to show all AABB boxes\n\ Press 'K' or 'J' to cycle through primitives rendered with gizmos\n\ Press 'H' or 'L' to decrease/increase the amount of segments in the primitives", TextStyle { font_size: 20., ..default() }, ) .with_style(Style { position_type: PositionType::Absolute, top: Val::Px(12.0), left: Val::Px(12.0), ..default() }), ); } fn rotate_camera(mut query: Query<&mut Transform, With>, time: Res