//! Demonstrates how to make a gltf pickable. Clicking on any mesh will print out the entity that //! was clicked on, even though we only need to add an `On>` event listener to the //! root of the scene. use bevy::prelude::*; use bevy_mod_picking::prelude::*; fn main() { App::new() .add_plugins(( DefaultPlugins.set(low_latency_window_plugin()), DefaultPickingPlugins .build() .disable::(), )) .insert_resource(DebugPickingMode::Normal) .add_systems(Startup, setup) .add_systems(Update, make_pickable) .run(); } fn setup(mut commands: Commands, asset_server: Res) { commands.spawn((Camera3dBundle { transform: Transform::from_xyz(0.7, 0.7, 1.0).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y), ..default() },)); commands.spawn(DirectionalLightBundle::default()); commands.spawn(( SceneBundle { scene: asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"), ..default() }, // Events that target children of the scene will bubble up to this level and will fire off a // `HelmetClicked` event. On::>::run(|event: Listener>| { info!("Clicked on entity {:?}", event.target); }), )); } /// Makes everything in the scene with a mesh pickable fn make_pickable( mut commands: Commands, meshes: Query>, Without)>, ) { for entity in meshes.iter() { commands .entity(entity) .insert((PickableBundle::default(), HIGHLIGHT_TINT.clone())); } } /// Used to tint the mesh instead of simply replacing the mesh's material with a single color. See /// `tinted_highlight` for more details. const HIGHLIGHT_TINT: Highlight = Highlight { hovered: Some(HighlightKind::new_dynamic(|matl| StandardMaterial { base_color: matl .base_color .mix(&Color::srgba(-0.5, -0.3, 0.9, 0.8), 0.5), // hovered is blue ..matl.to_owned() })), pressed: Some(HighlightKind::new_dynamic(|matl| StandardMaterial { base_color: matl .base_color .mix(&Color::srgba(-0.4, -0.4, 0.8, 0.8), 0.5), // pressed is a different blue ..matl.to_owned() })), selected: Some(HighlightKind::new_dynamic(|matl| StandardMaterial { base_color: matl .base_color .mix(&Color::srgba(-0.4, 0.8, -0.4, 0.0), 0.5), // selected is green ..matl.to_owned() })), };