Skeleton.js 2.8 KB

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