TimerNode.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 : this.scale !== 1;
  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.isReadonly = function () {
  19. // never use TimerNode as readonly but aways as "uniform"
  20. return false;
  21. };
  22. TimerNode.prototype.isUnique = 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. };
  45. TimerNode.prototype.toJSON = function ( meta ) {
  46. var data = this.getJSONNode( meta );
  47. if ( ! data ) {
  48. data = this.createJSONNode( meta );
  49. data.scope = this.scope;
  50. data.scale = this.scale;
  51. data.timeScale = this.timeScale;
  52. }
  53. return data;
  54. };
  55. NodeLib.addKeyword( 'time', function () {
  56. return new TimerNode();
  57. } );
  58. export { TimerNode };