Skeleton.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 (8 * 8 / 4)
  18. // 16x16 pixel texture max 64 bones (16 * 16 / 4)
  19. // 32x32 pixel texture max 256 bones (32 * 32 / 4)
  20. // 64x64 pixel texture max 1024 bones (64 * 64 / 4)
  21. var size;
  22. if ( this.bones.length > 256 )
  23. size = 64;
  24. else if ( this.bones.length > 64 )
  25. size = 32;
  26. else if ( this.bones.length > 16 )
  27. size = 16;
  28. else
  29. size = 8;
  30. this.boneTextureWidth = size;
  31. this.boneTextureHeight = size;
  32. this.boneMatrices = new Float32Array( this.boneTextureWidth * this.boneTextureHeight * 4 ); // 4 floats per RGBA pixel
  33. this.boneTexture = new THREE.DataTexture( this.boneMatrices, this.boneTextureWidth, this.boneTextureHeight, THREE.RGBAFormat, THREE.FloatType );
  34. this.boneTexture.minFilter = THREE.NearestFilter;
  35. this.boneTexture.magFilter = THREE.NearestFilter;
  36. this.boneTexture.generateMipmaps = false;
  37. this.boneTexture.flipY = false;
  38. } else {
  39. this.boneMatrices = new Float32Array( 16 * this.bones.length );
  40. }
  41. // use the supplied bone inverses or calculate the inverses
  42. if ( boneInverses === undefined ) {
  43. this.calculateInverses();
  44. } else {
  45. if ( this.bones.length === boneInverses.length ) {
  46. this.boneInverses = boneInverses.slice( 0 );
  47. } else {
  48. console.warn( 'THREE.Skeleton bonInverses is the wrong length.' );
  49. this.boneInverses = [];
  50. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  51. this.boneInverses.push( new THREE.Matrix4() );
  52. }
  53. }
  54. }
  55. };
  56. THREE.Skeleton.prototype.calculateInverses = function () {
  57. this.boneInverses = [];
  58. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  59. var inverse = new THREE.Matrix4();
  60. if ( this.bones[ b ] ) {
  61. inverse.getInverse( this.bones[ b ].matrixWorld );
  62. }
  63. this.boneInverses.push( inverse );
  64. }
  65. };
  66. THREE.Skeleton.prototype.pose = function () {
  67. var bone;
  68. // recover the bind-time world matrices
  69. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  70. bone = this.bones[ b ];
  71. if ( bone ) {
  72. bone.matrixWorld.getInverse( this.boneInverses[ b ] );
  73. }
  74. }
  75. // compute the local matrices, positions, rotations and scales
  76. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  77. bone = this.bones[ b ];
  78. if ( bone ) {
  79. if ( bone.parent ) {
  80. bone.matrix.getInverse( bone.parent.matrixWorld );
  81. bone.matrix.multiply( bone.matrixWorld );
  82. } else {
  83. bone.matrix.copy( bone.matrixWorld );
  84. }
  85. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  86. }
  87. }
  88. };
  89. THREE.Skeleton.prototype.update = ( function () {
  90. var offsetMatrix = new THREE.Matrix4();
  91. return function () {
  92. // flatten bone matrices to array
  93. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  94. // compute the offset between the current and the original transform
  95. var matrix = this.bones[ b ] ? this.bones[ b ].matrixWorld : this.identityMatrix;
  96. offsetMatrix.multiplyMatrices( matrix, this.boneInverses[ b ] );
  97. offsetMatrix.flattenToArrayOffset( this.boneMatrices, b * 16 );
  98. }
  99. if ( this.useVertexTexture ) {
  100. this.boneTexture.needsUpdate = true;
  101. }
  102. };
  103. } )();