 5faff84c10
			
		
	
	
		5faff84c10
		
			
		
	
	
	
	
		
			
			# Objective The debug features (`DebugPickingPlugin`) from `bevy_mod_picking` were not upstreamed with the rest of the core changes, this PR reintroduces it for usage inside `bevy_dev_tools` ## Solution Vast majority of this code is taken as-is from `bevy_mod_picking` aside from changes to ensure compilation and code style, as such @aevyrie was added as the co-author for this change. ### Main changes * `multiselection` support - the relevant code was explicitly not included in the process of upstreaming the rest of the package, so it also has been omitted here. * `bevy_egui` support - the old package had a preference for using `bevy_egui` instead of `bevy_ui` if possible, I couldn't see a way to support this in a core crate, so this has been removed. Relevant code has been added to the `bevy_dev_tools` crate instead of `bevy_picking` as it is a better fit and requires a dependency on `bevy_ui` for drawing debug elements. ### Minor changes * Changed the debug text size from `60` to `12` as the former was so large as to be unreadable in the new example. ## Testing * `cargo run -p ci` * Added a new example in `dev_tools/picking_debug` and visually verified the in-window results and the console messages --------- Co-authored-by: Aevyrie <aevyrie@gmail.com> Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
		
			
				
	
	
		
			111 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			111 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! A simple scene to demonstrate picking events for UI and mesh entities,
 | |
| //! Demonstrates how to change debug settings
 | |
| 
 | |
| use bevy::dev_tools::picking_debug::{DebugPickingMode, DebugPickingPlugin};
 | |
| use bevy::prelude::*;
 | |
| 
 | |
| fn main() {
 | |
|     App::new()
 | |
|         .add_plugins(DefaultPlugins.set(bevy::log::LogPlugin {
 | |
|             filter: "bevy_dev_tools=trace".into(), // Show picking logs trace level and up
 | |
|             ..default()
 | |
|         }))
 | |
|         // Unlike UiPickingPlugin, MeshPickingPlugin is not a default plugin
 | |
|         .add_plugins((MeshPickingPlugin, DebugPickingPlugin))
 | |
|         .add_systems(Startup, setup_scene)
 | |
|         .insert_resource(DebugPickingMode::Normal)
 | |
|         // A system that cycles the debugging state when you press F3:
 | |
|         .add_systems(
 | |
|             PreUpdate,
 | |
|             (|mut mode: ResMut<DebugPickingMode>| {
 | |
|                 *mode = match *mode {
 | |
|                     DebugPickingMode::Disabled => DebugPickingMode::Normal,
 | |
|                     DebugPickingMode::Normal => DebugPickingMode::Noisy,
 | |
|                     DebugPickingMode::Noisy => DebugPickingMode::Disabled,
 | |
|                 }
 | |
|             })
 | |
|             .distributive_run_if(bevy::input::common_conditions::input_just_pressed(
 | |
|                 KeyCode::F3,
 | |
|             )),
 | |
|         )
 | |
|         .run();
 | |
| }
 | |
| 
 | |
| fn setup_scene(
 | |
|     mut commands: Commands,
 | |
|     mut meshes: ResMut<Assets<Mesh>>,
 | |
|     mut materials: ResMut<Assets<StandardMaterial>>,
 | |
| ) {
 | |
|     commands
 | |
|         .spawn((
 | |
|             Text::new("Click Me to get a box\nDrag cubes to rotate\nPress F3 to cycle between picking debug levels"),
 | |
|             Node {
 | |
|                 position_type: PositionType::Absolute,
 | |
|                 top: Val::Percent(12.0),
 | |
|                 left: Val::Percent(12.0),
 | |
|                 ..default()
 | |
|             },
 | |
|         ))
 | |
|         .observe(on_click_spawn_cube)
 | |
|         .observe(
 | |
|             |out: Trigger<Pointer<Out>>, mut texts: Query<&mut TextColor>| {
 | |
|                 let mut text_color = texts.get_mut(out.target()).unwrap();
 | |
|                 text_color.0 = Color::WHITE;
 | |
|             },
 | |
|         )
 | |
|         .observe(
 | |
|             |over: Trigger<Pointer<Over>>, mut texts: Query<&mut TextColor>| {
 | |
|                 let mut color = texts.get_mut(over.target()).unwrap();
 | |
|                 color.0 = bevy::color::palettes::tailwind::CYAN_400.into();
 | |
|             },
 | |
|         );
 | |
| 
 | |
|     // Base
 | |
|     commands.spawn((
 | |
|         Name::new("Base"),
 | |
|         Mesh3d(meshes.add(Circle::new(4.0))),
 | |
|         MeshMaterial3d(materials.add(Color::WHITE)),
 | |
|         Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
 | |
|     ));
 | |
| 
 | |
|     // Light
 | |
|     commands.spawn((
 | |
|         PointLight {
 | |
|             shadows_enabled: true,
 | |
|             ..default()
 | |
|         },
 | |
|         Transform::from_xyz(4.0, 8.0, 4.0),
 | |
|     ));
 | |
| 
 | |
|     // Camera
 | |
|     commands.spawn((
 | |
|         Camera3d::default(),
 | |
|         Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
 | |
|     ));
 | |
| }
 | |
| 
 | |
| fn on_click_spawn_cube(
 | |
|     _click: Trigger<Pointer<Click>>,
 | |
|     mut commands: Commands,
 | |
|     mut meshes: ResMut<Assets<Mesh>>,
 | |
|     mut materials: ResMut<Assets<StandardMaterial>>,
 | |
|     mut num: Local<usize>,
 | |
| ) {
 | |
|     commands
 | |
|         .spawn((
 | |
|             Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
 | |
|             MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
 | |
|             Transform::from_xyz(0.0, 0.25 + 0.55 * *num as f32, 0.0),
 | |
|         ))
 | |
|         // With the MeshPickingPlugin added, you can add pointer event observers to meshes:
 | |
|         .observe(on_drag_rotate);
 | |
|     *num += 1;
 | |
| }
 | |
| 
 | |
| fn on_drag_rotate(drag: Trigger<Pointer<Drag>>, mut transforms: Query<&mut Transform>) {
 | |
|     if let Ok(mut transform) = transforms.get_mut(drag.target()) {
 | |
|         transform.rotate_y(drag.delta.x * 0.02);
 | |
|         transform.rotate_x(drag.delta.y * 0.02);
 | |
|     }
 | |
| }
 |