Skip to content

Commit

Permalink
Merge pull request #130 from ansonmiu0214/debug/fps-indicator
Browse files Browse the repository at this point in the history
Add FPS indicator to debug mode
  • Loading branch information
JamesWP committed Nov 27, 2020
2 parents fd70564 + b252bec commit aa0b752
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
"homepage": "https://github.com/FRUK-Simulator/SimulatorCore#readme",
"dependencies": {
"planck-js": "^0.3.18",
"stats.js": "^0.17.0",
"three": "^0.117.1",
"uuid": "^8.3.0"
},
"devDependencies": {
"@types/dat.gui": "^0.7.5",
"@types/faker": "^4.1.12",
"@types/jest": "^26.0.14",
"@types/stats.js": "^0.17.0",
"@types/uuid": "^8.3.0",
"@types/webpack": "^4.41.22",
"@typescript-eslint/eslint-plugin": "^3.10.1",
Expand Down
20 changes: 20 additions & 0 deletions src/engine/Sim3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { ZoneHandle } from "./handles/ZoneHandle";

import { EventEmitter } from "events";
import { wallSpecs } from "./utils/WallUtil";
import Stats from "stats.js";

interface ISimObjectContainer {
type: string;
Expand Down Expand Up @@ -68,6 +69,8 @@ export class Sim3D extends EventEmitter {
private renderer: THREE.Renderer;

private debugMesh: THREE.Mesh;
private perfMonitor: Stats;
private fpsIndicator?: Node;

private camera: THREE.PerspectiveCamera;
private cameraControls: OrbitControls;
Expand Down Expand Up @@ -100,6 +103,9 @@ export class Sim3D extends EventEmitter {

this.config = config;

this.perfMonitor = new Stats();
this.perfMonitor.showPanel(0); // 0: fps, 1: ms, 2: mb, 3+: custom

this.resetSimulator();
}

Expand Down Expand Up @@ -153,11 +159,19 @@ export class Sim3D extends EventEmitter {
const dt = (time - this.lastAnimateTime) / 1000;
this.lastAnimateTime = time;

if (this.isDebugMode()) {
this.perfMonitor.begin();
}

window.requestAnimationFrame(r);
if (this.physicsActive) {
this.updatePhysics(dt);
}
this.render(dt);

if (this.isDebugMode()) {
this.perfMonitor.end();
}
};

this.isRendering = true;
Expand Down Expand Up @@ -301,6 +315,12 @@ export class Sim3D extends EventEmitter {

setDebugMode(enabled: boolean): void {
this.debugMesh.visible = enabled;
if (enabled) {
this.fpsIndicator = document.body.appendChild(this.perfMonitor.dom);
} else {
document.body.removeChild(this.fpsIndicator!);
this.fpsIndicator = undefined;
}
}

setCameraMode(spec: CameraModeSpec): void {
Expand Down

0 comments on commit aa0b752

Please sign in to comment.