NodeFrame.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if ( updateType === NodeUpdateType.FRAME ) {
  32. const { frameMap } = this._getMaps( this.updateBeforeMap, reference );
  33. if ( frameMap.get( reference ) !== this.frameId ) {
  34. if ( node.updateBefore( this ) !== false ) {
  35. frameMap.set( reference, this.frameId );
  36. }
  37. }
  38. } else if ( updateType === NodeUpdateType.RENDER ) {
  39. const { renderMap } = this._getMaps( this.updateBeforeMap, reference );
  40. if ( renderMap.get( reference ) !== this.renderId ) {
  41. if ( node.updateBefore( this ) !== false ) {
  42. renderMap.set( reference, this.renderId );
  43. }
  44. }
  45. } else if ( updateType === NodeUpdateType.OBJECT ) {
  46. node.updateBefore( this );
  47. }
  48. }
  49. updateNode( node ) {
  50. const updateType = node.getUpdateType();
  51. const reference = node.updateReference( this );
  52. if ( updateType === NodeUpdateType.FRAME ) {
  53. const { frameMap } = this._getMaps( this.updateMap, reference );
  54. if ( frameMap.get( reference ) !== this.frameId ) {
  55. if ( node.update( this ) !== false ) {
  56. frameMap.set( reference, this.frameId );
  57. }
  58. }
  59. } else if ( updateType === NodeUpdateType.RENDER ) {
  60. const { renderMap } = this._getMaps( this.updateMap, reference );
  61. if ( renderMap.get( reference ) !== this.renderId ) {
  62. if ( node.update( this ) !== false ) {
  63. renderMap.set( reference, this.renderId );
  64. }
  65. }
  66. } else if ( updateType === NodeUpdateType.OBJECT ) {
  67. node.update( this );
  68. }
  69. }
  70. update() {
  71. this.frameId ++;
  72. if ( this.lastTime === undefined ) this.lastTime = performance.now();
  73. this.deltaTime = ( performance.now() - this.lastTime ) / 1000;
  74. this.lastTime = performance.now();
  75. this.time += this.deltaTime;
  76. }
  77. }
  78. export default NodeFrame;