use std::f64::consts::PI;
use std::rc::Rc;
use crate::core::{BufferAttribute, Object3D, ObjectRef};
use crate::geometries::octahedron_geometry;
use crate::materials::MeshBasicNodeMaterial;
use crate::math::{Color, Vector3};
use crate::objects::Mesh;
pub struct HemisphereLightHelper {
pub node: ObjectRef,
pub light: ObjectRef,
pub mesh: ObjectRef,
pub color: Option<Color>,
}
impl HemisphereLightHelper {
pub fn new(light: &ObjectRef, size: f64, color: Option<Color>) -> Self {
let node = Object3D {
object_type: "HemisphereLightHelper",
matrix_alias: Some(light.downgrade()),
matrix_auto_update: false,
..Default::default()
}
.into_node();
let mut geometry = octahedron_geometry(size, 0);
geometry.rotate_y(PI * 0.5);
let material = MeshBasicNodeMaterial {
wireframe: true,
fog: false,
vertex_colors: color.is_none(),
..MeshBasicNodeMaterial::new()
};
let count = geometry
.get_attribute("position")
.expect("three-rs: an OctahedronGeometry has positions")
.count();
geometry.set_attribute("color", BufferAttribute::new(vec![0.0; count * 3], 3));
let mesh = Mesh::new(Rc::new(geometry), material);
node.add(&mesh);
let helper = Self {
node,
light: light.clone(),
mesh,
color,
};
helper.update();
helper
}
pub fn update(&self) {
match self.color {
Some(color) => {
if let Some(material) = self
.mesh
.borrow_mut()
.mesh_mut()
.and_then(|mesh| mesh.material.as_mut())
{
material.color = color;
}
}
None => {
let (color1, color2) = {
let light = self.light.borrow();
let light = light
.light()
.expect("three-rs: a HemisphereLightHelper's light is a light");
(light.light.color, light.ground_color)
};
let geometry = self
.mesh
.borrow()
.geometry()
.expect("three-rs: the helper's octahedron is a mesh")
.clone();
let colors = geometry
.get_attribute("color")
.expect("three-rs: the octahedron has a color attribute");
let l = colors.count();
{
let mut array = colors.array_mut();
for i in 0..l {
let color = if (i as f64) < (l as f64 / 2.0) {
color1
} else {
color2
};
array[i * 3..i * 3 + 3].copy_from_slice(&[
color.r as f32,
color.g as f32,
color.b as f32,
]);
}
}
colors.set_needs_update();
}
}
self.node.borrow_mut().matrix_world_needs_update = true;
self.light.update_world_matrix(true, false);
let mut vector = Vector3::default();
vector.set_from_matrix_position(&self.light.borrow().matrix_world);
vector.negate();
self.mesh.look_at(&vector);
}
}