diff --git a/cocos/core/director.ts b/cocos/core/director.ts index 17e5cd4453..15bfa4260e 100644 --- a/cocos/core/director.ts +++ b/cocos/core/director.ts @@ -130,7 +130,7 @@ class Director extends EventTarget { * @property {String} EVENT_BEFORE_SCENE_LOADING * @readonly */ - public static EVENT_BEFORE_SCENE_LOADING = 'director_before_scene_loading'; + public static readonly EVENT_BEFORE_SCENE_LOADING = 'director_before_scene_loading'; /* * @en The event which will be triggered before launching a new scene. @@ -144,7 +144,7 @@ class Director extends EventTarget { * @property {String} EVENT_BEFORE_SCENE_LAUNCH * @readonly */ - public static EVENT_BEFORE_SCENE_LAUNCH = 'director_before_scene_launch'; + public static readonly EVENT_BEFORE_SCENE_LAUNCH = 'director_before_scene_launch'; /** * @en The event which will be triggered after launching a new scene. @@ -158,7 +158,7 @@ class Director extends EventTarget { * @property {String} EVENT_AFTER_SCENE_LAUNCH * @readonly */ - public static EVENT_AFTER_SCENE_LAUNCH = 'director_after_scene_launch'; + public static readonly EVENT_AFTER_SCENE_LAUNCH = 'director_after_scene_launch'; /** * @en The event which will be triggered at the beginning of every frame. @@ -171,7 +171,7 @@ class Director extends EventTarget { * @property {String} EVENT_BEFORE_UPDATE * @readonly */ - public static EVENT_BEFORE_UPDATE = 'director_before_update'; + public static readonly EVENT_BEFORE_UPDATE = 'director_before_update'; /** * @en The event which will be triggered after engine and components update logic. @@ -184,7 +184,7 @@ class Director extends EventTarget { * @property {String} EVENT_AFTER_UPDATE * @readonly */ - public static EVENT_AFTER_UPDATE = 'director_after_update'; + public static readonly EVENT_AFTER_UPDATE = 'director_after_update'; /** * @en The event which will be triggered before the rendering process. @@ -197,7 +197,7 @@ class Director extends EventTarget { * @property {String} EVENT_BEFORE_DRAW * @readonly */ - public static EVENT_BEFORE_DRAW = 'director_before_draw'; + public static readonly EVENT_BEFORE_DRAW = 'director_before_draw'; /** * @en The event which will be triggered after the rendering process. @@ -210,7 +210,15 @@ class Director extends EventTarget { * @property {String} EVENT_AFTER_DRAW * @readonly */ - public static EVENT_AFTER_DRAW = 'director_after_draw'; + public static readonly EVENT_AFTER_DRAW = 'director_after_draw'; + + /** + * @en The event which will be triggered before the physics process. + * @zh 物理过程之前所触发的事件。 + * @event cc.Director.EVENT_BEFORE_PHYSICS + * @readonly + */ + public static readonly EVENT_BEFORE_PHYSICS = 'director_before_physics'; private invalid: boolean; private _paused: boolean; @@ -264,11 +272,11 @@ class Director extends EventTarget { this._systems = []; const self = this; - cc.game.on (Game.EVENT_SHOW, () => { + cc.game.on(Game.EVENT_SHOW, () => { self._lastUpdate = performance.now(); }); - cc.game.once (Game.EVENT_ENGINE_INITED, this.init, this); + cc.game.once(Game.EVENT_ENGINE_INITED, this.init, this); } public init () { @@ -540,18 +548,18 @@ class Director extends EventTarget { // Scene cannot be cached in loader, because it will be destroyed after switching. cc.loader.removeItem(uuid); - if ( CC_BUILD && CC_DEBUG ) { + if (CC_BUILD && CC_DEBUG) { console.time('InitScene'); } scene._load(); // ensure scene initialized - if ( CC_BUILD && CC_DEBUG ) { + if (CC_BUILD && CC_DEBUG) { console.timeEnd('InitScene'); } // Re-attach or replace persist nodes - if ( CC_BUILD && CC_DEBUG ) { + if (CC_BUILD && CC_DEBUG) { console.time('AttachPersist'); } - const persistNodeList = Object.keys(cc.game._persistRootNodes).map( (x) => { + const persistNodeList = Object.keys(cc.game._persistRootNodes).map((x) => { return cc.game._persistRootNodes[x]; }); // tslint:disable-next-line: prefer-for-of @@ -569,24 +577,24 @@ class Director extends EventTarget { node.parent = scene; } } - if ( CC_BUILD && CC_DEBUG ) { + if (CC_BUILD && CC_DEBUG) { console.timeEnd('AttachPersist'); } const oldScene = this._scene; if (!CC_EDITOR) { // auto release assets - if ( CC_BUILD && CC_DEBUG ) { + if (CC_BUILD && CC_DEBUG) { console.time('AutoRelease'); } const autoReleaseAssets = oldScene && oldScene.autoReleaseAssets && oldScene.dependAssets; autoRelease(autoReleaseAssets, scene.dependAssets, persistNodeList); - if ( CC_BUILD && CC_DEBUG ){ + if (CC_BUILD && CC_DEBUG) { console.timeEnd('AutoRelease'); } } // unload scene - if ( CC_BUILD && CC_DEBUG ) { + if (CC_BUILD && CC_DEBUG) { console.time('Destroy'); } if (cc.isValid(oldScene)) { @@ -607,11 +615,11 @@ class Director extends EventTarget { // Run an Entity Scene this._scene = scene; - if ( CC_BUILD && CC_DEBUG ) { + if (CC_BUILD && CC_DEBUG) { console.time('Activate'); } scene._activate(); - if ( CC_BUILD && CC_DEBUG ) { + if (CC_BUILD && CC_DEBUG) { console.timeEnd('Activate'); } // start scene @@ -1056,7 +1064,7 @@ class Director extends EventTarget { // Update if (!this._paused) { - this.emit(cc.Director.EVENT_BEFORE_UPDATE); + this.emit(Director.EVENT_BEFORE_UPDATE); // Call start for new added components this._compScheduler.startPhase(); // Update for components @@ -1066,18 +1074,18 @@ class Director extends EventTarget { // Late update for components this._compScheduler.lateUpdatePhase(this._deltaTime); // User can use this event to do things after update - this.emit(cc.Director.EVENT_AFTER_UPDATE); + this.emit(Director.EVENT_AFTER_UPDATE); // Destroy entities that have been removed recently CCObject._deferredDestroy(); } - + this.emit(Director.EVENT_BEFORE_PHYSICS); this._physicsSystem!.update(this._deltaTime); - this.emit(cc.Director.EVENT_BEFORE_DRAW); + this.emit(Director.EVENT_BEFORE_DRAW); this._root!.frameMove(this._deltaTime); // Present current frame this._root!.device.present(); - this.emit(cc.Director.EVENT_AFTER_DRAW); + this.emit(Director.EVENT_AFTER_DRAW); eventManager.frameUpdateListeners(); if (this._scene) { this._scene.resetHasChanged(); } diff --git a/cocos/core/utils/profiler/profiler.ts b/cocos/core/utils/profiler/profiler.ts index 58b8861625..eb1602eae7 100644 --- a/cocos/core/utils/profiler/profiler.ts +++ b/cocos/core/utils/profiler/profiler.ts @@ -40,6 +40,8 @@ interface IProfilerState { logic: ICounterOption; render: ICounterOption; mode: ICounterOption; + physics: ICounterOption; + deferredDestory: ICounterOption; } let _showFPS = false; @@ -145,12 +147,14 @@ function generateStats () { const now = performance.now(); const opts = { - frame: { desc: 'Frame time (ms)', min: 0, max: 50, average: 500 }, + frame: { desc: 'Frame time (s)', min: 0, max: 50, average: 500 }, fps: { desc: 'Framerate (FPS)', below: 30, average: 500 }, draws: { desc: 'Draw call' }, tricount: { desc: 'Triangle' }, - logic: { desc: 'Game Logic (ms)', min: 0, max: 50, average: 500, color: '#080' }, - render: { desc: 'Renderer (ms)', min: 0, max: 50, average: 500, color: '#f90' }, + logic: { desc: 'Game Logic (s)', min: 0, max: 50, average: 500, color: '#080' }, + deferredDestory: { desc: 'Deferred Destory (s)', min: 0, max: 50, average: 500 }, + physics: { desc: 'Physics (s)', min: 0, max: 50, average: 500 }, + render: { desc: 'Renderer (s)', min: 0, max: 50, average: 500, color: '#f90' }, mode: { desc: cc.game.renderType === cc.game.RENDER_TYPE_WEBGL ? 'WebGL' : 'Canvas', min: 1 }, }; @@ -240,13 +244,33 @@ function afterUpdate () { } else { getCounter('logic').end(now); } - getCounter('render').start(now); + getCounter('deferredDestory').start(now); } // function updateLabel (stat: IProfilerStateOption) { // stat.label.string = stat.desc + ' ' + stat.counter.human(); // } +function beforePhysics (){ + if (!_stats){ + return; + } + + const now = performance.now(); + getCounter('deferredDestory').end(now); + getCounter('physics').start(now); +} + +function beforeDraw (){ + if (!_stats){ + return; + } + + const now = performance.now(); + getCounter('physics').end(now); + getCounter('render').start(now); +} + function afterDraw () { if (!_stats){ return; @@ -293,6 +317,8 @@ export const profiler = { cc.director.off(cc.Director.EVENT_BEFORE_UPDATE, beforeUpdate); cc.director.off(cc.Director.EVENT_AFTER_UPDATE, afterUpdate); + cc.director.off(cc.Director.EVENT_BEFORE_PHYSICS, beforePhysics); + cc.director.off(cc.Director.EVENT_BEFORE_DRAW, beforeDraw); cc.director.off(cc.Director.EVENT_AFTER_DRAW, afterDraw); _showFPS = false; } @@ -310,6 +336,8 @@ export const profiler = { cc.director.on(cc.Director.EVENT_BEFORE_UPDATE, beforeUpdate); cc.director.on(cc.Director.EVENT_AFTER_UPDATE, afterUpdate); + cc.director.on(cc.Director.EVENT_BEFORE_PHYSICS, beforePhysics); + cc.director.on(cc.Director.EVENT_BEFORE_DRAW, beforeDraw); cc.director.on(cc.Director.EVENT_AFTER_DRAW, afterDraw); _showFPS = true; }