Skeleton.js 2.9 KB

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