use std::rc::Rc;
use crate::core::{BufferAttribute, BufferGeometry, Object3D, ObjectRef};
use crate::materials::LineBasicNodeMaterial;
use crate::math::{Color, Vector3};
use crate::objects::Line;
pub struct DirectionalLightHelper {
pub node: ObjectRef,
pub light: ObjectRef,
pub light_plane: ObjectRef,
pub target_line: ObjectRef,
pub color: Option<Color>,
}
impl DirectionalLightHelper {
pub fn new(light: &ObjectRef, size: f64, color: Option<Color>) -> Self {
let node = Object3D {
object_type: "DirectionalLightHelper",
matrix_alias: Some(light.downgrade()),
matrix_auto_update: false,
..Default::default()
}
.into_node();
let s = size as f32;
#[rustfmt::skip]
let positions = vec![
-s, s, 0.0,
s, s, 0.0,
s, -s, 0.0,
-s, -s, 0.0,
-s, s, 0.0,
];
let mut geometry = BufferGeometry::new();
geometry.set_attribute("position", BufferAttribute::new(positions, 3));
let mut material = LineBasicNodeMaterial::line(Color::new(1.0, 1.0, 1.0));
material.fog = false;
let light_plane = Line::new(Rc::new(geometry), material.clone());
node.add(&light_plane);
let mut geometry = BufferGeometry::new();
geometry.set_attribute(
"position",
BufferAttribute::new(vec![0.0, 0.0, 0.0, 0.0, 0.0, 1.0], 3),
);
let target_line = Line::new(Rc::new(geometry), material);
node.add(&target_line);
let helper = Self {
node,
light: light.clone(),
light_plane,
target_line,
color,
};
helper.update();
helper
}
pub fn update(&self) {
self.node.borrow_mut().matrix_world_needs_update = true;
self.light.update_world_matrix(true, false);
let target = self
.light
.borrow()
.light()
.and_then(|light| light.target.clone())
.expect("three-rs: a DirectionalLightHelper's light has a target");
target.update_world_matrix(true, false);
let mut v1 = Vector3::default();
v1.set_from_matrix_position(&self.light.borrow().matrix_world);
let mut v2 = Vector3::default();
v2.set_from_matrix_position(&target.borrow().matrix_world);
let mut v3 = Vector3::default();
v3.sub_vectors(&v2, &v1);
self.light_plane.look_at(&v2);
let color = self.color.unwrap_or_else(|| light_color(&self.light));
for line in [&self.light_plane, &self.target_line] {
if let Some(material) = line
.borrow_mut()
.line_mut()
.and_then(|line| line.material.as_mut())
{
material.color = color;
}
}
self.target_line.look_at(&v2);
self.target_line.borrow_mut().scale.z = v3.length();
}
}
pub(super) fn light_color(light: &ObjectRef) -> Color {
light
.borrow()
.light()
.expect("three-rs: a light helper's light is a light")
.light
.color
}