Skeleton.js 5.1 KB

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