123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- import { NodeUpdateType } from './constants.js';
- class NodeFrame {
- constructor() {
- this.time = 0;
- this.deltaTime = 0;
- this.frameId = 0;
- this.renderId = 0;
- this.startTime = null;
- this.updateMap = new WeakMap();
- this.updateBeforeMap = new WeakMap();
- this.updateAfterMap = new WeakMap();
- this.renderer = null;
- this.material = null;
- this.camera = null;
- this.object = null;
- this.scene = null;
- }
- _getMaps( referenceMap, nodeRef ) {
- let maps = referenceMap.get( nodeRef );
- if ( maps === undefined ) {
- maps = {
- renderMap: new WeakMap(),
- frameMap: new WeakMap()
- };
- referenceMap.set( nodeRef, maps );
- }
- return maps;
- }
- updateBeforeNode( node ) {
- const updateType = node.getUpdateBeforeType();
- const reference = node.updateReference( this );
- if ( updateType === NodeUpdateType.FRAME ) {
- const { frameMap } = this._getMaps( this.updateBeforeMap, reference );
- if ( frameMap.get( reference ) !== this.frameId ) {
- if ( node.updateBefore( this ) !== false ) {
- frameMap.set( reference, this.frameId );
- }
- }
- } else if ( updateType === NodeUpdateType.RENDER ) {
- const { renderMap } = this._getMaps( this.updateBeforeMap, reference );
- if ( renderMap.get( reference ) !== this.renderId ) {
- if ( node.updateBefore( this ) !== false ) {
- renderMap.set( reference, this.renderId );
- }
- }
- } else if ( updateType === NodeUpdateType.OBJECT ) {
- node.updateBefore( this );
- }
- }
- updateAfterNode( node ) {
- const updateType = node.getUpdateAfterType();
- const reference = node.updateReference( this );
- if ( updateType === NodeUpdateType.FRAME ) {
- const { frameMap } = this._getMaps( this.updateAfterMap, reference );
- if ( frameMap.get( reference ) !== this.frameId ) {
- if ( node.updateAfter( this ) !== false ) {
- frameMap.set( reference, this.frameId );
- }
- }
- } else if ( updateType === NodeUpdateType.RENDER ) {
- const { renderMap } = this._getMaps( this.updateAfterMap, reference );
- if ( renderMap.get( reference ) !== this.renderId ) {
- if ( node.updateAfter( this ) !== false ) {
- renderMap.set( reference, this.renderId );
- }
- }
- } else if ( updateType === NodeUpdateType.OBJECT ) {
- node.updateAfter( this );
- }
- }
- updateNode( node ) {
- const updateType = node.getUpdateType();
- const reference = node.updateReference( this );
- if ( updateType === NodeUpdateType.FRAME ) {
- const { frameMap } = this._getMaps( this.updateMap, reference );
- if ( frameMap.get( reference ) !== this.frameId ) {
- if ( node.update( this ) !== false ) {
- frameMap.set( reference, this.frameId );
- }
- }
- } else if ( updateType === NodeUpdateType.RENDER ) {
- const { renderMap } = this._getMaps( this.updateMap, reference );
- if ( renderMap.get( reference ) !== this.renderId ) {
- if ( node.update( this ) !== false ) {
- renderMap.set( reference, this.renderId );
- }
- }
- } else if ( updateType === NodeUpdateType.OBJECT ) {
- node.update( this );
- }
- }
- update() {
- this.frameId ++;
- if ( this.lastTime === undefined ) this.lastTime = performance.now();
- this.deltaTime = ( performance.now() - this.lastTime ) / 1000;
- this.lastTime = performance.now();
- this.time += this.deltaTime;
- }
- }
- export default NodeFrame;
|