use std::f64::consts::PI;
use std::rc::Rc;
use crate::core::{BufferAttribute, BufferGeometry, Object3D, ObjectRef};
use crate::materials::LineBasicNodeMaterial;
use crate::math::{Color, Vector3};
use crate::objects::LineSegments;
use super::directional_light_helper::light_color;
pub struct SpotLightHelper {
pub node: ObjectRef,
pub light: ObjectRef,
pub cone: ObjectRef,
pub color: Option<Color>,
}
impl SpotLightHelper {
pub fn new(light: &ObjectRef, color: Option<Color>) -> Self {
let node = Object3D {
object_type: "SpotLightHelper",
matrix_auto_update: false,
..Default::default()
}
.into_node();
#[rustfmt::skip]
let mut positions: Vec<f32> = vec![
0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
0.0, 0.0, 0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 0.0, -1.0, 0.0, 1.0,
0.0, 0.0, 0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 0.0, 0.0, -1.0, 1.0,
];
let l = 32;
for i in 0..l {
let j = i + 1;
let p1 = (i as f64 / l as f64) * PI * 2.0;
let p2 = (j as f64 / l as f64) * PI * 2.0;
positions.extend_from_slice(&[
p1.cos() as f32,
p1.sin() as f32,
1.0,
p2.cos() as f32,
p2.sin() as f32,
1.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 cone = LineSegments::new(Rc::new(geometry), material);
node.add(&cone);
let helper = Self {
node,
light: light.clone(),
cone,
color,
};
helper.update();
helper
}
pub fn update(&self) {
self.light.update_world_matrix(true, false);
let target = self
.light
.borrow()
.light()
.and_then(|light| light.target.clone())
.expect("three-rs: a SpotLightHelper's light has a target");
target.update_world_matrix(true, false);
let light_matrix_world = self.light.borrow().matrix_world;
match self.node.parent() {
Some(parent) => {
parent.update_world_matrix(true, false);
let mut matrix = parent.borrow().matrix_world;
matrix.invert();
matrix.multiply(&light_matrix_world);
self.node.borrow_mut().matrix = matrix;
}
None => self.node.borrow_mut().matrix = light_matrix_world,
}
self.node.borrow_mut().matrix_world_needs_update = true;
let (distance, angle) = {
let light = self.light.borrow();
let light = light
.light()
.expect("three-rs: a SpotLightHelper's light is a light");
(light.distance, light.angle)
};
let cone_length = if distance != 0.0 && !distance.is_nan() {
distance
} else {
1000.0
};
let cone_width = cone_length * angle.tan();
self.cone
.borrow_mut()
.scale
.set(cone_width, cone_width, cone_length);
let mut vector = Vector3::default();
vector.set_from_matrix_position(&target.borrow().matrix_world);
self.cone.look_at(&vector);
let color = self.color.unwrap_or_else(|| light_color(&self.light));
if let Some(material) = self
.cone
.borrow_mut()
.line_mut()
.and_then(|line| line.material.as_mut())
{
material.color = color;
}
}
}