SkinnedMesh.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { Mesh } from './Mesh.js';
  2. import { Box3 } from '../math/Box3.js';
  3. import { Matrix4 } from '../math/Matrix4.js';
  4. import { Sphere } from '../math/Sphere.js';
  5. import { Vector3 } from '../math/Vector3.js';
  6. import { Vector4 } from '../math/Vector4.js';
  7. import { Ray } from '../math/Ray.js';
  8. import { AttachedBindMode, DetachedBindMode } from '../constants.js';
  9. const _basePosition = /*@__PURE__*/ new Vector3();
  10. const _skinIndex = /*@__PURE__*/ new Vector4();
  11. const _skinWeight = /*@__PURE__*/ new Vector4();
  12. const _vector3 = /*@__PURE__*/ new Vector3();
  13. const _matrix4 = /*@__PURE__*/ new Matrix4();
  14. const _vertex = /*@__PURE__*/ new Vector3();
  15. const _sphere = /*@__PURE__*/ new Sphere();
  16. const _inverseMatrix = /*@__PURE__*/ new Matrix4();
  17. const _ray = /*@__PURE__*/ new Ray();
  18. class SkinnedMesh extends Mesh {
  19. constructor( geometry, material ) {
  20. super( geometry, material );
  21. this.isSkinnedMesh = true;
  22. this.type = 'SkinnedMesh';
  23. this.bindMode = AttachedBindMode;
  24. this.bindMatrix = new Matrix4();
  25. this.bindMatrixInverse = new Matrix4();
  26. this.boundingBox = null;
  27. this.boundingSphere = null;
  28. }
  29. computeBoundingBox() {
  30. const geometry = this.geometry;
  31. if ( this.boundingBox === null ) {
  32. this.boundingBox = new Box3();
  33. }
  34. this.boundingBox.makeEmpty();
  35. const positionAttribute = geometry.getAttribute( 'position' );
  36. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  37. this.getVertexPosition( i, _vertex );
  38. this.boundingBox.expandByPoint( _vertex );
  39. }
  40. }
  41. computeBoundingSphere() {
  42. const geometry = this.geometry;
  43. if ( this.boundingSphere === null ) {
  44. this.boundingSphere = new Sphere();
  45. }
  46. this.boundingSphere.makeEmpty();
  47. const positionAttribute = geometry.getAttribute( 'position' );
  48. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  49. this.getVertexPosition( i, _vertex );
  50. this.boundingSphere.expandByPoint( _vertex );
  51. }
  52. }
  53. copy( source, recursive ) {
  54. super.copy( source, recursive );
  55. this.bindMode = source.bindMode;
  56. this.bindMatrix.copy( source.bindMatrix );
  57. this.bindMatrixInverse.copy( source.bindMatrixInverse );
  58. this.skeleton = source.skeleton;
  59. if ( source.boundingBox !== null ) this.boundingBox = source.boundingBox.clone();
  60. if ( source.boundingSphere !== null ) this.boundingSphere = source.boundingSphere.clone();
  61. return this;
  62. }
  63. raycast( raycaster, intersects ) {
  64. const material = this.material;
  65. const matrixWorld = this.matrixWorld;
  66. if ( material === undefined ) return;
  67. // test with bounding sphere in world space
  68. if ( this.boundingSphere === null ) this.computeBoundingSphere();
  69. _sphere.copy( this.boundingSphere );
  70. _sphere.applyMatrix4( matrixWorld );
  71. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  72. // convert ray to local space of skinned mesh
  73. _inverseMatrix.copy( matrixWorld ).invert();
  74. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  75. // test with bounding box in local space
  76. if ( this.boundingBox !== null ) {
  77. if ( _ray.intersectsBox( this.boundingBox ) === false ) return;
  78. }
  79. // test for intersections with geometry
  80. this._computeIntersections( raycaster, intersects, _ray );
  81. }
  82. getVertexPosition( index, target ) {
  83. super.getVertexPosition( index, target );
  84. this.applyBoneTransform( index, target );
  85. return target;
  86. }
  87. bind( skeleton, bindMatrix ) {
  88. this.skeleton = skeleton;
  89. if ( bindMatrix === undefined ) {
  90. this.updateMatrixWorld( true );
  91. this.skeleton.calculateInverses();
  92. bindMatrix = this.matrixWorld;
  93. }
  94. this.bindMatrix.copy( bindMatrix );
  95. this.bindMatrixInverse.copy( bindMatrix ).invert();
  96. }
  97. pose() {
  98. this.skeleton.pose();
  99. }
  100. normalizeSkinWeights() {
  101. const vector = new Vector4();
  102. const skinWeight = this.geometry.attributes.skinWeight;
  103. for ( let i = 0, l = skinWeight.count; i < l; i ++ ) {
  104. vector.fromBufferAttribute( skinWeight, i );
  105. const scale = 1.0 / vector.manhattanLength();
  106. if ( scale !== Infinity ) {
  107. vector.multiplyScalar( scale );
  108. } else {
  109. vector.set( 1, 0, 0, 0 ); // do something reasonable
  110. }
  111. skinWeight.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
  112. }
  113. }
  114. updateMatrixWorld( force ) {
  115. super.updateMatrixWorld( force );
  116. if ( this.bindMode === AttachedBindMode ) {
  117. this.bindMatrixInverse.copy( this.matrixWorld ).invert();
  118. } else if ( this.bindMode === DetachedBindMode ) {
  119. this.bindMatrixInverse.copy( this.bindMatrix ).invert();
  120. } else {
  121. console.warn( 'THREE.SkinnedMesh: Unrecognized bindMode: ' + this.bindMode );
  122. }
  123. }
  124. applyBoneTransform( index, vector ) {
  125. const skeleton = this.skeleton;
  126. const geometry = this.geometry;
  127. _skinIndex.fromBufferAttribute( geometry.attributes.skinIndex, index );
  128. _skinWeight.fromBufferAttribute( geometry.attributes.skinWeight, index );
  129. _basePosition.copy( vector ).applyMatrix4( this.bindMatrix );
  130. vector.set( 0, 0, 0 );
  131. for ( let i = 0; i < 4; i ++ ) {
  132. const weight = _skinWeight.getComponent( i );
  133. if ( weight !== 0 ) {
  134. const boneIndex = _skinIndex.getComponent( i );
  135. _matrix4.multiplyMatrices( skeleton.bones[ boneIndex ].matrixWorld, skeleton.boneInverses[ boneIndex ] );
  136. vector.addScaledVector( _vector3.copy( _basePosition ).applyMatrix4( _matrix4 ), weight );
  137. }
  138. }
  139. return vector.applyMatrix4( this.bindMatrixInverse );
  140. }
  141. }
  142. export { SkinnedMesh };
粤ICP备19079148号