Bone.js 1.8 KB
Newer Older
1 2 3 4 5
/**
 * @author mikael emtinger / http://gomo.se/
 */

THREE.Bone = function( belongsToSkin ) {
6

7
	THREE.Object3D.call( this );
8 9 10 11

	this.skin = belongsToSkin;
	this.skinMatrix = new THREE.Matrix4();
	this.hasNoneBoneChildren = false;
12 13 14

};

15
THREE.Bone.prototype = new THREE.Object3D();
16
THREE.Bone.prototype.constructor = THREE.Bone;
17
THREE.Bone.prototype.supr = THREE.Object3D.prototype;
18 19 20 21 22 23


/*
 * Update
 */

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

26 27
	// update local

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

		forceUpdate |= this.updateMatrix();

	}

34 35
	// update skin matrix

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

		if( parentSkinMatrix ) {

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

		} else {

			this.skinMatrix.copy( this.matrix );

		}

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

	}

	// update children
A
alteredq 已提交
54 55

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

57 58 59 60 61 62 63
	if ( this.hasNoneBoneChildren ) {

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


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

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

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

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

			} else {

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

			}

A
alteredq 已提交
76
		}
77

78
	} else {
79

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

A
alteredq 已提交
82
			this.children[ i ].update( this.skinMatrix, forceUpdate, camera );
83

84 85
		}

86 87 88 89 90 91 92 93 94 95 96
	}

};


/*
 * Add child
 */

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

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

		if ( child.parent !== undefined ) {
100 101

			child.parent.removeChild( child );
102 103 104 105

		}

		child.parent = this;
106 107
		this.children.push( child );

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

A
alteredq 已提交
110 111
			this.hasNoneBoneChildren = true;

112 113
		}

114 115 116 117 118
	}

};

/*
119
 * TODO: Remove Children: see if any remaining are none-Bone
120
 */