NodeFrame.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.updateMap = new WeakMap();
  10. this.updateBeforeMap = new WeakMap();
  11. this.updateAfterMap = new WeakMap();
  12. this.renderer = null;
  13. this.material = null;
  14. this.camera = null;
  15. this.object = null;
  16. this.scene = null;
  17. }
  18. _getMaps( referenceMap, nodeRef ) {
  19. let maps = referenceMap.get( nodeRef );
  20. if ( maps === undefined ) {
  21. maps = {
  22. renderMap: new WeakMap(),
  23. frameMap: new WeakMap()
  24. };
  25. referenceMap.set( nodeRef, maps );
  26. }
  27. return maps;
  28. }
  29. updateBeforeNode( node ) {
  30. const updateType = node.getUpdateBeforeType();
  31. const reference = node.updateReference( this );
  32. if ( updateType === NodeUpdateType.FRAME ) {
  33. const { frameMap } = this._getMaps( this.updateBeforeMap, reference );
  34. if ( frameMap.get( reference ) !== this.frameId ) {
  35. if ( node.updateBefore( this ) !== false ) {
  36. frameMap.set( reference, this.frameId );
  37. }
  38. }
  39. } else if ( updateType === NodeUpdateType.RENDER ) {
  40. const { renderMap } = this._getMaps( this.updateBeforeMap, reference );
  41. if ( renderMap.get( reference ) !== this.renderId ) {
  42. if ( node.updateBefore( this ) !== false ) {
  43. renderMap.set( reference, this.renderId );
  44. }
  45. }
  46. } else if ( updateType === NodeUpdateType.OBJECT ) {
  47. node.updateBefore( this );
  48. }
  49. }
  50. updateAfterNode( node ) {
  51. const updateType = node.getUpdateAfterType();
  52. const reference = node.updateReference( this );
  53. if ( updateType === NodeUpdateType.FRAME ) {
  54. const { frameMap } = this._getMaps( this.updateAfterMap, reference );
  55. if ( frameMap.get( reference ) !== this.frameId ) {
  56. if ( node.updateAfter( this ) !== false ) {
  57. frameMap.set( reference, this.frameId );
  58. }
  59. }
  60. } else if ( updateType === NodeUpdateType.RENDER ) {
  61. const { renderMap } = this._getMaps( this.updateAfterMap, reference );
  62. if ( renderMap.get( reference ) !== this.renderId ) {
  63. if ( node.updateAfter( this ) !== false ) {
  64. renderMap.set( reference, this.renderId );
  65. }
  66. }
  67. } else if ( updateType === NodeUpdateType.OBJECT ) {
  68. node.updateAfter( this );
  69. }
  70. }
  71. updateNode( node ) {
  72. const updateType = node.getUpdateType();
  73. const reference = node.updateReference( this );
  74. if ( updateType === NodeUpdateType.FRAME ) {
  75. const { frameMap } = this._getMaps( this.updateMap, reference );
  76. if ( frameMap.get( reference ) !== this.frameId ) {
  77. if ( node.update( this ) !== false ) {
  78. frameMap.set( reference, this.frameId );
  79. }
  80. }
  81. } else if ( updateType === NodeUpdateType.RENDER ) {
  82. const { renderMap } = this._getMaps( this.updateMap, reference );
  83. if ( renderMap.get( reference ) !== this.renderId ) {
  84. if ( node.update( this ) !== false ) {
  85. renderMap.set( reference, this.renderId );
  86. }
  87. }
  88. } else if ( updateType === NodeUpdateType.OBJECT ) {
  89. node.update( this );
  90. }
  91. }
  92. update() {
  93. this.frameId ++;
  94. if ( this.lastTime === undefined ) this.lastTime = performance.now();
  95. this.deltaTime = ( performance.now() - this.lastTime ) / 1000;
  96. this.lastTime = performance.now();
  97. this.time += this.deltaTime;
  98. }
  99. }
  100. export default NodeFrame;