Skeleton.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { Matrix4 } from '../math/Matrix4.js';
  2. const _offsetMatrix = new Matrix4();
  3. const _identityMatrix = new Matrix4();
  4. function Skeleton( bones, boneInverses ) {
  5. // copy the bone array
  6. bones = bones || [];
  7. this.bones = bones.slice( 0 );
  8. this.boneMatrices = new Float32Array( this.bones.length * 16 );
  9. this.frame = - 1;
  10. // use the supplied bone inverses or calculate the inverses
  11. if ( boneInverses === undefined ) {
  12. this.calculateInverses();
  13. } else {
  14. if ( this.bones.length === boneInverses.length ) {
  15. this.boneInverses = boneInverses.slice( 0 );
  16. } else {
  17. console.warn( 'THREE.Skeleton boneInverses is the wrong length.' );
  18. this.boneInverses = [];
  19. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  20. this.boneInverses.push( new Matrix4() );
  21. }
  22. }
  23. }
  24. }
  25. Object.assign( Skeleton.prototype, {
  26. calculateInverses: function () {
  27. this.boneInverses = [];
  28. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  29. const inverse = new Matrix4();
  30. if ( this.bones[ i ] ) {
  31. inverse.copy( this.bones[ i ].matrixWorld ).invert();
  32. }
  33. this.boneInverses.push( inverse );
  34. }
  35. },
  36. pose: function () {
  37. // recover the bind-time world matrices
  38. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  39. const bone = this.bones[ i ];
  40. if ( bone ) {
  41. bone.matrixWorld.copy( this.boneInverses[ i ] ).invert();
  42. }
  43. }
  44. // compute the local matrices, positions, rotations and scales
  45. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  46. const bone = this.bones[ i ];
  47. if ( bone ) {
  48. if ( bone.parent && bone.parent.isBone ) {
  49. bone.matrix.copy( bone.parent.matrixWorld ).invert();
  50. bone.matrix.multiply( bone.matrixWorld );
  51. } else {
  52. bone.matrix.copy( bone.matrixWorld );
  53. }
  54. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  55. }
  56. }
  57. },
  58. update: function () {
  59. const bones = this.bones;
  60. const boneInverses = this.boneInverses;
  61. const boneMatrices = this.boneMatrices;
  62. const boneTexture = this.boneTexture;
  63. // flatten bone matrices to array
  64. for ( let i = 0, il = bones.length; i < il; i ++ ) {
  65. // compute the offset between the current and the original transform
  66. const matrix = bones[ i ] ? bones[ i ].matrixWorld : _identityMatrix;
  67. _offsetMatrix.multiplyMatrices( matrix, boneInverses[ i ] );
  68. _offsetMatrix.toArray( boneMatrices, i * 16 );
  69. }
  70. if ( boneTexture !== undefined ) {
  71. boneTexture.needsUpdate = true;
  72. }
  73. },
  74. clone: function () {
  75. return new Skeleton( this.bones, this.boneInverses );
  76. },
  77. getBoneByName: function ( name ) {
  78. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  79. const bone = this.bones[ i ];
  80. if ( bone.name === name ) {
  81. return bone;
  82. }
  83. }
  84. return undefined;
  85. },
  86. dispose: function ( ) {
  87. if ( this.boneTexture ) {
  88. this.boneTexture.dispose();
  89. this.boneTexture = undefined;
  90. }
  91. }
  92. } );
  93. export { Skeleton };
粤ICP备19079148号