Skeleton.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import {
  2. RGBAFormat,
  3. FloatType
  4. } from '../constants.js';
  5. import { Bone } from './Bone.js';
  6. import { Matrix4 } from '../math/Matrix4.js';
  7. import { DataTexture } from '../textures/DataTexture.js';
  8. import * as MathUtils from '../math/MathUtils.js';
  9. const _offsetMatrix = /*@__PURE__*/ new Matrix4();
  10. const _identityMatrix = /*@__PURE__*/ new Matrix4();
  11. class Skeleton {
  12. constructor( bones = [], boneInverses = [] ) {
  13. this.uuid = MathUtils.generateUUID();
  14. this.bones = bones.slice( 0 );
  15. this.boneInverses = boneInverses;
  16. this.boneMatrices = null;
  17. this.boneTexture = null;
  18. this.init();
  19. }
  20. init() {
  21. const bones = this.bones;
  22. const boneInverses = this.boneInverses;
  23. this.boneMatrices = new Float32Array( bones.length * 16 );
  24. // calculate inverse bone matrices if necessary
  25. if ( boneInverses.length === 0 ) {
  26. this.calculateInverses();
  27. } else {
  28. // handle special case
  29. if ( bones.length !== boneInverses.length ) {
  30. console.warn( 'THREE.Skeleton: Number of inverse bone matrices does not match amount of bones.' );
  31. this.boneInverses = [];
  32. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  33. this.boneInverses.push( new Matrix4() );
  34. }
  35. }
  36. }
  37. }
  38. calculateInverses() {
  39. this.boneInverses.length = 0;
  40. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  41. const inverse = new Matrix4();
  42. if ( this.bones[ i ] ) {
  43. inverse.copy( this.bones[ i ].matrixWorld ).invert();
  44. }
  45. this.boneInverses.push( inverse );
  46. }
  47. }
  48. pose() {
  49. // recover the bind-time world matrices
  50. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  51. const bone = this.bones[ i ];
  52. if ( bone ) {
  53. bone.matrixWorld.copy( this.boneInverses[ i ] ).invert();
  54. }
  55. }
  56. // compute the local matrices, positions, rotations and scales
  57. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  58. const bone = this.bones[ i ];
  59. if ( bone ) {
  60. if ( bone.parent && bone.parent.isBone ) {
  61. bone.matrix.copy( bone.parent.matrixWorld ).invert();
  62. bone.matrix.multiply( bone.matrixWorld );
  63. } else {
  64. bone.matrix.copy( bone.matrixWorld );
  65. }
  66. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  67. }
  68. }
  69. }
  70. update() {
  71. const bones = this.bones;
  72. const boneInverses = this.boneInverses;
  73. const boneMatrices = this.boneMatrices;
  74. const boneTexture = this.boneTexture;
  75. // flatten bone matrices to array
  76. for ( let i = 0, il = bones.length; i < il; i ++ ) {
  77. // compute the offset between the current and the original transform
  78. const matrix = bones[ i ] ? bones[ i ].matrixWorld : _identityMatrix;
  79. _offsetMatrix.multiplyMatrices( matrix, boneInverses[ i ] );
  80. _offsetMatrix.toArray( boneMatrices, i * 16 );
  81. }
  82. if ( boneTexture !== null ) {
  83. boneTexture.needsUpdate = true;
  84. }
  85. }
  86. clone() {
  87. return new Skeleton( this.bones, this.boneInverses );
  88. }
  89. computeBoneTexture() {
  90. // layout (1 matrix = 4 pixels)
  91. // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
  92. // with 8x8 pixel texture max 16 bones * 4 pixels = (8 * 8)
  93. // 16x16 pixel texture max 64 bones * 4 pixels = (16 * 16)
  94. // 32x32 pixel texture max 256 bones * 4 pixels = (32 * 32)
  95. // 64x64 pixel texture max 1024 bones * 4 pixels = (64 * 64)
  96. let size = Math.sqrt( this.bones.length * 4 ); // 4 pixels needed for 1 matrix
  97. size = Math.ceil( size / 4 ) * 4;
  98. size = Math.max( size, 4 );
  99. const boneMatrices = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
  100. boneMatrices.set( this.boneMatrices ); // copy current values
  101. const boneTexture = new DataTexture( boneMatrices, size, size, RGBAFormat, FloatType );
  102. boneTexture.needsUpdate = true;
  103. this.boneMatrices = boneMatrices;
  104. this.boneTexture = boneTexture;
  105. return this;
  106. }
  107. getBoneByName( name ) {
  108. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  109. const bone = this.bones[ i ];
  110. if ( bone.name === name ) {
  111. return bone;
  112. }
  113. }
  114. return undefined;
  115. }
  116. dispose( ) {
  117. if ( this.boneTexture !== null ) {
  118. this.boneTexture.dispose();
  119. this.boneTexture = null;
  120. }
  121. }
  122. fromJSON( json, bones ) {
  123. this.uuid = json.uuid;
  124. for ( let i = 0, l = json.bones.length; i < l; i ++ ) {
  125. const uuid = json.bones[ i ];
  126. let bone = bones[ uuid ];
  127. if ( bone === undefined ) {
  128. console.warn( 'THREE.Skeleton: No bone found with UUID:', uuid );
  129. bone = new Bone();
  130. }
  131. this.bones.push( bone );
  132. this.boneInverses.push( new Matrix4().fromArray( json.boneInverses[ i ] ) );
  133. }
  134. this.init();
  135. return this;
  136. }
  137. toJSON() {
  138. const data = {
  139. metadata: {
  140. version: 4.6,
  141. type: 'Skeleton',
  142. generator: 'Skeleton.toJSON'
  143. },
  144. bones: [],
  145. boneInverses: []
  146. };
  147. data.uuid = this.uuid;
  148. const bones = this.bones;
  149. const boneInverses = this.boneInverses;
  150. for ( let i = 0, l = bones.length; i < l; i ++ ) {
  151. const bone = bones[ i ];
  152. data.bones.push( bone.uuid );
  153. const boneInverse = boneInverses[ i ];
  154. data.boneInverses.push( boneInverse.toArray() );
  155. }
  156. return data;
  157. }
  158. }
  159. export { Skeleton };
粤ICP备19079148号