VelocityNode.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.VelocityNode = function ( target, params ) {
  5. THREE.Vector3Node.call( this );
  6. this.params = {};
  7. this.velocity = new THREE.Vector3();
  8. this.setTarget( target );
  9. this.setParams( params );
  10. };
  11. THREE.VelocityNode.prototype = Object.create( THREE.Vector3Node.prototype );
  12. THREE.VelocityNode.prototype.constructor = THREE.VelocityNode;
  13. THREE.VelocityNode.prototype.nodeType = "Velocity";
  14. THREE.VelocityNode.prototype.isReadonly = function ( builder ) {
  15. return false;
  16. };
  17. THREE.VelocityNode.prototype.setParams = function ( params ) {
  18. switch ( this.params.type ) {
  19. case "elastic":
  20. delete this.moment;
  21. delete this.speed;
  22. delete this.springVelocity;
  23. delete this.lastVelocity;
  24. break;
  25. }
  26. this.params = params || {};
  27. switch ( this.params.type ) {
  28. case "elastic":
  29. this.moment = new THREE.Vector3();
  30. this.speed = new THREE.Vector3();
  31. this.springVelocity = new THREE.Vector3();
  32. this.lastVelocity = new THREE.Vector3();
  33. break;
  34. }
  35. };
  36. THREE.VelocityNode.prototype.setTarget = function ( target ) {
  37. if ( this.target ) {
  38. delete this.position;
  39. delete this.oldPosition;
  40. }
  41. this.target = target;
  42. if ( target ) {
  43. this.position = target.getWorldPosition( this.position || new THREE.Vector3() );
  44. this.oldPosition = this.position.clone();
  45. }
  46. };
  47. THREE.VelocityNode.prototype.updateFrameVelocity = function ( frame ) {
  48. if ( this.target ) {
  49. this.position = this.target.getWorldPosition( this.position || new THREE.Vector3() );
  50. this.velocity.subVectors( this.position, this.oldPosition );
  51. this.oldPosition.copy( this.position );
  52. }
  53. };
  54. THREE.VelocityNode.prototype.updateFrame = function ( frame ) {
  55. this.updateFrameVelocity( frame );
  56. switch ( this.params.type ) {
  57. case "elastic":
  58. // convert to real scale: 0 at 1 values
  59. var deltaFps = frame.delta * ( this.params.fps || 60 );
  60. var spring = Math.pow( this.params.spring, deltaFps ),
  61. damping = Math.pow( this.params.damping, deltaFps );
  62. // fix relative frame-rate
  63. this.velocity.multiplyScalar( Math.exp( - this.params.damping * deltaFps ) );
  64. // elastic
  65. this.velocity.add( this.springVelocity );
  66. this.velocity.add( this.speed.multiplyScalar( damping ).multiplyScalar( 1 - spring ) );
  67. // speed
  68. this.speed.subVectors( this.velocity, this.lastVelocity );
  69. // spring velocity
  70. this.springVelocity.add( this.speed );
  71. this.springVelocity.multiplyScalar( spring );
  72. // moment
  73. this.moment.add( this.springVelocity );
  74. // damping
  75. this.moment.multiplyScalar( damping );
  76. this.lastVelocity.copy( this.velocity );
  77. this.value.copy( this.moment );
  78. break;
  79. default:
  80. this.value.copy( this.velocity );
  81. }
  82. };
  83. THREE.VelocityNode.prototype.toJSON = function ( meta ) {
  84. var data = this.getJSONNode( meta );
  85. if ( ! data ) {
  86. data = this.createJSONNode( meta );
  87. if ( this.target ) data.target = this.target.uuid;
  88. // clone params
  89. data.params = JSON.parse( JSON.stringify( this.params ) );
  90. }
  91. return data;
  92. };