TimerNode.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import UniformNode from '../core/UniformNode.js';
  2. import { NodeUpdateType } from '../core/constants.js';
  3. import { nodeObject, nodeImmutable } from '../shadernode/ShaderNode.js';
  4. import { addNodeClass } from '../core/Node.js';
  5. class TimerNode extends UniformNode {
  6. constructor( scope = TimerNode.LOCAL, scale = 1, value = 0 ) {
  7. super( value );
  8. this.scope = scope;
  9. this.scale = scale;
  10. this.updateType = NodeUpdateType.FRAME;
  11. }
  12. /*
  13. @TODO:
  14. getNodeType( builder ) {
  15. const scope = this.scope;
  16. if ( scope === TimerNode.FRAME ) {
  17. return 'uint';
  18. }
  19. return 'float';
  20. }
  21. */
  22. update( frame ) {
  23. const scope = this.scope;
  24. const scale = this.scale;
  25. if ( scope === TimerNode.LOCAL ) {
  26. this.value += frame.deltaTime * scale;
  27. } else if ( scope === TimerNode.DELTA ) {
  28. this.value = frame.deltaTime * scale;
  29. } else if ( scope === TimerNode.FRAME ) {
  30. this.value = frame.frameId;
  31. } else {
  32. // global
  33. this.value = frame.time * scale;
  34. }
  35. }
  36. serialize( data ) {
  37. super.serialize( data );
  38. data.scope = this.scope;
  39. data.scale = this.scale;
  40. }
  41. deserialize( data ) {
  42. super.deserialize( data );
  43. this.scope = data.scope;
  44. this.scale = data.scale;
  45. }
  46. }
  47. TimerNode.LOCAL = 'local';
  48. TimerNode.GLOBAL = 'global';
  49. TimerNode.DELTA = 'delta';
  50. TimerNode.FRAME = 'frame';
  51. export default TimerNode;
  52. // @TODO: add support to use node in timeScale
  53. export const timerLocal = ( timeScale, value = 0 ) => nodeObject( new TimerNode( TimerNode.LOCAL, timeScale, value ) );
  54. export const timerGlobal = ( timeScale, value = 0 ) => nodeObject( new TimerNode( TimerNode.GLOBAL, timeScale, value ) );
  55. export const timerDelta = ( timeScale, value = 0 ) => nodeObject( new TimerNode( TimerNode.DELTA, timeScale, value ) );
  56. export const frameId = nodeImmutable( TimerNode, TimerNode.FRAME ).toUint();
  57. addNodeClass( 'TimerNode', TimerNode );