use std::collections::HashMap;
use std::rc::Rc;
use crate::cameras::RenderCamera;
use crate::core::{BufferAttribute, BufferGeometry, Node};
use crate::materials::MeshBasicNodeMaterial;
use crate::math::{Color, CoordinateSystem, Vector3};
use crate::objects::LineSegments;
pub struct CameraHelper {
pub node: Node,
point_map: HashMap<&'static str, Vec<usize>>,
}
const LINES: [(&str, &str); 25] = [
("n1", "n2"),
("n2", "n4"),
("n4", "n3"),
("n3", "n1"),
("f1", "f2"),
("f2", "f4"),
("f4", "f3"),
("f3", "f1"),
("n1", "f1"),
("n2", "f2"),
("n3", "f3"),
("n4", "f4"),
("p", "n1"),
("p", "n2"),
("p", "n3"),
("p", "n4"),
("u1", "u2"),
("u2", "u3"),
("u3", "u1"),
("c", "t"),
("p", "c"),
("cn1", "cn2"),
("cn3", "cn4"),
("cf1", "cf2"),
("cf3", "cf4"),
];
impl CameraHelper {
pub fn new(camera: &dyn RenderCamera) -> Self {
let mut vertices: Vec<f32> = Vec::new();
let mut colors: Vec<f32> = Vec::new();
let mut point_map: HashMap<&'static str, Vec<usize>> = HashMap::new();
let mut add_point = |id: &'static str| {
vertices.extend([0.0, 0.0, 0.0]);
colors.extend([0.0, 0.0, 0.0]);
point_map
.entry(id)
.or_default()
.push(vertices.len() / 3 - 1);
};
for (a, b) in LINES {
add_point(a);
add_point(b);
}
let mut geometry = BufferGeometry::new();
geometry.set_attribute("position", BufferAttribute::new(vertices, 3));
geometry.set_attribute("color", BufferAttribute::new(colors, 3));
let mut material = MeshBasicNodeMaterial::line(Color::from_hex(0xffffff));
material.vertex_colors = true;
let node = LineSegments::new(Rc::new(geometry), material);
{
let mut object = node.borrow_mut();
object.object_type = "CameraHelper";
match camera.node() {
Some(camera_node) => object.matrix_alias = Some(camera_node.downgrade()),
None => object.matrix = camera.matrix_world(),
}
object.matrix_auto_update = false;
}
let helper = Self { node, point_map };
helper.update(camera);
helper.set_colors(
Color::from_hex(0xffaa00),
Color::from_hex(0xff0000),
Color::from_hex(0x00aaff),
Color::from_hex(0xffffff),
Color::from_hex(0x333333),
);
helper
}
fn geometry(&self) -> Rc<BufferGeometry> {
self.node
.borrow()
.geometry()
.expect("three-rs: a CameraHelper is a LineSegments")
.clone()
}
pub fn set_colors(&self, frustum: Color, cone: Color, up: Color, target: Color, cross: Color) {
let geometry = self.geometry();
let color_attribute = geometry
.get_attribute("color")
.expect("three-rs: the helper has a color attribute");
{
let mut array = color_attribute.array_mut();
for i in 0..50 {
let color = match i {
0..=23 => frustum,
24..=31 => cone,
32..=37 => up,
38..=39 => target,
_ => cross,
};
array[i * 3..i * 3 + 3].copy_from_slice(&[
color.r as f32,
color.g as f32,
color.b as f32,
]);
}
}
color_attribute.set_needs_update();
}
pub fn update(&self, camera: &dyn RenderCamera) {
let (w, h) = (1.0, 1.0);
let (near_z, far_z) = match camera.coordinate_system() {
CoordinateSystem::WebGl => (-1.0, 1.0),
CoordinateSystem::WebGpu => (0.0, 1.0),
};
let projection_matrix_inverse = camera.projection_matrix_inverse();
let geometry = self.geometry();
let position = geometry
.get_attribute("position")
.expect("three-rs: the helper has a position attribute");
let points: [(&str, f64, f64, f64); 21] = [
("c", 0.0, 0.0, near_z),
("t", 0.0, 0.0, far_z),
("n1", -w, -h, near_z),
("n2", w, -h, near_z),
("n3", -w, h, near_z),
("n4", w, h, near_z),
("f1", -w, -h, far_z),
("f2", w, -h, far_z),
("f3", -w, h, far_z),
("f4", w, h, far_z),
("u1", w * 0.7, h * 1.1, near_z),
("u2", -w * 0.7, h * 1.1, near_z),
("u3", 0.0, h * 2.0, near_z),
("cf1", -w, 0.0, far_z),
("cf2", w, 0.0, far_z),
("cf3", 0.0, -h, far_z),
("cf4", 0.0, h, far_z),
("cn1", -w, 0.0, near_z),
("cn2", w, 0.0, near_z),
("cn3", 0.0, -h, near_z),
("cn4", 0.0, h, near_z),
];
{
let mut array = position.array_mut();
for (point, x, y, z) in points {
let Some(indices) = self.point_map.get(point) else {
continue;
};
let mut vector = Vector3::new(x, y, z);
vector.apply_matrix4(&projection_matrix_inverse);
for &i in indices {
array[i * 3..i * 3 + 3].copy_from_slice(&[
vector.x as f32,
vector.y as f32,
vector.z as f32,
]);
}
}
}
position.set_needs_update();
}
}