Refactored rStats to its own module, added config option for displaying stats

This commit is contained in:
Paul Graffam 2018-02-25 19:30:01 -05:00
parent 4b584783d7
commit 4ef5184b16
4 changed files with 101 additions and 59 deletions

View File

@ -0,0 +1,55 @@
// Local vars for rStats
let rS, bS, glS, tS;
export default class Stats {
constructor(renderer) {
this.renderer = renderer;
}
setUp() {
bS = new BrowserStats();
glS = new glStats();
tS = new threeStats(this.renderer.threeRenderer);
rS = new rStats({
CSSPath: './assets/css/',
userTimingAPI: true,
values: {
frame: { caption: 'Total frame time (ms)', over: 16, average: true, avgMs: 100 },
fps: { caption: 'Framerate (FPS)', below: 30 },
calls: { caption: 'Calls (three.js)', over: 3000 },
raf: { caption: 'Time since last rAF (ms)', average: true, avgMs: 100 },
rstats: { caption: 'rStats update (ms)', average: true, avgMs: 100 },
texture: { caption: 'GenTex', average: true, avgMs: 100 }
},
groups: [
{ caption: 'Framerate', values: ['fps', 'raf'] },
{ caption: 'Frame Budget', values: ['frame', 'texture', 'setup', 'render'] }
],
fractions: [
{ base: 'frame', steps: ['texture', 'setup', 'render'] }
],
plugins: [bS, tS, glS]
});
};
static start() {
rS('frame').start();
glS.start();
rS('rAF').tick();
rS('FPS').frame();
rS('render').start();
};
static end() {
rS('render').end(); // render finished
rS('frame').end(); // frame finished
// Local rStats update
rS('rStats').start();
rS().update();
rS('rStats').end();
};
}

View File

@ -11,6 +11,7 @@ import Controls from './components/controls';
// Helpers // Helpers
import Geometry from './helpers/geometry'; import Geometry from './helpers/geometry';
import Stats from './helpers/stats';
// Model // Model
import Texture from './model/texture'; import Texture from './model/texture';
@ -24,10 +25,6 @@ import DatGUI from './managers/datGUI';
import Config from './../data/config'; import Config from './../data/config';
// -- End of imports // -- End of imports
// Local vars for rStats
let rS, bS, glS, tS;
// This class instantiates and ties all of the components together, starts the loading process and renders the main loop // This class instantiates and ties all of the components together, starts the loading process and renders the main loop
export default class Main { export default class Main {
constructor(container) { constructor(container) {
@ -46,51 +43,27 @@ export default class Main {
Config.dpr = window.devicePixelRatio; Config.dpr = window.devicePixelRatio;
} }
// Main renderer instantiation // Main renderer constructor
this.renderer = new Renderer(this.scene, container); this.renderer = new Renderer(this.scene, container);
// Components instantiation // Components instantiations
this.camera = new Camera(this.renderer.threeRenderer); this.camera = new Camera(this.renderer.threeRenderer);
this.controls = new Controls(this.camera.threeCamera, container); this.controls = new Controls(this.camera.threeCamera, container);
this.light = new Light(this.scene); this.light = new Light(this.scene);
// Create and place lights in scene // Create and place lights in scene
const lights = ['ambient', 'directional', 'point', 'hemi']; const lights = ['ambient', 'directional', 'point', 'hemi'];
for(let i = 0; i < lights.length; i++) { lights.forEach((light) => this.light.place(light));
this.light.place(lights[i]);
}
// Create and place geo in scene // Create and place geo in scene
this.geometry = new Geometry(this.scene); this.geometry = new Geometry(this.scene);
this.geometry.make('plane')(150, 150, 10, 10); this.geometry.make('plane')(150, 150, 10, 10);
this.geometry.place([0, -20, 0], [Math.PI/2, 0, 0]); this.geometry.place([0, -20, 0], [Math.PI / 2, 0, 0]);
// Set up rStats if dev environment // Set up rStats if dev environment
if(Config.isDev) { if(Config.isDev && Config.isShowingStats) {
bS = new BrowserStats(); this.stats = new Stats(this.renderer);
glS = new glStats(); this.stats.setUp();
tS = new threeStats(this.renderer.threeRenderer);
rS = new rStats({
CSSPath: './assets/css/',
userTimingAPI: true,
values: {
frame: { caption: 'Total frame time (ms)', over: 16, average: true, avgMs: 100 },
fps: { caption: 'Framerate (FPS)', below: 30 },
calls: { caption: 'Calls (three.js)', over: 3000 },
raf: { caption: 'Time since last rAF (ms)', average: true, avgMs: 100 },
rstats: { caption: 'rStats update (ms)', average: true, avgMs: 100 },
texture: { caption: 'GenTex', average: true, avgMs: 100 }
},
groups: [
{ caption: 'Framerate', values: [ 'fps', 'raf' ] },
{ caption: 'Frame Budget', values: [ 'frame', 'texture', 'setup', 'render' ] }
],
fractions: [
{ base: 'frame', steps: [ 'texture', 'setup', 'render' ] }
],
plugins: [bS, tS, glS]
});
} }
// Instantiate texture class // Instantiate texture class
@ -131,34 +104,22 @@ export default class Main {
render() { render() {
// Render rStats if Dev // Render rStats if Dev
if(Config.isDev) { if(Config.isDev && Config.isShowingStats) {
rS('frame').start(); Stats.start();
glS.start();
rS('rAF').tick();
rS('FPS').frame();
rS('render').start();
} }
// Call render function and pass in created scene and camera // Call render function and pass in created scene and camera
this.renderer.render(this.scene, this.camera.threeCamera); this.renderer.render(this.scene, this.camera.threeCamera);
// rStats has finished determining render call now // rStats has finished determining render call now
if(Config.isDev) { if(Config.isDev && Config.isShowingStats) {
rS('render').end(); // render finished Stats.end();
rS('frame').end(); // frame finished
// Local rStats update
rS('rStats').start();
rS().update();
rS('rStats').end();
} }
// Delta time is sometimes needed for certain updates // Delta time is sometimes needed for certain updates
//const delta = this.clock.getDelta(); //const delta = this.clock.getDelta();
// Call any vendor or module updates here // Call any vendor or module frame updates here
TWEEN.update(); TWEEN.update();
this.controls.threeControls.update(); this.controls.threeControls.update();

View File

@ -3,6 +3,7 @@ import TWEEN from 'tween.js';
// This object contains the state of the app // This object contains the state of the app
export default { export default {
isDev: false, isDev: false,
isShowingStats: true,
isLoaded: false, isLoaded: false,
isTweening: false, isTweening: false,
isRotating: true, isRotating: true,

View File

@ -32,6 +32,32 @@
} )(); } )();
/**
* @class rStats
* @param {rStats~Settings} [settings] Settings for the rStats instance.
*/
/**
* @typedef {Object} rStats~Settings
* @property {Array.<String>} [colours] An array of CSS colour values.
* @property {String} [CSSPath=''] Base URL where rStats.css is located.
* @property {Array.<String>} [css] URLs of CSS or font files to import.
* @property {Object.<String, rStats~CounterProperties>} [values] Properties to use for each counter.
* @property {Array.<Object>} [groups] Define groups of counters.
* @property {Array.<Object>} [fractions] Define stacked counters.
* @property {Array.<Object>} [plugins] Additional plugins.
*/
/**
* @typedef {Object} rStats~CounterProperties
* @property {String} [caption] Caption for this counter.
* @property {Boolean} [average=false] Whether the values should be averaged.
* @property {Number} [avgMs=1000] Duration for which the values should be averaged.
* @property {Number} [below] Value below which the graph should be highlighted.
* @property {Number} [over] Value over which the graph should be highlighted.
* @property {Boolean} [interpolate=true] Whether framerate should be interpolated.
*/
window.rStats = function rStats ( settings ) { window.rStats = function rStats ( settings ) {
function iterateKeys ( array, callback ) { function iterateKeys ( array, callback ) {
@ -194,8 +220,6 @@ window.rStats = function rStats ( settings ) {
_graph = new Graph( _dom, _id, _def ), _graph = new Graph( _dom, _id, _def ),
_started = false; _started = false;
_dom.className = 'rs-counter-base';
_spanId.className = 'rs-counter-id'; _spanId.className = 'rs-counter-id';
_spanId.textContent = ( _def && _def.caption ) ? _def.caption : _id; _spanId.textContent = ( _def && _def.caption ) ? _def.caption : _id;
@ -250,7 +274,8 @@ window.rStats = function rStats ( settings ) {
_spanValueText.nodeValue = Math.round( v * 100 ) / 100; _spanValueText.nodeValue = Math.round( v * 100 ) / 100;
var a = ( _def && ( ( _def.below && _value < _def.below ) || ( _def.over && _value > _def.over ) ) ); var a = ( _def && ( ( _def.below && _value < _def.below ) || ( _def.over && _value > _def.over ) ) );
_graph.draw( _value, a ); _graph.draw( _value, a );
_dom.style.color = a ? '#b70000' : '#ffffff'; _dom.className = a ? 'rs-counter-base alarm' : 'rs-counter-base';
} }
function _frame () { function _frame () {