Skeleton.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. this.identityMatrix = new Matrix4();
  10. // copy the bone array
  11. bones = bones || [];
  12. this.bones = bones.slice( 0 );
  13. this.boneMatrices = new Float32Array( this.bones.length * 16 );
  14. // use the supplied bone inverses or calculate the inverses
  15. if ( boneInverses === undefined ) {
  16. this.calculateInverses();
  17. } else {
  18. if ( this.bones.length === boneInverses.length ) {
  19. this.boneInverses = boneInverses.slice( 0 );
  20. } else {
  21. console.warn( 'THREE.Skeleton boneInverses is the wrong length.' );
  22. this.boneInverses = [];
  23. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  24. this.boneInverses.push( new Matrix4() );
  25. }
  26. }
  27. }
  28. }
  29. Object.assign( Skeleton.prototype, {
  30. calculateInverses: function () {
  31. this.boneInverses = [];
  32. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  33. var inverse = new Matrix4();
  34. if ( this.bones[ b ] ) {
  35. inverse.getInverse( this.bones[ b ].matrixWorld );
  36. }
  37. this.boneInverses.push( inverse );
  38. }
  39. },
  40. pose: function () {
  41. var bone;
  42. // recover the bind-time world matrices
  43. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  44. bone = this.bones[ b ];
  45. if ( bone ) {
  46. bone.matrixWorld.getInverse( this.boneInverses[ b ] );
  47. }
  48. }
  49. // compute the local matrices, positions, rotations and scales
  50. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  51. bone = this.bones[ b ];
  52. if ( bone ) {
  53. if ( bone.parent && bone.parent.isBone ) {
  54. bone.matrix.getInverse( bone.parent.matrixWorld );
  55. bone.matrix.multiply( bone.matrixWorld );
  56. } else {
  57. bone.matrix.copy( bone.matrixWorld );
  58. }
  59. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  60. }
  61. }
  62. },
  63. update: ( function () {
  64. var offsetMatrix = 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 b = 0, bl = bones.length; b < bl; b ++ ) {
  72. // compute the offset between the current and the original transform
  73. var matrix = bones[ b ] ? bones[ b ].matrixWorld : this.identityMatrix;
  74. offsetMatrix.multiplyMatrices( matrix, boneInverses[ b ] );
  75. offsetMatrix.toArray( boneMatrices, b * 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号