1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! # Immediate Mode Raycasting API
//!
//! See the `minimal` example for reference.
//!
//! This is the simplest way to get started. Add the [`Raycast`] [`SystemParam`] to your system, and
//! call [`Raycast::cast_ray`], to get a list of intersections. Raycasts are performed immediately
//! when you call the `cast_ray` method. See the [`Raycast`] documentation for more details. You
//! don't even need to add a plugin to your application.

use bevy::{
    ecs::system::{lifetimeless::Read, SystemParam},
    prelude::*,
    render::primitives::Aabb,
    utils::FloatOrd,
};

use crate::prelude::*;

/// How a raycast should handle visibility
#[derive(Clone, Copy, Reflect)]
pub enum RaycastVisibility {
    /// Completely ignore visibility checks. Hidden items can still be raycasted against.
    Ignore,
    /// Only raycast against entities that are visible in the hierarchy; see [`Visibility`].
    MustBeVisible,
    /// Only raycast against entities that are visible in the hierarchy and visible to a camera or
    /// light; see [`Visibility`].
    MustBeVisibleAndInView,
}

/// Settings for a raycast.
#[derive(Clone)]
pub struct RaycastSettings<'a> {
    /// Determines how raycasting should consider entity visibility.
    pub visibility: RaycastVisibility,
    /// A filtering function that is applied to every entity that is raycasted. Only entities that
    /// return `true` will be considered.
    pub filter: &'a dyn Fn(Entity) -> bool,
    /// A function that is run every time a hit is found. Raycasting will continue to check for hits
    /// along the ray as long as this returns false.
    pub early_exit_test: &'a dyn Fn(Entity) -> bool,
}

impl<'a> RaycastSettings<'a> {
    /// Set the filter to apply to the raycast.
    pub fn with_filter(mut self, filter: &'a impl Fn(Entity) -> bool) -> Self {
        self.filter = filter;
        self
    }

    /// Set the early exit test to apply to the raycast.
    pub fn with_early_exit_test(mut self, early_exit_test: &'a impl Fn(Entity) -> bool) -> Self {
        self.early_exit_test = early_exit_test;
        self
    }

    /// Set the [`RaycastVisibility`] setting to apply to the raycast.
    pub fn with_visibility(mut self, visibility: RaycastVisibility) -> Self {
        self.visibility = visibility;
        self
    }

    /// This raycast should exit as soon as the nearest hit is found.
    pub fn always_early_exit(self) -> Self {
        self.with_early_exit_test(&|_| true)
    }

    /// This raycast should check all entities whose AABB intersects the ray and return all hits.
    pub fn never_early_exit(self) -> Self {
        self.with_early_exit_test(&|_| false)
    }
}

impl<'a> Default for RaycastSettings<'a> {
    fn default() -> Self {
        Self {
            visibility: RaycastVisibility::MustBeVisibleAndInView,
            filter: &|_| true,
            early_exit_test: &|_| true,
        }
    }
}

#[cfg(feature = "2d")]
type MeshFilter = Or<(With<Handle<Mesh>>, With<bevy::sprite::Mesh2dHandle>)>;
#[cfg(not(feature = "2d"))]
type MeshFilter = With<Handle<Mesh>>;

/// Add this raycasting [`SystemParam`] to your system to raycast into the world with an
/// immediate-mode API. Call `cast_ray` to immediately perform a raycast and get a result. Under the
/// hood, this is a collection of regular bevy queries, resources, and locals that are added to your
/// system.
///
/// ## Usage
///
/// The following system raycasts into the world with a ray positioned at the origin, pointing in
/// the x-direction, and returns a list of intersections:
///
/// ```
/// # use bevy_mod_raycast::prelude::*;
/// # use bevy::prelude::*;
/// fn raycast_system(mut raycast: Raycast) {
///     let ray = Ray3d::new(Vec3::ZERO, Vec3::X);
///     let hits = raycast.cast_ray(ray, &RaycastSettings::default());
/// }
/// ```
/// ## Configuration
///
/// You can specify behavior of the raycast using [`RaycastSettings`]. This allows you to filter out
/// entities, configure early-out, and set whether the [`Visibility`] of an entity should be
/// considered.
///
/// ```
/// # use bevy_mod_raycast::prelude::*;
/// # use bevy::prelude::*;
/// # #[derive(Component)]
/// # struct Foo;
/// fn raycast_system(mut raycast: Raycast, foo_query: Query<With<Foo>>) {
///     let ray = Ray3d::new(Vec3::ZERO, Vec3::X);
///
///     // Only raycast against entities with the `Foo` component.
///     let filter = |entity| foo_query.contains(entity);
///     // Never early-exit. Note that you can change behavior per-entity.
///     let early_exit_test = |_entity| false;
///     // Ignore the visibility of entities. This allows raycasting hidden entities.
///     let visibility = RaycastVisibility::Ignore;
///
///     let settings = RaycastSettings::default()
///         .with_filter(&filter)
///         .with_early_exit_test(&early_exit_test)
///         .with_visibility(visibility);
///
///     let hits = raycast.cast_ray(ray, &settings);
/// }
/// ```
#[derive(SystemParam)]
pub struct Raycast<'w, 's> {
    #[doc(hidden)]
    pub meshes: Res<'w, Assets<Mesh>>,
    #[doc(hidden)]
    pub hits: Local<'s, Vec<(FloatOrd, (Entity, IntersectionData))>>,
    #[doc(hidden)]
    pub output: Local<'s, Vec<(Entity, IntersectionData)>>,
    #[doc(hidden)]
    pub culled_list: Local<'s, Vec<(FloatOrd, Entity)>>,
    #[doc(hidden)]
    pub culling_query: Query<
        'w,
        's,
        (
            Read<ComputedVisibility>,
            Read<Aabb>,
            Read<GlobalTransform>,
            Entity,
        ),
        MeshFilter,
    >,
    #[doc(hidden)]
    pub mesh_query: Query<
        'w,
        's,
        (
            Read<Handle<Mesh>>,
            Option<Read<SimplifiedMesh>>,
            Option<Read<NoBackfaceCulling>>,
            Read<GlobalTransform>,
        ),
    >,
    #[cfg(feature = "2d")]
    #[doc(hidden)]
    pub mesh2d_query: Query<
        'w,
        's,
        (
            Read<bevy::sprite::Mesh2dHandle>,
            Option<Read<SimplifiedMesh>>,
            Read<GlobalTransform>,
        ),
    >,
}

impl<'w, 's> Raycast<'w, 's> {
    #[cfg(feature = "debug")]
    /// Like [`Raycast::cast_ray`], but debug-draws the ray and intersection.
    pub fn debug_cast_ray(
        &mut self,
        ray: Ray3d,
        settings: &RaycastSettings,
        gizmos: &mut Gizmos,
    ) -> &[(Entity, IntersectionData)] {
        let orientation = Quat::from_rotation_arc(Vec3::NEG_Z, ray.direction());
        gizmos.ray(ray.origin(), ray.direction(), Color::BLUE);
        gizmos.sphere(ray.origin(), orientation, 0.1, Color::BLUE);

        let hits = self.cast_ray(ray, settings);

        for (is_first, intersection) in hits
            .iter()
            .map(|i| i.1.clone())
            .enumerate()
            .map(|(i, hit)| (i == 0, hit))
        {
            let color = match is_first {
                true => Color::GREEN,
                false => Color::PINK,
            };
            gizmos.ray(intersection.position(), intersection.normal(), color);
            gizmos.circle(intersection.position(), intersection.normal(), 0.1, color);
        }

        if let Some(hit) = hits.first() {
            debug!("{:?}", hit);
        }

        hits
    }

    /// Casts the `ray` into the world and returns a sorted list of intersections, nearest first.
    pub fn cast_ray(
        &mut self,
        ray: Ray3d,
        settings: &RaycastSettings,
    ) -> &[(Entity, IntersectionData)] {
        let ray_cull = info_span!("ray culling");
        let ray_cull_guard = ray_cull.enter();

        self.hits.clear();
        self.culled_list.clear();
        self.output.clear();

        // Check all entities to see if the ray intersects the AABB, use this to build a short list
        // of entities that are in the path of the ray.
        let (aabb_hits_tx, aabb_hits_rx) = crossbeam_channel::unbounded::<(FloatOrd, Entity)>();
        let visibility_setting = settings.visibility;
        self.culling_query
            .par_iter()
            .for_each(|(visibility, aabb, transform, entity)| {
                let should_raycast = match visibility_setting {
                    RaycastVisibility::Ignore => true,
                    RaycastVisibility::MustBeVisible => visibility.is_visible_in_hierarchy(),
                    RaycastVisibility::MustBeVisibleAndInView => visibility.is_visible_in_view(),
                };
                if should_raycast {
                    if let Some([near, _]) = ray
                        .intersects_aabb(aabb, &transform.compute_matrix())
                        .filter(|[_, far]| *far >= 0.0)
                    {
                        aabb_hits_tx.send((FloatOrd(near), entity)).ok();
                    }
                }
            });
        *self.culled_list = aabb_hits_rx.try_iter().collect();
        self.culled_list.sort_by_key(|(aabb_near, _)| *aabb_near);
        drop(ray_cull_guard);

        let mut nearest_blocking_hit = FloatOrd(f32::INFINITY);
        let raycast_guard = debug_span!("raycast");
        self.culled_list
            .iter()
            .filter(|(_, entity)| (settings.filter)(*entity))
            .for_each(|(aabb_near, entity)| {
                let mut raycast_mesh =
                    |mesh_handle: &Handle<Mesh>,
                     simplified_mesh: Option<&SimplifiedMesh>,
                     no_backface_culling: Option<&NoBackfaceCulling>,
                     transform: &GlobalTransform| {
                        // Is it even possible the mesh could be closer than the current best?
                        if *aabb_near > nearest_blocking_hit {
                            return;
                        }

                        // Does the mesh handle resolve?
                        let mesh_handle = simplified_mesh.map(|m| &m.mesh).unwrap_or(mesh_handle);
                        let Some(mesh) = self.meshes.get(mesh_handle) else {
                            return;
                        };

                        let _raycast_guard = raycast_guard.enter();
                        let backfaces = match no_backface_culling {
                            Some(_) => Backfaces::Include,
                            None => Backfaces::Cull,
                        };
                        let transform = transform.compute_matrix();
                        let intersection =
                            ray_intersection_over_mesh(mesh, &transform, &ray, backfaces);
                        if let Some(intersection) = intersection {
                            let distance = FloatOrd(intersection.distance());
                            if (settings.early_exit_test)(*entity)
                                && distance < nearest_blocking_hit
                            {
                                // The reason we don't just return here is because right now we are
                                // going through the AABBs in order, but that doesn't mean that an
                                // AABB that starts further away cant end up with a closer hit than
                                // an AABB that starts closer. We need to keep checking AABBs that
                                // could possibly contain a nearer hit.
                                nearest_blocking_hit = distance.min(nearest_blocking_hit);
                            }
                            self.hits.push((distance, (*entity, intersection)));
                        };
                    };

                if let Ok((mesh, simp_mesh, culling, transform)) = self.mesh_query.get(*entity) {
                    raycast_mesh(mesh, simp_mesh, culling, transform);
                }

                #[cfg(feature = "2d")]
                if let Ok((mesh, simp_mesh, transform)) = self.mesh2d_query.get(*entity) {
                    raycast_mesh(&mesh.0, simp_mesh, Some(&NoBackfaceCulling), transform);
                }
            });

        self.hits.retain(|(dist, _)| *dist <= nearest_blocking_hit);
        self.hits.sort_by_key(|(k, _)| *k);
        let hits = self.hits.iter().map(|(_, (e, i))| (*e, i.to_owned()));
        *self.output = hits.collect();
        self.output.as_ref()
    }
}