SkinnedMesh.js 3.3 KB

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