Object bounds not updated with animation

@Mugen87 sent me here from another thread.

I was able to solve this for dddance.party with some help from the illustrious Vince Mckelvie. I remember it being an extremely annoying challenge to solve…

I am using animated DAE files but hopefully it will work for any format? Depends on the SkeletonHelper?

Here is the code from when the animated DAE (object) is loaded:

	var avatar = object.scene;
mixer = new THREE.AnimationMixer(avatar);
mixer.clipAction(animations[0]).play();
scene.add(avatar);
var skeleton = new THREE.SkeletonHelper(avatar); 
var box_helper = new THREE.BoxHelper( skeleton, 0xffffff ); //box_helper is just lines
box_helper.material.visible = false;
box_helper.update();
scene.add(box_helper);
var geo = new THREE.BoxBufferGeometry( 1, 1, 1 );
var mat = new THREE.MeshBasicMaterial( { color: 0xeeeeee } ); 
var box_mesh = new THREE.Mesh( geo, mat );  //box_mesh is a transparent box
box_mesh.material.visible = false;
box_mesh.material.transparent = true;
box_mesh.visible = true;

Here is the code running every animation loop:

for(d=0;d<dancers.length;d++){
		dancers[d].box_helper.update();
		cc = getCenterPoint(dancers[d].box_helper);
		ww = Math.abs(dancers[d].box_helper.geometry.boundingBox.min.x - dancers[d].box_helper.geometry.boundingBox.max.x);
		hh = Math.abs(dancers[d].box_helper.geometry.boundingBox.min.y - dancers[d].box_helper.geometry.boundingBox.max.y);
		dd = Math.abs(dancers[d].box_helper.geometry.boundingBox.min.z - dancers[d].box_helper.geometry.boundingBox.max.z);
		dancers[d].box_mesh.position.set(cc.x,cc.y,cc.z);
		dancers[d].box_mesh.scale.x = ww;
		dancers[d].box_mesh.scale.y = hh;
		dancers[d].box_mesh.scale.z = dd;
}

The get center function:

function getCenterPoint(mesh) {
var geometry = mesh.geometry;
geometry.computeBoundingBox();
center = geometry.boundingBox.getCenter();
mesh.localToWorld( center );
return center; }

I hope this helps :)))

1 Like