VelocityNode.js 3.2 KB

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