use std::rc::Rc;
use crate::core::{BufferAttribute, BufferGeometry, Intersection, Object3D, ObjectRef, Raycaster};
use crate::materials::{MeshBasicNodeMaterial, SpriteNodeMaterial};
use crate::math::{Matrix4, Sphere, Triangle, Vector2, Vector3};
use crate::objects::Payload;
thread_local! {
static GEOMETRY: Rc<BufferGeometry> = Rc::new(sprite_geometry());
}
fn sprite_geometry() -> BufferGeometry {
#[rustfmt::skip]
let interleaved: [f32; 20] = [
-0.5, -0.5, 0.0, 0.0, 0.0,
0.5, -0.5, 0.0, 1.0, 0.0,
0.5, 0.5, 0.0, 1.0, 1.0,
-0.5, 0.5, 0.0, 0.0, 1.0,
];
let position: Vec<f32> = interleaved
.chunks(5)
.flat_map(|v| [v[0], v[1], v[2]])
.collect();
let uv: Vec<f32> = interleaved.chunks(5).flat_map(|v| [v[3], v[4]]).collect();
let mut geometry = BufferGeometry::new();
geometry.set_index(&[0, 1, 2, 0, 2, 3]);
geometry.set_attribute("position", BufferAttribute::new(position, 3));
geometry.set_attribute("uv", BufferAttribute::new(uv, 2));
geometry
}
#[derive(Clone)]
pub struct Sprite {
pub geometry: Rc<BufferGeometry>,
pub material: SpriteNodeMaterial,
pub center: Vector2,
pub count: usize,
}
impl Sprite {
#[allow(clippy::new_ret_no_self)] pub fn new(material: impl Into<Option<SpriteNodeMaterial>>) -> ObjectRef {
let mut object = Object3D {
object_type: "Sprite",
..Default::default()
};
object.payload = Payload::Sprite(Self::of(material.into()));
object.into_node()
}
pub fn of(material: Option<SpriteNodeMaterial>) -> Self {
Self {
geometry: GEOMETRY.with(Rc::clone),
material: material.unwrap_or_else(MeshBasicNodeMaterial::sprite),
center: Vector2::new(0.5, 0.5),
count: 1,
}
}
pub(crate) fn bounding_sphere_in(&self, matrix_world: &Matrix4) -> Sphere {
let default_center = Vector2::new(0.5, 0.5);
let offset = default_center.distance_to(&self.center);
let mut sphere = Sphere::new(
Vector3::new(0.0, 0.0, 0.0),
std::f64::consts::FRAC_1_SQRT_2 + offset,
);
sphere.apply_matrix4(matrix_world);
sphere
}
pub fn raycast(
&self,
matrix_world: &Matrix4,
object: &ObjectRef,
raycaster: &Raycaster,
intersects: &mut Vec<Intersection>,
) {
let Some(camera) = &raycaster.camera else {
eprintln!(
"THREE.Sprite: \"Raycaster.camera\" needs to be set in order to raycast against sprites."
);
return;
};
let mut world_scale = Vector3::ZERO;
world_scale.set_from_matrix_scale(matrix_world);
let view_world_matrix = camera.matrix_world;
let mut model_view_matrix = Matrix4::identity();
model_view_matrix.multiply_matrices(&camera.matrix_world_inverse, matrix_world);
let mut mv_position = Vector3::ZERO;
mv_position.set_from_matrix_position(&model_view_matrix);
if camera.is_perspective_camera && !self.material.size_attenuation {
world_scale.multiply_scalar(-mv_position.z);
}
let rotation = self.material.rotation;
let sin_cos = (rotation != 0.0).then(|| (rotation.sin(), rotation.cos()));
let center = self.center;
let vertex = |x: f64, y: f64| {
transform_vertex(
Vector2::new(x, y),
&mv_position,
¢er,
&world_scale,
sin_cos,
&view_world_matrix,
)
};
let v_a = vertex(-0.5, -0.5);
let mut v_b = vertex(0.5, -0.5);
let v_c = vertex(0.5, 0.5);
let uv_a = Vector3::new(0.0, 0.0, 0.0);
let mut uv_b = Vector3::new(1.0, 0.0, 0.0);
let uv_c = Vector3::new(1.0, 1.0, 0.0);
let intersect_point = match raycaster.ray.intersect_triangle(&v_a, &v_b, &v_c, false) {
Some(point) => point,
None => {
v_b = vertex(-0.5, 0.5);
uv_b = Vector3::new(0.0, 1.0, 0.0);
match raycaster.ray.intersect_triangle(&v_a, &v_c, &v_b, false) {
Some(point) => point,
None => return,
}
}
};
let distance = raycaster.ray.origin.distance_to(&intersect_point);
if distance < raycaster.near || distance > raycaster.far {
return;
}
let mut intersection = Intersection::new(distance, intersect_point, object.clone());
intersection.uv = Triangle::static_get_interpolation(
&intersect_point,
&v_a,
&v_b,
&v_c,
&uv_a,
&uv_b,
&uv_c,
)
.map(|uv| Vector2::new(uv.x, uv.y));
intersects.push(intersection);
}
}
fn transform_vertex(
vertex_position: Vector2,
mv_position: &Vector3,
center: &Vector2,
scale: &Vector3,
sin_cos: Option<(f64, f64)>,
view_world_matrix: &Matrix4,
) -> Vector3 {
let aligned_x = (vertex_position.x - center.x + 0.5) * scale.x;
let aligned_y = (vertex_position.y - center.y + 0.5) * scale.y;
let (rotated_x, rotated_y) = match sin_cos {
Some((sin, cos)) => (
cos * aligned_x - sin * aligned_y,
sin * aligned_x + cos * aligned_y,
),
None => (aligned_x, aligned_y),
};
let mut position = *mv_position;
position.x += rotated_x;
position.y += rotated_y;
position.apply_matrix4(view_world_matrix);
position
}