playcanvas 1.49.2 → 1.50.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * PlayCanvas Engine v1.49.2 revision eb81a3372
3
+ * PlayCanvas Engine v1.50.1 revision 1f42a385f
4
4
  * Copyright 2011-2021 PlayCanvas Ltd. All rights reserved.
5
5
  */
6
6
  (function (global, factory) {
@@ -26,806 +26,806 @@
26
26
  }
27
27
 
28
28
  var CpuTimer = function () {
29
- function CpuTimer(app) {
30
- this._frameIndex = 0;
31
- this._frameTimings = [];
32
- this._timings = [];
33
- this._prevTimings = [];
34
- this.unitsName = "ms";
35
- this.decimalPlaces = 1;
36
- this.enabled = true;
37
- app.on('frameupdate', this.begin.bind(this, 'update'));
38
- app.on('framerender', this.mark.bind(this, 'render'));
39
- app.on('frameend', this.mark.bind(this, 'other'));
40
- }
41
-
42
- var _proto = CpuTimer.prototype;
43
-
44
- _proto.begin = function begin(name) {
45
- if (!this.enabled) {
46
- return;
47
- }
48
-
49
- if (this._frameIndex < this._frameTimings.length) {
50
- this._frameTimings.splice(this._frameIndex);
51
- }
52
-
53
- var tmp = this._prevTimings;
54
- this._prevTimings = this._timings;
55
- this._timings = this._frameTimings;
56
- this._frameTimings = tmp;
57
- this._frameIndex = 0;
58
- this.mark(name);
59
- };
60
-
61
- _proto.mark = function mark(name) {
62
- if (!this.enabled) {
63
- return;
64
- }
65
-
66
- var timestamp = pc.now();
67
-
68
- if (this._frameIndex > 0) {
69
- var prev = this._frameTimings[this._frameIndex - 1];
70
- prev[1] = timestamp - prev[1];
71
- } else if (this._timings.length > 0) {
72
- var _prev = this._timings[this._timings.length - 1];
73
- _prev[1] = timestamp - _prev[1];
74
- }
75
-
76
- if (this._frameIndex >= this._frameTimings.length) {
77
- this._frameTimings.push([name, timestamp]);
78
- } else {
79
- var timing = this._frameTimings[this._frameIndex];
80
- timing[0] = name;
81
- timing[1] = timestamp;
82
- }
83
-
84
- this._frameIndex++;
85
- };
86
-
87
- _createClass(CpuTimer, [{
88
- key: "timings",
89
- get: function get() {
90
- return this._timings.slice(0, -1).map(function (v) {
91
- return v[1];
92
- });
93
- }
94
- }]);
95
-
96
- return CpuTimer;
29
+ function CpuTimer(app) {
30
+ this._frameIndex = 0;
31
+ this._frameTimings = [];
32
+ this._timings = [];
33
+ this._prevTimings = [];
34
+ this.unitsName = "ms";
35
+ this.decimalPlaces = 1;
36
+ this.enabled = true;
37
+ app.on('frameupdate', this.begin.bind(this, 'update'));
38
+ app.on('framerender', this.mark.bind(this, 'render'));
39
+ app.on('frameend', this.mark.bind(this, 'other'));
40
+ }
41
+
42
+ var _proto = CpuTimer.prototype;
43
+
44
+ _proto.begin = function begin(name) {
45
+ if (!this.enabled) {
46
+ return;
47
+ }
48
+
49
+ if (this._frameIndex < this._frameTimings.length) {
50
+ this._frameTimings.splice(this._frameIndex);
51
+ }
52
+
53
+ var tmp = this._prevTimings;
54
+ this._prevTimings = this._timings;
55
+ this._timings = this._frameTimings;
56
+ this._frameTimings = tmp;
57
+ this._frameIndex = 0;
58
+ this.mark(name);
59
+ };
60
+
61
+ _proto.mark = function mark(name) {
62
+ if (!this.enabled) {
63
+ return;
64
+ }
65
+
66
+ var timestamp = pc.now();
67
+
68
+ if (this._frameIndex > 0) {
69
+ var prev = this._frameTimings[this._frameIndex - 1];
70
+ prev[1] = timestamp - prev[1];
71
+ } else if (this._timings.length > 0) {
72
+ var _prev = this._timings[this._timings.length - 1];
73
+ _prev[1] = timestamp - _prev[1];
74
+ }
75
+
76
+ if (this._frameIndex >= this._frameTimings.length) {
77
+ this._frameTimings.push([name, timestamp]);
78
+ } else {
79
+ var timing = this._frameTimings[this._frameIndex];
80
+ timing[0] = name;
81
+ timing[1] = timestamp;
82
+ }
83
+
84
+ this._frameIndex++;
85
+ };
86
+
87
+ _createClass(CpuTimer, [{
88
+ key: "timings",
89
+ get: function get() {
90
+ return this._timings.slice(0, -1).map(function (v) {
91
+ return v[1];
92
+ });
93
+ }
94
+ }]);
95
+
96
+ return CpuTimer;
97
97
  }();
98
98
 
99
99
  var GpuTimer = function () {
100
- function GpuTimer(app) {
101
- this._gl = app.graphicsDevice.gl;
102
- this._ext = app.graphicsDevice.extDisjointTimerQuery;
103
- this._freeQueries = [];
104
- this._frameQueries = [];
105
- this._frames = [];
106
- this._timings = [];
107
- this._prevTimings = [];
108
- this.enabled = true;
109
- this.unitsName = "ms";
110
- this.decimalPlaces = 1;
111
- app.on('frameupdate', this.begin.bind(this, 'update'));
112
- app.on('framerender', this.mark.bind(this, 'render'));
113
- app.on('frameend', this.end.bind(this));
114
- }
115
-
116
- var _proto = GpuTimer.prototype;
117
-
118
- _proto.loseContext = function loseContext() {
119
- this._freeQueries = [];
120
- this._frameQueries = [];
121
- this._frames = [];
122
- };
123
-
124
- _proto.begin = function begin(name) {
125
- if (!this.enabled) {
126
- return;
127
- }
128
-
129
- if (this._frameQueries.length > 0) {
130
- this.end();
131
- }
132
-
133
- this._checkDisjoint();
134
-
135
- if (this._frames.length > 0) {
136
- if (this._resolveFrameTimings(this._frames[0], this._prevTimings)) {
137
- var tmp = this._prevTimings;
138
- this._prevTimings = this._timings;
139
- this._timings = tmp;
140
- this._freeQueries = this._freeQueries.concat(this._frames.splice(0, 1)[0]);
141
- }
142
- }
143
-
144
- this.mark(name);
145
- };
146
-
147
- _proto.mark = function mark(name) {
148
- if (!this.enabled) {
149
- return;
150
- }
151
-
152
- if (this._frameQueries.length > 0) {
153
- this._gl.endQuery(this._ext.TIME_ELAPSED_EXT);
154
- }
155
-
156
- var query = this._allocateQuery();
157
-
158
- query[0] = name;
159
-
160
- this._gl.beginQuery(this._ext.TIME_ELAPSED_EXT, query[1]);
161
-
162
- this._frameQueries.push(query);
163
- };
164
-
165
- _proto.end = function end() {
166
- if (!this.enabled) {
167
- return;
168
- }
169
-
170
- this._gl.endQuery(this._ext.TIME_ELAPSED_EXT);
171
-
172
- this._frames.push(this._frameQueries);
173
-
174
- this._frameQueries = [];
175
- };
176
-
177
- _proto._checkDisjoint = function _checkDisjoint() {
178
- var disjoint = this._gl.getParameter(this._ext.GPU_DISJOINT_EXT);
179
-
180
- if (disjoint) {
181
- this._freeQueries = [this._frames, [this._frameQueries], [this._freeQueries]].flat(2);
182
- this._frameQueries = [];
183
- this._frames = [];
184
- }
185
- };
186
-
187
- _proto._allocateQuery = function _allocateQuery() {
188
- return this._freeQueries.length > 0 ? this._freeQueries.splice(-1, 1)[0] : ["", this._gl.createQuery()];
189
- };
190
-
191
- _proto._resolveFrameTimings = function _resolveFrameTimings(frame, timings) {
192
- if (!this._gl.getQueryParameter(frame[frame.length - 1][1], this._gl.QUERY_RESULT_AVAILABLE)) {
193
- return false;
194
- }
195
-
196
- for (var i = 0; i < frame.length; ++i) {
197
- timings[i] = [frame[i][0], this._gl.getQueryParameter(frame[i][1], this._gl.QUERY_RESULT) * 0.000001];
198
- }
199
-
200
- return true;
201
- };
202
-
203
- _createClass(GpuTimer, [{
204
- key: "timings",
205
- get: function get() {
206
- return this._timings.map(function (v) {
207
- return v[1];
208
- });
209
- }
210
- }]);
100
+ function GpuTimer(app) {
101
+ this._gl = app.graphicsDevice.gl;
102
+ this._ext = app.graphicsDevice.extDisjointTimerQuery;
103
+ this._freeQueries = [];
104
+ this._frameQueries = [];
105
+ this._frames = [];
106
+ this._timings = [];
107
+ this._prevTimings = [];
108
+ this.enabled = true;
109
+ this.unitsName = "ms";
110
+ this.decimalPlaces = 1;
111
+ app.on('frameupdate', this.begin.bind(this, 'update'));
112
+ app.on('framerender', this.mark.bind(this, 'render'));
113
+ app.on('frameend', this.end.bind(this));
114
+ }
115
+
116
+ var _proto = GpuTimer.prototype;
117
+
118
+ _proto.loseContext = function loseContext() {
119
+ this._freeQueries = [];
120
+ this._frameQueries = [];
121
+ this._frames = [];
122
+ };
123
+
124
+ _proto.begin = function begin(name) {
125
+ if (!this.enabled) {
126
+ return;
127
+ }
128
+
129
+ if (this._frameQueries.length > 0) {
130
+ this.end();
131
+ }
132
+
133
+ this._checkDisjoint();
134
+
135
+ if (this._frames.length > 0) {
136
+ if (this._resolveFrameTimings(this._frames[0], this._prevTimings)) {
137
+ var tmp = this._prevTimings;
138
+ this._prevTimings = this._timings;
139
+ this._timings = tmp;
140
+ this._freeQueries = this._freeQueries.concat(this._frames.splice(0, 1)[0]);
141
+ }
142
+ }
143
+
144
+ this.mark(name);
145
+ };
146
+
147
+ _proto.mark = function mark(name) {
148
+ if (!this.enabled) {
149
+ return;
150
+ }
151
+
152
+ if (this._frameQueries.length > 0) {
153
+ this._gl.endQuery(this._ext.TIME_ELAPSED_EXT);
154
+ }
155
+
156
+ var query = this._allocateQuery();
157
+
158
+ query[0] = name;
159
+
160
+ this._gl.beginQuery(this._ext.TIME_ELAPSED_EXT, query[1]);
161
+
162
+ this._frameQueries.push(query);
163
+ };
164
+
165
+ _proto.end = function end() {
166
+ if (!this.enabled) {
167
+ return;
168
+ }
169
+
170
+ this._gl.endQuery(this._ext.TIME_ELAPSED_EXT);
171
+
172
+ this._frames.push(this._frameQueries);
173
+
174
+ this._frameQueries = [];
175
+ };
176
+
177
+ _proto._checkDisjoint = function _checkDisjoint() {
178
+ var disjoint = this._gl.getParameter(this._ext.GPU_DISJOINT_EXT);
211
179
 
212
- return GpuTimer;
180
+ if (disjoint) {
181
+ this._freeQueries = [this._frames, [this._frameQueries], [this._freeQueries]].flat(2);
182
+ this._frameQueries = [];
183
+ this._frames = [];
184
+ }
185
+ };
186
+
187
+ _proto._allocateQuery = function _allocateQuery() {
188
+ return this._freeQueries.length > 0 ? this._freeQueries.splice(-1, 1)[0] : ["", this._gl.createQuery()];
189
+ };
190
+
191
+ _proto._resolveFrameTimings = function _resolveFrameTimings(frame, timings) {
192
+ if (!this._gl.getQueryParameter(frame[frame.length - 1][1], this._gl.QUERY_RESULT_AVAILABLE)) {
193
+ return false;
194
+ }
195
+
196
+ for (var i = 0; i < frame.length; ++i) {
197
+ timings[i] = [frame[i][0], this._gl.getQueryParameter(frame[i][1], this._gl.QUERY_RESULT) * 0.000001];
198
+ }
199
+
200
+ return true;
201
+ };
202
+
203
+ _createClass(GpuTimer, [{
204
+ key: "timings",
205
+ get: function get() {
206
+ return this._timings.map(function (v) {
207
+ return v[1];
208
+ });
209
+ }
210
+ }]);
211
+
212
+ return GpuTimer;
213
213
  }();
214
214
 
215
215
  var StatsTimer = function () {
216
- function StatsTimer(app, statNames, decimalPlaces, unitsName, multiplier) {
217
- var _this = this;
218
-
219
- this.app = app;
220
- this.values = [];
221
- this.statNames = statNames;
222
- if (this.statNames.length > 3) this.statNames.length = 3;
223
- this.unitsName = unitsName;
224
- this.decimalPlaces = decimalPlaces;
225
- this.multiplier = multiplier || 1;
226
-
227
- var resolve = function resolve(path, obj) {
228
- return path.split('.').reduce(function (prev, curr) {
229
- return prev ? prev[curr] : null;
230
- }, obj || _this);
231
- };
232
-
233
- app.on('frameupdate', function (ms) {
234
- for (var i = 0; i < _this.statNames.length; i++) {
235
- _this.values[i] = resolve(_this.statNames[i], _this.app.stats) * _this.multiplier;
236
- }
237
- });
238
- }
239
-
240
- _createClass(StatsTimer, [{
241
- key: "timings",
242
- get: function get() {
243
- return this.values;
244
- }
245
- }]);
246
-
247
- return StatsTimer;
216
+ function StatsTimer(app, statNames, decimalPlaces, unitsName, multiplier) {
217
+ var _this = this;
218
+
219
+ this.app = app;
220
+ this.values = [];
221
+ this.statNames = statNames;
222
+ if (this.statNames.length > 3) this.statNames.length = 3;
223
+ this.unitsName = unitsName;
224
+ this.decimalPlaces = decimalPlaces;
225
+ this.multiplier = multiplier || 1;
226
+
227
+ var resolve = function resolve(path, obj) {
228
+ return path.split('.').reduce(function (prev, curr) {
229
+ return prev ? prev[curr] : null;
230
+ }, obj || _this);
231
+ };
232
+
233
+ app.on('frameupdate', function (ms) {
234
+ for (var i = 0; i < _this.statNames.length; i++) {
235
+ _this.values[i] = resolve(_this.statNames[i], _this.app.stats) * _this.multiplier;
236
+ }
237
+ });
238
+ }
239
+
240
+ _createClass(StatsTimer, [{
241
+ key: "timings",
242
+ get: function get() {
243
+ return this.values;
244
+ }
245
+ }]);
246
+
247
+ return StatsTimer;
248
248
  }();
249
249
 
250
250
  var Graph = function () {
251
- function Graph(name, app, watermark, textRefreshRate, timer) {
252
- this.name = name;
253
- this.device = app.graphicsDevice;
254
- this.timer = timer;
255
- this.watermark = watermark;
256
- this.enabled = false;
257
- this.textRefreshRate = textRefreshRate;
258
- this.avgTotal = 0;
259
- this.avgTimer = 0;
260
- this.avgCount = 0;
261
- this.timingText = "";
262
- this.texture = null;
263
- this.yOffset = 0;
264
- this.cursor = 0;
265
- this.sample = new Uint8ClampedArray(4);
266
- this.sample.set([0, 0, 0, 255]);
267
- app.on('frameupdate', this.update.bind(this));
268
- this.counter = 0;
269
- }
270
-
271
- var _proto = Graph.prototype;
272
-
273
- _proto.loseContext = function loseContext() {
274
- if (this.timer && typeof this.timer.loseContext === 'function') {
275
- this.timer.loseContext();
276
- }
277
- };
278
-
279
- _proto.update = function update(ms) {
280
- var timings = this.timer.timings;
281
- var total = timings.reduce(function (a, v) {
282
- return a + v;
283
- }, 0);
284
- this.avgTotal += total;
285
- this.avgTimer += ms;
286
- this.avgCount++;
287
-
288
- if (this.avgTimer > this.textRefreshRate) {
289
- this.timingText = (this.avgTotal / this.avgCount).toFixed(this.timer.decimalPlaces);
290
- this.avgTimer = 0;
291
- this.avgTotal = 0;
292
- this.avgCount = 0;
293
- }
294
-
295
- if (this.enabled) {
296
- var value = 0;
297
- var range = 1.5 * this.watermark;
298
-
299
- for (var i = 0; i < timings.length; ++i) {
300
- value += Math.floor(timings[i] / range * 255);
301
- this.sample[i] = value;
302
- }
303
-
304
- this.sample[3] = this.watermark / range * 255;
305
- var gl = this.device.gl;
306
- this.device.bindTexture(this.texture);
307
- gl.texSubImage2D(gl.TEXTURE_2D, 0, this.cursor, this.yOffset, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, this.sample);
308
- this.cursor++;
309
-
310
- if (this.cursor === this.texture.width) {
311
- this.cursor = 0;
312
- }
313
- }
314
- };
315
-
316
- _proto.render = function render(render2d, x, y, w, h) {
317
- render2d.quad(this.texture, x + w, y, -w, h, this.cursor, 0.5 + this.yOffset, -w, 0, this.enabled);
318
- };
319
-
320
- return Graph;
251
+ function Graph(name, app, watermark, textRefreshRate, timer) {
252
+ this.name = name;
253
+ this.device = app.graphicsDevice;
254
+ this.timer = timer;
255
+ this.watermark = watermark;
256
+ this.enabled = false;
257
+ this.textRefreshRate = textRefreshRate;
258
+ this.avgTotal = 0;
259
+ this.avgTimer = 0;
260
+ this.avgCount = 0;
261
+ this.timingText = "";
262
+ this.texture = null;
263
+ this.yOffset = 0;
264
+ this.cursor = 0;
265
+ this.sample = new Uint8ClampedArray(4);
266
+ this.sample.set([0, 0, 0, 255]);
267
+ app.on('frameupdate', this.update.bind(this));
268
+ this.counter = 0;
269
+ }
270
+
271
+ var _proto = Graph.prototype;
272
+
273
+ _proto.loseContext = function loseContext() {
274
+ if (this.timer && typeof this.timer.loseContext === 'function') {
275
+ this.timer.loseContext();
276
+ }
277
+ };
278
+
279
+ _proto.update = function update(ms) {
280
+ var timings = this.timer.timings;
281
+ var total = timings.reduce(function (a, v) {
282
+ return a + v;
283
+ }, 0);
284
+ this.avgTotal += total;
285
+ this.avgTimer += ms;
286
+ this.avgCount++;
287
+
288
+ if (this.avgTimer > this.textRefreshRate) {
289
+ this.timingText = (this.avgTotal / this.avgCount).toFixed(this.timer.decimalPlaces);
290
+ this.avgTimer = 0;
291
+ this.avgTotal = 0;
292
+ this.avgCount = 0;
293
+ }
294
+
295
+ if (this.enabled) {
296
+ var value = 0;
297
+ var range = 1.5 * this.watermark;
298
+
299
+ for (var i = 0; i < timings.length; ++i) {
300
+ value += Math.floor(timings[i] / range * 255);
301
+ this.sample[i] = value;
302
+ }
303
+
304
+ this.sample[3] = this.watermark / range * 255;
305
+ var gl = this.device.gl;
306
+ this.device.bindTexture(this.texture);
307
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, this.cursor, this.yOffset, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, this.sample);
308
+ this.cursor++;
309
+
310
+ if (this.cursor === this.texture.width) {
311
+ this.cursor = 0;
312
+ }
313
+ }
314
+ };
315
+
316
+ _proto.render = function render(render2d, x, y, w, h) {
317
+ render2d.quad(this.texture, x + w, y, -w, h, this.cursor, 0.5 + this.yOffset, -w, 0, this.enabled);
318
+ };
319
+
320
+ return Graph;
321
321
  }();
322
322
 
323
323
  var WordAtlas = function () {
324
- function WordAtlas(texture, words) {
325
- var canvas = document.createElement('canvas');
326
- canvas.width = texture.width;
327
- canvas.height = texture.height;
328
- var context = canvas.getContext('2d', {
329
- alpha: true
330
- });
331
- context.font = '10px "Lucida Console", Monaco, monospace';
332
- context.textAlign = "left";
333
- context.textBaseline = "alphabetic";
334
- context.fillStyle = "rgb(255, 255, 255)";
335
- var padding = 5;
336
- var x = padding;
337
- var y = padding;
338
- var placements = [];
339
-
340
- for (var i = 0; i < words.length; ++i) {
341
- var measurement = context.measureText(words[i]);
342
- var l = Math.ceil(-measurement.actualBoundingBoxLeft);
343
- var r = Math.ceil(measurement.actualBoundingBoxRight);
344
- var a = Math.ceil(measurement.actualBoundingBoxAscent);
345
- var d = Math.ceil(measurement.actualBoundingBoxDescent);
346
- var w = l + r;
347
- var h = a + d;
348
-
349
- if (x + w >= canvas.width) {
350
- x = padding;
351
- y += 16;
352
- }
353
-
354
- context.fillStyle = words[i].length === 1 ? "rgb(255, 255, 255)" : "rgb(150, 150, 150)";
355
- context.fillText(words[i], x - l, y + a);
356
- placements.push({
357
- l: l,
358
- r: r,
359
- a: a,
360
- d: d,
361
- x: x,
362
- y: y,
363
- w: w,
364
- h: h
365
- });
366
- x += w + padding;
367
- }
368
-
369
- var wordMap = {};
370
- words.forEach(function (w, i) {
371
- wordMap[w] = i;
372
- });
373
- this.words = words;
374
- this.wordMap = wordMap;
375
- this.placements = placements;
376
- this.texture = texture;
377
- var source = context.getImageData(0, 0, canvas.width, canvas.height);
378
- var dest = texture.lock();
379
-
380
- for (var _y = 0; _y < source.height; ++_y) {
381
- for (var _x = 0; _x < source.width; ++_x) {
382
- var offset = (_x + _y * texture.width) * 4;
383
- dest[offset] = 255;
384
- dest[offset + 1] = 255;
385
- dest[offset + 2] = 255;
386
- var red = source.data[(_x + (source.height - 1 - _y) * source.width) * 4];
387
- var alpha = source.data[(_x + (source.height - 1 - _y) * source.width) * 4 + 3];
388
- dest[offset + 3] = alpha * (red > 150 ? 1 : 0.7);
389
- }
390
- }
391
- }
392
-
393
- var _proto = WordAtlas.prototype;
394
-
395
- _proto.render = function render(render2d, word, x, y) {
396
- var p = this.placements[this.wordMap[word]];
397
-
398
- if (p) {
399
- var padding = 1;
400
- render2d.quad(this.texture, x + p.l - padding, y - p.d + padding, p.w + padding * 2, p.h + padding * 2, p.x - padding, 64 - p.y - p.h - padding, undefined, undefined, true);
401
- return p.w;
402
- }
403
-
404
- return 0;
405
- };
406
-
407
- return WordAtlas;
324
+ function WordAtlas(texture, words) {
325
+ var canvas = document.createElement('canvas');
326
+ canvas.width = texture.width;
327
+ canvas.height = texture.height;
328
+ var context = canvas.getContext('2d', {
329
+ alpha: true
330
+ });
331
+ context.font = '10px "Lucida Console", Monaco, monospace';
332
+ context.textAlign = "left";
333
+ context.textBaseline = "alphabetic";
334
+ context.fillStyle = "rgb(255, 255, 255)";
335
+ var padding = 5;
336
+ var x = padding;
337
+ var y = padding;
338
+ var placements = [];
339
+
340
+ for (var i = 0; i < words.length; ++i) {
341
+ var measurement = context.measureText(words[i]);
342
+ var l = Math.ceil(-measurement.actualBoundingBoxLeft);
343
+ var r = Math.ceil(measurement.actualBoundingBoxRight);
344
+ var a = Math.ceil(measurement.actualBoundingBoxAscent);
345
+ var d = Math.ceil(measurement.actualBoundingBoxDescent);
346
+ var w = l + r;
347
+ var h = a + d;
348
+
349
+ if (x + w >= canvas.width) {
350
+ x = padding;
351
+ y += 16;
352
+ }
353
+
354
+ context.fillStyle = words[i].length === 1 ? "rgb(255, 255, 255)" : "rgb(150, 150, 150)";
355
+ context.fillText(words[i], x - l, y + a);
356
+ placements.push({
357
+ l: l,
358
+ r: r,
359
+ a: a,
360
+ d: d,
361
+ x: x,
362
+ y: y,
363
+ w: w,
364
+ h: h
365
+ });
366
+ x += w + padding;
367
+ }
368
+
369
+ var wordMap = {};
370
+ words.forEach(function (w, i) {
371
+ wordMap[w] = i;
372
+ });
373
+ this.words = words;
374
+ this.wordMap = wordMap;
375
+ this.placements = placements;
376
+ this.texture = texture;
377
+ var source = context.getImageData(0, 0, canvas.width, canvas.height);
378
+ var dest = texture.lock();
379
+
380
+ for (var _y = 0; _y < source.height; ++_y) {
381
+ for (var _x = 0; _x < source.width; ++_x) {
382
+ var offset = (_x + _y * texture.width) * 4;
383
+ dest[offset] = 255;
384
+ dest[offset + 1] = 255;
385
+ dest[offset + 2] = 255;
386
+ var red = source.data[(_x + (source.height - 1 - _y) * source.width) * 4];
387
+ var alpha = source.data[(_x + (source.height - 1 - _y) * source.width) * 4 + 3];
388
+ dest[offset + 3] = alpha * (red > 150 ? 1 : 0.7);
389
+ }
390
+ }
391
+ }
392
+
393
+ var _proto = WordAtlas.prototype;
394
+
395
+ _proto.render = function render(render2d, word, x, y) {
396
+ var p = this.placements[this.wordMap[word]];
397
+
398
+ if (p) {
399
+ var padding = 1;
400
+ render2d.quad(this.texture, x + p.l - padding, y - p.d + padding, p.w + padding * 2, p.h + padding * 2, p.x - padding, 64 - p.y - p.h - padding, undefined, undefined, true);
401
+ return p.w;
402
+ }
403
+
404
+ return 0;
405
+ };
406
+
407
+ return WordAtlas;
408
408
  }();
409
409
 
410
410
  var Render2d = function () {
411
- function Render2d(device, colors, maxQuads) {
412
- var _this = this;
413
-
414
- if (maxQuads === void 0) {
415
- maxQuads = 512;
416
- }
417
-
418
- var vertexShader = 'attribute vec3 vertex_position;\n' + 'attribute vec4 vertex_texCoord0;\n' + 'uniform vec4 screenAndTextureSize;\n' + 'varying vec4 uv0;\n' + 'varying float enabled;\n' + 'void main(void) {\n' + ' vec2 pos = vertex_position.xy / screenAndTextureSize.xy;\n' + ' gl_Position = vec4(pos * 2.0 - 1.0, 0.5, 1.0);\n' + ' uv0 = vec4(vertex_texCoord0.xy / screenAndTextureSize.zw, vertex_texCoord0.zw);\n' + ' enabled = vertex_position.z;\n' + '}\n';
419
- var fragmentShader = 'varying vec4 uv0;\n' + 'varying float enabled;\n' + 'uniform vec4 clr;\n' + 'uniform vec4 col0;\n' + 'uniform vec4 col1;\n' + 'uniform vec4 col2;\n' + 'uniform vec4 watermark;\n' + 'uniform float watermarkSize;\n' + 'uniform vec4 background;\n' + 'uniform sampler2D source;\n' + 'void main (void) {\n' + ' vec4 tex = texture2D(source, uv0.xy);\n' + ' if (!(tex.rgb == vec3(1.0, 1.0, 1.0))) {\n' + ' if (enabled < 0.5)\n' + ' tex = background;\n' + ' else if (abs(uv0.w - tex.a) < watermarkSize)\n' + ' tex = watermark;\n' + ' else if (uv0.w < tex.r)\n' + ' tex = col0;\n' + ' else if (uv0.w < tex.g)\n' + ' tex = col1;\n' + ' else if (uv0.w < tex.b)\n' + ' tex = col2;\n' + ' else\n' + ' tex = background;\n' + ' }\n' + ' gl_FragColor = tex * clr;\n' + '}\n';
420
- var format = new pc.VertexFormat(device, [{
421
- semantic: pc.SEMANTIC_POSITION,
422
- components: 3,
423
- type: pc.TYPE_FLOAT32
424
- }, {
425
- semantic: pc.SEMANTIC_TEXCOORD0,
426
- components: 4,
427
- type: pc.TYPE_FLOAT32
428
- }]);
429
- var indices = new Uint16Array(maxQuads * 6);
430
-
431
- for (var i = 0; i < maxQuads; ++i) {
432
- indices[i * 6 + 0] = i * 4;
433
- indices[i * 6 + 1] = i * 4 + 1;
434
- indices[i * 6 + 2] = i * 4 + 2;
435
- indices[i * 6 + 3] = i * 4;
436
- indices[i * 6 + 4] = i * 4 + 2;
437
- indices[i * 6 + 5] = i * 4 + 3;
438
- }
439
-
440
- this.device = device;
441
- this.shader = pc.shaderChunks.createShaderFromCode(device, vertexShader, fragmentShader, "mini-stats");
442
- this.buffer = new pc.VertexBuffer(device, format, maxQuads * 4, pc.BUFFER_STREAM);
443
- this.data = new Float32Array(this.buffer.numBytes / 4);
444
- this.indexBuffer = new pc.IndexBuffer(device, pc.INDEXFORMAT_UINT16, maxQuads * 6, pc.BUFFER_STATIC, indices);
445
- this.prims = [];
446
- this.prim = null;
447
- this.primIndex = -1;
448
- this.quads = 0;
449
-
450
- var setupColor = function setupColor(name, value) {
451
- _this[name] = new Float32Array([value.r, value.g, value.b, value.a]);
452
- _this[name + "Id"] = device.scope.resolve(name);
453
- };
454
-
455
- setupColor("col0", colors.graph0);
456
- setupColor("col1", colors.graph1);
457
- setupColor("col2", colors.graph2);
458
- setupColor("watermark", colors.watermark);
459
- setupColor("background", colors.background);
460
- this.watermarkSizeId = device.scope.resolve('watermarkSize');
461
- this.clrId = device.scope.resolve('clr');
462
- this.clr = new Float32Array(4);
463
- this.screenTextureSizeId = device.scope.resolve('screenAndTextureSize');
464
- this.screenTextureSize = new Float32Array(4);
465
- }
466
-
467
- var _proto = Render2d.prototype;
468
-
469
- _proto.quad = function quad(texture, x, y, w, h, u, v, uw, uh, enabled) {
470
- var quad = this.quads++;
471
- var prim = this.prim;
472
-
473
- if (prim && prim.texture === texture) {
474
- prim.count += 6;
475
- } else {
476
- this.primIndex++;
477
-
478
- if (this.primIndex === this.prims.length) {
479
- prim = {
480
- type: pc.PRIMITIVE_TRIANGLES,
481
- indexed: true,
482
- base: quad * 6,
483
- count: 6,
484
- texture: texture
485
- };
486
- this.prims.push(prim);
487
- } else {
488
- prim = this.prims[this.primIndex];
489
- prim.base = quad * 6;
490
- prim.count = 6;
491
- prim.texture = texture;
492
- }
493
-
494
- this.prim = prim;
495
- }
496
-
497
- var x1 = x + w;
498
- var y1 = y + h;
499
- var u1 = u + (uw === undefined ? w : uw);
500
- var v1 = v + (uh === undefined ? h : uh);
501
- var colorize = enabled ? 1 : 0;
502
- this.data.set([x, y, colorize, u, v, 0, 0, x1, y, colorize, u1, v, 1, 0, x1, y1, colorize, u1, v1, 1, 1, x, y1, colorize, u, v1, 0, 1], 4 * 7 * quad);
503
- };
504
-
505
- _proto.render = function render(clr, height) {
506
- var device = this.device;
507
- var buffer = this.buffer;
508
- buffer.setData(this.data.buffer);
509
- device.updateBegin();
510
- device.setDepthTest(false);
511
- device.setDepthWrite(false);
512
- device.setCullMode(pc.CULLFACE_NONE);
513
- device.setBlending(true);
514
- device.setBlendFunctionSeparate(pc.BLENDMODE_SRC_ALPHA, pc.BLENDMODE_ONE_MINUS_SRC_ALPHA, pc.BLENDMODE_ONE, pc.BLENDMODE_ONE);
515
- device.setBlendEquationSeparate(pc.BLENDEQUATION_ADD, pc.BLENDEQUATION_ADD);
516
- device.setVertexBuffer(buffer, 0);
517
- device.setIndexBuffer(this.indexBuffer);
518
- device.setShader(this.shader);
519
- var pr = Math.min(device.maxPixelRatio, window.devicePixelRatio);
520
- this.clr.set(clr, 0);
521
- this.clrId.setValue(this.clr);
522
- this.screenTextureSize[0] = device.width / pr;
523
- this.screenTextureSize[1] = device.height / pr;
524
- this.col0Id.setValue(this.col0);
525
- this.col1Id.setValue(this.col1);
526
- this.col2Id.setValue(this.col2);
527
- this.watermarkId.setValue(this.watermark);
528
- this.backgroundId.setValue(this.background);
529
-
530
- for (var i = 0; i <= this.primIndex; ++i) {
531
- var prim = this.prims[i];
532
- this.screenTextureSize[2] = prim.texture.width;
533
- this.screenTextureSize[3] = prim.texture.height;
534
- this.screenTextureSizeId.setValue(this.screenTextureSize);
535
- device.constantTexSource.setValue(prim.texture);
536
- this.watermarkSizeId.setValue(0.5 / height);
537
- device.draw(prim);
538
- }
539
-
540
- device.updateEnd();
541
- this.prim = null;
542
- this.primIndex = -1;
543
- this.quads = 0;
544
- };
545
-
546
- return Render2d;
411
+ function Render2d(device, colors, maxQuads) {
412
+ var _this = this;
413
+
414
+ if (maxQuads === void 0) {
415
+ maxQuads = 512;
416
+ }
417
+
418
+ var vertexShader = 'attribute vec3 vertex_position;\n' + 'attribute vec4 vertex_texCoord0;\n' + 'uniform vec4 screenAndTextureSize;\n' + 'varying vec4 uv0;\n' + 'varying float enabled;\n' + 'void main(void) {\n' + ' vec2 pos = vertex_position.xy / screenAndTextureSize.xy;\n' + ' gl_Position = vec4(pos * 2.0 - 1.0, 0.5, 1.0);\n' + ' uv0 = vec4(vertex_texCoord0.xy / screenAndTextureSize.zw, vertex_texCoord0.zw);\n' + ' enabled = vertex_position.z;\n' + '}\n';
419
+ var fragmentShader = 'varying vec4 uv0;\n' + 'varying float enabled;\n' + 'uniform vec4 clr;\n' + 'uniform vec4 col0;\n' + 'uniform vec4 col1;\n' + 'uniform vec4 col2;\n' + 'uniform vec4 watermark;\n' + 'uniform float watermarkSize;\n' + 'uniform vec4 background;\n' + 'uniform sampler2D source;\n' + 'void main (void) {\n' + ' vec4 tex = texture2D(source, uv0.xy);\n' + ' if (!(tex.rgb == vec3(1.0, 1.0, 1.0))) {\n' + ' if (enabled < 0.5)\n' + ' tex = background;\n' + ' else if (abs(uv0.w - tex.a) < watermarkSize)\n' + ' tex = watermark;\n' + ' else if (uv0.w < tex.r)\n' + ' tex = col0;\n' + ' else if (uv0.w < tex.g)\n' + ' tex = col1;\n' + ' else if (uv0.w < tex.b)\n' + ' tex = col2;\n' + ' else\n' + ' tex = background;\n' + ' }\n' + ' gl_FragColor = tex * clr;\n' + '}\n';
420
+ var format = new pc.VertexFormat(device, [{
421
+ semantic: pc.SEMANTIC_POSITION,
422
+ components: 3,
423
+ type: pc.TYPE_FLOAT32
424
+ }, {
425
+ semantic: pc.SEMANTIC_TEXCOORD0,
426
+ components: 4,
427
+ type: pc.TYPE_FLOAT32
428
+ }]);
429
+ var indices = new Uint16Array(maxQuads * 6);
430
+
431
+ for (var i = 0; i < maxQuads; ++i) {
432
+ indices[i * 6 + 0] = i * 4;
433
+ indices[i * 6 + 1] = i * 4 + 1;
434
+ indices[i * 6 + 2] = i * 4 + 2;
435
+ indices[i * 6 + 3] = i * 4;
436
+ indices[i * 6 + 4] = i * 4 + 2;
437
+ indices[i * 6 + 5] = i * 4 + 3;
438
+ }
439
+
440
+ this.device = device;
441
+ this.shader = pc.shaderChunks.createShaderFromCode(device, vertexShader, fragmentShader, "mini-stats");
442
+ this.buffer = new pc.VertexBuffer(device, format, maxQuads * 4, pc.BUFFER_STREAM);
443
+ this.data = new Float32Array(this.buffer.numBytes / 4);
444
+ this.indexBuffer = new pc.IndexBuffer(device, pc.INDEXFORMAT_UINT16, maxQuads * 6, pc.BUFFER_STATIC, indices);
445
+ this.prims = [];
446
+ this.prim = null;
447
+ this.primIndex = -1;
448
+ this.quads = 0;
449
+
450
+ var setupColor = function setupColor(name, value) {
451
+ _this[name] = new Float32Array([value.r, value.g, value.b, value.a]);
452
+ _this[name + "Id"] = device.scope.resolve(name);
453
+ };
454
+
455
+ setupColor("col0", colors.graph0);
456
+ setupColor("col1", colors.graph1);
457
+ setupColor("col2", colors.graph2);
458
+ setupColor("watermark", colors.watermark);
459
+ setupColor("background", colors.background);
460
+ this.watermarkSizeId = device.scope.resolve('watermarkSize');
461
+ this.clrId = device.scope.resolve('clr');
462
+ this.clr = new Float32Array(4);
463
+ this.screenTextureSizeId = device.scope.resolve('screenAndTextureSize');
464
+ this.screenTextureSize = new Float32Array(4);
465
+ }
466
+
467
+ var _proto = Render2d.prototype;
468
+
469
+ _proto.quad = function quad(texture, x, y, w, h, u, v, uw, uh, enabled) {
470
+ var quad = this.quads++;
471
+ var prim = this.prim;
472
+
473
+ if (prim && prim.texture === texture) {
474
+ prim.count += 6;
475
+ } else {
476
+ this.primIndex++;
477
+
478
+ if (this.primIndex === this.prims.length) {
479
+ prim = {
480
+ type: pc.PRIMITIVE_TRIANGLES,
481
+ indexed: true,
482
+ base: quad * 6,
483
+ count: 6,
484
+ texture: texture
485
+ };
486
+ this.prims.push(prim);
487
+ } else {
488
+ prim = this.prims[this.primIndex];
489
+ prim.base = quad * 6;
490
+ prim.count = 6;
491
+ prim.texture = texture;
492
+ }
493
+
494
+ this.prim = prim;
495
+ }
496
+
497
+ var x1 = x + w;
498
+ var y1 = y + h;
499
+ var u1 = u + (uw === undefined ? w : uw);
500
+ var v1 = v + (uh === undefined ? h : uh);
501
+ var colorize = enabled ? 1 : 0;
502
+ this.data.set([x, y, colorize, u, v, 0, 0, x1, y, colorize, u1, v, 1, 0, x1, y1, colorize, u1, v1, 1, 1, x, y1, colorize, u, v1, 0, 1], 4 * 7 * quad);
503
+ };
504
+
505
+ _proto.render = function render(clr, height) {
506
+ var device = this.device;
507
+ var buffer = this.buffer;
508
+ buffer.setData(this.data.buffer);
509
+ device.updateBegin();
510
+ device.setDepthTest(false);
511
+ device.setDepthWrite(false);
512
+ device.setCullMode(pc.CULLFACE_NONE);
513
+ device.setBlending(true);
514
+ device.setBlendFunctionSeparate(pc.BLENDMODE_SRC_ALPHA, pc.BLENDMODE_ONE_MINUS_SRC_ALPHA, pc.BLENDMODE_ONE, pc.BLENDMODE_ONE);
515
+ device.setBlendEquationSeparate(pc.BLENDEQUATION_ADD, pc.BLENDEQUATION_ADD);
516
+ device.setVertexBuffer(buffer, 0);
517
+ device.setIndexBuffer(this.indexBuffer);
518
+ device.setShader(this.shader);
519
+ var pr = Math.min(device.maxPixelRatio, window.devicePixelRatio);
520
+ this.clr.set(clr, 0);
521
+ this.clrId.setValue(this.clr);
522
+ this.screenTextureSize[0] = device.width / pr;
523
+ this.screenTextureSize[1] = device.height / pr;
524
+ this.col0Id.setValue(this.col0);
525
+ this.col1Id.setValue(this.col1);
526
+ this.col2Id.setValue(this.col2);
527
+ this.watermarkId.setValue(this.watermark);
528
+ this.backgroundId.setValue(this.background);
529
+
530
+ for (var i = 0; i <= this.primIndex; ++i) {
531
+ var prim = this.prims[i];
532
+ this.screenTextureSize[2] = prim.texture.width;
533
+ this.screenTextureSize[3] = prim.texture.height;
534
+ this.screenTextureSizeId.setValue(this.screenTextureSize);
535
+ device.constantTexSource.setValue(prim.texture);
536
+ this.watermarkSizeId.setValue(0.5 / height);
537
+ device.draw(prim);
538
+ }
539
+
540
+ device.updateEnd();
541
+ this.prim = null;
542
+ this.primIndex = -1;
543
+ this.quads = 0;
544
+ };
545
+
546
+ return Render2d;
547
547
  }();
548
548
 
549
549
  var MiniStats = function () {
550
- function MiniStats(app, options) {
551
- var _this = this;
552
-
553
- var device = app.graphicsDevice;
554
-
555
- this._contextLostHandler = function (event) {
556
- event.preventDefault();
557
-
558
- if (_this.graphs) {
559
- for (var i = 0; i < _this.graphs.length; i++) {
560
- _this.graphs[i].loseContext();
561
- }
562
- }
563
- };
564
-
565
- device.canvas.addEventListener("webglcontextlost", this._contextLostHandler, false);
566
- options = options || MiniStats.getDefaultOptions();
567
- var graphs = this.initGraphs(app, device, options);
568
- var words = ["", "ms", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."];
569
- graphs.forEach(function (graph) {
570
- words.push(graph.name);
571
- });
572
-
573
- if (options.stats) {
574
- options.stats.forEach(function (stat) {
575
- if (stat.unitsName) words.push(stat.unitsName);
576
- });
577
- }
578
-
579
- words = words.filter(function (item, index) {
580
- return words.indexOf(item) >= index;
581
- });
582
- var maxWidth = options.sizes.reduce(function (max, v) {
583
- return v.width > max ? v.width : max;
584
- }, 0);
585
- var wordAtlasData = this.initWordAtlas(device, words, maxWidth, graphs.length);
586
- var texture = wordAtlasData.texture;
587
- graphs.forEach(function (graph, i) {
588
- graph.texture = texture;
589
- graph.yOffset = i;
590
- });
591
- this.sizes = options.sizes;
592
- this._activeSizeIndex = options.startSizeIndex;
593
- var div = document.createElement('div');
594
- div.style.cssText = 'position:fixed;bottom:0;left:0;background:transparent;';
595
- document.body.appendChild(div);
596
- div.addEventListener('mouseenter', function (event) {
597
- _this.opacity = 1.0;
598
- });
599
- div.addEventListener('mouseleave', function (event) {
600
- _this.opacity = 0.5;
601
- });
602
- div.addEventListener('click', function (event) {
603
- event.preventDefault();
604
-
605
- if (_this._enabled) {
606
- _this.activeSizeIndex = (_this.activeSizeIndex + 1) % _this.sizes.length;
607
-
608
- _this.resize(_this.sizes[_this.activeSizeIndex].width, _this.sizes[_this.activeSizeIndex].height, _this.sizes[_this.activeSizeIndex].graphs);
609
- }
610
- });
611
- device.on("resizecanvas", function () {
612
- _this.updateDiv();
613
- });
614
- app.on('postrender', function () {
615
- if (_this._enabled) {
616
- _this.render();
617
- }
618
- });
619
- this.device = device;
620
- this.texture = texture;
621
- this.wordAtlas = wordAtlasData.atlas;
622
- this.render2d = new Render2d(device, options.colors);
623
- this.graphs = graphs;
624
- this.div = div;
625
- this.width = 0;
626
- this.height = 0;
627
- this.gspacing = 2;
628
- this.clr = [1, 1, 1, 0.5];
629
- this._enabled = true;
630
- this.activeSizeIndex = this._activeSizeIndex;
631
- }
632
-
633
- MiniStats.getDefaultOptions = function getDefaultOptions() {
634
- return {
635
- sizes: [{
636
- width: 100,
637
- height: 16,
638
- spacing: 0,
639
- graphs: false
640
- }, {
641
- width: 128,
642
- height: 32,
643
- spacing: 2,
644
- graphs: true
645
- }, {
646
- width: 256,
647
- height: 64,
648
- spacing: 2,
649
- graphs: true
650
- }],
651
- startSizeIndex: 0,
652
- textRefreshRate: 500,
653
- colors: {
654
- graph0: new pc.Color(0.7, 0.2, 0.2, 1),
655
- graph1: new pc.Color(0.2, 0.7, 0.2, 1),
656
- graph2: new pc.Color(0.2, 0.2, 0.7, 1),
657
- watermark: new pc.Color(0.4, 0.4, 0.2, 1),
658
- background: new pc.Color(0, 0, 0, 1.0)
659
- },
660
- cpu: {
661
- enabled: true,
662
- watermark: 33
663
- },
664
- gpu: {
665
- enabled: true,
666
- watermark: 33
667
- },
668
- stats: [{
669
- name: "Frame",
670
- stats: ["frame.ms"],
671
- decimalPlaces: 1,
672
- unitsName: "ms",
673
- watermark: 33
674
- }, {
675
- name: "DrawCalls",
676
- stats: ["drawCalls.total"],
677
- watermark: 1000
678
- }]
679
- };
680
- };
681
-
682
- var _proto = MiniStats.prototype;
683
-
684
- _proto.initWordAtlas = function initWordAtlas(device, words, maxWidth, numGraphs) {
685
- var texture = new pc.Texture(device, {
686
- name: 'mini-stats',
687
- width: pc.math.nextPowerOfTwo(maxWidth),
688
- height: 64,
689
- mipmaps: false,
690
- minFilter: pc.FILTER_NEAREST,
691
- magFilter: pc.FILTER_NEAREST
692
- });
693
- var wordAtlas = new WordAtlas(texture, words);
694
- var dest = texture.lock();
695
-
696
- for (var i = 0; i < texture.width * numGraphs; ++i) {
697
- dest.set([0, 0, 0, 255], i * 4);
698
- }
699
-
700
- texture.unlock();
701
- device.setTexture(texture, 0);
702
- return {
703
- atlas: wordAtlas,
704
- texture: texture
705
- };
706
- };
707
-
708
- _proto.initGraphs = function initGraphs(app, device, options) {
709
- var graphs = [];
710
-
711
- if (options.cpu.enabled) {
712
- var timer = new CpuTimer(app);
713
- var graph = new Graph('CPU', app, options.cpu.watermark, options.textRefreshRate, timer);
714
- graphs.push(graph);
715
- }
716
-
717
- if (options.gpu.enabled && device.extDisjointTimerQuery) {
718
- var _timer = new GpuTimer(app);
719
-
720
- var _graph = new Graph('GPU', app, options.gpu.watermark, options.textRefreshRate, _timer);
721
-
722
- graphs.push(_graph);
723
- }
724
-
725
- if (options.stats) {
726
- options.stats.forEach(function (entry) {
727
- var timer = new StatsTimer(app, entry.stats, entry.decimalPlaces, entry.unitsName, entry.multiplier);
728
- var graph = new Graph(entry.name, app, entry.watermark, options.textRefreshRate, timer);
729
- graphs.push(graph);
730
- });
731
- }
732
-
733
- return graphs;
734
- };
735
-
736
- _proto.render = function render() {
737
- var graphs = this.graphs;
738
- var wordAtlas = this.wordAtlas;
739
- var render2d = this.render2d;
740
- var width = this.width;
741
- var height = this.height;
742
- var gspacing = this.gspacing;
743
-
744
- for (var i = 0; i < graphs.length; ++i) {
745
- var graph = graphs[i];
746
- var y = i * (height + gspacing);
747
- graph.render(render2d, 0, y, width, height);
748
- var x = 1;
749
- y += height - 13;
750
- x += wordAtlas.render(render2d, graph.name, x, y) + 10;
751
- var timingText = graph.timingText;
752
-
753
- for (var j = 0; j < timingText.length; ++j) {
754
- x += wordAtlas.render(render2d, timingText[j], x, y);
755
- }
756
-
757
- if (graph.timer.unitsName) {
758
- x += 3;
759
- wordAtlas.render(render2d, graph.timer.unitsName, x, y);
760
- }
761
- }
762
-
763
- render2d.render(this.clr, height);
764
- };
765
-
766
- _proto.resize = function resize(width, height, showGraphs) {
767
- var graphs = this.graphs;
768
-
769
- for (var i = 0; i < graphs.length; ++i) {
770
- graphs[i].enabled = showGraphs;
771
- }
772
-
773
- this.width = width;
774
- this.height = height;
775
- this.updateDiv();
776
- };
777
-
778
- _proto.updateDiv = function updateDiv() {
779
- var rect = this.device.canvas.getBoundingClientRect();
780
- this.div.style.left = rect.left + "px";
781
- this.div.style.bottom = window.innerHeight - rect.bottom + "px";
782
- this.div.style.width = this.width + "px";
783
- this.div.style.height = this.overallHeight + "px";
784
- };
785
-
786
- _createClass(MiniStats, [{
787
- key: "activeSizeIndex",
788
- get: function get() {
789
- return this._activeSizeIndex;
790
- },
791
- set: function set(value) {
792
- this._activeSizeIndex = value;
793
- this.gspacing = this.sizes[value].spacing;
794
- this.resize(this.sizes[value].width, this.sizes[value].height, this.sizes[value].graphs);
795
- }
796
- }, {
797
- key: "opacity",
798
- get: function get() {
799
- return this.clr[3];
800
- },
801
- set: function set(value) {
802
- this.clr[3] = value;
803
- }
804
- }, {
805
- key: "overallHeight",
806
- get: function get() {
807
- var graphs = this.graphs;
808
- var spacing = this.gspacing;
809
- return this.height * graphs.length + spacing * (graphs.length - 1);
810
- }
811
- }, {
812
- key: "enabled",
813
- get: function get() {
814
- return this._enabled;
815
- },
816
- set: function set(value) {
817
- if (value !== this._enabled) {
818
- this._enabled = value;
819
-
820
- for (var i = 0; i < this.graphs.length; ++i) {
821
- this.graphs[i].enabled = value;
822
- this.graphs[i].timer.enabled = value;
823
- }
824
- }
825
- }
826
- }]);
827
-
828
- return MiniStats;
550
+ function MiniStats(app, options) {
551
+ var _this = this;
552
+
553
+ var device = app.graphicsDevice;
554
+
555
+ this._contextLostHandler = function (event) {
556
+ event.preventDefault();
557
+
558
+ if (_this.graphs) {
559
+ for (var i = 0; i < _this.graphs.length; i++) {
560
+ _this.graphs[i].loseContext();
561
+ }
562
+ }
563
+ };
564
+
565
+ device.canvas.addEventListener("webglcontextlost", this._contextLostHandler, false);
566
+ options = options || MiniStats.getDefaultOptions();
567
+ var graphs = this.initGraphs(app, device, options);
568
+ var words = ["", "ms", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."];
569
+ graphs.forEach(function (graph) {
570
+ words.push(graph.name);
571
+ });
572
+
573
+ if (options.stats) {
574
+ options.stats.forEach(function (stat) {
575
+ if (stat.unitsName) words.push(stat.unitsName);
576
+ });
577
+ }
578
+
579
+ words = words.filter(function (item, index) {
580
+ return words.indexOf(item) >= index;
581
+ });
582
+ var maxWidth = options.sizes.reduce(function (max, v) {
583
+ return v.width > max ? v.width : max;
584
+ }, 0);
585
+ var wordAtlasData = this.initWordAtlas(device, words, maxWidth, graphs.length);
586
+ var texture = wordAtlasData.texture;
587
+ graphs.forEach(function (graph, i) {
588
+ graph.texture = texture;
589
+ graph.yOffset = i;
590
+ });
591
+ this.sizes = options.sizes;
592
+ this._activeSizeIndex = options.startSizeIndex;
593
+ var div = document.createElement('div');
594
+ div.style.cssText = 'position:fixed;bottom:0;left:0;background:transparent;';
595
+ document.body.appendChild(div);
596
+ div.addEventListener('mouseenter', function (event) {
597
+ _this.opacity = 1.0;
598
+ });
599
+ div.addEventListener('mouseleave', function (event) {
600
+ _this.opacity = 0.5;
601
+ });
602
+ div.addEventListener('click', function (event) {
603
+ event.preventDefault();
604
+
605
+ if (_this._enabled) {
606
+ _this.activeSizeIndex = (_this.activeSizeIndex + 1) % _this.sizes.length;
607
+
608
+ _this.resize(_this.sizes[_this.activeSizeIndex].width, _this.sizes[_this.activeSizeIndex].height, _this.sizes[_this.activeSizeIndex].graphs);
609
+ }
610
+ });
611
+ device.on("resizecanvas", function () {
612
+ _this.updateDiv();
613
+ });
614
+ app.on('postrender', function () {
615
+ if (_this._enabled) {
616
+ _this.render();
617
+ }
618
+ });
619
+ this.device = device;
620
+ this.texture = texture;
621
+ this.wordAtlas = wordAtlasData.atlas;
622
+ this.render2d = new Render2d(device, options.colors);
623
+ this.graphs = graphs;
624
+ this.div = div;
625
+ this.width = 0;
626
+ this.height = 0;
627
+ this.gspacing = 2;
628
+ this.clr = [1, 1, 1, 0.5];
629
+ this._enabled = true;
630
+ this.activeSizeIndex = this._activeSizeIndex;
631
+ }
632
+
633
+ MiniStats.getDefaultOptions = function getDefaultOptions() {
634
+ return {
635
+ sizes: [{
636
+ width: 100,
637
+ height: 16,
638
+ spacing: 0,
639
+ graphs: false
640
+ }, {
641
+ width: 128,
642
+ height: 32,
643
+ spacing: 2,
644
+ graphs: true
645
+ }, {
646
+ width: 256,
647
+ height: 64,
648
+ spacing: 2,
649
+ graphs: true
650
+ }],
651
+ startSizeIndex: 0,
652
+ textRefreshRate: 500,
653
+ colors: {
654
+ graph0: new pc.Color(0.7, 0.2, 0.2, 1),
655
+ graph1: new pc.Color(0.2, 0.7, 0.2, 1),
656
+ graph2: new pc.Color(0.2, 0.2, 0.7, 1),
657
+ watermark: new pc.Color(0.4, 0.4, 0.2, 1),
658
+ background: new pc.Color(0, 0, 0, 1.0)
659
+ },
660
+ cpu: {
661
+ enabled: true,
662
+ watermark: 33
663
+ },
664
+ gpu: {
665
+ enabled: true,
666
+ watermark: 33
667
+ },
668
+ stats: [{
669
+ name: "Frame",
670
+ stats: ["frame.ms"],
671
+ decimalPlaces: 1,
672
+ unitsName: "ms",
673
+ watermark: 33
674
+ }, {
675
+ name: "DrawCalls",
676
+ stats: ["drawCalls.total"],
677
+ watermark: 1000
678
+ }]
679
+ };
680
+ };
681
+
682
+ var _proto = MiniStats.prototype;
683
+
684
+ _proto.initWordAtlas = function initWordAtlas(device, words, maxWidth, numGraphs) {
685
+ var texture = new pc.Texture(device, {
686
+ name: 'mini-stats',
687
+ width: pc.math.nextPowerOfTwo(maxWidth),
688
+ height: 64,
689
+ mipmaps: false,
690
+ minFilter: pc.FILTER_NEAREST,
691
+ magFilter: pc.FILTER_NEAREST
692
+ });
693
+ var wordAtlas = new WordAtlas(texture, words);
694
+ var dest = texture.lock();
695
+
696
+ for (var i = 0; i < texture.width * numGraphs; ++i) {
697
+ dest.set([0, 0, 0, 255], i * 4);
698
+ }
699
+
700
+ texture.unlock();
701
+ device.setTexture(texture, 0);
702
+ return {
703
+ atlas: wordAtlas,
704
+ texture: texture
705
+ };
706
+ };
707
+
708
+ _proto.initGraphs = function initGraphs(app, device, options) {
709
+ var graphs = [];
710
+
711
+ if (options.cpu.enabled) {
712
+ var timer = new CpuTimer(app);
713
+ var graph = new Graph('CPU', app, options.cpu.watermark, options.textRefreshRate, timer);
714
+ graphs.push(graph);
715
+ }
716
+
717
+ if (options.gpu.enabled && device.extDisjointTimerQuery) {
718
+ var _timer = new GpuTimer(app);
719
+
720
+ var _graph = new Graph('GPU', app, options.gpu.watermark, options.textRefreshRate, _timer);
721
+
722
+ graphs.push(_graph);
723
+ }
724
+
725
+ if (options.stats) {
726
+ options.stats.forEach(function (entry) {
727
+ var timer = new StatsTimer(app, entry.stats, entry.decimalPlaces, entry.unitsName, entry.multiplier);
728
+ var graph = new Graph(entry.name, app, entry.watermark, options.textRefreshRate, timer);
729
+ graphs.push(graph);
730
+ });
731
+ }
732
+
733
+ return graphs;
734
+ };
735
+
736
+ _proto.render = function render() {
737
+ var graphs = this.graphs;
738
+ var wordAtlas = this.wordAtlas;
739
+ var render2d = this.render2d;
740
+ var width = this.width;
741
+ var height = this.height;
742
+ var gspacing = this.gspacing;
743
+
744
+ for (var i = 0; i < graphs.length; ++i) {
745
+ var graph = graphs[i];
746
+ var y = i * (height + gspacing);
747
+ graph.render(render2d, 0, y, width, height);
748
+ var x = 1;
749
+ y += height - 13;
750
+ x += wordAtlas.render(render2d, graph.name, x, y) + 10;
751
+ var timingText = graph.timingText;
752
+
753
+ for (var j = 0; j < timingText.length; ++j) {
754
+ x += wordAtlas.render(render2d, timingText[j], x, y);
755
+ }
756
+
757
+ if (graph.timer.unitsName) {
758
+ x += 3;
759
+ wordAtlas.render(render2d, graph.timer.unitsName, x, y);
760
+ }
761
+ }
762
+
763
+ render2d.render(this.clr, height);
764
+ };
765
+
766
+ _proto.resize = function resize(width, height, showGraphs) {
767
+ var graphs = this.graphs;
768
+
769
+ for (var i = 0; i < graphs.length; ++i) {
770
+ graphs[i].enabled = showGraphs;
771
+ }
772
+
773
+ this.width = width;
774
+ this.height = height;
775
+ this.updateDiv();
776
+ };
777
+
778
+ _proto.updateDiv = function updateDiv() {
779
+ var rect = this.device.canvas.getBoundingClientRect();
780
+ this.div.style.left = rect.left + "px";
781
+ this.div.style.bottom = window.innerHeight - rect.bottom + "px";
782
+ this.div.style.width = this.width + "px";
783
+ this.div.style.height = this.overallHeight + "px";
784
+ };
785
+
786
+ _createClass(MiniStats, [{
787
+ key: "activeSizeIndex",
788
+ get: function get() {
789
+ return this._activeSizeIndex;
790
+ },
791
+ set: function set(value) {
792
+ this._activeSizeIndex = value;
793
+ this.gspacing = this.sizes[value].spacing;
794
+ this.resize(this.sizes[value].width, this.sizes[value].height, this.sizes[value].graphs);
795
+ }
796
+ }, {
797
+ key: "opacity",
798
+ get: function get() {
799
+ return this.clr[3];
800
+ },
801
+ set: function set(value) {
802
+ this.clr[3] = value;
803
+ }
804
+ }, {
805
+ key: "overallHeight",
806
+ get: function get() {
807
+ var graphs = this.graphs;
808
+ var spacing = this.gspacing;
809
+ return this.height * graphs.length + spacing * (graphs.length - 1);
810
+ }
811
+ }, {
812
+ key: "enabled",
813
+ get: function get() {
814
+ return this._enabled;
815
+ },
816
+ set: function set(value) {
817
+ if (value !== this._enabled) {
818
+ this._enabled = value;
819
+
820
+ for (var i = 0; i < this.graphs.length; ++i) {
821
+ this.graphs[i].enabled = value;
822
+ this.graphs[i].timer.enabled = value;
823
+ }
824
+ }
825
+ }
826
+ }]);
827
+
828
+ return MiniStats;
829
829
  }();
830
830
 
831
831
  exports.MiniStats = MiniStats;