content/hifi-content/dave/walk-tools/walkTools/libraries/three.js/extras/helpers/SkeletonHelper.js
2022-02-13 22:49:05 +01:00

98 lines
2.1 KiB
JavaScript

/**
* @author Sean Griffin / http://twitter.com/sgrif
* @author Michael Guerrero / http://realitymeltdown.com
* @author mrdoob / http://mrdoob.com/
* @author ikerr / http://verold.com
*/
THREE.SkeletonHelper = function ( object ) {
this.bones = this.getBoneList( object );
var geometry = new THREE.Geometry();
for ( var i = 0; i < this.bones.length; i ++ ) {
var bone = this.bones[ i ];
if ( bone.parent instanceof THREE.Bone ) {
geometry.vertices.push( new THREE.Vector3() );
geometry.vertices.push( new THREE.Vector3() );
geometry.colors.push( new THREE.Color( 0, 0, 1 ) );
geometry.colors.push( new THREE.Color( 0, 1, 0 ) );
}
}
var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors, depthTest: false, depthWrite: false, transparent: true } );
THREE.Line.call( this, geometry, material, THREE.LinePieces );
this.root = object;
this.matrix = object.matrixWorld;
this.matrixAutoUpdate = false;
this.update();
};
THREE.SkeletonHelper.prototype = Object.create( THREE.Line.prototype );
THREE.SkeletonHelper.prototype.constructor = THREE.SkeletonHelper;
THREE.SkeletonHelper.prototype.getBoneList = function( object ) {
var boneList = [];
if ( object instanceof THREE.Bone ) {
boneList.push( object );
}
for ( var i = 0; i < object.children.length; i ++ ) {
boneList.push.apply( boneList, this.getBoneList( object.children[ i ] ) );
}
return boneList;
};
THREE.SkeletonHelper.prototype.update = function () {
var geometry = this.geometry;
var matrixWorldInv = new THREE.Matrix4().getInverse( this.root.matrixWorld );
var boneMatrix = new THREE.Matrix4();
var j = 0;
for ( var i = 0; i < this.bones.length; i ++ ) {
var bone = this.bones[ i ];
if ( bone.parent instanceof THREE.Bone ) {
boneMatrix.multiplyMatrices( matrixWorldInv, bone.matrixWorld );
geometry.vertices[ j ].setFromMatrixPosition( boneMatrix );
boneMatrix.multiplyMatrices( matrixWorldInv, bone.parent.matrixWorld );
geometry.vertices[ j + 1 ].setFromMatrixPosition( boneMatrix );
j += 2;
}
}
geometry.verticesNeedUpdate = true;
geometry.computeBoundingSphere();
};