use crate::cameras::RenderCamera;
use crate::core::{Layers, Node};
use crate::math::{Matrix4, Ray, Vector2, Vector3};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RaycasterCamera {
pub is_perspective_camera: bool,
pub is_orthographic_camera: bool,
pub near: f64,
pub far: f64,
pub matrix_world: Matrix4,
pub matrix_world_inverse: Matrix4,
pub projection_matrix: Matrix4,
pub projection_matrix_inverse: Matrix4,
}
impl RaycasterCamera {
pub fn of(camera: &impl RenderCamera) -> Self {
Self {
is_perspective_camera: camera.is_perspective_camera(),
is_orthographic_camera: camera.is_orthographic_camera(),
near: camera.near(),
far: camera.far(),
matrix_world: camera.matrix_world(),
matrix_world_inverse: camera.matrix_world_inverse(),
projection_matrix: camera.projection_matrix(),
projection_matrix_inverse: camera.projection_matrix_inverse(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Threshold {
pub threshold: f64,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct RaycasterParams {
pub line: Threshold,
pub points: Threshold,
pub line2: Threshold,
}
impl Default for RaycasterParams {
fn default() -> Self {
Self {
line: Threshold { threshold: 1.0 },
points: Threshold { threshold: 1.0 },
line2: Threshold { threshold: 0.0 },
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Face {
pub a: usize,
pub b: usize,
pub c: usize,
pub normal: Vector3,
pub material_index: usize,
}
#[derive(Clone, Debug)]
pub struct Intersection {
pub distance: f64,
pub distance_to_ray: Option<f64>,
pub point: Vector3,
pub point_on_line: Option<Vector3>,
pub index: Option<usize>,
pub face: Option<Face>,
pub face_index: Option<usize>,
pub uv: Option<Vector2>,
pub uv1: Option<Vector2>,
pub normal: Option<Vector3>,
pub barycoord: Option<Vector3>,
pub instance_id: Option<usize>,
pub batch_id: Option<usize>,
pub object: Node,
}
impl Intersection {
pub fn new(distance: f64, point: Vector3, object: Node) -> Self {
Self {
distance,
distance_to_ray: None,
point,
point_on_line: None,
index: None,
face: None,
face_index: None,
uv: None,
uv1: None,
normal: None,
barycoord: None,
instance_id: None,
batch_id: None,
object,
}
}
}
#[derive(Clone, Debug)]
pub struct Raycaster {
pub ray: Ray,
pub near: f64,
pub far: f64,
pub camera: Option<RaycasterCamera>,
pub layers: Layers,
pub params: RaycasterParams,
}
impl Default for Raycaster {
fn default() -> Self {
let ray = Ray::default();
Self::new(ray.origin, ray.direction, 0.0, f64::INFINITY)
}
}
impl Raycaster {
pub fn new(origin: Vector3, direction: Vector3, near: f64, far: f64) -> Self {
Self {
ray: Ray::new(origin, direction),
near,
far,
camera: None,
layers: Layers::new(),
params: RaycasterParams::default(),
}
}
pub fn set(&mut self, origin: &Vector3, direction: &Vector3) {
self.ray.set(origin, direction);
}
pub fn set_from_camera(&mut self, coords: &Vector2, camera: &impl RenderCamera) {
if camera.is_perspective_camera() {
self.ray
.origin
.set_from_matrix_position(&camera.matrix_world());
let origin = self.ray.origin;
self.ray.direction = Vector3::new(coords.x, coords.y, 0.5);
self.ray
.direction
.unproject(camera)
.sub(&origin)
.normalize();
self.camera = Some(RaycasterCamera::of(camera));
} else if camera.is_orthographic_camera() {
let z = camera.projection_matrix().elements[14];
self.ray.origin = Vector3::new(coords.x, coords.y, z);
self.ray.origin.unproject(camera);
self.ray.direction = Vector3::new(0.0, 0.0, -1.0);
self.ray
.direction
.transform_direction(&camera.matrix_world());
self.camera = Some(RaycasterCamera::of(camera));
} else {
eprintln!("THREE.Raycaster: Unsupported camera type.");
}
}
pub fn intersect_object(&self, object: &Node, recursive: bool) -> Vec<Intersection> {
let mut intersects = Vec::new();
intersect(object, self, &mut intersects, recursive);
sort(&mut intersects);
intersects
}
pub fn intersect_objects(&self, objects: &[Node], recursive: bool) -> Vec<Intersection> {
let mut intersects = Vec::new();
for object in objects {
intersect(object, self, &mut intersects, recursive);
}
sort(&mut intersects);
intersects
}
}
fn sort(intersects: &mut [Intersection]) {
intersects.sort_by(|a, b| {
a.distance
.partial_cmp(&b.distance)
.unwrap_or(std::cmp::Ordering::Equal)
});
}
fn intersect(
object: &Node,
raycaster: &Raycaster,
intersects: &mut Vec<Intersection>,
recursive: bool,
) {
let tested = object.borrow().layers.test(&raycaster.layers);
if tested {
object.raycast(raycaster, intersects);
}
if recursive {
for child in object.children() {
intersect(&child, raycaster, intersects, true);
}
}
}
impl Node {
pub fn raycast(&self, raycaster: &Raycaster, intersects: &mut Vec<Intersection>) {
use crate::objects::Payload;
let mut object = self.borrow_mut();
let matrix_world = object.matrix_world;
let scale = object.scale;
match &mut object.payload {
Payload::None | Payload::Light(_) => {}
Payload::Mesh(mesh) => {
if mesh.line_segments.is_some() {
crate::addons::lines::raycast(mesh, &matrix_world, self, raycaster, intersects);
} else {
mesh.raycast(&matrix_world, self, raycaster, intersects);
}
}
Payload::SkinnedMesh(skinned) => {
skinned.raycast(&matrix_world, self, raycaster, intersects)
}
Payload::InstancedMesh(instanced) => {
instanced.raycast(&matrix_world, self, raycaster, intersects)
}
Payload::BatchedMesh(batched) => {
batched.raycast(&matrix_world, self, raycaster, intersects)
}
Payload::Line(line) => line.raycast(&matrix_world, &scale, self, raycaster, intersects),
Payload::Points(points) => {
points.raycast(&matrix_world, &scale, self, raycaster, intersects)
}
Payload::Sprite(sprite) => sprite.raycast(&matrix_world, self, raycaster, intersects),
}
}
}