Skeleton.js 3.9 KB

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