Skip to content

Commit 2533a59

Browse files
committed
Add THREE.js stats and fix rendering jitter
I've been getting frustrated by the jittery, difficult performance that RTT has shown recently. I already did some profiling in the `profile-performance` branch to start tracking it down, and that led to a major pathfinding improvement. This is all part of #13. To help track things down I added the THREE.js Stats visualiser [1]. To my complete surprise, I was not using `requestAnimationFrame` to schedule updates. Instead I was using `setTimeout` and aiming for 30fps. I think I found strange `requestAnimationFrame` behaviour when frames regularly took a long time to compute, but that is no longer the case. Switching back to `requestAnimationFrame` shows an enormous improvement in game experience! :) [1] https://www.npmjs.com/package/stats-js
1 parent e323d24 commit 2533a59

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"nanoid": "^3.1.12",
3535
"navmesh": "^2.0.3",
3636
"source-map-support": "^0.5.19",
37+
"stats-js": "1.0.1",
3738
"three": "0.120.1",
3839
"three.meshline": "v1.2.0",
3940
"tslib": "^2.0.1",

src/dependency_types.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// These definitions are for RTT. If considering re-using them, be aware
2+
// they probably miss important information that RTT doesn't need to know.
3+
14
type Point = [number, number];
25
type Edge = [number, number];
36

@@ -20,3 +23,13 @@ declare module "navmesh" {
2023
}
2124
export = NavMesh;
2225
}
26+
27+
declare module "stats-js" {
28+
class Stats {
29+
dom: HTMLElement;
30+
showPanel(panelNumber: number): void;
31+
begin(): void;
32+
end(): void;
33+
}
34+
export = Stats;
35+
}

src/main.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as THREE from 'three';
2+
import Stats from 'stats-js';
23
import * as rtt_engine from './rtt_engine';
34
import * as rtt_renderer from './rtt_renderer';
45
import { IAI, ExistingAI, AttackNearestAI, ExpansionAI } from './ai';
@@ -168,19 +169,20 @@ function main() {
168169
const rttSidebar = rttDiv.getElementsByClassName("game--sidebar")[0]! as HTMLElement;
169170
let gameRenderer = new rtt_renderer.GameRenderer(map, game, rttViewport, rttSidebar);
170171

171-
setInterval(() => {
172-
if (window.paused) {
173-
return;
174-
}
172+
const stats = new Stats();
173+
stats.showPanel(0);
174+
document.body.appendChild(stats.dom);
175175

176-
rtt_renderer.time("update", () => {
176+
const animate = () => {
177+
stats.begin();
178+
if (!window.paused) {
177179
game.update(context);
178-
});
179-
180-
rtt_renderer.time("update rendering", () => {
181180
gameRenderer.update();
182-
});
183-
}, 1000 / 30);
181+
}
182+
stats.end();
183+
requestAnimationFrame(animate);
184+
};
185+
requestAnimationFrame(animate);
184186
}
185187

186188
main();

src/rtt_renderer/ui.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class UI {
9898
}
9999

100100
update() {
101-
const timeElapsedInSeconds = this.game.updateCounter / 30;
101+
const timeElapsedInSeconds = this.game.updateCounter / 60;
102102
let minutesElapsed = Math.floor(timeElapsedInSeconds / 60);
103103
let secondsElapsed = Math.floor(timeElapsedInSeconds % 60);
104104
if (secondsElapsed == 60) {

0 commit comments

Comments
 (0)