Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: use RenderLayers to filter interactions between box frames and cameras #6

Merged
merged 2 commits into from
Feb 23, 2024
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_fsl_box_frame"
version = "0.1.0"
version = "0.1.1"
description = "A gizmo for manipulating an OBB via 3D picking"
edition = "2021"
authors = ["Duncan Fairbanks <duncan.fairbanks@fslabs.ca>"]
Expand Down
16 changes: 12 additions & 4 deletions src/picking_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,35 @@ use crate::{ray_map::RayMap, BoxFrame};
use bevy::{
ecs::prelude::*,
prelude::{Camera, GlobalTransform},
render::view::RenderLayers,
};
use bevy_mod_picking::backend::{HitData, PointerHits};
use parry3d::{na::Isometry3, query::RayCast};

/// Generates pointer hits for the box frame's AABB and handles.
pub(crate) fn box_frame_backend(
ray_map: Res<RayMap>,
cameras: Query<&Camera>,
box_frames: Query<(Entity, &BoxFrame, &GlobalTransform)>,
cameras: Query<(&Camera, Option<&RenderLayers>)>,
box_frames: Query<(Entity, &BoxFrame, &GlobalTransform, Option<&RenderLayers>)>,
transforms: Query<&GlobalTransform>,
mut picking_out: EventWriter<PointerHits>,
) {
for (&ray_id, &ray) in ray_map.map().iter() {
let Ok(camera) = cameras.get(ray_id.camera) else {
let Ok((camera, view_mask)) = cameras.get(ray_id.camera) else {
continue;
};

let cam_view_mask = view_mask.copied().unwrap_or_default();

let ray = parry3d::query::Ray::new(ray.origin.into(), ray.direction.into());

let mut picks = Vec::new();
for (frame_entity, frame, frame_transform) in &box_frames {
for (frame_entity, frame, frame_transform, frame_view_mask) in &box_frames {
let frame_view_mask = frame_view_mask.copied().unwrap_or_default();
if !frame_view_mask.intersects(&cam_view_mask) {
continue;
}

let world_frame_center = frame_transform.transform_point(frame.center());
// Check handle intersections first, they always take priority.
let ball = frame.handle_ball();
Expand Down