bevy_mod_picking 0.20.1

A modular picking plugin for Bevy.
Documentation
//! Demonstrates that picking respects camera render order.

use bevy::{prelude::*, render::camera::ClearColorConfig};
use bevy_color::palettes;
use bevy_mod_picking::prelude::*;

fn main() {
    App::new()
        .add_plugins((
            DefaultPlugins.set(low_latency_window_plugin()),
            DefaultPickingPlugins,
        ))
        .insert_resource(DebugPickingMode::Normal)
        .add_systems(Startup, setup)
        .run();
}

/// set up a simple 3D scene
fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    // plane
    commands.spawn((
        PbrBundle {
            mesh: meshes.add(bevy_render::mesh::PlaneMeshBuilder::from_length(5.0)),
            material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
            ..default()
        },
        PickableBundle::default(), // <- Makes the mesh pickable.
    ));
    // cube
    commands.spawn((
        PbrBundle {
            mesh: meshes.add(Cuboid::default()),
            material: materials.add(Color::srgb(0.8, 0.7, 0.6)),
            transform: Transform::from_xyz(0.0, 0.5, 0.0),
            ..default()
        },
        PickableBundle::default(), // <- Makes the mesh pickable.
    ));
    // light
    commands.spawn(PointLightBundle {
        point_light: PointLight {
            shadows_enabled: true,
            ..default()
        },
        transform: Transform::from_xyz(4.0, 8.0, 4.0),
        ..default()
    });
    // camera
    commands.spawn((Camera3dBundle {
        transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
        ..default()
    },));

    // plane 2
    commands.spawn((
        PbrBundle {
            mesh: meshes.add(bevy_render::mesh::PlaneMeshBuilder::from_length(5.0)),
            material: materials.add(Color::from(palettes::basic::BLUE)),
            transform: Transform::from_xyz(20., 20., 20.),
            ..default()
        },
        PickableBundle::default(), // <- Makes the mesh pickable.
    ));
    // cube 2
    commands.spawn((
        PbrBundle {
            mesh: meshes.add(Cuboid::default()),
            material: materials.add(Color::from(palettes::basic::YELLOW)),
            transform: Transform::from_xyz(20., 20.5, 20.),
            ..default()
        },
        PickableBundle::default(), // <- Makes the mesh pickable.
    ));
    // light
    commands.spawn(PointLightBundle {
        point_light: PointLight {
            shadows_enabled: true,
            ..default()
        },
        transform: Transform::from_xyz(18.0, 22.0, 28.0),
        ..default()
    });
    // camera 2
    commands.spawn((Camera3dBundle {
        transform: Transform::from_xyz(30., 30., 30.0)
            .looking_at(Vec3::new(20., 20.5, 20.), Vec3::Y),
        camera: Camera {
            clear_color: ClearColorConfig::None,
            // renders after / on top of the main camera
            order: 1,
            ..default()
        },
        ..default()
    },));
}