VelocityNode.js 3.2 KB

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