use std::rc::Rc;
use crate::core::{BufferAttribute, BufferGeometry, ObjectRef};
use crate::materials::LineBasicNodeMaterial;
use crate::math::{Color, Matrix4, Vector3};
use crate::objects::{is_bone, LineSegments};
pub struct SkeletonHelper {
pub node: ObjectRef,
pub root: ObjectRef,
pub bones: Vec<ObjectRef>,
}
impl SkeletonHelper {
pub const IS_SKELETON_HELPER: bool = true;
pub fn new(object: &ObjectRef) -> Self {
let bones = get_bone_list(object);
let segments = bones.iter().filter(|bone| has_bone_parent(bone)).count();
let mut geometry = BufferGeometry::new();
geometry.set_attribute("position", BufferAttribute::new(vec![0.0; segments * 6], 3));
geometry.set_attribute("color", BufferAttribute::new(vec![0.0; segments * 6], 3));
let mut material = LineBasicNodeMaterial::line(Color::new(1.0, 1.0, 1.0));
material.vertex_colors = true;
material.depth_test = false;
material.depth_write = false;
material.transparent = true;
let node = LineSegments::new(Rc::new(geometry), material);
{
let mut helper = node.borrow_mut();
helper.object_type = "SkeletonHelper";
helper.matrix_alias = Some(object.downgrade());
helper.matrix_auto_update = false;
}
let helper = Self {
node,
root: object.clone(),
bones,
};
helper.set_colors(Color::from_hex(0x0000ff), Color::from_hex(0x00ff00));
helper
}
fn geometry(&self) -> Rc<BufferGeometry> {
self.node
.borrow()
.geometry()
.expect("three-rs: a SkeletonHelper is a LineSegments")
.clone()
}
pub fn update_matrix_world(&self, force: bool) {
let geometry = self.geometry();
let position = geometry
.get_attribute("position")
.expect("three-rs: the helper has a position attribute");
let mut matrix_world_inv = self.root.borrow().matrix_world;
matrix_world_inv.invert();
let local = |matrix_world: &Matrix4| {
let mut bone_matrix = Matrix4::default();
bone_matrix.multiply_matrices(&matrix_world_inv, matrix_world);
let mut vector = Vector3::default();
vector.set_from_matrix_position(&bone_matrix);
[vector.x as f32, vector.y as f32, vector.z as f32]
};
{
let mut array = position.array_mut();
let mut j = 0;
for bone in &self.bones {
let Some(parent) = bone.parent().filter(|parent| is_bone(&parent.borrow())) else {
continue;
};
let Some(pair) = array.get_mut(j * 3..j * 3 + 6) else {
break;
};
pair[..3].copy_from_slice(&local(&bone.borrow().matrix_world));
pair[3..].copy_from_slice(&local(&parent.borrow().matrix_world));
j += 2;
}
}
position.set_needs_update();
self.node.update_matrix_world(force);
}
pub fn set_colors(&self, color1: Color, color2: Color) -> &Self {
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();
let rgb1 = [color1.r as f32, color1.g as f32, color1.b as f32];
let rgb2 = [color2.r as f32, color2.g as f32, color2.b as f32];
let (pairs, _) = array.as_chunks_mut::<6>();
for pair in pairs {
pair[..3].copy_from_slice(&rgb1);
pair[3..].copy_from_slice(&rgb2);
}
}
color_attribute.set_needs_update();
self
}
}
fn has_bone_parent(bone: &ObjectRef) -> bool {
bone.parent()
.is_some_and(|parent| is_bone(&parent.borrow()))
}
fn get_bone_list(object: &ObjectRef) -> Vec<ObjectRef> {
let mut bone_list = Vec::new();
if is_bone(&object.borrow()) {
bone_list.push(object.clone());
}
for child in object.children() {
bone_list.extend(get_bone_list(&child));
}
bone_list
}