2
0

BlendCharacter.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * @author Michael Guerrero / http://realitymeltdown.com
  3. */
  4. THREE.BlendCharacter = function () {
  5. var self = this;
  6. this.animations = {};
  7. this.boneHelpers = [];
  8. this.weightSchedule = [];
  9. this.warpSchedule = [];
  10. this.load = function(url, loadedCB) {
  11. var loader = new THREE.JSONLoader();
  12. loader.load( url, function( geometry, materials ) {
  13. var originalMaterial = materials[ 0 ];
  14. originalMaterial.skinning = true;
  15. THREE.SkinnedMesh.call( self, geometry, originalMaterial );
  16. // Create the animations
  17. for ( var i = 0; i < geometry.animations.length; ++i ) {
  18. THREE.AnimationHandler.add( geometry.animations[ i ] );
  19. var animName = geometry.animations[ i ].name;
  20. self.animations[ animName ] = new THREE.Animation( self, animName );
  21. }
  22. // Create the debug visualization
  23. self.skeletonHelper = new THREE.SkeletonHelper( self.skeleton, 0.5, 1 );
  24. self.add( self.skeletonHelper );
  25. self.toggleShowBones( false );
  26. // Loading is complete, fire the callback
  27. loadedCB && loadedCB();
  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 fade 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. // length 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.updateBoneHelpers = function() {
  79. for ( var i = 0; i < this.boneHelpers.length; ++i ) {
  80. this.boneHelpers[ i ].update();
  81. }
  82. };
  83. this.play = function(animName, weight) {
  84. this.animations[ animName ].play( 0, weight );
  85. };
  86. this.crossfade = function( fromAnimName, toAnimName, duration ) {
  87. var fromAnim = this.animations[ fromAnimName ];
  88. var toAnim = this.animations[ toAnimName ];
  89. fromAnim.play( 0, 1 );
  90. toAnim.play( 0, 0 );
  91. this.weightSchedule.push( {
  92. anim: fromAnim,
  93. startWeight: 1,
  94. endWeight: 0,
  95. timeElapsed: 0,
  96. duration: duration
  97. } );
  98. this.weightSchedule.push( {
  99. anim: toAnim,
  100. startWeight: 0,
  101. endWeight: 1,
  102. timeElapsed: 0,
  103. duration: duration
  104. } );
  105. };
  106. this.warp = function( fromAnimName, toAnimName, duration ) {
  107. var fromAnim = this.animations[ fromAnimName ];
  108. var toAnim = this.animations[ toAnimName ];
  109. fromAnim.play( 0, 1 );
  110. toAnim.play( 0, 0 );
  111. this.warpSchedule.push( {
  112. from: fromAnim,
  113. to: toAnim,
  114. timeElapsed: 0,
  115. duration: duration
  116. } );
  117. };
  118. this.applyWeight = function(animName, weight) {
  119. this.animations[animName].weight = weight;
  120. };
  121. this.pauseAll = function() {
  122. for ( var a in this.animations ) {
  123. if ( this.animations[ a ].isPlaying ) {
  124. this.animations[ a ].pause();
  125. }
  126. }
  127. };
  128. this.unPauseAll = function() {
  129. for ( var a in this.animations ) {
  130. if ( this.animations[ a ].isPaused ) {
  131. this.animations[ a ].pause();
  132. }
  133. }
  134. };
  135. this.stopAll = function() {
  136. for (a in this.animations) {
  137. if ( this.animations[ a ].isPlaying ) {
  138. this.animations[ a ].stop(0);
  139. }
  140. this.animations[ a ].weight = 0;
  141. }
  142. this.weightSchedule.length = 0;
  143. this.warpSchedule.length = 0;
  144. }
  145. this.toggleShowBones = function( shouldShow ) {
  146. this.visible = !shouldShow;
  147. for ( var i = 0; i < self.boneHelpers.length; ++i ) {
  148. self.boneHelpers[ i ].traverse( function( obj ) {
  149. obj.visible = shouldShow;
  150. } );
  151. }
  152. }
  153. };
  154. THREE.BlendCharacter.prototype = Object.create( THREE.SkinnedMesh.prototype );
  155. THREE.BlendCharacter.prototype.getForward = function() {
  156. var forward = new THREE.Vector3();
  157. return function() {
  158. // pull the character's forward basis vector out of the matrix
  159. forward.set(
  160. -this.matrix.elements[ 8 ],
  161. -this.matrix.elements[ 9 ],
  162. -this.matrix.elements[ 10 ]
  163. );
  164. return forward;
  165. }
  166. }