Skip to content

Commit 3e7f9b3

Browse files
committed
stats
1 parent 58cbd6b commit 3e7f9b3

File tree

2 files changed

+178
-1
lines changed

2 files changed

+178
-1
lines changed

index.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<script type="module">
5858
import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/build/three.module.js';
5959
import { BufferGeometryUtils } from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/examples/jsm/utils/BufferGeometryUtils.js';
60-
60+
import Stats from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/examples/jsm/libs/stats.module.js';
6161

6262
class Terrain{
6363
constructor(options){
@@ -330,6 +330,10 @@
330330
FirstPersonControls(document.getElementById("cross"),camera,0.07)
331331

332332

333+
var stats = new Stats();
334+
stats.showPanel( 0 ); // 0: fps, 1: ms, 2: mb, 3+: custom
335+
document.body.appendChild( stats.dom );
336+
333337
var ambientLight = new THREE.AmbientLight( 0xcccccc );
334338
scene.add( ambientLight );
335339
var directionalLight = new THREE.DirectionalLight( 0x666666, 2 );
@@ -491,7 +495,9 @@
491495

492496

493497
function animate() {
498+
stats.begin();
494499
render()
500+
stats.end();
495501
requestAnimationFrame(animate)
496502
}
497503

js/stats.js

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/**
2+
* @author mrdoob / http://mrdoob.com/
3+
*/
4+
5+
var Stats = function () {
6+
7+
var mode = 0;
8+
9+
var container = document.createElement( 'div' );
10+
container.style.cssText = 'position:fixed;top:0;left:0;cursor:pointer;opacity:0.9;z-index:10000';
11+
container.addEventListener( 'click', function ( event ) {
12+
13+
event.preventDefault();
14+
showPanel( ++ mode % container.children.length );
15+
16+
}, false );
17+
18+
//
19+
20+
function addPanel( panel ) {
21+
22+
container.appendChild( panel.dom );
23+
return panel;
24+
25+
}
26+
27+
function showPanel( id ) {
28+
29+
for ( var i = 0; i < container.children.length; i ++ ) {
30+
31+
container.children[ i ].style.display = i === id ? 'block' : 'none';
32+
33+
}
34+
35+
mode = id;
36+
37+
}
38+
39+
//
40+
41+
var beginTime = ( performance || Date ).now(), prevTime = beginTime, frames = 0;
42+
43+
var fpsPanel = addPanel( new Stats.Panel( 'FPS', '#0ff', '#002' ) );
44+
var msPanel = addPanel( new Stats.Panel( 'MS', '#0f0', '#020' ) );
45+
46+
if ( self.performance && self.performance.memory ) {
47+
48+
var memPanel = addPanel( new Stats.Panel( 'MB', '#f08', '#201' ) );
49+
50+
}
51+
52+
showPanel( 0 );
53+
54+
return {
55+
56+
REVISION: 16,
57+
58+
dom: container,
59+
60+
addPanel: addPanel,
61+
showPanel: showPanel,
62+
63+
begin: function () {
64+
65+
beginTime = ( performance || Date ).now();
66+
67+
},
68+
69+
end: function () {
70+
71+
frames ++;
72+
73+
var time = ( performance || Date ).now();
74+
75+
msPanel.update( time - beginTime, 200 );
76+
77+
if ( time >= prevTime + 1000 ) {
78+
79+
fpsPanel.update( ( frames * 1000 ) / ( time - prevTime ), 100 );
80+
81+
prevTime = time;
82+
frames = 0;
83+
84+
if ( memPanel ) {
85+
86+
var memory = performance.memory;
87+
memPanel.update( memory.usedJSHeapSize / 1048576, memory.jsHeapSizeLimit / 1048576 );
88+
89+
}
90+
91+
}
92+
93+
return time;
94+
95+
},
96+
97+
update: function () {
98+
99+
beginTime = this.end();
100+
101+
},
102+
103+
// Backwards Compatibility
104+
105+
domElement: container,
106+
setMode: showPanel
107+
108+
};
109+
110+
};
111+
112+
Stats.Panel = function ( name, fg, bg ) {
113+
114+
var min = Infinity, max = 0, round = Math.round;
115+
var PR = round( window.devicePixelRatio || 1 );
116+
117+
var WIDTH = 80 * PR, HEIGHT = 48 * PR,
118+
TEXT_X = 3 * PR, TEXT_Y = 2 * PR,
119+
GRAPH_X = 3 * PR, GRAPH_Y = 15 * PR,
120+
GRAPH_WIDTH = 74 * PR, GRAPH_HEIGHT = 30 * PR;
121+
122+
var canvas = document.createElement( 'canvas' );
123+
canvas.width = WIDTH;
124+
canvas.height = HEIGHT;
125+
canvas.style.cssText = 'width:80px;height:48px';
126+
127+
var context = canvas.getContext( '2d' );
128+
context.font = 'bold ' + ( 9 * PR ) + 'px Helvetica,Arial,sans-serif';
129+
context.textBaseline = 'top';
130+
131+
context.fillStyle = bg;
132+
context.fillRect( 0, 0, WIDTH, HEIGHT );
133+
134+
context.fillStyle = fg;
135+
context.fillText( name, TEXT_X, TEXT_Y );
136+
context.fillRect( GRAPH_X, GRAPH_Y, GRAPH_WIDTH, GRAPH_HEIGHT );
137+
138+
context.fillStyle = bg;
139+
context.globalAlpha = 0.9;
140+
context.fillRect( GRAPH_X, GRAPH_Y, GRAPH_WIDTH, GRAPH_HEIGHT );
141+
142+
return {
143+
144+
dom: canvas,
145+
146+
update: function ( value, maxValue ) {
147+
148+
min = Math.min( min, value );
149+
max = Math.max( max, value );
150+
151+
context.fillStyle = bg;
152+
context.globalAlpha = 1;
153+
context.fillRect( 0, 0, WIDTH, GRAPH_Y );
154+
context.fillStyle = fg;
155+
context.fillText( round( value ) + ' ' + name + ' (' + round( min ) + '-' + round( max ) + ')', TEXT_X, TEXT_Y );
156+
157+
context.drawImage( canvas, GRAPH_X + PR, GRAPH_Y, GRAPH_WIDTH - PR, GRAPH_HEIGHT, GRAPH_X, GRAPH_Y, GRAPH_WIDTH - PR, GRAPH_HEIGHT );
158+
159+
context.fillRect( GRAPH_X + GRAPH_WIDTH - PR, GRAPH_Y, PR, GRAPH_HEIGHT );
160+
161+
context.fillStyle = bg;
162+
context.globalAlpha = 0.9;
163+
context.fillRect( GRAPH_X + GRAPH_WIDTH - PR, GRAPH_Y, PR, round( ( 1 - ( value / maxValue ) ) * GRAPH_HEIGHT ) );
164+
165+
}
166+
167+
};
168+
169+
};
170+
171+
export { Stats as default };

0 commit comments

Comments
 (0)