Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MaterialMesh2dBundle #38

Merged
merged 2 commits into from
May 22, 2022
Merged
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
64 changes: 64 additions & 0 deletions examples/mouse_picking_2d.rs
Original file line number Diff line number Diff line change
@@ -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::<MyRaycastSet>::default())
.add_system_to_stage(
CoreStage::First,
update_raycast_with_cursor.before(RaycastSystem::BuildRays::<MyRaycastSet>),
)
.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<CursorMoved>,
mut query: Query<&mut RayCastSource<MyRaycastSet>>,
) {
// 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<MyRaycastSet>>) {
for intersection in query.iter() {
info!(
"Distance {:?}, Position {:?}",
intersection.distance(),
intersection.position()
);
}
}

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands
.spawn_bundle(OrthographicCameraBundle::new_2d())
.insert(RayCastSource::<MyRaycastSet>::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::<MyRaycastSet>::default()); // Make this mesh ray cast-able;
}
38 changes: 34 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use bevy::{
mesh::{Indices, Mesh, VertexAttributeValues},
render_resource::PrimitiveTopology,
},
sprite::Mesh2dHandle,
tasks::ComputeTaskPool,
utils::FloatOrd,
};
Expand Down Expand Up @@ -419,6 +420,15 @@ pub fn update_raycast<T: 'static>(
),
With<RayCastMesh<T>>,
>,
mesh2d_query: Query<
(
&Mesh2dHandle,
Option<&SimplifiedMesh>,
&GlobalTransform,
Entity,
),
With<RayCastMesh<T>>,
>,
) {
for mut pick_source in pick_source_query.iter_mut() {
if let Some(ray) = pick_source.ray {
Expand Down Expand Up @@ -464,10 +474,15 @@ pub fn update_raycast<T: 'static>(
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<Mesh>,
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
Expand All @@ -491,8 +506,23 @@ pub fn update_raycast<T: 'static>(
}
}
}
};

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();
}
Expand Down