BlendCharacter.js 2.9 KB

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