TimerNode.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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, useTimeScale ) {
  7. FloatNode.call( this );
  8. this.scale = scale !== undefined ? scale : 1;
  9. this.scope = scope || TimerNode.GLOBAL;
  10. this.useTimeScale = useTimeScale !== undefined ? useTimeScale : 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. return false;
  20. };
  21. TimerNode.prototype.isUnique = function () {
  22. // share TimerNode "uniform" input if is used on more time with others TimerNode
  23. return this.timeScale && ( this.scope === TimerNode.GLOBAL || this.scope === TimerNode.DELTA );
  24. };
  25. TimerNode.prototype.updateFrame = function ( frame ) {
  26. var scale = this.timeScale ? this.scale : 1;
  27. switch( this.scope ) {
  28. case TimerNode.LOCAL:
  29. this.value += frame.delta * scale;
  30. break;
  31. case TimerNode.DELTA:
  32. this.value = frame.delta * scale;
  33. break;
  34. default:
  35. this.value = frame.time * scale;
  36. }
  37. };
  38. TimerNode.prototype.copy = function ( source ) {
  39. FloatNode.prototype.copy.call( this, source );
  40. this.scope = source.scope;
  41. this.scale = source.scale;
  42. this.useTimeScale = source.useTimeScale;
  43. };
  44. TimerNode.prototype.toJSON = function ( meta ) {
  45. var data = this.getJSONNode( meta );
  46. if ( ! data ) {
  47. data = this.createJSONNode( meta );
  48. data.scope = this.scope;
  49. data.scale = this.scale;
  50. data.useTimeScale = this.useTimeScale;
  51. }
  52. return data;
  53. };
  54. NodeLib.addKeyword( 'time', function () {
  55. return new TimerNode();
  56. } );
  57. export { TimerNode };