use bevy::prelude::*;
use bevy_mod_raycast::{prelude::*, print_intersections};
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(low_latency_window_plugin()))
.add_plugins(DefaultRaycastingPlugin::<MyRaycastSet>::default())
.add_systems(
First,
update_raycast_with_cursor.before(RaycastSystem::BuildRays::<MyRaycastSet>),
)
.add_systems(Startup, setup)
.add_systems(Update, print_intersections::<MyRaycastSet>)
.run();
}
#[derive(Reflect)]
struct MyRaycastSet;
fn update_raycast_with_cursor(
mut cursor: EventReader<CursorMoved>,
mut query: Query<&mut RaycastSource<MyRaycastSet>>,
) {
let Some(cursor_moved) = cursor.iter().last() else {
return;
};
for mut pick_source in &mut query {
pick_source.cast_method = RaycastMethod::Screenspace(cursor_moved.position);
}
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.insert_resource(RaycastPluginState::<MyRaycastSet>::default().with_debug_cursor());
commands
.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.0, 2.0).looking_at(Vec3::ZERO, Vec3::Y),
camera: Camera {
viewport: Some(bevy::render::camera::Viewport {
physical_position: UVec2::new(200, 200),
physical_size: UVec2::new(400, 400),
..default()
}),
..default()
},
..default()
})
.insert(RaycastSource::<MyRaycastSet>::new()); commands
.spawn(PbrBundle {
mesh: meshes.add(Mesh::try_from(shape::Icosphere::default()).unwrap()),
material: materials.add(Color::rgb(1.0, 1.0, 1.0).into()),
..Default::default()
})
.insert(RaycastMesh::<MyRaycastSet>::default()); commands.spawn(PointLightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
..Default::default()
});
}