use std::rc::Rc;
use crate::core::{BufferGeometry, Intersection, Node, Object3D, Raycaster};
use crate::materials::MeshBasicNodeMaterial;
use crate::math::{Matrix4, Ray, Sphere, Vector3};
use crate::objects::Payload;
#[derive(Clone)]
pub struct Line {
pub geometry: Rc<BufferGeometry>,
pub material: Option<MeshBasicNodeMaterial>,
pub is_line_segments: bool,
}
impl Line {
#[allow(clippy::new_ret_no_self)] pub fn new(geometry: Rc<BufferGeometry>, material: MeshBasicNodeMaterial) -> Node {
Self::node(geometry, material, false)
}
pub(crate) fn bounding_sphere_in(&self, matrix_world: &Matrix4) -> Option<Sphere> {
let bounding_sphere = self.geometry.compute_bounding_sphere()?;
let mut sphere = Sphere::new(bounding_sphere.center, bounding_sphere.radius);
sphere.apply_matrix4(matrix_world);
Some(sphere)
}
pub fn raycast(
&self,
matrix_world: &Matrix4,
scale: &Vector3,
object: &Node,
raycaster: &Raycaster,
intersects: &mut Vec<Intersection>,
) {
let geometry = &self.geometry;
let threshold = raycaster.params.line.threshold;
let draw_range = geometry.draw_range;
let Some(mut sphere) = self.bounding_sphere_in(matrix_world) else {
return;
};
sphere.radius += threshold;
if !raycaster.ray.intersects_sphere(&sphere) {
return;
}
let mut inverse_matrix = *matrix_world;
inverse_matrix.invert();
let mut ray = raycaster.ray;
ray.apply_matrix4(&inverse_matrix);
let local_threshold = threshold / ((scale.x + scale.y + scale.z) / 3.0);
let local_threshold_sq = local_threshold * local_threshold;
let step = if self.is_line_segments { 2 } else { 1 };
let Some(position) = geometry.position() else {
return;
};
let end_of = |count: usize| match draw_range.count {
Some(draw_count) => count.min(draw_range.start + draw_count),
None => count,
};
let mut check = |a: usize, b: usize, i: usize| {
let start = position.get_vector3(a);
let end = position.get_vector3(b);
if let Some(intersect) = check_intersection(
matrix_world,
object,
raycaster,
&ray,
local_threshold_sq,
&start,
&end,
i,
) {
intersects.push(intersect);
}
};
let start = draw_range.start;
if let Some(index) = &geometry.index {
let end = end_of(index.count());
let mut i = start;
while i + 1 < end {
check(index.get_x(i), index.get_x(i + 1), i);
i += step;
}
} else {
let end = end_of(position.count());
let mut i = start;
while i + 1 < end {
check(i, i + 1, i);
i += step;
}
}
}
pub fn set_positions(&self, positions: &[f32]) {
let attribute = self
.geometry
.get_attribute("position")
.expect("three-rs: the line geometry has a position attribute");
let mut array = attribute.array_mut();
array.clear();
array.extend_from_slice(positions);
drop(array);
attribute.set_needs_update();
}
fn node(
geometry: Rc<BufferGeometry>,
material: MeshBasicNodeMaterial,
is_line_segments: bool,
) -> Node {
let mut object = Object3D {
object_type: if is_line_segments {
"LineSegments"
} else {
"Line"
},
..Default::default()
};
object.payload = Payload::Line(Self {
geometry,
material: Some(material),
is_line_segments,
});
object.into_node()
}
}
pub struct LineSegments;
impl LineSegments {
#[allow(clippy::new_ret_no_self)] pub fn new(geometry: Rc<BufferGeometry>, material: MeshBasicNodeMaterial) -> Node {
Line::node(geometry, material, true)
}
}
#[allow(clippy::too_many_arguments)]
fn check_intersection(
matrix_world: &Matrix4,
object: &Node,
raycaster: &Raycaster,
ray: &Ray,
threshold_sq: f64,
start: &Vector3,
end: &Vector3,
i: usize,
) -> Option<Intersection> {
let mut point_on_ray = Vector3::ZERO;
let mut point_on_segment = Vector3::ZERO;
let dist_sq = ray.distance_sq_to_segment(
start,
end,
Some(&mut point_on_ray),
Some(&mut point_on_segment),
);
if dist_sq > threshold_sq {
return None;
}
point_on_ray.apply_matrix4(matrix_world);
let distance = raycaster.ray.origin.distance_to(&point_on_ray);
if distance < raycaster.near || distance > raycaster.far {
return None;
}
point_on_segment.apply_matrix4(matrix_world);
let mut intersection = Intersection::new(distance, point_on_segment, object.clone());
intersection.index = Some(i);
Some(intersection)
}