@galacean/effects-plugin-stats 2.2.0-alpha.0 → 2.2.0

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.
package/dist/index.mjs CHANGED
@@ -3,7 +3,7 @@
3
3
  * Description: Galacean Effects runtime stats for the web
4
4
  * Author: Ant Group CO., Ltd.
5
5
  * Contributors: 云垣
6
- * Version: v2.2.0-alpha.0
6
+ * Version: v2.2.0
7
7
  */
8
8
 
9
9
  import * as EFFECTS from '@galacean/effects';
@@ -36,6 +36,17 @@ function _async_to_generator(fn) {
36
36
  };
37
37
  }
38
38
 
39
+ function _extends() {
40
+ _extends = Object.assign || function assign(target) {
41
+ for(var i = 1; i < arguments.length; i++){
42
+ var source = arguments[i];
43
+ for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
44
+ }
45
+ return target;
46
+ };
47
+ return _extends.apply(this, arguments);
48
+ }
49
+
39
50
  function _instanceof1(left, right) {
40
51
  if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
41
52
  return !!right[Symbol.hasInstance](left);
@@ -160,9 +171,12 @@ function _inherits(subClass, superClass) {
160
171
  if (superClass) _set_prototype_of(subClass, superClass);
161
172
  }
162
173
 
163
- var DrawCallHook = /*#__PURE__*/ function() {
164
- function DrawCallHook(gl) {
174
+ /**
175
+ * DrawCallHook
176
+ */ var DrawCallHook = /*#__PURE__*/ function() {
177
+ function DrawCallHook(gl, debug) {
165
178
  this.gl = gl;
179
+ this.debug = debug;
166
180
  this.drawCall = 0;
167
181
  this.triangles = 0;
168
182
  this.lines = 0;
@@ -172,7 +186,7 @@ var DrawCallHook = /*#__PURE__*/ function() {
172
186
  this.realDrawArrays = gl.drawArrays;
173
187
  gl.drawElements = this.hookedDrawElements.bind(this);
174
188
  gl.drawArrays = this.hookedDrawArrays.bind(this);
175
- if (Stats.options.debug) {
189
+ if (debug) {
176
190
  console.debug("DrawCall is hooked.");
177
191
  }
178
192
  }
@@ -229,14 +243,18 @@ var DrawCallHook = /*#__PURE__*/ function() {
229
243
  return DrawCallHook;
230
244
  }();
231
245
 
232
- var ProgramHook = /*#__PURE__*/ function() {
233
- function ProgramHook(gl) {
246
+ /**
247
+ * ProgramHook
248
+ * 每一帧的 shader 数量
249
+ */ var ProgramHook = /*#__PURE__*/ function() {
250
+ function ProgramHook(gl, debug) {
234
251
  this.gl = gl;
252
+ this.debug = debug;
235
253
  this.programs = 0;
236
254
  this.hooked = true;
237
255
  this.realUseProgram = gl.useProgram;
238
256
  gl.useProgram = this.hookedUseProgram.bind(this);
239
- if (Stats.options.debug) {
257
+ if (debug) {
240
258
  console.debug("Program is hooked.");
241
259
  }
242
260
  }
@@ -244,7 +262,7 @@ var ProgramHook = /*#__PURE__*/ function() {
244
262
  _proto.hookedUseProgram = function hookedUseProgram(program) {
245
263
  this.realUseProgram.call(this.gl, program);
246
264
  this.programs++;
247
- if (Stats.options.debug) {
265
+ if (this.debug) {
248
266
  console.debug("UseProgram: " + program + ", program: " + this.programs + ".");
249
267
  }
250
268
  };
@@ -260,16 +278,19 @@ var ProgramHook = /*#__PURE__*/ function() {
260
278
  return ProgramHook;
261
279
  }();
262
280
 
263
- var ShaderHook = /*#__PURE__*/ function() {
264
- function ShaderHook(gl) {
281
+ /**
282
+ * ShaderHook
283
+ */ var ShaderHook = /*#__PURE__*/ function() {
284
+ function ShaderHook(gl, debug) {
265
285
  this.gl = gl;
286
+ this.debug = debug;
266
287
  this.shaders = 0;
267
288
  this.hooked = true;
268
289
  this.realAttachShader = gl.attachShader;
269
290
  this.realDetachShader = gl.detachShader;
270
291
  gl.attachShader = this.hookedAttachShader.bind(this);
271
292
  gl.detachShader = this.hookedDetachShader.bind(this);
272
- if (Stats.options.debug) {
293
+ if (debug) {
273
294
  console.debug("Shader is hooked.");
274
295
  }
275
296
  }
@@ -277,14 +298,14 @@ var ShaderHook = /*#__PURE__*/ function() {
277
298
  _proto.hookedAttachShader = function hookedAttachShader(program, shader) {
278
299
  this.realAttachShader.call(this.gl, program, shader);
279
300
  this.shaders++;
280
- if (Stats.options.debug) {
301
+ if (this.debug) {
281
302
  console.debug("AttachShader: " + shader + ". shaders: " + this.shaders);
282
303
  }
283
304
  };
284
305
  _proto.hookedDetachShader = function hookedDetachShader(program, shader) {
285
306
  this.realDetachShader.call(this.gl, program, shader);
286
307
  this.shaders--;
287
- if (Stats.options.debug) {
308
+ if (this.debug) {
288
309
  console.debug("DetachShader. shaders: " + this.shaders);
289
310
  }
290
311
  };
@@ -301,16 +322,19 @@ var ShaderHook = /*#__PURE__*/ function() {
301
322
  return ShaderHook;
302
323
  }();
303
324
 
304
- var TextureHook = /*#__PURE__*/ function() {
305
- function TextureHook(gl) {
325
+ /**
326
+ * TextureHook
327
+ */ var TextureHook = /*#__PURE__*/ function() {
328
+ function TextureHook(gl, debug) {
306
329
  this.gl = gl;
330
+ this.debug = debug;
307
331
  this.textures = 0;
308
332
  this.hooked = true;
309
333
  this.realCreateTexture = gl.createTexture;
310
334
  this.realDeleteTexture = gl.deleteTexture;
311
335
  gl.createTexture = this.hookedCreateTexture.bind(this);
312
336
  gl.deleteTexture = this.hookedDeleteTexture.bind(this);
313
- if (Stats.options.debug) {
337
+ if (debug) {
314
338
  console.debug("Texture is hooked.");
315
339
  }
316
340
  }
@@ -318,7 +342,7 @@ var TextureHook = /*#__PURE__*/ function() {
318
342
  _proto.hookedCreateTexture = function hookedCreateTexture() {
319
343
  var texture = this.realCreateTexture.call(this.gl);
320
344
  this.textures++;
321
- if (Stats.options.debug) {
345
+ if (this.debug) {
322
346
  console.debug("CreateTexture: " + texture + ", textures: " + this.textures + ".");
323
347
  }
324
348
  return texture;
@@ -326,7 +350,7 @@ var TextureHook = /*#__PURE__*/ function() {
326
350
  _proto.hookedDeleteTexture = function hookedDeleteTexture(texture) {
327
351
  this.realDeleteTexture.call(this.gl, texture);
328
352
  this.textures--;
329
- if (Stats.options.debug) {
353
+ if (this.debug) {
330
354
  console.debug("DeleteTexture, textures: " + this.textures + ".");
331
355
  }
332
356
  };
@@ -346,7 +370,8 @@ var TextureHook = /*#__PURE__*/ function() {
346
370
  /**
347
371
  * Hook gl to calculate stats
348
372
  */ var Core = /*#__PURE__*/ function() {
349
- function Core(gl) {
373
+ function Core(gl, debug) {
374
+ if (debug === void 0) debug = false;
350
375
  this.gl = gl;
351
376
  this.samplingFrames = 60;
352
377
  this.samplingIndex = 0;
@@ -355,14 +380,14 @@ var TextureHook = /*#__PURE__*/ function() {
355
380
  if (!gl) {
356
381
  throw new Error("Unsupported WebGL context.");
357
382
  }
358
- this.hook(gl);
383
+ this.hook(gl, debug);
359
384
  }
360
385
  var _proto = Core.prototype;
361
- _proto.hook = function hook(gl) {
362
- this.drawCallHook = new DrawCallHook(gl);
363
- this.textureHook = new TextureHook(gl);
364
- this.shaderHook = new ShaderHook(gl);
365
- this.programHook = new ProgramHook(gl);
386
+ _proto.hook = function hook(gl, debug) {
387
+ this.drawCallHook = new DrawCallHook(gl, debug);
388
+ this.textureHook = new TextureHook(gl, debug);
389
+ this.shaderHook = new ShaderHook(gl, debug);
390
+ this.programHook = new ProgramHook(gl, debug);
366
391
  };
367
392
  /**
368
393
  * reset draw call hook
@@ -398,12 +423,12 @@ var TextureHook = /*#__PURE__*/ function() {
398
423
  fps: Math.round(this.updateCounter * 1000 / (now - this.updateTime)),
399
424
  // eslint-disable-next-line compat/compat -- performance.memory is not standard
400
425
  memory: performance.memory && performance.memory.usedJSHeapSize / 1048576 >> 0,
401
- drawCall: this.drawCallHook.drawCall === 0 ? 0 : this.drawCallHook.drawCall - 1,
402
- triangles: this.drawCallHook.triangles === 0 ? 0 : this.drawCallHook.triangles - 2,
426
+ drawCall: this.drawCallHook.drawCall === 0 ? 0 : this.drawCallHook.drawCall,
427
+ triangles: this.drawCallHook.triangles === 0 ? 0 : this.drawCallHook.triangles,
403
428
  lines: this.drawCallHook.lines,
404
429
  points: this.drawCallHook.points,
405
430
  textures: this.textureHook.textures,
406
- shaders: this.shaderHook.shaders === 0 ? 0 : this.shaderHook.shaders + 1,
431
+ shaders: this.shaderHook.shaders === 0 ? 0 : this.shaderHook.shaders,
407
432
  programs: this.programHook.programs,
408
433
  webglContext: _instanceof1(this.gl, WebGL2RenderingContext) ? "2.0" : "1.0"
409
434
  };
@@ -416,9 +441,10 @@ var TextureHook = /*#__PURE__*/ function() {
416
441
  }();
417
442
 
418
443
  var tpl = '\n <dl>\n <dt>FPS</dt>\n <dd>0</dd>\n <dt>Memory <span class="unit">(MB)</span></dt>\n <dd>0</dd>\n <dt>DrawCall</dt>\n <dd>0</dd>\n <dt>Triangles</dt>\n <dd>0</dd>\n <dt>Textures</dt>\n <dd>0</dd>\n <dt>Shaders</dt>\n <dd>0</dd>\n <dt>Program</dt>\n <dd>0</dd>\n <dt>WebGL</dt>\n <dd></dd>\n </dl>\n';
419
- var css = "\n .gl-perf {\n pointer-events: none;\n user-select: none;\n position: fixed;\n top: 0;\n left: 0;\n padding: " + 10 / 7.5 + "vh " + 10 / 7.5 + "vh 0 " + 10 / 7.5 + "vh;\n background: rgba(0, 0, 0, 0.3);\n color: #fff;\n font: " + 10 / 7.5 + "vh arial;\n }\n\n .gl-perf dl,\n .gl-perf dt,\n .gl-perf dd {\n padding: 0;\n margin: 0;\n }\n\n .gl-perf dt {\n color: #fff;\n text-shadow: #000 0 0 1px;\n }\n\n .gl-perf dt .unit{\n font-size: " + 10 / 7.5 + "vh;\n }\n\n .gl-perf dd {\n font-size: " + 20 / 7.5 + "vh;\n padding: " + 10 / 7.5 + "vh 0 " + 10 / 7.5 + "vh;\n }\n";
444
+ var css = "\n .gl-perf {\n pointer-events: none;\n user-select: none;\n position: absolute;\n top: 0;\n left: 0;\n padding: " + 10 / 7.5 + "vh " + 10 / 7.5 + "vh 0 " + 10 / 7.5 + "vh;\n background: rgba(0, 0, 0, 0.3);\n color: #fff;\n font: " + 10 / 7.5 + "vh arial;\n }\n\n .gl-perf dl,\n .gl-perf dt,\n .gl-perf dd {\n padding: 0;\n margin: 0;\n }\n\n .gl-perf dt {\n color: #fff;\n text-shadow: #000 0 0 1px;\n }\n\n .gl-perf dt .unit{\n font-size: " + 10 / 7.5 + "vh;\n }\n\n .gl-perf dd {\n font-size: " + 20 / 7.5 + "vh;\n padding: " + 10 / 7.5 + "vh 0 " + 10 / 7.5 + "vh;\n }\n";
420
445
  var Monitor = /*#__PURE__*/ function() {
421
- function Monitor(gl) {
446
+ function Monitor(gl, options) {
447
+ this.options = options;
422
448
  this.items = [
423
449
  "fps",
424
450
  "memory",
@@ -441,19 +467,22 @@ var Monitor = /*#__PURE__*/ function() {
441
467
  programs: 0,
442
468
  webglContext: "2.0"
443
469
  };
444
- this.core = new Core(gl);
470
+ this.core = new Core(gl, options.debug);
445
471
  this.createContainer();
446
472
  this.update = this.update.bind(this);
447
473
  }
448
474
  var _proto = Monitor.prototype;
449
475
  _proto.createContainer = function createContainer() {
450
476
  var container = document.createElement("div");
477
+ this.container = container;
478
+ if (!this.options.visible) {
479
+ this.hide();
480
+ }
451
481
  container.classList.add("gl-perf");
452
482
  container.innerHTML = tpl;
453
483
  container.appendChild(this.createStyle());
454
- document.body.appendChild(container);
484
+ this.options.container.appendChild(container);
455
485
  this.doms = Array.prototype.slice.apply(container.querySelectorAll("dd"));
456
- this.container = container;
457
486
  };
458
487
  _proto.createStyle = function createStyle() {
459
488
  var style = document.createElement("style");
@@ -473,11 +502,17 @@ var Monitor = /*#__PURE__*/ function() {
473
502
  });
474
503
  };
475
504
  var data = this.core.update(dt);
476
- if (!data || data.drawCall === 0 || data.triangles === 0) {
505
+ if (!data || data.triangles === 0) {
477
506
  return;
478
507
  }
479
508
  for(var i = 0, l = this.items.length; i < l; i++)_this = this, _loop(i);
480
509
  };
510
+ _proto.hide = function hide() {
511
+ this.container.style.display = "none";
512
+ };
513
+ _proto.show = function show() {
514
+ this.container.style.display = "block";
515
+ };
481
516
  /**
482
517
  * reset all hooks
483
518
  */ _proto.reset = function reset() {
@@ -502,7 +537,7 @@ var Monitor = /*#__PURE__*/ function() {
502
537
  * dispose the instance
503
538
  */ _proto.dispose = function dispose() {
504
539
  this.release();
505
- document.body.removeChild(this.container);
540
+ this.container.remove();
506
541
  };
507
542
  return Monitor;
508
543
  }();
@@ -513,10 +548,10 @@ var StatsComponent = /*#__PURE__*/ function(Behaviour) {
513
548
  return Behaviour.apply(this, arguments);
514
549
  }
515
550
  var _proto = StatsComponent.prototype;
516
- _proto.onStart = function onStart() {
551
+ _proto.init = function init(options) {
517
552
  var renderer = this.engine.renderer;
518
553
  var gl = renderer.pipelineContext.gl;
519
- this.monitor = new Monitor(gl);
554
+ this.monitor = new Monitor(gl, options);
520
555
  };
521
556
  _proto.onUpdate = function onUpdate(dt) {
522
557
  this.monitor.update(dt);
@@ -528,6 +563,11 @@ var StatsComponent = /*#__PURE__*/ function(Behaviour) {
528
563
  return StatsComponent;
529
564
  }(Behaviour);
530
565
 
566
+ var defaultStatsOptions = {
567
+ debug: false,
568
+ visible: true,
569
+ container: document.body
570
+ };
531
571
  var json = {
532
572
  "images": [],
533
573
  "fonts": [],
@@ -622,18 +662,16 @@ var json = {
622
662
  *
623
663
  */ var Stats = /*#__PURE__*/ function() {
624
664
  function Stats(player, options) {
625
- if (options === void 0) options = {
626
- debug: false
627
- };
628
665
  this.player = player;
629
- Stats.options = options;
666
+ this.options = options;
667
+ this.disposed = false;
630
668
  void this.init();
631
669
  }
632
670
  var _proto = Stats.prototype;
633
671
  _proto.init = function init() {
634
672
  var _this = this;
635
673
  return _async_to_generator(function() {
636
- var composition, component, e;
674
+ var composition, item, e;
637
675
  return __generator(this, function(_state) {
638
676
  switch(_state.label){
639
677
  case 0:
@@ -649,8 +687,11 @@ var json = {
649
687
  ];
650
688
  case 1:
651
689
  composition = _state.sent();
652
- component = composition.getItemByName("component");
653
- component == null ? void 0 : component.addComponent(StatsComponent);
690
+ item = composition.getItemByName("component");
691
+ if (item) {
692
+ item.addComponent(StatsComponent).init(_extends({}, defaultStatsOptions, _this.options));
693
+ _this.component = item.getComponent(StatsComponent);
694
+ }
654
695
  return [
655
696
  3,
656
697
  3
@@ -666,12 +707,29 @@ var json = {
666
707
  });
667
708
  })();
668
709
  };
710
+ _proto.hide = function hide() {
711
+ var _this_component;
712
+ (_this_component = this.component) == null ? void 0 : _this_component.monitor.hide();
713
+ };
714
+ _proto.show = function show() {
715
+ var _this_component;
716
+ (_this_component = this.component) == null ? void 0 : _this_component.monitor.show();
717
+ };
718
+ _proto.dispose = function dispose() {
719
+ var _this_component;
720
+ if (this.disposed) {
721
+ return;
722
+ }
723
+ this.disposed = true;
724
+ (_this_component = this.component) == null ? void 0 : _this_component.dispose();
725
+ this.component = undefined;
726
+ };
669
727
  return Stats;
670
728
  }();
671
729
 
672
730
  /**
673
731
  * 插件版本号
674
- */ var version = "2.2.0-alpha.0";
732
+ */ var version = "2.2.0";
675
733
  logger.info("Plugin stats version: " + version + ".");
676
734
  if (version !== EFFECTS.version) {
677
735
  console.error("注意:请统一 Stats 插件与 Player 版本,不统一的版本混用会有不可预知的后果!", "\nAttention: Please ensure the Stats plugin is synchronized with the Player version. Mixing and matching incompatible versions may result in unpredictable consequences!");