TimerNode.js 1.8 KB

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