From 04035f3ac643e55ffc0354a0291f5667376c3e3b Mon Sep 17 00:00:00 2001 From: Toniman20 Date: Thu, 10 Feb 2022 16:59:59 +0100 Subject: [PATCH 1/2] Add support for `MaterialMesh2dBundle` --- src/lib.rs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bbf353b..28efe61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ use bevy::{ mesh::{Indices, Mesh, VertexAttributeValues}, render_resource::PrimitiveTopology, }, + sprite::Mesh2dHandle, tasks::ComputeTaskPool, utils::FloatOrd, }; @@ -419,6 +420,15 @@ pub fn update_raycast( ), With>, >, + mesh2d_query: Query< + ( + &Mesh2dHandle, + Option<&SimplifiedMesh>, + &GlobalTransform, + Entity, + ), + With>, + >, ) { for mut pick_source in pick_source_query.iter_mut() { if let Some(ray) = pick_source.ray { @@ -464,10 +474,15 @@ pub fn update_raycast( drop(ray_cull_guard); let picks = Arc::new(Mutex::new(BTreeMap::new())); - mesh_query.par_for_each( - &task_pool, - 32, - |(mesh_handle, simplified_mesh, no_backface_culling, transform, entity)| { + + let pick_mesh = + |(mesh_handle, simplified_mesh, no_backface_culling, transform, entity): ( + &Handle, + Option<&SimplifiedMesh>, + Option<&NoBackfaceCulling>, + &GlobalTransform, + Entity, + )| { if culled_list.contains(&entity) { let _raycast_guard = raycast.enter(); // Use the mesh handle to get a reference to a mesh asset @@ -491,8 +506,23 @@ pub fn update_raycast( } } } + }; + + mesh_query.par_for_each(&task_pool, 32, pick_mesh); + mesh2d_query.par_for_each( + &task_pool, + 32, + |(mesh_handle, simplified_mesh, transform, entity)| { + pick_mesh(( + &mesh_handle.0, + simplified_mesh, + Some(&NoBackfaceCulling), + transform, + entity, + )) }, ); + let picks = Arc::try_unwrap(picks).unwrap().into_inner().unwrap(); pick_source.intersections = picks.into_values().map(|(e, i)| (e, i)).collect(); } From 472c10c290867bbcd27a21235d8b2e54415c7335 Mon Sep 17 00:00:00 2001 From: Aevyrie Roessler Date: Sat, 21 May 2022 22:56:44 -0700 Subject: [PATCH 2/2] add 2d example --- examples/mouse_picking_2d.rs | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 examples/mouse_picking_2d.rs diff --git a/examples/mouse_picking_2d.rs b/examples/mouse_picking_2d.rs new file mode 100644 index 0000000..31c84ab --- /dev/null +++ b/examples/mouse_picking_2d.rs @@ -0,0 +1,64 @@ +use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; +use bevy_mod_raycast::{ + DefaultRaycastingPlugin, Intersection, RayCastMesh, RayCastMethod, RayCastSource, RaycastSystem, +}; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_plugin(DefaultRaycastingPlugin::::default()) + .add_system_to_stage( + CoreStage::First, + update_raycast_with_cursor.before(RaycastSystem::BuildRays::), + ) + .add_system(intersection) + .add_startup_system(setup) + .run(); +} + +struct MyRaycastSet; + +// Update our `RayCastSource` with the current cursor position every frame. +fn update_raycast_with_cursor( + mut cursor: EventReader, + mut query: Query<&mut RayCastSource>, +) { + // Grab the most recent cursor event if it exists: + let cursor_position = match cursor.iter().last() { + Some(cursor_moved) => cursor_moved.position, + None => return, + }; + + for mut pick_source in &mut query.iter_mut() { + pick_source.cast_method = RayCastMethod::Screenspace(cursor_position); + } +} + +/// Report intersections +fn intersection(query: Query<&Intersection>) { + for intersection in query.iter() { + info!( + "Distance {:?}, Position {:?}", + intersection.distance(), + intersection.position() + ); + } +} + +fn setup( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + commands + .spawn_bundle(OrthographicCameraBundle::new_2d()) + .insert(RayCastSource::::new()); // Designate the camera as our source; + commands + .spawn_bundle(MaterialMesh2dBundle { + mesh: meshes.add(Mesh::from(shape::Quad::default())).into(), + transform: Transform::default().with_scale(Vec3::splat(128.)), + material: materials.add(ColorMaterial::from(Color::PURPLE)), + ..default() + }) + .insert(RayCastMesh::::default()); // Make this mesh ray cast-able; +}