TimerNode.js 1.5 KB

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