TimerNode.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { FloatNode } from '../inputs/FloatNode.js';
  5. import { NodeLib } from '../core/NodeLib.js';
  6. function TimerNode( scale, scope, timeScale ) {
  7. FloatNode.call( this );
  8. this.scale = scale !== undefined ? scale : 1;
  9. this.scope = scope || TimerNode.GLOBAL;
  10. this.timeScale = timeScale !== undefined ? timeScale : scale !== undefined;
  11. }
  12. TimerNode.GLOBAL = 'global';
  13. TimerNode.LOCAL = 'local';
  14. TimerNode.DELTA = 'delta';
  15. TimerNode.prototype = Object.create( FloatNode.prototype );
  16. TimerNode.prototype.constructor = TimerNode;
  17. TimerNode.prototype.nodeType = "Timer";
  18. TimerNode.prototype.getReadonly = function () {
  19. // never use TimerNode as readonly but aways as "uniform"
  20. return false;
  21. };
  22. TimerNode.prototype.getUnique = function () {
  23. // share TimerNode "uniform" input if is used on more time with others TimerNode
  24. return this.timeScale && ( this.scope === TimerNode.GLOBAL || this.scope === TimerNode.DELTA );
  25. };
  26. TimerNode.prototype.updateFrame = function ( frame ) {
  27. var scale = this.timeScale ? this.scale : 1;
  28. switch ( this.scope ) {
  29. case TimerNode.LOCAL:
  30. this.value += frame.delta * scale;
  31. break;
  32. case TimerNode.DELTA:
  33. this.value = frame.delta * scale;
  34. break;
  35. default:
  36. this.value = frame.time * scale;
  37. }
  38. };
  39. TimerNode.prototype.copy = function ( source ) {
  40. FloatNode.prototype.copy.call( this, source );
  41. this.scope = source.scope;
  42. this.scale = source.scale;
  43. this.timeScale = source.timeScale;
  44. return this;
  45. };
  46. TimerNode.prototype.toJSON = function ( meta ) {
  47. var data = this.getJSONNode( meta );
  48. if ( ! data ) {
  49. data = this.createJSONNode( meta );
  50. data.scope = this.scope;
  51. data.scale = this.scale;
  52. data.timeScale = this.timeScale;
  53. }
  54. return data;
  55. };
  56. NodeLib.addKeyword( 'time', function () {
  57. return new TimerNode();
  58. } );
  59. export { TimerNode };