NodeFrame.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { NodeUpdateType } from './constants.js';
  2. class NodeFrame {
  3. constructor() {
  4. this.time = 0;
  5. this.deltaTime = 0;
  6. this.frameId = 0;
  7. this.renderId = 0;
  8. this.startTime = null;
  9. this.frameMap = new WeakMap();
  10. this.frameBeforeMap = new WeakMap();
  11. this.renderMap = new WeakMap();
  12. this.renderBeforeMap = new WeakMap();
  13. this.renderer = null;
  14. this.material = null;
  15. this.camera = null;
  16. this.object = null;
  17. this.scene = null;
  18. }
  19. updateBeforeNode( node ) {
  20. const updateType = node.getUpdateBeforeType();
  21. if ( updateType === NodeUpdateType.FRAME ) {
  22. if ( this.frameBeforeMap.get( node ) !== this.frameId ) {
  23. this.frameBeforeMap.set( node, this.frameId );
  24. node.updateBefore( this );
  25. }
  26. } else if ( updateType === NodeUpdateType.RENDER ) {
  27. if ( this.renderBeforeMap.get( node ) !== this.renderId || this.frameBeforeMap.get( node ) !== this.frameId ) {
  28. this.renderBeforeMap.set( node, this.renderId );
  29. this.frameBeforeMap.set( node, this.frameId );
  30. node.updateBefore( this );
  31. }
  32. } else if ( updateType === NodeUpdateType.OBJECT ) {
  33. node.updateBefore( this );
  34. }
  35. }
  36. updateNode( node ) {
  37. const updateType = node.getUpdateType();
  38. if ( updateType === NodeUpdateType.FRAME ) {
  39. if ( this.frameMap.get( node ) !== this.frameId ) {
  40. this.frameMap.set( node, this.frameId );
  41. node.update( this );
  42. }
  43. } else if ( updateType === NodeUpdateType.RENDER ) {
  44. if ( this.renderMap.get( node ) !== this.renderId || this.frameMap.get( node ) !== this.frameId ) {
  45. this.renderMap.set( node, this.renderId );
  46. this.frameMap.set( node, this.frameId );
  47. node.update( this );
  48. }
  49. } else if ( updateType === NodeUpdateType.OBJECT ) {
  50. node.update( this );
  51. }
  52. }
  53. update() {
  54. this.frameId ++;
  55. if ( this.lastTime === undefined ) this.lastTime = performance.now();
  56. this.deltaTime = ( performance.now() - this.lastTime ) / 1000;
  57. this.lastTime = performance.now();
  58. this.time += this.deltaTime;
  59. }
  60. }
  61. export default NodeFrame;