use std::rc::Rc;
use crate::core::ObjectRef;
use crate::geometries::sphere_geometry;
use crate::materials::MeshBasicNodeMaterial;
use crate::math::Color;
use crate::objects::Mesh;
use super::directional_light_helper::light_color;
pub struct PointLightHelper {
pub node: ObjectRef,
pub light: ObjectRef,
pub color: Option<Color>,
}
impl PointLightHelper {
pub fn new(light: &ObjectRef, sphere_size: f64, color: Option<Color>) -> Self {
let geometry = sphere_geometry(sphere_size, 4, 2);
let material = MeshBasicNodeMaterial {
wireframe: true,
fog: false,
..MeshBasicNodeMaterial::new()
};
let node = Mesh::new(Rc::new(geometry), material);
{
let mut object = node.borrow_mut();
object.object_type = "PointLightHelper";
object.matrix_alias = Some(light.downgrade());
object.matrix_auto_update = false;
}
let helper = Self {
node,
light: light.clone(),
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 color = self.color.unwrap_or_else(|| light_color(&self.light));
if let Some(material) = self
.node
.borrow_mut()
.mesh_mut()
.and_then(|mesh| mesh.material.as_mut())
{
material.color = color;
}
}
}