three_rs/objects/
points.rs1use std::rc::Rc;
12
13use crate::core::{BufferGeometry, Intersection, Object3D, ObjectRef, Raycaster};
14use crate::materials::MeshBasicNodeMaterial;
15use crate::math::{Matrix4, Sphere, Vector3};
16use crate::objects::Payload;
17
18#[derive(Clone)]
21pub struct Points {
22 pub geometry: Rc<BufferGeometry>,
24 pub material: Option<MeshBasicNodeMaterial>,
26 pub count: Option<usize>,
34}
35
36impl Points {
37 #[allow(clippy::new_ret_no_self)] pub fn new(geometry: Rc<BufferGeometry>, material: MeshBasicNodeMaterial) -> ObjectRef {
40 let mut object = Object3D {
41 object_type: "Points",
42 ..Default::default()
43 };
44 object.payload = Payload::Points(Self {
45 geometry,
46 material: Some(material),
47 count: None,
48 });
49 object.into_node()
50 }
51
52 pub(crate) fn bounding_sphere_in(&self, matrix_world: &Matrix4) -> Option<Sphere> {
56 let bounding_sphere = self.geometry.compute_bounding_sphere()?;
57 let mut sphere = Sphere::new(bounding_sphere.center, bounding_sphere.radius);
58 sphere.apply_matrix4(matrix_world);
59 Some(sphere)
60 }
61
62 pub fn raycast(
66 &self,
67 matrix_world: &Matrix4,
68 scale: &Vector3,
69 object: &ObjectRef,
70 raycaster: &Raycaster,
71 intersects: &mut Vec<Intersection>,
72 ) {
73 let geometry = &self.geometry;
74 let threshold = raycaster.params.points.threshold;
75 let draw_range = geometry.draw_range;
76
77 let Some(mut sphere) = self.bounding_sphere_in(matrix_world) else {
79 return;
80 };
81 sphere.radius += threshold;
82 if !raycaster.ray.intersects_sphere(&sphere) {
83 return;
84 }
85
86 let mut inverse_matrix = *matrix_world;
87 inverse_matrix.invert();
88 let mut ray = raycaster.ray;
89 ray.apply_matrix4(&inverse_matrix);
90
91 let local_threshold = threshold / ((scale.x + scale.y + scale.z) / 3.0);
92 let local_threshold_sq = local_threshold * local_threshold;
93
94 let Some(position) = geometry.position() else {
95 return;
96 };
97 let end_of = |count: usize| match draw_range.count {
98 Some(draw_count) => count.min(draw_range.start + draw_count),
99 None => count,
100 };
101
102 let mut test_point = |index: usize| {
105 let point = position.get_vector3(index);
106 let ray_point_distance_sq = ray.distance_sq_to_point(&point);
107 if ray_point_distance_sq < local_threshold_sq {
108 let mut intersect_point = ray.closest_point_to_point(&point);
109 intersect_point.apply_matrix4(matrix_world);
110 let distance = raycaster.ray.origin.distance_to(&intersect_point);
111 if distance < raycaster.near || distance > raycaster.far {
112 return;
113 }
114 let mut intersection = Intersection::new(distance, intersect_point, object.clone());
115 intersection.distance_to_ray = Some(ray_point_distance_sq.sqrt());
116 intersection.index = Some(index);
117 intersects.push(intersection);
118 }
119 };
120
121 if let Some(index) = &geometry.index {
122 for i in draw_range.start..end_of(index.count()) {
123 test_point(index.get_x(i));
124 }
125 } else {
126 for i in draw_range.start..end_of(position.count()) {
127 test_point(i);
128 }
129 }
130 }
131}