use std::rc::Rc;
use crate::core::{BufferAttribute, BufferGeometry, Object3D, ObjectRef};
use crate::geometries::cone_geometry;
use crate::materials::{LineBasicNodeMaterial, MeshBasicNodeMaterial};
use crate::math::{Color, Vector3};
use crate::objects::{Line, Mesh};
thread_local! {
static GEOMETRIES: (Rc<BufferGeometry>, Rc<BufferGeometry>) = {
let mut line = BufferGeometry::new();
line.set_attribute(
"position",
BufferAttribute::new(vec![0.0, 0.0, 0.0, 0.0, 1.0, 0.0], 3),
);
let mut cone = cone_geometry(0.5, 1.0, 5, 1);
cone.translate(0.0, -0.5, 0.0);
(Rc::new(line), Rc::new(cone))
};
}
pub struct ArrowHelper {
pub node: ObjectRef,
pub line: ObjectRef,
pub cone: ObjectRef,
}
impl Default for ArrowHelper {
fn default() -> Self {
Self::new(
Vector3::new(0.0, 0.0, 1.0),
Vector3::new(0.0, 0.0, 0.0),
1.0,
Color::from_hex(0xffff00),
None,
None,
)
}
}
impl ArrowHelper {
pub fn new(
dir: Vector3,
origin: Vector3,
length: f64,
color: Color,
head_length: Option<f64>,
head_width: Option<f64>,
) -> Self {
let (line_geometry, cone_geometry) = GEOMETRIES.with(|g| (g.0.clone(), g.1.clone()));
let node = Object3D {
object_type: "ArrowHelper",
position: origin,
..Default::default()
}
.into_node();
let line = Line::new(line_geometry, LineBasicNodeMaterial::line(color));
line.borrow_mut().matrix_auto_update = false;
node.add(&line);
let cone_material = MeshBasicNodeMaterial {
color,
..MeshBasicNodeMaterial::new()
};
let cone = Mesh::new(cone_geometry, cone_material);
cone.borrow_mut().matrix_auto_update = false;
node.add(&cone);
let helper = Self { node, line, cone };
helper.set_direction(&dir);
helper.set_length(length, head_length, head_width);
helper
}
pub fn set_direction(&self, dir: &Vector3) {
let mut object = self.node.borrow_mut();
if dir.y > 0.99999 {
object.quaternion.set(0.0, 0.0, 0.0, 1.0);
} else if dir.y < -0.99999 {
object.quaternion.set(1.0, 0.0, 0.0, 0.0);
} else {
let mut axis = Vector3::new(dir.z, 0.0, -dir.x);
axis.normalize();
let radians = dir.y.acos();
object.quaternion.set_from_axis_angle(&axis, radians);
}
object.sync_rotation_from_quaternion();
}
pub fn set_length(&self, length: f64, head_length: Option<f64>, head_width: Option<f64>) {
let head_length = head_length.unwrap_or(length * 0.2);
let head_width = head_width.unwrap_or(head_length * 0.2);
{
let mut line = self.line.borrow_mut();
line.scale.set(1.0, (length - head_length).max(0.0001), 1.0); line.update_matrix();
}
let mut cone = self.cone.borrow_mut();
cone.scale.set(head_width, head_length, head_width);
cone.position.y = length;
cone.update_matrix();
}
pub fn set_color(&self, color: Color) {
if let Some(material) = self
.line
.borrow_mut()
.line_mut()
.and_then(|line| line.material.as_mut())
{
material.color = color;
}
if let Some(material) = self
.cone
.borrow_mut()
.mesh_mut()
.and_then(|mesh| mesh.material.as_mut())
{
material.color = color;
}
}
}