BlendCharacter.js 2.8 KB

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