BlendCharacter.js 2.9 KB

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