|
@@ -2,134 +2,177 @@
|
|
|
* @author mikael emtinger / http://gomo.se/
|
|
|
* @author alteredq / http://alteredqualia.com/
|
|
|
* @author michael guerrero / http://realitymeltdown.com
|
|
|
+ * @author ikerr / http://verold.com
|
|
|
*/
|
|
|
|
|
|
-THREE.Skeleton = function ( boneList, useVertexTexture ) {
|
|
|
+THREE.Skeleton = function ( bones, boneInverses, useVertexTexture ) {
|
|
|
|
|
|
this.useVertexTexture = useVertexTexture !== undefined ? useVertexTexture : true;
|
|
|
|
|
|
- // init bones
|
|
|
+ this.identityMatrix = new THREE.Matrix4();
|
|
|
|
|
|
- this.bones = [];
|
|
|
- this.boneMatrices = [];
|
|
|
+ // copy the bone array
|
|
|
|
|
|
- var bone, gbone, p, q, s;
|
|
|
+ bones = bones || [];
|
|
|
|
|
|
- if ( boneList !== undefined ) {
|
|
|
+ this.bones = bones.slice( 0 );
|
|
|
|
|
|
- for ( var b = 0; b < boneList.length; ++b ) {
|
|
|
+ // create a bone texture or an array of floats
|
|
|
|
|
|
- gbone = boneList[ b ];
|
|
|
+ if ( this.useVertexTexture ) {
|
|
|
|
|
|
- p = gbone.pos;
|
|
|
- q = gbone.rotq;
|
|
|
- s = gbone.scl;
|
|
|
+ // layout (1 matrix = 4 pixels)
|
|
|
+ // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
|
|
|
+ // with 8x8 pixel texture max 16 bones (8 * 8 / 4)
|
|
|
+ // 16x16 pixel texture max 64 bones (16 * 16 / 4)
|
|
|
+ // 32x32 pixel texture max 256 bones (32 * 32 / 4)
|
|
|
+ // 64x64 pixel texture max 1024 bones (64 * 64 / 4)
|
|
|
|
|
|
- bone = this.addBone();
|
|
|
+ var size;
|
|
|
|
|
|
- bone.name = gbone.name;
|
|
|
- bone.position.set( p[ 0 ], p[ 1 ], p[ 2 ] );
|
|
|
- bone.quaternion.set( q[ 0 ], q[ 1 ], q[ 2 ], q[ 3 ] );
|
|
|
+ if ( this.bones.length > 256 )
|
|
|
+ size = 64;
|
|
|
+ else if ( this.bones.length > 64 )
|
|
|
+ size = 32;
|
|
|
+ else if ( this.bones.length > 16 )
|
|
|
+ size = 16;
|
|
|
+ else
|
|
|
+ size = 8;
|
|
|
|
|
|
- if ( s !== undefined ) {
|
|
|
+ this.boneTextureWidth = size;
|
|
|
+ this.boneTextureHeight = size;
|
|
|
|
|
|
- bone.scale.set( s[ 0 ], s[ 1 ], s[ 2 ] );
|
|
|
+ this.boneMatrices = new Float32Array( this.boneTextureWidth * this.boneTextureHeight * 4 ); // 4 floats per RGBA pixel
|
|
|
+ this.boneTexture = new THREE.DataTexture( this.boneMatrices, this.boneTextureWidth, this.boneTextureHeight, THREE.RGBAFormat, THREE.FloatType );
|
|
|
+ this.boneTexture.minFilter = THREE.NearestFilter;
|
|
|
+ this.boneTexture.magFilter = THREE.NearestFilter;
|
|
|
+ this.boneTexture.generateMipmaps = false;
|
|
|
+ this.boneTexture.flipY = false;
|
|
|
|
|
|
- } else {
|
|
|
+ } else {
|
|
|
|
|
|
- bone.scale.set( 1, 1, 1 );
|
|
|
+ this.boneMatrices = new Float32Array( 16 * this.bones.length );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // use the supplied bone inverses or calculate the inverses
|
|
|
+
|
|
|
+ if ( boneInverses === undefined ) {
|
|
|
+
|
|
|
+ this.calculateInverses();
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ if ( this.bones.length === boneInverses.length ) {
|
|
|
+
|
|
|
+ this.boneInverses = boneInverses.slice( 0 );
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ console.warn( 'THREE.Skeleton bonInverses is the wrong length.' );
|
|
|
+
|
|
|
+ this.boneInverses = [];
|
|
|
+
|
|
|
+ for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
|
|
|
+
|
|
|
+ this.boneInverses.push( new THREE.Matrix4() );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- for ( var b = 0; b < boneList.length; ++b ) {
|
|
|
+ }
|
|
|
|
|
|
- gbone = boneList[ b ];
|
|
|
+};
|
|
|
|
|
|
- if ( gbone.parent !== - 1 ) {
|
|
|
+THREE.Skeleton.prototype.calculateInverses = function () {
|
|
|
|
|
|
- this.bones[ gbone.parent ].add( this.bones[ b ] );
|
|
|
+ this.boneInverses = [];
|
|
|
|
|
|
- }
|
|
|
+ for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
|
|
|
+
|
|
|
+ var inverse = new THREE.Matrix4();
|
|
|
+
|
|
|
+ if ( this.bones[ b ] ) {
|
|
|
+
|
|
|
+ inverse.getInverse( this.bones[ b ].matrixWorld );
|
|
|
|
|
|
}
|
|
|
|
|
|
- //
|
|
|
+ this.boneInverses.push( inverse );
|
|
|
|
|
|
- var nBones = this.bones.length;
|
|
|
+ }
|
|
|
|
|
|
- if ( this.useVertexTexture ) {
|
|
|
+};
|
|
|
|
|
|
- // layout (1 matrix = 4 pixels)
|
|
|
- // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
|
|
|
- // with 8x8 pixel texture max 16 bones (8 * 8 / 4)
|
|
|
- // 16x16 pixel texture max 64 bones (16 * 16 / 4)
|
|
|
- // 32x32 pixel texture max 256 bones (32 * 32 / 4)
|
|
|
- // 64x64 pixel texture max 1024 bones (64 * 64 / 4)
|
|
|
+THREE.Skeleton.prototype.pose = function () {
|
|
|
|
|
|
- var size;
|
|
|
+ var bone;
|
|
|
|
|
|
- if ( nBones > 256 )
|
|
|
- size = 64;
|
|
|
- else if ( nBones > 64 )
|
|
|
- size = 32;
|
|
|
- else if ( nBones > 16 )
|
|
|
- size = 16;
|
|
|
- else
|
|
|
- size = 8;
|
|
|
+ // recover the bind-time world matrices
|
|
|
|
|
|
- this.boneTextureWidth = size;
|
|
|
- this.boneTextureHeight = size;
|
|
|
+ for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
|
|
|
|
|
|
- this.boneMatrices = new Float32Array( this.boneTextureWidth * this.boneTextureHeight * 4 ); // 4 floats per RGBA pixel
|
|
|
- this.boneTexture = new THREE.DataTexture( this.boneMatrices, this.boneTextureWidth, this.boneTextureHeight, THREE.RGBAFormat, THREE.FloatType );
|
|
|
- this.boneTexture.minFilter = THREE.NearestFilter;
|
|
|
- this.boneTexture.magFilter = THREE.NearestFilter;
|
|
|
- this.boneTexture.generateMipmaps = false;
|
|
|
- this.boneTexture.flipY = false;
|
|
|
+ bone = this.bones[ b ];
|
|
|
|
|
|
- } else {
|
|
|
+ if ( bone ) {
|
|
|
|
|
|
- this.boneMatrices = new Float32Array( 16 * nBones );
|
|
|
+ bone.matrixWorld.getInverse( this.boneInverses[ b ] );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
-};
|
|
|
+ // compute the local matrices, positions, rotations and scales
|
|
|
|
|
|
+ for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
|
|
|
|
|
|
-THREE.Skeleton.prototype = Object.create( THREE.Mesh.prototype );
|
|
|
+ bone = this.bones[ b ];
|
|
|
|
|
|
+ if ( bone ) {
|
|
|
|
|
|
-THREE.Skeleton.prototype.addBone = function ( bone ) {
|
|
|
+ if ( bone.parent ) {
|
|
|
|
|
|
- if ( bone === undefined ) {
|
|
|
+ bone.matrix.getInverse( bone.parent.matrixWorld );
|
|
|
+ bone.matrix.multiply( bone.matrixWorld );
|
|
|
|
|
|
- bone = new THREE.Bone( this );
|
|
|
+ }
|
|
|
+ else {
|
|
|
|
|
|
- }
|
|
|
+ bone.matrix.copy( bone.matrixWorld );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
|
|
|
|
|
|
- this.bones.push( bone );
|
|
|
+ }
|
|
|
|
|
|
- return bone;
|
|
|
+ }
|
|
|
|
|
|
};
|
|
|
|
|
|
+THREE.Skeleton.prototype.update = function () {
|
|
|
|
|
|
-THREE.Skeleton.prototype.calculateInverses = function () {
|
|
|
+ var offsetMatrix = new THREE.Matrix4();
|
|
|
|
|
|
- this.boneInverses = [];
|
|
|
+ // flatten bone matrices to array
|
|
|
|
|
|
- for ( var b = 0, bl = this.bones.length; b < bl; ++b ) {
|
|
|
+ for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
|
|
|
|
|
|
- var inverse = new THREE.Matrix4();
|
|
|
+ // compute the offset between the current and the original transform
|
|
|
|
|
|
- inverse.getInverse( this.bones[ b ].skinMatrix );
|
|
|
+ var matrix = this.bones[ b ] ? this.bones[ b ].matrixWorld : this.identityMatrix;
|
|
|
|
|
|
- this.boneInverses.push( inverse );
|
|
|
+ offsetMatrix.multiplyMatrices( matrix, this.boneInverses[ b ] );
|
|
|
+ offsetMatrix.flattenToArrayOffset( this.boneMatrices, b * 16 );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( this.useVertexTexture ) {
|
|
|
+
|
|
|
+ this.boneTexture.needsUpdate = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
+
|