Skeleton.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { Bone } from './Bone.js';
  2. import { Matrix4 } from '../math/Matrix4.js';
  3. import { MathUtils } from '../math/MathUtils.js';
  4. const _offsetMatrix = new Matrix4();
  5. const _identityMatrix = new Matrix4();
  6. function Skeleton( bones = [], boneInverses = [] ) {
  7. this.uuid = MathUtils.generateUUID();
  8. this.bones = bones.slice( 0 );
  9. this.boneInverses = boneInverses;
  10. this.boneMatrices = null;
  11. this.boneTexture = null;
  12. this.boneTextureSize = 0;
  13. this.frame = - 1;
  14. this.init();
  15. }
  16. Object.assign( Skeleton.prototype, {
  17. init: function () {
  18. const bones = this.bones;
  19. const boneInverses = this.boneInverses;
  20. this.boneMatrices = new Float32Array( bones.length * 16 );
  21. // calculate inverse bone matrices if necessary
  22. if ( boneInverses.length === 0 ) {
  23. this.calculateInverses();
  24. } else {
  25. // handle special case
  26. if ( bones.length !== boneInverses.length ) {
  27. console.warn( 'THREE.Skeleton: Number of inverse bone matrices does not match amount of bones.' );
  28. this.boneInverses = [];
  29. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  30. this.boneInverses.push( new Matrix4() );
  31. }
  32. }
  33. }
  34. },
  35. calculateInverses: function () {
  36. this.boneInverses.length = 0;
  37. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  38. const inverse = new Matrix4();
  39. if ( this.bones[ i ] ) {
  40. inverse.copy( this.bones[ i ].matrixWorld ).invert();
  41. }
  42. this.boneInverses.push( inverse );
  43. }
  44. },
  45. pose: function () {
  46. // recover the bind-time world matrices
  47. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  48. const bone = this.bones[ i ];
  49. if ( bone ) {
  50. bone.matrixWorld.copy( this.boneInverses[ i ] ).invert();
  51. }
  52. }
  53. // compute the local matrices, positions, rotations and scales
  54. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  55. const bone = this.bones[ i ];
  56. if ( bone ) {
  57. if ( bone.parent && bone.parent.isBone ) {
  58. bone.matrix.copy( bone.parent.matrixWorld ).invert();
  59. bone.matrix.multiply( bone.matrixWorld );
  60. } else {
  61. bone.matrix.copy( bone.matrixWorld );
  62. }
  63. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  64. }
  65. }
  66. },
  67. update: function () {
  68. const bones = this.bones;
  69. const boneInverses = this.boneInverses;
  70. const boneMatrices = this.boneMatrices;
  71. const boneTexture = this.boneTexture;
  72. // flatten bone matrices to array
  73. for ( let i = 0, il = bones.length; i < il; i ++ ) {
  74. // compute the offset between the current and the original transform
  75. const matrix = bones[ i ] ? bones[ i ].matrixWorld : _identityMatrix;
  76. _offsetMatrix.multiplyMatrices( matrix, boneInverses[ i ] );
  77. _offsetMatrix.toArray( boneMatrices, i * 16 );
  78. }
  79. if ( boneTexture !== null ) {
  80. boneTexture.needsUpdate = true;
  81. }
  82. },
  83. clone: function () {
  84. return new Skeleton( this.bones, this.boneInverses );
  85. },
  86. getBoneByName: function ( name ) {
  87. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  88. const bone = this.bones[ i ];
  89. if ( bone.name === name ) {
  90. return bone;
  91. }
  92. }
  93. return undefined;
  94. },
  95. dispose: function ( ) {
  96. if ( this.boneTexture !== null ) {
  97. this.boneTexture.dispose();
  98. this.boneTexture = null;
  99. }
  100. },
  101. fromJSON: function ( json, bones ) {
  102. this.uuid = json.uuid;
  103. for ( let i = 0, l = json.bones.length; i < l; i ++ ) {
  104. const uuid = json.bones[ i ];
  105. let bone = bones[ uuid ];
  106. if ( bone === undefined ) {
  107. console.warn( 'THREE.Skeleton: No bone found with UUID:', uuid );
  108. bone = new Bone();
  109. }
  110. this.bones.push( bone );
  111. this.boneInverses.push( new Matrix4().fromArray( json.boneInverses[ i ] ) );
  112. }
  113. this.init();
  114. return this;
  115. },
  116. toJSON: function () {
  117. const data = {
  118. metadata: {
  119. version: 4.5,
  120. type: 'Skeleton',
  121. generator: 'Skeleton.toJSON'
  122. },
  123. bones: [],
  124. boneInverses: []
  125. };
  126. data.uuid = this.uuid;
  127. const bones = this.bones;
  128. const boneInverses = this.boneInverses;
  129. for ( let i = 0, l = bones.length; i < l; i ++ ) {
  130. const bone = bones[ i ];
  131. data.bones.push( bone.uuid );
  132. const boneInverse = boneInverses[ i ];
  133. data.boneInverses.push( boneInverse.toArray() );
  134. }
  135. return data;
  136. }
  137. } );
  138. export { Skeleton };