Skip to content

Commit c7f57a8

Browse files
authored
Inspector: Improve precision (#32007)
* improve fps * updates
1 parent 489b3cd commit c7f57a8

File tree

4 files changed

+70
-47
lines changed

4 files changed

+70
-47
lines changed

examples/jsm/inspector/Inspector.js

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import { Performance } from './tabs/Performance.js';
55
import { Console } from './tabs/Console.js';
66
import { Parameters } from './tabs/Parameters.js';
77
import { Viewer } from './tabs/Viewer.js';
8-
import { setText, ease, splitPath, splitCamelCase } from './ui/utils.js';
8+
import { setText, splitPath, splitCamelCase } from './ui/utils.js';
99

1010
import { QuadMesh, NodeMaterial, CanvasTarget, setConsoleFunction, REVISION, NoToneMapping } from 'three/webgpu';
1111
import { renderOutput, vec3, vec4 } from 'three/tsl';
1212

13-
const EASE_FACTOR = 0.1;
14-
1513
class Inspector extends RendererInspector {
1614

1715
constructor() {
@@ -40,9 +38,6 @@ class Inspector extends RendererInspector {
4038

4139
//
4240

43-
this.deltaTime = 0;
44-
this.softDeltaTime = 0;
45-
4641
this.statsData = new Map();
4742
this.canvasNodes = new Map();
4843
this.profiler = profiler;
@@ -60,7 +55,7 @@ class Inspector extends RendererInspector {
6055
},
6156
graph: {
6257
needsUpdate: false,
63-
duration: .05,
58+
duration: .02,
6459
time: 0
6560
}
6661
};
@@ -228,18 +223,29 @@ class Inspector extends RendererInspector {
228223

229224
data.cpu = stats.cpu;
230225
data.gpu = stats.gpu;
226+
data.stats = [];
231227

232228
data.initialized = true;
233229

234230
}
235231

236-
// TODO: Smooth values
232+
// store stats
233+
234+
if ( data.stats.length > this.maxFrames ) {
235+
236+
data.stats.shift();
237+
238+
}
239+
240+
data.stats.push( stats );
241+
242+
// compute averages
237243

238-
data.cpu = stats.cpu; // ease( .. )
239-
data.gpu = stats.gpu;
244+
data.cpu = this.getAverageDeltaTime( data, 'cpu' );
245+
data.gpu = this.getAverageDeltaTime( data, 'gpu' );
240246
data.total = data.cpu + data.gpu;
241247

242-
//
248+
// children
243249

244250
for ( const child of stats.children ) {
245251

@@ -330,6 +336,33 @@ class Inspector extends RendererInspector {
330336

331337
}
332338

339+
getAverageDeltaTime( statsData, property, frames = this.fps ) {
340+
341+
const statsArray = statsData.stats;
342+
343+
let sum = 0;
344+
let count = 0;
345+
346+
for ( let i = statsArray.length - 1; i >= 0 && count < frames; i -- ) {
347+
348+
const stats = statsArray[ i ];
349+
const value = stats[ property ];
350+
351+
if ( value > 0 ) {
352+
353+
// ignore invalid values
354+
355+
sum += value;
356+
count ++;
357+
358+
}
359+
360+
}
361+
362+
return sum / count;
363+
364+
}
365+
333366
resolveFrame( frame ) {
334367

335368
const nextFrame = this.getFrameById( frame.frameId + 1 );
@@ -367,21 +400,12 @@ class Inspector extends RendererInspector {
367400

368401
//
369402

370-
if ( this.softDeltaTime === 0 ) {
371-
372-
this.softDeltaTime = frame.deltaTime;
373-
374-
}
375-
376-
this.deltaTime = frame.deltaTime;
377-
this.softDeltaTime = ease( this.softDeltaTime, frame.deltaTime, this.nodeFrame.deltaTime, EASE_FACTOR );
378-
379403
this.updateCycle( this.displayCycle.text );
380404
this.updateCycle( this.displayCycle.graph );
381405

382406
if ( this.displayCycle.text.needsUpdate ) {
383407

384-
setText( 'fps-counter', this.softFPS.toFixed() );
408+
setText( 'fps-counter', this.fps.toFixed() );
385409

386410
this.performance.updateText( this, frame );
387411

@@ -398,18 +422,6 @@ class Inspector extends RendererInspector {
398422

399423
}
400424

401-
get fps() {
402-
403-
return 1000 / this.deltaTime;
404-
405-
}
406-
407-
get softFPS() {
408-
409-
return 1000 / this.softDeltaTime;
410-
411-
}
412-
413425
updateCycle( cycle ) {
414426

415427
cycle.time += this.nodeFrame.deltaTime;

examples/jsm/inspector/RendererInspector.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ObjectStats {
1111
this.timestamp = 0;
1212
this.cpu = 0;
1313
this.gpu = 0;
14+
this.fps = 0;
1415

1516
this.children = [];
1617
this.parent = null;
@@ -111,6 +112,8 @@ export class RendererInspector extends InspectorBase {
111112

112113
this.addFrame( frame );
113114

115+
this.fps = this._getFPS();
116+
114117
this.lastFrame = frame;
115118

116119
this.currentFrame = null;
@@ -121,6 +124,26 @@ export class RendererInspector extends InspectorBase {
121124

122125
}
123126

127+
_getFPS() {
128+
129+
let frameSum = 0;
130+
let timeSum = 0;
131+
132+
for ( let i = this.frames.length - 1; i >= 0; i -- ) {
133+
134+
const frame = this.frames[ i ];
135+
136+
frameSum ++;
137+
timeSum += frame.deltaTime;
138+
139+
if ( timeSum >= 1000 ) break;
140+
141+
}
142+
143+
return ( frameSum * 1000 ) / timeSum;
144+
145+
}
146+
124147
_createFrame() {
125148

126149
return {

examples/jsm/inspector/tabs/Performance.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class Performance extends Tab {
162162

163163
updateGraph( inspector/*, frame*/ ) {
164164

165-
this.graph.addPoint( 'fps', inspector.softFPS );
165+
this.graph.addPoint( 'fps', inspector.fps );
166166
this.graph.update();
167167

168168
}
@@ -243,7 +243,7 @@ class Performance extends Tab {
243243

244244
//
245245

246-
setText( 'graph-fps-counter', inspector.softFPS.toFixed() + ' FPS' );
246+
setText( 'graph-fps-counter', inspector.fps.toFixed() + ' FPS' );
247247

248248
//
249249

examples/jsm/inspector/ui/utils.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
export function ease( target, current, deltaTime, duration ) {
2-
3-
if ( duration <= 0 ) return current;
4-
5-
const t = Math.min( 1, deltaTime / duration );
6-
7-
target += ( current - target ) * t;
8-
9-
return target;
10-
11-
}
12-
131
export function createValueSpan( id = null ) {
142

153
const span = document.createElement( 'span' );

0 commit comments

Comments
 (0)