SkinnedMesh.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { Mesh } from './Mesh.js';
  2. import { Matrix4 } from '../math/Matrix4.js';
  3. import { Vector3 } from '../math/Vector3.js';
  4. import { Vector4 } from '../math/Vector4.js';
  5. const _basePosition = /*@__PURE__*/ new Vector3();
  6. const _skinIndex = /*@__PURE__*/ new Vector4();
  7. const _skinWeight = /*@__PURE__*/ new Vector4();
  8. const _vector = /*@__PURE__*/ new Vector3();
  9. const _matrix = /*@__PURE__*/ new Matrix4();
  10. class SkinnedMesh extends Mesh {
  11. constructor( geometry, material ) {
  12. super( geometry, material );
  13. this.type = 'SkinnedMesh';
  14. this.bindMode = 'attached';
  15. this.bindMatrix = new Matrix4();
  16. this.bindMatrixInverse = new Matrix4();
  17. }
  18. copy( source ) {
  19. super.copy( source );
  20. this.bindMode = source.bindMode;
  21. this.bindMatrix.copy( source.bindMatrix );
  22. this.bindMatrixInverse.copy( source.bindMatrixInverse );
  23. this.skeleton = source.skeleton;
  24. return this;
  25. }
  26. bind( skeleton, bindMatrix ) {
  27. this.skeleton = skeleton;
  28. if ( bindMatrix === undefined ) {
  29. this.updateMatrixWorld( true );
  30. this.skeleton.calculateInverses();
  31. bindMatrix = this.matrixWorld;
  32. }
  33. this.bindMatrix.copy( bindMatrix );
  34. this.bindMatrixInverse.copy( bindMatrix ).invert();
  35. }
  36. pose() {
  37. this.skeleton.pose();
  38. }
  39. normalizeSkinWeights() {
  40. const vector = new Vector4();
  41. const skinWeight = this.geometry.attributes.skinWeight;
  42. for ( let i = 0, l = skinWeight.count; i < l; i ++ ) {
  43. vector.x = skinWeight.getX( i );
  44. vector.y = skinWeight.getY( i );
  45. vector.z = skinWeight.getZ( i );
  46. vector.w = skinWeight.getW( i );
  47. const scale = 1.0 / vector.manhattanLength();
  48. if ( scale !== Infinity ) {
  49. vector.multiplyScalar( scale );
  50. } else {
  51. vector.set( 1, 0, 0, 0 ); // do something reasonable
  52. }
  53. skinWeight.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
  54. }
  55. }
  56. updateMatrixWorld( force ) {
  57. super.updateMatrixWorld( force );
  58. if ( this.bindMode === 'attached' ) {
  59. this.bindMatrixInverse.copy( this.matrixWorld ).invert();
  60. } else if ( this.bindMode === 'detached' ) {
  61. this.bindMatrixInverse.copy( this.bindMatrix ).invert();
  62. } else {
  63. console.warn( 'THREE.SkinnedMesh: Unrecognized bindMode: ' + this.bindMode );
  64. }
  65. }
  66. boneTransform( index, target ) {
  67. const skeleton = this.skeleton;
  68. const geometry = this.geometry;
  69. _skinIndex.fromBufferAttribute( geometry.attributes.skinIndex, index );
  70. _skinWeight.fromBufferAttribute( geometry.attributes.skinWeight, index );
  71. _basePosition.copy( target ).applyMatrix4( this.bindMatrix );
  72. target.set( 0, 0, 0 );
  73. for ( let i = 0; i < 4; i ++ ) {
  74. const weight = _skinWeight.getComponent( i );
  75. if ( weight !== 0 ) {
  76. const boneIndex = _skinIndex.getComponent( i );
  77. _matrix.multiplyMatrices( skeleton.bones[ boneIndex ].matrixWorld, skeleton.boneInverses[ boneIndex ] );
  78. target.addScaledVector( _vector.copy( _basePosition ).applyMatrix4( _matrix ), weight );
  79. }
  80. }
  81. return target.applyMatrix4( this.bindMatrixInverse );
  82. }
  83. }
  84. SkinnedMesh.prototype.isSkinnedMesh = true;
  85. export { SkinnedMesh };