TimerNode.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import UniformNode from '../core/UniformNode.js';
  2. import { NodeUpdateType } from '../core/constants.js';
  3. class TimerNode extends UniformNode {
  4. constructor( scope = TimerNode.LOCAL, scale = 1, value = 0 ) {
  5. super( value );
  6. this.scope = scope;
  7. this.scale = scale;
  8. this.updateType = NodeUpdateType.FRAME;
  9. }
  10. /*
  11. @TODO:
  12. getNodeType( builder ) {
  13. const scope = this.scope;
  14. if ( scope === TimerNode.FRAME ) {
  15. return 'uint';
  16. }
  17. return 'float';
  18. }
  19. */
  20. update( frame ) {
  21. const scope = this.scope;
  22. const scale = this.scale;
  23. if ( scope === TimerNode.LOCAL ) {
  24. this.value += frame.deltaTime * scale;
  25. } else if ( scope === TimerNode.DELTA ) {
  26. this.value = frame.deltaTime * scale;
  27. } else if ( scope === TimerNode.FRAME ) {
  28. this.value = frame.frameId;
  29. } else {
  30. // global
  31. this.value = frame.time * scale;
  32. }
  33. }
  34. serialize( data ) {
  35. super.serialize( data );
  36. data.scope = this.scope;
  37. data.scale = this.scale;
  38. }
  39. deserialize( data ) {
  40. super.deserialize( data );
  41. this.scope = data.scope;
  42. this.scale = data.scale;
  43. }
  44. }
  45. TimerNode.LOCAL = 'local';
  46. TimerNode.GLOBAL = 'global';
  47. TimerNode.DELTA = 'delta';
  48. TimerNode.FRAME = 'frame';
  49. export default TimerNode;