SkinnedMesh.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author ikerr / http://verold.com
  5. */
  6. import { Mesh } from './Mesh.js';
  7. import { Matrix4 } from '../math/Matrix4.js';
  8. import { Vector4 } from '../math/Vector4.js';
  9. function SkinnedMesh( geometry, material ) {
  10. if ( geometry && geometry.isGeometry ) {
  11. console.error( 'THREE.SkinnedMesh no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
  12. }
  13. Mesh.call( this, geometry, material );
  14. this.type = 'SkinnedMesh';
  15. this.bindMode = 'attached';
  16. this.bindMatrix = new Matrix4();
  17. this.bindMatrixInverse = new Matrix4();
  18. }
  19. SkinnedMesh.prototype = Object.assign( Object.create( Mesh.prototype ), {
  20. constructor: SkinnedMesh,
  21. isSkinnedMesh: true,
  22. bind: function ( skeleton, bindMatrix ) {
  23. this.skeleton = skeleton;
  24. if ( bindMatrix === undefined ) {
  25. this.updateMatrixWorld( true );
  26. this.skeleton.calculateInverses();
  27. bindMatrix = this.matrixWorld;
  28. }
  29. this.bindMatrix.copy( bindMatrix );
  30. this.bindMatrixInverse.getInverse( bindMatrix );
  31. },
  32. pose: function () {
  33. this.skeleton.pose();
  34. },
  35. normalizeSkinWeights: function () {
  36. var vector = new Vector4();
  37. var skinWeight = this.geometry.attributes.skinWeight;
  38. for ( var i = 0, l = skinWeight.count; i < l; i ++ ) {
  39. vector.x = skinWeight.getX( i );
  40. vector.y = skinWeight.getY( i );
  41. vector.z = skinWeight.getZ( i );
  42. vector.w = skinWeight.getW( i );
  43. var scale = 1.0 / vector.manhattanLength();
  44. if ( scale !== Infinity ) {
  45. vector.multiplyScalar( scale );
  46. } else {
  47. vector.set( 1, 0, 0, 0 ); // do something reasonable
  48. }
  49. skinWeight.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
  50. }
  51. },
  52. updateMatrixWorld: function ( force ) {
  53. Mesh.prototype.updateMatrixWorld.call( this, force );
  54. if ( this.bindMode === 'attached' ) {
  55. this.bindMatrixInverse.getInverse( this.matrixWorld );
  56. } else if ( this.bindMode === 'detached' ) {
  57. this.bindMatrixInverse.getInverse( this.bindMatrix );
  58. } else {
  59. console.warn( 'THREE.SkinnedMesh: Unrecognized bindMode: ' + this.bindMode );
  60. }
  61. },
  62. clone: function () {
  63. return new this.constructor( this.geometry, this.material ).copy( this );
  64. }
  65. } );
  66. export { SkinnedMesh };
粤ICP备19079148号