Bone.js 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/**
 * @author mikael emtinger / http://gomo.se/
 */

THREE.Bone = function( belongsToSkin ) {
	
	THREE.Object3D.call( this );
	
	this.skin                = belongsToSkin;
	this.skinMatrix          = new THREE.Matrix4();
	this.hasNoneBoneChildren = false;	

};

THREE.Bone.prototype             = new THREE.Object3D();
THREE.Bone.prototype.constructor = THREE.Bone;
THREE.Bone.prototype.supr        = THREE.Object3D.prototype;


/*
 * Update
 */

THREE.Bone.prototype.update = function( parentSkinMatrix, forceUpdate, camera, renderer ) {
	
	// update local
	
	if( this.autoUpdateMatrix )
		forceUpdate |= this.updateMatrix();			

	
	// update skin matrix

	if( forceUpdate || this.matrixNeedsToUpdate ) {
		
		if( parentSkinMatrix )
			this.skinMatrix.multiply( parentSkinMatrix, this.localMatrix );
		else
			this.skinMatrix.copy( this.localMatrix );
		
		this.matrixNeedsToUpdate = false;
		forceUpdate              = true;

	}


	// update children
	
	if( this.hasNoneBoneChildren ) {
		
		this.globalMatrix.multiply( this.skin.globalMatrix, this.skinMatrix );

		for( var i = 0; i < this.children.length; i++ )
			if( !( this.children[ i ] instanceof THREE.Bone ))
				this.children[ i ].update( this.globalMatrix, true, camera, renderer );
			else
				this.children[ i ].update( this.skinMatrix, forceUpdate, camera, renderer );
		
	} else {
		
		for( var i = 0; i < this.children.length; i++ )
			this.children[ i ].update( this.skinMatrix, forceUpdate, camera, renderer );

	}

};


/*
 * Add child
 */

THREE.Bone.prototype.addChild = function( child ) {

	if( this.children.indexOf( child ) === -1 ) {

		if( child.parent !== undefined )
			child.parent.removeChild( child );
		
		child.parent = this;		
		this.children.push( child );

		if( !( child instanceof THREE.Bone ))
			this.hasNoneBoneChildren = true;	
	}

};

/*
 * Todo: Remove Children: see if any remaining are none-Bone
 */