Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions examples/mouse_picking.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
//! This example will show you how to use your mouse cursor as a ray casting source, cast into the
//! scene, intersect a mesh, and mark the intersection with the built in debug cursor. If you are
//! looking for a more fully-featured mouse picking plugin, try out bevy_mod_picking.
use bevy::{color::palettes::css, prelude::*};
use bevy_mod_raycast::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(bevy_mod_raycast::low_latency_window_plugin()))
.add_plugins(CursorRayPlugin)
.add_systems(Startup, setup)
.add_systems(Update, raycast)
.run();
}
fn raycast(cursor_ray: Res<CursorRay>, mut raycast: Raycast, mut gizmos: Gizmos) {
if let Some(cursor_ray) = **cursor_ray {
raycast.debug_cast_ray(cursor_ray, &default(), &mut gizmos);
}
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn(Camera3dBundle::default());
commands.spawn(PointLightBundle::default());
commands.spawn(PbrBundle {
mesh: meshes.add(Sphere::default()),
material: materials.add(Color::from(css::GRAY)),
transform: Transform::from_xyz(0.0, 0.0, -5.0),
..default()
});
}
//! This example will show you how to use your mouse cursor as a ray casting source, cast into the
//! scene, intersect a mesh, and mark the intersection with the built in debug cursor. If you are
//! looking for a more fully-featured mouse picking plugin, try out bevy_mod_picking.

use bevy::{color::palettes::css, prelude::*};
use bevy_mod_raycast::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins.set(bevy_mod_raycast::low_latency_window_plugin()))
.add_plugins(CursorRayPlugin)
.add_systems(Startup, setup)
.add_systems(Update, raycast)
.run();
}

fn raycast(cursor_ray: Res<CursorRay>, mut raycast: Raycast, mut gizmos: Gizmos) {
if let Some(cursor_ray) = **cursor_ray {
raycast.debug_cast_ray(cursor_ray, &default(), &mut gizmos);
}
}

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((Camera3dBundle::default(), CursorRayCam));
commands.spawn(PointLightBundle::default());
commands.spawn(PbrBundle {
mesh: meshes.add(Sphere::default()),
material: materials.add(Color::from(css::GRAY)),
transform: Transform::from_xyz(0.0, 0.0, -5.0),
..default()
});
}
1 change: 1 addition & 0 deletions examples/reflecting_laser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fn setup_scene(
..default()
},
BloomSettings::default(),
CursorRayCam,
));
// Make a box of planes facing inward so the laser gets trapped inside:
let plane = PbrBundle {
Expand Down
2 changes: 1 addition & 1 deletion examples/simplified_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn setup_scene(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn(Camera3dBundle::default());
commands.spawn((Camera3dBundle::default(), CursorRayCam));
commands.spawn((
PbrBundle {
// This is a very complex mesh that will be hard to raycast on
Expand Down
8 changes: 6 additions & 2 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy_window::Window;

use crate::ray_from_screenspace;

/// Automatically generates a ray in world space corresponding to the mouse cursor, and stores it in
/// Automatically generates a ray in world space corresponding to the mouse cursor and [`CursorRayCam`] marked camera(s), then stores it in
/// [`CursorRay`].
#[derive(Default)]
pub struct CursorRayPlugin;
Expand All @@ -23,6 +23,10 @@ impl Plugin for CursorRayPlugin {
}
}

/// Marks the camera this component is attached to as a [`CursorRay`] ray source.
#[derive(Component)]
pub struct CursorRayCam;

/// Holds the latest cursor position as a 3d ray.
///
/// Requires the [`CursorRayPlugin`] is added to your app. This is updated in both [`First`] and
Expand All @@ -36,7 +40,7 @@ pub struct CursorRay(pub Option<Ray3d>);
pub fn update_cursor_ray(
primary_window: Query<Entity, With<bevy_window::PrimaryWindow>>,
windows: Query<&Window>,
cameras: Query<(&Camera, &GlobalTransform)>,
cameras: Query<(&Camera, &GlobalTransform), With<CursorRayCam>>,
mut cursor_ray: ResMut<CursorRay>,
) {
cursor_ray.0 = cameras
Expand Down