@galacean/effects-plugin-stats 2.1.5 → 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.1.5
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);
@@ -51,12 +62,8 @@ function __generator(thisArg, body) {
51
62
  },
52
63
  trys: [],
53
64
  ops: []
54
- }, f, y, t, g;
55
- return g = {
56
- next: verb(0),
57
- "throw": verb(1),
58
- "return": verb(2)
59
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
65
+ }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
66
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
60
67
  return this;
61
68
  }), g;
62
69
  function verb(n) {
@@ -164,9 +171,12 @@ function _inherits(subClass, superClass) {
164
171
  if (superClass) _set_prototype_of(subClass, superClass);
165
172
  }
166
173
 
167
- var DrawCallHook = /*#__PURE__*/ function() {
168
- function DrawCallHook(gl) {
174
+ /**
175
+ * DrawCallHook
176
+ */ var DrawCallHook = /*#__PURE__*/ function() {
177
+ function DrawCallHook(gl, debug) {
169
178
  this.gl = gl;
179
+ this.debug = debug;
170
180
  this.drawCall = 0;
171
181
  this.triangles = 0;
172
182
  this.lines = 0;
@@ -176,7 +186,7 @@ var DrawCallHook = /*#__PURE__*/ function() {
176
186
  this.realDrawArrays = gl.drawArrays;
177
187
  gl.drawElements = this.hookedDrawElements.bind(this);
178
188
  gl.drawArrays = this.hookedDrawArrays.bind(this);
179
- if (Stats.options.debug) {
189
+ if (debug) {
180
190
  console.debug("DrawCall is hooked.");
181
191
  }
182
192
  }
@@ -233,14 +243,18 @@ var DrawCallHook = /*#__PURE__*/ function() {
233
243
  return DrawCallHook;
234
244
  }();
235
245
 
236
- var ProgramHook = /*#__PURE__*/ function() {
237
- function ProgramHook(gl) {
246
+ /**
247
+ * ProgramHook
248
+ * 每一帧的 shader 数量
249
+ */ var ProgramHook = /*#__PURE__*/ function() {
250
+ function ProgramHook(gl, debug) {
238
251
  this.gl = gl;
252
+ this.debug = debug;
239
253
  this.programs = 0;
240
254
  this.hooked = true;
241
255
  this.realUseProgram = gl.useProgram;
242
256
  gl.useProgram = this.hookedUseProgram.bind(this);
243
- if (Stats.options.debug) {
257
+ if (debug) {
244
258
  console.debug("Program is hooked.");
245
259
  }
246
260
  }
@@ -248,7 +262,7 @@ var ProgramHook = /*#__PURE__*/ function() {
248
262
  _proto.hookedUseProgram = function hookedUseProgram(program) {
249
263
  this.realUseProgram.call(this.gl, program);
250
264
  this.programs++;
251
- if (Stats.options.debug) {
265
+ if (this.debug) {
252
266
  console.debug("UseProgram: " + program + ", program: " + this.programs + ".");
253
267
  }
254
268
  };
@@ -264,16 +278,19 @@ var ProgramHook = /*#__PURE__*/ function() {
264
278
  return ProgramHook;
265
279
  }();
266
280
 
267
- var ShaderHook = /*#__PURE__*/ function() {
268
- function ShaderHook(gl) {
281
+ /**
282
+ * ShaderHook
283
+ */ var ShaderHook = /*#__PURE__*/ function() {
284
+ function ShaderHook(gl, debug) {
269
285
  this.gl = gl;
286
+ this.debug = debug;
270
287
  this.shaders = 0;
271
288
  this.hooked = true;
272
289
  this.realAttachShader = gl.attachShader;
273
290
  this.realDetachShader = gl.detachShader;
274
291
  gl.attachShader = this.hookedAttachShader.bind(this);
275
292
  gl.detachShader = this.hookedDetachShader.bind(this);
276
- if (Stats.options.debug) {
293
+ if (debug) {
277
294
  console.debug("Shader is hooked.");
278
295
  }
279
296
  }
@@ -281,14 +298,14 @@ var ShaderHook = /*#__PURE__*/ function() {
281
298
  _proto.hookedAttachShader = function hookedAttachShader(program, shader) {
282
299
  this.realAttachShader.call(this.gl, program, shader);
283
300
  this.shaders++;
284
- if (Stats.options.debug) {
301
+ if (this.debug) {
285
302
  console.debug("AttachShader: " + shader + ". shaders: " + this.shaders);
286
303
  }
287
304
  };
288
305
  _proto.hookedDetachShader = function hookedDetachShader(program, shader) {
289
306
  this.realDetachShader.call(this.gl, program, shader);
290
307
  this.shaders--;
291
- if (Stats.options.debug) {
308
+ if (this.debug) {
292
309
  console.debug("DetachShader. shaders: " + this.shaders);
293
310
  }
294
311
  };
@@ -305,16 +322,19 @@ var ShaderHook = /*#__PURE__*/ function() {
305
322
  return ShaderHook;
306
323
  }();
307
324
 
308
- var TextureHook = /*#__PURE__*/ function() {
309
- function TextureHook(gl) {
325
+ /**
326
+ * TextureHook
327
+ */ var TextureHook = /*#__PURE__*/ function() {
328
+ function TextureHook(gl, debug) {
310
329
  this.gl = gl;
330
+ this.debug = debug;
311
331
  this.textures = 0;
312
332
  this.hooked = true;
313
333
  this.realCreateTexture = gl.createTexture;
314
334
  this.realDeleteTexture = gl.deleteTexture;
315
335
  gl.createTexture = this.hookedCreateTexture.bind(this);
316
336
  gl.deleteTexture = this.hookedDeleteTexture.bind(this);
317
- if (Stats.options.debug) {
337
+ if (debug) {
318
338
  console.debug("Texture is hooked.");
319
339
  }
320
340
  }
@@ -322,7 +342,7 @@ var TextureHook = /*#__PURE__*/ function() {
322
342
  _proto.hookedCreateTexture = function hookedCreateTexture() {
323
343
  var texture = this.realCreateTexture.call(this.gl);
324
344
  this.textures++;
325
- if (Stats.options.debug) {
345
+ if (this.debug) {
326
346
  console.debug("CreateTexture: " + texture + ", textures: " + this.textures + ".");
327
347
  }
328
348
  return texture;
@@ -330,7 +350,7 @@ var TextureHook = /*#__PURE__*/ function() {
330
350
  _proto.hookedDeleteTexture = function hookedDeleteTexture(texture) {
331
351
  this.realDeleteTexture.call(this.gl, texture);
332
352
  this.textures--;
333
- if (Stats.options.debug) {
353
+ if (this.debug) {
334
354
  console.debug("DeleteTexture, textures: " + this.textures + ".");
335
355
  }
336
356
  };
@@ -350,7 +370,8 @@ var TextureHook = /*#__PURE__*/ function() {
350
370
  /**
351
371
  * Hook gl to calculate stats
352
372
  */ var Core = /*#__PURE__*/ function() {
353
- function Core(gl) {
373
+ function Core(gl, debug) {
374
+ if (debug === void 0) debug = false;
354
375
  this.gl = gl;
355
376
  this.samplingFrames = 60;
356
377
  this.samplingIndex = 0;
@@ -359,14 +380,14 @@ var TextureHook = /*#__PURE__*/ function() {
359
380
  if (!gl) {
360
381
  throw new Error("Unsupported WebGL context.");
361
382
  }
362
- this.hook(gl);
383
+ this.hook(gl, debug);
363
384
  }
364
385
  var _proto = Core.prototype;
365
- _proto.hook = function hook(gl) {
366
- this.drawCallHook = new DrawCallHook(gl);
367
- this.textureHook = new TextureHook(gl);
368
- this.shaderHook = new ShaderHook(gl);
369
- 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);
370
391
  };
371
392
  /**
372
393
  * reset draw call hook
@@ -402,12 +423,12 @@ var TextureHook = /*#__PURE__*/ function() {
402
423
  fps: Math.round(this.updateCounter * 1000 / (now - this.updateTime)),
403
424
  // eslint-disable-next-line compat/compat -- performance.memory is not standard
404
425
  memory: performance.memory && performance.memory.usedJSHeapSize / 1048576 >> 0,
405
- drawCall: this.drawCallHook.drawCall === 0 ? 0 : this.drawCallHook.drawCall - 1,
406
- 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,
407
428
  lines: this.drawCallHook.lines,
408
429
  points: this.drawCallHook.points,
409
430
  textures: this.textureHook.textures,
410
- shaders: this.shaderHook.shaders === 0 ? 0 : this.shaderHook.shaders + 1,
431
+ shaders: this.shaderHook.shaders === 0 ? 0 : this.shaderHook.shaders,
411
432
  programs: this.programHook.programs,
412
433
  webglContext: _instanceof1(this.gl, WebGL2RenderingContext) ? "2.0" : "1.0"
413
434
  };
@@ -420,9 +441,10 @@ var TextureHook = /*#__PURE__*/ function() {
420
441
  }();
421
442
 
422
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';
423
- 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";
424
445
  var Monitor = /*#__PURE__*/ function() {
425
- function Monitor(gl) {
446
+ function Monitor(gl, options) {
447
+ this.options = options;
426
448
  this.items = [
427
449
  "fps",
428
450
  "memory",
@@ -445,19 +467,22 @@ var Monitor = /*#__PURE__*/ function() {
445
467
  programs: 0,
446
468
  webglContext: "2.0"
447
469
  };
448
- this.core = new Core(gl);
470
+ this.core = new Core(gl, options.debug);
449
471
  this.createContainer();
450
472
  this.update = this.update.bind(this);
451
473
  }
452
474
  var _proto = Monitor.prototype;
453
475
  _proto.createContainer = function createContainer() {
454
476
  var container = document.createElement("div");
477
+ this.container = container;
478
+ if (!this.options.visible) {
479
+ this.hide();
480
+ }
455
481
  container.classList.add("gl-perf");
456
482
  container.innerHTML = tpl;
457
483
  container.appendChild(this.createStyle());
458
- document.body.appendChild(container);
484
+ this.options.container.appendChild(container);
459
485
  this.doms = Array.prototype.slice.apply(container.querySelectorAll("dd"));
460
- this.container = container;
461
486
  };
462
487
  _proto.createStyle = function createStyle() {
463
488
  var style = document.createElement("style");
@@ -477,11 +502,17 @@ var Monitor = /*#__PURE__*/ function() {
477
502
  });
478
503
  };
479
504
  var data = this.core.update(dt);
480
- if (!data || data.drawCall === 0 || data.triangles === 0) {
505
+ if (!data || data.triangles === 0) {
481
506
  return;
482
507
  }
483
508
  for(var i = 0, l = this.items.length; i < l; i++)_this = this, _loop(i);
484
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
+ };
485
516
  /**
486
517
  * reset all hooks
487
518
  */ _proto.reset = function reset() {
@@ -506,7 +537,7 @@ var Monitor = /*#__PURE__*/ function() {
506
537
  * dispose the instance
507
538
  */ _proto.dispose = function dispose() {
508
539
  this.release();
509
- document.body.removeChild(this.container);
540
+ this.container.remove();
510
541
  };
511
542
  return Monitor;
512
543
  }();
@@ -517,10 +548,10 @@ var StatsComponent = /*#__PURE__*/ function(Behaviour) {
517
548
  return Behaviour.apply(this, arguments);
518
549
  }
519
550
  var _proto = StatsComponent.prototype;
520
- _proto.onStart = function onStart() {
551
+ _proto.init = function init(options) {
521
552
  var renderer = this.engine.renderer;
522
553
  var gl = renderer.pipelineContext.gl;
523
- this.monitor = new Monitor(gl);
554
+ this.monitor = new Monitor(gl, options);
524
555
  };
525
556
  _proto.onUpdate = function onUpdate(dt) {
526
557
  this.monitor.update(dt);
@@ -532,6 +563,11 @@ var StatsComponent = /*#__PURE__*/ function(Behaviour) {
532
563
  return StatsComponent;
533
564
  }(Behaviour);
534
565
 
566
+ var defaultStatsOptions = {
567
+ debug: false,
568
+ visible: true,
569
+ container: document.body
570
+ };
535
571
  var json = {
536
572
  "images": [],
537
573
  "fonts": [],
@@ -626,18 +662,16 @@ var json = {
626
662
  *
627
663
  */ var Stats = /*#__PURE__*/ function() {
628
664
  function Stats(player, options) {
629
- if (options === void 0) options = {
630
- debug: false
631
- };
632
665
  this.player = player;
633
- Stats.options = options;
666
+ this.options = options;
667
+ this.disposed = false;
634
668
  void this.init();
635
669
  }
636
670
  var _proto = Stats.prototype;
637
671
  _proto.init = function init() {
638
672
  var _this = this;
639
673
  return _async_to_generator(function() {
640
- var composition, component, e;
674
+ var composition, item, e;
641
675
  return __generator(this, function(_state) {
642
676
  switch(_state.label){
643
677
  case 0:
@@ -653,8 +687,11 @@ var json = {
653
687
  ];
654
688
  case 1:
655
689
  composition = _state.sent();
656
- component = composition.getItemByName("component");
657
- 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
+ }
658
695
  return [
659
696
  3,
660
697
  3
@@ -670,12 +707,29 @@ var json = {
670
707
  });
671
708
  })();
672
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
+ };
673
727
  return Stats;
674
728
  }();
675
729
 
676
730
  /**
677
731
  * 插件版本号
678
- */ var version = "2.1.5";
732
+ */ var version = "2.2.0";
679
733
  logger.info("Plugin stats version: " + version + ".");
680
734
  if (version !== EFFECTS.version) {
681
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!");