Skeleton.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author michael guerrero / http://realitymeltdown.com
  5. * @author ikerr / http://verold.com
  6. */
  7. THREE.Skeleton = function ( bones, boneInverses, useVertexTexture ) {
  8. this.useVertexTexture = useVertexTexture !== undefined ? useVertexTexture : true;
  9. this.identityMatrix = new THREE.Matrix4();
  10. // copy the bone array
  11. bones = bones || [];
  12. this.bones = bones.slice( 0 );
  13. // create a bone texture or an array of floats
  14. if ( this.useVertexTexture ) {
  15. // layout (1 matrix = 4 pixels)
  16. // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
  17. // with 8x8 pixel texture max 16 bones * 4 pixels = (8 * 8)
  18. // 16x16 pixel texture max 64 bones * 4 pixels = (16 * 16)
  19. // 32x32 pixel texture max 256 bones * 4 pixels = (32 * 32)
  20. // 64x64 pixel texture max 1024 bones * 4 pixels = (64 * 64)
  21. var size = Math.sqrt( this.bones.length * 4 ); // 4 pixels needed for 1 matrix
  22. size = THREE.Math.nextPowerOfTwo( Math.ceil( size ) );
  23. size = Math.max( size, 4 );
  24. this.boneTextureWidth = size;
  25. this.boneTextureHeight = size;
  26. this.boneMatrices = new Float32Array( this.boneTextureWidth * this.boneTextureHeight * 4 ); // 4 floats per RGBA pixel
  27. this.boneTexture = new THREE.DataTexture( this.boneMatrices, this.boneTextureWidth, this.boneTextureHeight, THREE.RGBAFormat, THREE.FloatType );
  28. } else {
  29. this.boneMatrices = new Float32Array( 16 * this.bones.length );
  30. }
  31. // use the supplied bone inverses or calculate the inverses
  32. if ( boneInverses === undefined ) {
  33. this.calculateInverses();
  34. } else {
  35. if ( this.bones.length === boneInverses.length ) {
  36. this.boneInverses = boneInverses.slice( 0 );
  37. } else {
  38. console.warn( 'THREE.Skeleton bonInverses is the wrong length.' );
  39. this.boneInverses = [];
  40. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  41. this.boneInverses.push( new THREE.Matrix4() );
  42. }
  43. }
  44. }
  45. };
  46. THREE.Skeleton.prototype.calculateInverses = function () {
  47. this.boneInverses = [];
  48. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  49. var inverse = new THREE.Matrix4();
  50. if ( this.bones[ b ] ) {
  51. inverse.getInverse( this.bones[ b ].matrixWorld );
  52. }
  53. this.boneInverses.push( inverse );
  54. }
  55. };
  56. THREE.Skeleton.prototype.pose = function () {
  57. var bone;
  58. // recover the bind-time world matrices
  59. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  60. bone = this.bones[ b ];
  61. if ( bone ) {
  62. bone.matrixWorld.getInverse( this.boneInverses[ b ] );
  63. }
  64. }
  65. // compute the local matrices, positions, rotations and scales
  66. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  67. bone = this.bones[ b ];
  68. if ( bone ) {
  69. if ( bone.parent ) {
  70. bone.matrix.getInverse( bone.parent.matrixWorld );
  71. bone.matrix.multiply( bone.matrixWorld );
  72. } else {
  73. bone.matrix.copy( bone.matrixWorld );
  74. }
  75. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  76. }
  77. }
  78. };
  79. THREE.Skeleton.prototype.update = ( function () {
  80. var offsetMatrix = new THREE.Matrix4();
  81. return function update() {
  82. // flatten bone matrices to array
  83. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  84. // compute the offset between the current and the original transform
  85. var matrix = this.bones[ b ] ? this.bones[ b ].matrixWorld : this.identityMatrix;
  86. offsetMatrix.multiplyMatrices( matrix, this.boneInverses[ b ] );
  87. offsetMatrix.flattenToArrayOffset( this.boneMatrices, b * 16 );
  88. }
  89. if ( this.useVertexTexture ) {
  90. this.boneTexture.needsUpdate = true;
  91. }
  92. };
  93. } )();
  94. THREE.Skeleton.prototype.clone = function () {
  95. return new THREE.Skeleton( this.bones, this.boneInverses, this.useVertexTexture );
  96. };