use std::rc::Rc;
use crate::core::{BufferAttribute, BufferGeometry, ObjectRef};
use crate::materials::LineBasicNodeMaterial;
use crate::math::{Box3, Color};
use crate::objects::LineSegments;
pub struct Box3Helper {
pub node: ObjectRef,
pub box3: Box3,
}
impl Box3Helper {
pub fn new(box3: Box3, color: Color) -> Self {
#[rustfmt::skip]
let indices = [
0, 1, 1, 2, 2, 3, 3, 0,
4, 5, 5, 6, 6, 7, 7, 4,
0, 4, 1, 5, 2, 6, 3, 7,
];
#[rustfmt::skip]
let positions = vec![
1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0,
1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0,
];
let mut geometry = BufferGeometry::new();
geometry.set_index(&indices);
geometry.set_attribute("position", BufferAttribute::new(positions, 3));
let node = LineSegments::new(Rc::new(geometry), LineBasicNodeMaterial::line(color));
node.borrow_mut().object_type = "Box3Helper";
Self { node, box3 }
}
pub fn update_matrix_world(&self, force: bool) {
let bounds = self.box3;
if bounds.is_empty() {
return;
}
{
let mut object = self.node.borrow_mut();
object.position = bounds.get_center();
object.scale = bounds.get_size();
object.scale.multiply_scalar(0.5);
}
self.node.update_matrix_world(force);
}
}