BlendCharacter.js 5.2 KB

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