Bone.js 1.8 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
/**
 * @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
 */

A
alteredq 已提交
24
THREE.Bone.prototype.update = function( parentSkinMatrix, forceUpdate, camera ) {
25

26 27
	// update local

28 29 30 31 32 33 34
	if ( this.matrixAutoUpdate ) {

		forceUpdate |= this.updateMatrix();

	}


35 36
	// update skin matrix

37 38 39 40 41 42 43 44 45 46 47 48
	if ( forceUpdate || this.matrixNeedsUpdate ) {

		if( parentSkinMatrix ) {

			this.skinMatrix.multiply( parentSkinMatrix, this.matrix );

		} else {

			this.skinMatrix.copy( this.matrix );

		}

49 50
		this.matrixNeedsUpdate = false;
		forceUpdate = true;
51 52 53 54 55

	}


	// update children
A
alteredq 已提交
56 57

	var child, i, l = this.children.length;
58

59 60 61 62 63 64 65
	if ( this.hasNoneBoneChildren ) {

		this.matrixWorld.multiply( this.skin.matrixWorld, this.skinMatrix );


		for ( i = 0; i < l; i++ ) {

A
alteredq 已提交
66
			child = this.children[ i ];
67 68 69 70 71 72 73

			if ( ! ( child instanceof THREE.Bone ) ) {

				child.update( this.matrixWorld, true, camera );

			} else {

A
alteredq 已提交
74
				child.update( this.skinMatrix, forceUpdate, camera );
75 76 77

			}

A
alteredq 已提交
78
		}
79

80
	} else {
81

A
alteredq 已提交
82 83
		for( i = 0; i < l; i++ )
			this.children[ i ].update( this.skinMatrix, forceUpdate, camera );
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

	}

};


/*
 * 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 );

A
alteredq 已提交
104 105 106
		if( ! ( child instanceof THREE.Bone ) )
			this.hasNoneBoneChildren = true;

107 108 109 110 111 112
	}

};

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