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