TimerNode.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 = FloatNode.prototype.toJSON.call( this, meta );
  45. data.scope = this.scope;
  46. data.scale = this.scale;
  47. data.timeScale = this.timeScale;
  48. return data;
  49. };
  50. NodeLib.addKeyword( 'time', function () {
  51. return new TimerNode();
  52. } );
  53. export { TimerNode };