BlendCharacter.js 2.8 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 ] = THREE.AnimationClip.FromJSONLoaderAnimation( geometry.animations[ i ], geometry.bones );
  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 ], 0, 1, 1, true ) );
  31. };
  32. this.crossfade = function( fromAnimName, toAnimName, duration ) {
  33. this.mixer.removeAllActions();
  34. var fromAction = new THREE.AnimationAction( this.animations[ fromAnimName ], 0, 1, 1, true );
  35. var toAction = new THREE.AnimationAction( this.animations[ toAnimName ], 0, 1, 1, true );
  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 ], 0, 1, 1, true );
  43. var toAction = new THREE.AnimationAction( this.animations[ toAnimName ], 0, 1, 1, true );
  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. if( this.mixer[ animName ] ) {
  50. this.mixer[ animName ].weight = weight;
  51. }
  52. };
  53. this.pauseAll = function() {
  54. this.mixer.timeScale = 0;
  55. };
  56. this.unPauseAll = function() {
  57. this.mixer.timeScale = 1;
  58. };
  59. this.stopAll = function() {
  60. this.mixer.removeAllActions();
  61. };
  62. this.showModel = function( boolean ) {
  63. this.visible = boolean;
  64. }
  65. };
  66. THREE.BlendCharacter.prototype = Object.create( THREE.SkinnedMesh.prototype );
  67. THREE.BlendCharacter.prototype.constructor = THREE.BlendCharacter;
  68. THREE.BlendCharacter.prototype.getForward = function() {
  69. var forward = new THREE.Vector3();
  70. return function() {
  71. // pull the character's forward basis vector out of the matrix
  72. forward.set(
  73. - this.matrix.elements[ 8 ],
  74. - this.matrix.elements[ 9 ],
  75. - this.matrix.elements[ 10 ]
  76. );
  77. return forward;
  78. }
  79. };