BlendCharacter.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. // Create the animations
  16. for ( var i = 0; i < geometry.animations.length; ++i ) {
  17. THREE.AnimationHandler.add( geometry.animations[ i ] );
  18. var animName = geometry.animations[ i ].name;
  19. scope.animations[ animName ] = new THREE.Animation( scope, animName );
  20. }
  21. // Create the debug visualization
  22. scope.skeletonHelper = new THREE.SkeletonHelper( scope );
  23. scope.skeletonHelper.material.linewidth = 3;
  24. scope.add( scope.skeletonHelper );
  25. scope.showSkeleton( false );
  26. // Loading is complete, fire the callback
  27. if ( onLoad !== undefined ) onLoad();
  28. } );
  29. };
  30. this.update = function( dt ) {
  31. for ( var i = this.weightSchedule.length - 1; i >= 0; --i ) {
  32. var data = this.weightSchedule[ i ];
  33. data.timeElapsed += dt;
  34. // If the transition is complete, remove it from the schedule
  35. if ( data.timeElapsed > data.duration ) {
  36. data.anim.weight = data.endWeight;
  37. this.weightSchedule.splice( i, 1 );
  38. // If we've faded out completely, stop the animation
  39. if ( data.anim.weight == 0 ) {
  40. data.anim.stop( 0 );
  41. }
  42. } else {
  43. // interpolate the weight for the current time
  44. data.anim.weight = data.startWeight + (data.endWeight - data.startWeight) * data.timeElapsed / data.duration;
  45. }
  46. }
  47. this.updateWarps( dt );
  48. this.skeletonHelper.update();
  49. };
  50. this.updateWarps = function( dt ) {
  51. // Warping modifies the time scale over time to make 2 animations of different
  52. // lengths match. This is useful for smoothing out transitions that get out of
  53. // phase such as between a walk and run cycle
  54. for ( var i = this.warpSchedule.length - 1; i >= 0; --i ) {
  55. var data = this.warpSchedule[ i ];
  56. data.timeElapsed += dt;
  57. if ( data.timeElapsed > data.duration ) {
  58. data.to.weight = 1;
  59. data.to.timeScale = 1;
  60. data.from.weight = 0;
  61. data.from.timeScale = 1;
  62. data.from.stop( 0 );
  63. this.warpSchedule.splice( i, 1 );
  64. } else {
  65. var alpha = data.timeElapsed / data.duration;
  66. var fromLength = data.from.data.length;
  67. var toLength = data.to.data.length;
  68. var fromToRatio = fromLength / toLength;
  69. var toFromRatio = toLength / fromLength;
  70. // scale from each time proportionally to the other animation
  71. data.from.timeScale = ( 1 - alpha ) + fromToRatio * alpha;
  72. data.to.timeScale = alpha + toFromRatio * ( 1 - alpha );
  73. data.from.weight = 1 - alpha;
  74. data.to.weight = alpha;
  75. }
  76. }
  77. }
  78. this.play = function(animName, weight) {
  79. this.animations[ animName ].play( 0, weight );
  80. };
  81. this.crossfade = function( fromAnimName, toAnimName, duration ) {
  82. var fromAnim = this.animations[ fromAnimName ];
  83. var toAnim = this.animations[ toAnimName ];
  84. fromAnim.play( 0, 1 );
  85. toAnim.play( 0, 0 );
  86. this.weightSchedule.push( {
  87. anim: fromAnim,
  88. startWeight: 1,
  89. endWeight: 0,
  90. timeElapsed: 0,
  91. duration: duration
  92. } );
  93. this.weightSchedule.push( {
  94. anim: toAnim,
  95. startWeight: 0,
  96. endWeight: 1,
  97. timeElapsed: 0,
  98. duration: duration
  99. } );
  100. };
  101. this.warp = function( fromAnimName, toAnimName, duration ) {
  102. var fromAnim = this.animations[ fromAnimName ];
  103. var toAnim = this.animations[ toAnimName ];
  104. fromAnim.play( 0, 1 );
  105. toAnim.play( 0, 0 );
  106. this.warpSchedule.push( {
  107. from: fromAnim,
  108. to: toAnim,
  109. timeElapsed: 0,
  110. duration: duration
  111. } );
  112. };
  113. this.applyWeight = function(animName, weight) {
  114. this.animations[ animName ].weight = weight;
  115. };
  116. this.pauseAll = function() {
  117. for ( var a in this.animations ) {
  118. if ( this.animations[ a ].isPlaying ) {
  119. this.animations[ a ].pause();
  120. }
  121. }
  122. };
  123. this.unPauseAll = function() {
  124. for ( var a in this.animations ) {
  125. if ( this.animations[ a ].isPlaying && this.animations[ a ].isPaused ) {
  126. this.animations[ a ].pause();
  127. }
  128. }
  129. };
  130. this.stopAll = function() {
  131. for ( a in this.animations ) {
  132. if ( this.animations[ a ].isPlaying ) {
  133. this.animations[ a ].stop(0);
  134. }
  135. this.animations[ a ].weight = 0;
  136. }
  137. this.weightSchedule.length = 0;
  138. this.warpSchedule.length = 0;
  139. }
  140. this.showSkeleton = function( boolean ) {
  141. this.skeletonHelper.visible = boolean;
  142. }
  143. this.showModel = function( boolean ) {
  144. this.visible = boolean;
  145. }
  146. };
  147. THREE.BlendCharacter.prototype = Object.create( THREE.SkinnedMesh.prototype );
  148. THREE.BlendCharacter.prototype.getForward = function() {
  149. var forward = new THREE.Vector3();
  150. return function() {
  151. // pull the character's forward basis vector out of the matrix
  152. forward.set(
  153. -this.matrix.elements[ 8 ],
  154. -this.matrix.elements[ 9 ],
  155. -this.matrix.elements[ 10 ]
  156. );
  157. return forward;
  158. }
  159. }