BlendCharacter.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @author Michael Guerrero / http://realitymeltdown.com
  3. */
  4. THREE.BlendCharacter = function () {
  5. this.animations = {};
  6. this.weightSchedule = [];
  7. this.warpSchedule = [];
  8. this.load = function ( url, onLoad ) {
  9. var scope = this;
  10. var loader = new THREE.JSONLoader();
  11. loader.load( url, function( geometry, materials ) {
  12. var originalMaterial = materials[ 0 ];
  13. originalMaterial.skinning = true;
  14. THREE.SkinnedMesh.call( scope, geometry, originalMaterial );
  15. scope.mixer = new THREE.AnimationMixer( scope );
  16. // Create the animations
  17. for ( var i = 0; i < geometry.animations.length; ++ i ) {
  18. var animName = geometry.animations[ i ].name;
  19. scope.animations[ animName ] = geometry.animations[ i ];
  20. }
  21. // Loading is complete, fire the callback
  22. if ( onLoad !== undefined ) onLoad();
  23. } );
  24. };
  25. this.update = function( dt ) {
  26. this.mixer.update( dt );
  27. };
  28. this.play = function( animName, weight ) {
  29. this.mixer.removeAllActions();
  30. this.mixer.play( new THREE.AnimationAction( this.animations[ animName ] ) );
  31. };
  32. this.crossfade = function( fromAnimName, toAnimName, duration ) {
  33. this.mixer.removeAllActions();
  34. var fromAction = new THREE.AnimationAction( this.animations[ fromAnimName ] );
  35. var toAction = new THREE.AnimationAction( this.animations[ toAnimName ] );
  36. this.mixer.play( fromAction );
  37. this.mixer.play( toAction );
  38. this.mixer.crossFade( fromAction, toAction, duration, false );
  39. };
  40. this.warp = function( fromAnimName, toAnimName, duration ) {
  41. this.mixer.removeAllActions();
  42. var fromAction = new THREE.AnimationAction( this.animations[ fromAnimName ] );
  43. var toAction = new THREE.AnimationAction( this.animations[ toAnimName ] );
  44. this.mixer.play( fromAction );
  45. this.mixer.play( toAction );
  46. this.mixer.crossFade( fromAction, toAction, duration, true );
  47. };
  48. this.applyWeight = function( animName, weight ) {
  49. var action = this.mixer.findActionByName( animName );
  50. if( action ) {
  51. action.weight = weight;
  52. }
  53. };
  54. this.pauseAll = function() {
  55. this.mixer.timeScale = 0;
  56. };
  57. this.unPauseAll = function() {
  58. this.mixer.timeScale = 1;
  59. };
  60. this.stopAll = function() {
  61. this.mixer.removeAllActions();
  62. };
  63. this.showModel = function( boolean ) {
  64. this.visible = boolean;
  65. }
  66. };
  67. THREE.BlendCharacter.prototype = Object.create( THREE.SkinnedMesh.prototype );
  68. THREE.BlendCharacter.prototype.constructor = THREE.BlendCharacter;
  69. THREE.BlendCharacter.prototype.getForward = function() {
  70. var forward = new THREE.Vector3();
  71. return function() {
  72. // pull the character's forward basis vector out of the matrix
  73. forward.set(
  74. - this.matrix.elements[ 8 ],
  75. - this.matrix.elements[ 9 ],
  76. - this.matrix.elements[ 10 ]
  77. );
  78. return forward;
  79. }
  80. };