BlendCharacter.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.JSONLoader();
  10. loader.load( url, function( geometry, materials ) {
  11. var originalMaterial = materials[ 0 ];
  12. originalMaterial.skinning = true;
  13. THREE.SkinnedMesh.call( scope, geometry, originalMaterial );
  14. var mixer = new THREE.AnimationMixer( scope );
  15. scope.mixer = mixer;
  16. // Create the animations
  17. for ( var i = 0; i < geometry.animations.length; ++ i ) {
  18. mixer.clipAction( geometry.animations[ i ] );
  19. }
  20. // Loading is complete, fire the callback
  21. if ( onLoad !== undefined ) onLoad();
  22. } );
  23. };
  24. this.update = function( dt ) {
  25. this.mixer.update( dt );
  26. };
  27. this.play = function( animName, weight ) {
  28. //console.log("play('%s', %f)", animName, weight);
  29. return this.mixer.clipAction( animName ).
  30. setEffectiveWeight( weight ).play();
  31. };
  32. this.crossfade = function( fromAnimName, toAnimName, duration ) {
  33. this.mixer.stopAllAction();
  34. var fromAction = this.play( fromAnimName, 1 );
  35. var toAction = this.play( toAnimName, 1 );
  36. fromAction.crossFadeTo( toAction, duration, false );
  37. };
  38. this.warp = function( fromAnimName, toAnimName, duration ) {
  39. this.mixer.stopAllAction();
  40. var fromAction = this.play( fromAnimName, 1 );
  41. var toAction = this.play( toAnimName, 1 );
  42. fromAction.crossFadeTo( toAction, duration, true );
  43. };
  44. this.applyWeight = function( animName, weight ) {
  45. this.mixer.clipAction( animName ).setEffectiveWeight( weight );
  46. };
  47. this.getWeight = function( animName ) {
  48. return this.mixer.clipAction( animName ).getEffectiveWeight();
  49. }
  50. this.pauseAll = function() {
  51. this.mixer.timeScale = 0;
  52. };
  53. this.unPauseAll = function() {
  54. this.mixer.timeScale = 1;
  55. };
  56. this.stopAll = function() {
  57. this.mixer.stopAllAction();
  58. };
  59. this.showModel = function( boolean ) {
  60. this.visible = boolean;
  61. }
  62. };
  63. THREE.BlendCharacter.prototype = Object.create( THREE.SkinnedMesh.prototype );
  64. THREE.BlendCharacter.prototype.constructor = THREE.BlendCharacter;
  65. THREE.BlendCharacter.prototype.getForward = function() {
  66. var forward = new THREE.Vector3();
  67. return function() {
  68. // pull the character's forward basis vector out of the matrix
  69. forward.set(
  70. - this.matrix.elements[ 8 ],
  71. - this.matrix.elements[ 9 ],
  72. - this.matrix.elements[ 10 ]
  73. );
  74. return forward;
  75. }
  76. };