NodeFrame.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.renderer = null;
  12. this.material = null;
  13. this.camera = null;
  14. this.object = null;
  15. this.scene = null;
  16. }
  17. _getMaps( referenceMap, nodeRef ) {
  18. let maps = referenceMap.get( nodeRef );
  19. if ( maps === undefined ) {
  20. maps = {
  21. renderMap: new WeakMap(),
  22. frameMap: new WeakMap()
  23. };
  24. referenceMap.set( nodeRef, maps );
  25. }
  26. return maps;
  27. }
  28. updateBeforeNode( node ) {
  29. const updateType = node.getUpdateBeforeType();
  30. const reference = node.updateReference( this );
  31. const { frameMap, renderMap } = this._getMaps( this.updateBeforeMap, reference );
  32. if ( updateType === NodeUpdateType.FRAME ) {
  33. if ( frameMap.get( node ) !== this.frameId ) {
  34. frameMap.set( node, this.frameId );
  35. node.updateBefore( this );
  36. }
  37. } else if ( updateType === NodeUpdateType.RENDER ) {
  38. if ( renderMap.get( node ) !== this.renderId || frameMap.get( node ) !== this.frameId ) {
  39. renderMap.set( node, this.renderId );
  40. frameMap.set( node, this.frameId );
  41. node.updateBefore( this );
  42. }
  43. } else if ( updateType === NodeUpdateType.OBJECT ) {
  44. node.updateBefore( this );
  45. }
  46. }
  47. updateNode( node ) {
  48. const updateType = node.getUpdateType();
  49. const reference = node.updateReference( this );
  50. const { frameMap, renderMap } = this._getMaps( this.updateMap, reference );
  51. if ( updateType === NodeUpdateType.FRAME ) {
  52. if ( frameMap.get( node ) !== this.frameId ) {
  53. frameMap.set( node, this.frameId );
  54. node.update( this );
  55. }
  56. } else if ( updateType === NodeUpdateType.RENDER ) {
  57. if ( renderMap.get( node ) !== this.renderId || frameMap.get( node ) !== this.frameId ) {
  58. renderMap.set( node, this.renderId );
  59. frameMap.set( node, this.frameId );
  60. node.update( this );
  61. }
  62. } else if ( updateType === NodeUpdateType.OBJECT ) {
  63. node.update( this );
  64. }
  65. }
  66. update() {
  67. this.frameId ++;
  68. if ( this.lastTime === undefined ) this.lastTime = performance.now();
  69. this.deltaTime = ( performance.now() - this.lastTime ) / 1000;
  70. this.lastTime = performance.now();
  71. this.time += this.deltaTime;
  72. }
  73. }
  74. export default NodeFrame;