BlendCharacter.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @author Michael Guerrero / http://realitymeltdown.com
  3. */
  4. THREE.BlendCharacter = function () {
  5. this.weightSchedule = [];
  6. this.warpSchedule = [];
  7. this.load = function ( url, onLoad ) {
  8. var scope = this;
  9. var loader = new THREE.ObjectLoader();
  10. loader.load( url, function( loadedObject ) {
  11. // The exporter does not currently allow exporting a skinned mesh by itself
  12. // so we must fish it out of the hierarchy it is embedded in (scene)
  13. loadedObject.traverse( function( object ) {
  14. if ( object instanceof THREE.SkinnedMesh ) {
  15. scope.skinnedMesh = object;
  16. }
  17. } );
  18. THREE.SkinnedMesh.call( scope, scope.skinnedMesh.geometry, scope.skinnedMesh.material );
  19. // If we didn't successfully find the mesh, bail out
  20. if ( scope.skinnedMesh == undefined ) {
  21. console.log( 'unable to find skinned mesh in ' + url );
  22. return;
  23. }
  24. scope.mixer = new THREE.AnimationMixer( scope );
  25. // Create the animations
  26. for ( var i = 0; i < scope.geometry.animations.length; ++ i ) {
  27. scope.mixer.clipAction( scope.geometry.animations[ i ] );
  28. }
  29. // Loading is complete, fire the callback
  30. if ( onLoad !== undefined ) onLoad();
  31. } );
  32. };
  33. this.loadJSON = function ( url, onLoad ) {
  34. var scope = this;
  35. var loader = new THREE.JSONLoader();
  36. loader.load( url, function ( geometry, materials ) {
  37. var originalMaterial = materials[ 0 ];
  38. THREE.SkinnedMesh.call( scope, geometry, originalMaterial );
  39. var mixer = new THREE.AnimationMixer( scope );
  40. scope.mixer = mixer;
  41. // Create the animations
  42. for ( var i = 0; i < geometry.animations.length; ++ i ) {
  43. mixer.clipAction( geometry.animations[ i ] );
  44. }
  45. // Loading is complete, fire the callback
  46. if ( onLoad !== undefined ) onLoad();
  47. } );
  48. };
  49. this.update = function( dt ) {
  50. this.mixer.update( dt );
  51. };
  52. this.play = function( animName, weight ) {
  53. //console.log("play('%s', %f)", animName, weight);
  54. return this.mixer.clipAction( animName ).
  55. setEffectiveWeight( weight ).play();
  56. };
  57. this.crossfade = function( fromAnimName, toAnimName, duration ) {
  58. this.mixer.stopAllAction();
  59. var fromAction = this.play( fromAnimName, 1 );
  60. var toAction = this.play( toAnimName, 1 );
  61. fromAction.crossFadeTo( toAction, duration, false );
  62. };
  63. this.warp = function( fromAnimName, toAnimName, duration ) {
  64. this.mixer.stopAllAction();
  65. var fromAction = this.play( fromAnimName, 1 );
  66. var toAction = this.play( toAnimName, 1 );
  67. fromAction.crossFadeTo( toAction, duration, true );
  68. };
  69. this.applyWeight = function( animName, weight ) {
  70. this.mixer.clipAction( animName ).setEffectiveWeight( weight );
  71. };
  72. this.getWeight = function( animName ) {
  73. return this.mixer.clipAction( animName ).getEffectiveWeight();
  74. };
  75. this.pauseAll = function() {
  76. this.mixer.timeScale = 0;
  77. };
  78. this.unPauseAll = function() {
  79. this.mixer.timeScale = 1;
  80. };
  81. this.stopAll = function() {
  82. this.mixer.stopAllAction();
  83. };
  84. this.showModel = function( boolean ) {
  85. this.visible = boolean;
  86. };
  87. };
  88. THREE.BlendCharacter.prototype = Object.create( THREE.SkinnedMesh.prototype );
  89. THREE.BlendCharacter.prototype.constructor = THREE.BlendCharacter;
  90. THREE.BlendCharacter.prototype.getForward = function() {
  91. var forward = new THREE.Vector3();
  92. return function() {
  93. // pull the character's forward basis vector out of the matrix
  94. forward.set(
  95. - this.matrix.elements[ 8 ],
  96. - this.matrix.elements[ 9 ],
  97. - this.matrix.elements[ 10 ]
  98. );
  99. return forward;
  100. }
  101. };