@galacean/effects-plugin-stats 2.2.0-alpha.0 → 2.2.1-alpha.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/core.d.ts CHANGED
@@ -16,7 +16,7 @@ export declare class Core {
16
16
  private updateCounter;
17
17
  private updateTime;
18
18
  private programHook;
19
- constructor(gl: WebGLRenderingContext | WebGL2RenderingContext);
19
+ constructor(gl: WebGLRenderingContext | WebGL2RenderingContext, debug?: boolean);
20
20
  private hook;
21
21
  /**
22
22
  * reset draw call hook
@@ -3,6 +3,7 @@
3
3
  */
4
4
  export default class DrawCallHook {
5
5
  private readonly gl;
6
+ private readonly debug;
6
7
  drawCall: number;
7
8
  triangles: number;
8
9
  lines: number;
@@ -10,7 +11,7 @@ export default class DrawCallHook {
10
11
  private hooked;
11
12
  private readonly realDrawElements;
12
13
  private readonly realDrawArrays;
13
- constructor(gl: WebGLRenderingContext | WebGL2RenderingContext);
14
+ constructor(gl: WebGLRenderingContext | WebGL2RenderingContext, debug: boolean);
14
15
  private hookedDrawElements;
15
16
  private hookedDrawArrays;
16
17
  private update;
@@ -4,10 +4,11 @@
4
4
  */
5
5
  export default class ProgramHook {
6
6
  private readonly gl;
7
+ private readonly debug;
7
8
  programs: number;
8
9
  private readonly realUseProgram;
9
10
  private hooked;
10
- constructor(gl: WebGLRenderingContext | WebGL2RenderingContext);
11
+ constructor(gl: WebGLRenderingContext | WebGL2RenderingContext, debug: boolean);
11
12
  private hookedUseProgram;
12
13
  reset(): void;
13
14
  release(): void;
@@ -3,11 +3,12 @@
3
3
  */
4
4
  export default class ShaderHook {
5
5
  private readonly gl;
6
+ private readonly debug;
6
7
  shaders: number;
7
8
  private readonly realAttachShader;
8
9
  private readonly realDetachShader;
9
10
  private hooked;
10
- constructor(gl: WebGLRenderingContext | WebGL2RenderingContext);
11
+ constructor(gl: WebGLRenderingContext | WebGL2RenderingContext, debug: boolean);
11
12
  private hookedAttachShader;
12
13
  private hookedDetachShader;
13
14
  reset(): void;
@@ -3,11 +3,12 @@
3
3
  */
4
4
  export default class TextureHook {
5
5
  private readonly gl;
6
+ private readonly debug;
6
7
  textures: number;
7
8
  private readonly realCreateTexture;
8
9
  private readonly realDeleteTexture;
9
10
  private hooked;
10
- constructor(gl: WebGLRenderingContext | WebGL2RenderingContext);
11
+ constructor(gl: WebGLRenderingContext | WebGL2RenderingContext, debug: boolean);
11
12
  private hookedCreateTexture;
12
13
  private hookedDeleteTexture;
13
14
  reset(): void;
package/dist/index.js 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.1-alpha.0
7
7
  */
8
8
 
9
9
  'use strict';
@@ -59,6 +59,17 @@ function _async_to_generator(fn) {
59
59
  };
60
60
  }
61
61
 
62
+ function _extends() {
63
+ _extends = Object.assign || function assign(target) {
64
+ for(var i = 1; i < arguments.length; i++){
65
+ var source = arguments[i];
66
+ for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
67
+ }
68
+ return target;
69
+ };
70
+ return _extends.apply(this, arguments);
71
+ }
72
+
62
73
  function _instanceof1(left, right) {
63
74
  if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
64
75
  return !!right[Symbol.hasInstance](left);
@@ -183,9 +194,12 @@ function _inherits(subClass, superClass) {
183
194
  if (superClass) _set_prototype_of(subClass, superClass);
184
195
  }
185
196
 
186
- var DrawCallHook = /*#__PURE__*/ function() {
187
- function DrawCallHook(gl) {
197
+ /**
198
+ * DrawCallHook
199
+ */ var DrawCallHook = /*#__PURE__*/ function() {
200
+ function DrawCallHook(gl, debug) {
188
201
  this.gl = gl;
202
+ this.debug = debug;
189
203
  this.drawCall = 0;
190
204
  this.triangles = 0;
191
205
  this.lines = 0;
@@ -195,7 +209,7 @@ var DrawCallHook = /*#__PURE__*/ function() {
195
209
  this.realDrawArrays = gl.drawArrays;
196
210
  gl.drawElements = this.hookedDrawElements.bind(this);
197
211
  gl.drawArrays = this.hookedDrawArrays.bind(this);
198
- if (Stats.options.debug) {
212
+ if (debug) {
199
213
  console.debug("DrawCall is hooked.");
200
214
  }
201
215
  }
@@ -252,14 +266,18 @@ var DrawCallHook = /*#__PURE__*/ function() {
252
266
  return DrawCallHook;
253
267
  }();
254
268
 
255
- var ProgramHook = /*#__PURE__*/ function() {
256
- function ProgramHook(gl) {
269
+ /**
270
+ * ProgramHook
271
+ * 每一帧的 shader 数量
272
+ */ var ProgramHook = /*#__PURE__*/ function() {
273
+ function ProgramHook(gl, debug) {
257
274
  this.gl = gl;
275
+ this.debug = debug;
258
276
  this.programs = 0;
259
277
  this.hooked = true;
260
278
  this.realUseProgram = gl.useProgram;
261
279
  gl.useProgram = this.hookedUseProgram.bind(this);
262
- if (Stats.options.debug) {
280
+ if (debug) {
263
281
  console.debug("Program is hooked.");
264
282
  }
265
283
  }
@@ -267,7 +285,7 @@ var ProgramHook = /*#__PURE__*/ function() {
267
285
  _proto.hookedUseProgram = function hookedUseProgram(program) {
268
286
  this.realUseProgram.call(this.gl, program);
269
287
  this.programs++;
270
- if (Stats.options.debug) {
288
+ if (this.debug) {
271
289
  console.debug("UseProgram: " + program + ", program: " + this.programs + ".");
272
290
  }
273
291
  };
@@ -283,16 +301,19 @@ var ProgramHook = /*#__PURE__*/ function() {
283
301
  return ProgramHook;
284
302
  }();
285
303
 
286
- var ShaderHook = /*#__PURE__*/ function() {
287
- function ShaderHook(gl) {
304
+ /**
305
+ * ShaderHook
306
+ */ var ShaderHook = /*#__PURE__*/ function() {
307
+ function ShaderHook(gl, debug) {
288
308
  this.gl = gl;
309
+ this.debug = debug;
289
310
  this.shaders = 0;
290
311
  this.hooked = true;
291
312
  this.realAttachShader = gl.attachShader;
292
313
  this.realDetachShader = gl.detachShader;
293
314
  gl.attachShader = this.hookedAttachShader.bind(this);
294
315
  gl.detachShader = this.hookedDetachShader.bind(this);
295
- if (Stats.options.debug) {
316
+ if (debug) {
296
317
  console.debug("Shader is hooked.");
297
318
  }
298
319
  }
@@ -300,14 +321,14 @@ var ShaderHook = /*#__PURE__*/ function() {
300
321
  _proto.hookedAttachShader = function hookedAttachShader(program, shader) {
301
322
  this.realAttachShader.call(this.gl, program, shader);
302
323
  this.shaders++;
303
- if (Stats.options.debug) {
324
+ if (this.debug) {
304
325
  console.debug("AttachShader: " + shader + ". shaders: " + this.shaders);
305
326
  }
306
327
  };
307
328
  _proto.hookedDetachShader = function hookedDetachShader(program, shader) {
308
329
  this.realDetachShader.call(this.gl, program, shader);
309
330
  this.shaders--;
310
- if (Stats.options.debug) {
331
+ if (this.debug) {
311
332
  console.debug("DetachShader. shaders: " + this.shaders);
312
333
  }
313
334
  };
@@ -324,16 +345,19 @@ var ShaderHook = /*#__PURE__*/ function() {
324
345
  return ShaderHook;
325
346
  }();
326
347
 
327
- var TextureHook = /*#__PURE__*/ function() {
328
- function TextureHook(gl) {
348
+ /**
349
+ * TextureHook
350
+ */ var TextureHook = /*#__PURE__*/ function() {
351
+ function TextureHook(gl, debug) {
329
352
  this.gl = gl;
353
+ this.debug = debug;
330
354
  this.textures = 0;
331
355
  this.hooked = true;
332
356
  this.realCreateTexture = gl.createTexture;
333
357
  this.realDeleteTexture = gl.deleteTexture;
334
358
  gl.createTexture = this.hookedCreateTexture.bind(this);
335
359
  gl.deleteTexture = this.hookedDeleteTexture.bind(this);
336
- if (Stats.options.debug) {
360
+ if (debug) {
337
361
  console.debug("Texture is hooked.");
338
362
  }
339
363
  }
@@ -341,7 +365,7 @@ var TextureHook = /*#__PURE__*/ function() {
341
365
  _proto.hookedCreateTexture = function hookedCreateTexture() {
342
366
  var texture = this.realCreateTexture.call(this.gl);
343
367
  this.textures++;
344
- if (Stats.options.debug) {
368
+ if (this.debug) {
345
369
  console.debug("CreateTexture: " + texture + ", textures: " + this.textures + ".");
346
370
  }
347
371
  return texture;
@@ -349,7 +373,7 @@ var TextureHook = /*#__PURE__*/ function() {
349
373
  _proto.hookedDeleteTexture = function hookedDeleteTexture(texture) {
350
374
  this.realDeleteTexture.call(this.gl, texture);
351
375
  this.textures--;
352
- if (Stats.options.debug) {
376
+ if (this.debug) {
353
377
  console.debug("DeleteTexture, textures: " + this.textures + ".");
354
378
  }
355
379
  };
@@ -369,7 +393,8 @@ var TextureHook = /*#__PURE__*/ function() {
369
393
  /**
370
394
  * Hook gl to calculate stats
371
395
  */ var Core = /*#__PURE__*/ function() {
372
- function Core(gl) {
396
+ function Core(gl, debug) {
397
+ if (debug === void 0) debug = false;
373
398
  this.gl = gl;
374
399
  this.samplingFrames = 60;
375
400
  this.samplingIndex = 0;
@@ -378,14 +403,14 @@ var TextureHook = /*#__PURE__*/ function() {
378
403
  if (!gl) {
379
404
  throw new Error("Unsupported WebGL context.");
380
405
  }
381
- this.hook(gl);
406
+ this.hook(gl, debug);
382
407
  }
383
408
  var _proto = Core.prototype;
384
- _proto.hook = function hook(gl) {
385
- this.drawCallHook = new DrawCallHook(gl);
386
- this.textureHook = new TextureHook(gl);
387
- this.shaderHook = new ShaderHook(gl);
388
- this.programHook = new ProgramHook(gl);
409
+ _proto.hook = function hook(gl, debug) {
410
+ this.drawCallHook = new DrawCallHook(gl, debug);
411
+ this.textureHook = new TextureHook(gl, debug);
412
+ this.shaderHook = new ShaderHook(gl, debug);
413
+ this.programHook = new ProgramHook(gl, debug);
389
414
  };
390
415
  /**
391
416
  * reset draw call hook
@@ -421,12 +446,12 @@ var TextureHook = /*#__PURE__*/ function() {
421
446
  fps: Math.round(this.updateCounter * 1000 / (now - this.updateTime)),
422
447
  // eslint-disable-next-line compat/compat -- performance.memory is not standard
423
448
  memory: performance.memory && performance.memory.usedJSHeapSize / 1048576 >> 0,
424
- drawCall: this.drawCallHook.drawCall === 0 ? 0 : this.drawCallHook.drawCall - 1,
425
- triangles: this.drawCallHook.triangles === 0 ? 0 : this.drawCallHook.triangles - 2,
449
+ drawCall: this.drawCallHook.drawCall === 0 ? 0 : this.drawCallHook.drawCall,
450
+ triangles: this.drawCallHook.triangles === 0 ? 0 : this.drawCallHook.triangles,
426
451
  lines: this.drawCallHook.lines,
427
452
  points: this.drawCallHook.points,
428
453
  textures: this.textureHook.textures,
429
- shaders: this.shaderHook.shaders === 0 ? 0 : this.shaderHook.shaders + 1,
454
+ shaders: this.shaderHook.shaders === 0 ? 0 : this.shaderHook.shaders,
430
455
  programs: this.programHook.programs,
431
456
  webglContext: _instanceof1(this.gl, WebGL2RenderingContext) ? "2.0" : "1.0"
432
457
  };
@@ -439,9 +464,10 @@ var TextureHook = /*#__PURE__*/ function() {
439
464
  }();
440
465
 
441
466
  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';
442
- 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";
467
+ 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";
443
468
  var Monitor = /*#__PURE__*/ function() {
444
- function Monitor(gl) {
469
+ function Monitor(gl, options) {
470
+ this.options = options;
445
471
  this.items = [
446
472
  "fps",
447
473
  "memory",
@@ -464,19 +490,22 @@ var Monitor = /*#__PURE__*/ function() {
464
490
  programs: 0,
465
491
  webglContext: "2.0"
466
492
  };
467
- this.core = new Core(gl);
493
+ this.core = new Core(gl, options.debug);
468
494
  this.createContainer();
469
495
  this.update = this.update.bind(this);
470
496
  }
471
497
  var _proto = Monitor.prototype;
472
498
  _proto.createContainer = function createContainer() {
473
499
  var container = document.createElement("div");
500
+ this.container = container;
501
+ if (!this.options.visible) {
502
+ this.hide();
503
+ }
474
504
  container.classList.add("gl-perf");
475
505
  container.innerHTML = tpl;
476
506
  container.appendChild(this.createStyle());
477
- document.body.appendChild(container);
507
+ this.options.container.appendChild(container);
478
508
  this.doms = Array.prototype.slice.apply(container.querySelectorAll("dd"));
479
- this.container = container;
480
509
  };
481
510
  _proto.createStyle = function createStyle() {
482
511
  var style = document.createElement("style");
@@ -496,11 +525,17 @@ var Monitor = /*#__PURE__*/ function() {
496
525
  });
497
526
  };
498
527
  var data = this.core.update(dt);
499
- if (!data || data.drawCall === 0 || data.triangles === 0) {
528
+ if (!data || data.triangles === 0) {
500
529
  return;
501
530
  }
502
531
  for(var i = 0, l = this.items.length; i < l; i++)_this = this, _loop(i);
503
532
  };
533
+ _proto.hide = function hide() {
534
+ this.container.style.display = "none";
535
+ };
536
+ _proto.show = function show() {
537
+ this.container.style.display = "block";
538
+ };
504
539
  /**
505
540
  * reset all hooks
506
541
  */ _proto.reset = function reset() {
@@ -525,7 +560,7 @@ var Monitor = /*#__PURE__*/ function() {
525
560
  * dispose the instance
526
561
  */ _proto.dispose = function dispose() {
527
562
  this.release();
528
- document.body.removeChild(this.container);
563
+ this.container.remove();
529
564
  };
530
565
  return Monitor;
531
566
  }();
@@ -536,10 +571,10 @@ var StatsComponent = /*#__PURE__*/ function(Behaviour) {
536
571
  return Behaviour.apply(this, arguments);
537
572
  }
538
573
  var _proto = StatsComponent.prototype;
539
- _proto.onStart = function onStart() {
574
+ _proto.init = function init(options) {
540
575
  var renderer = this.engine.renderer;
541
576
  var gl = renderer.pipelineContext.gl;
542
- this.monitor = new Monitor(gl);
577
+ this.monitor = new Monitor(gl, options);
543
578
  };
544
579
  _proto.onUpdate = function onUpdate(dt) {
545
580
  this.monitor.update(dt);
@@ -551,6 +586,11 @@ var StatsComponent = /*#__PURE__*/ function(Behaviour) {
551
586
  return StatsComponent;
552
587
  }(EFFECTS.Behaviour);
553
588
 
589
+ var defaultStatsOptions = {
590
+ debug: false,
591
+ visible: true,
592
+ container: document.body
593
+ };
554
594
  var json = {
555
595
  "images": [],
556
596
  "fonts": [],
@@ -645,18 +685,16 @@ var json = {
645
685
  *
646
686
  */ var Stats = /*#__PURE__*/ function() {
647
687
  function Stats(player, options) {
648
- if (options === void 0) options = {
649
- debug: false
650
- };
651
688
  this.player = player;
652
- Stats.options = options;
689
+ this.options = options;
690
+ this.disposed = false;
653
691
  void this.init();
654
692
  }
655
693
  var _proto = Stats.prototype;
656
694
  _proto.init = function init() {
657
695
  var _this = this;
658
696
  return _async_to_generator(function() {
659
- var composition, component, e;
697
+ var composition, item, e;
660
698
  return __generator(this, function(_state) {
661
699
  switch(_state.label){
662
700
  case 0:
@@ -672,8 +710,11 @@ var json = {
672
710
  ];
673
711
  case 1:
674
712
  composition = _state.sent();
675
- component = composition.getItemByName("component");
676
- component == null ? void 0 : component.addComponent(StatsComponent);
713
+ item = composition.getItemByName("component");
714
+ if (item) {
715
+ item.addComponent(StatsComponent).init(_extends({}, defaultStatsOptions, _this.options));
716
+ _this.component = item.getComponent(StatsComponent);
717
+ }
677
718
  return [
678
719
  3,
679
720
  3
@@ -689,12 +730,29 @@ var json = {
689
730
  });
690
731
  })();
691
732
  };
733
+ _proto.hide = function hide() {
734
+ var _this_component;
735
+ (_this_component = this.component) == null ? void 0 : _this_component.monitor.hide();
736
+ };
737
+ _proto.show = function show() {
738
+ var _this_component;
739
+ (_this_component = this.component) == null ? void 0 : _this_component.monitor.show();
740
+ };
741
+ _proto.dispose = function dispose() {
742
+ var _this_component;
743
+ if (this.disposed) {
744
+ return;
745
+ }
746
+ this.disposed = true;
747
+ (_this_component = this.component) == null ? void 0 : _this_component.dispose();
748
+ this.component = undefined;
749
+ };
692
750
  return Stats;
693
751
  }();
694
752
 
695
753
  /**
696
754
  * 插件版本号
697
- */ var version = "2.2.0-alpha.0";
755
+ */ var version = "2.2.1-alpha.0";
698
756
  EFFECTS.logger.info("Plugin stats version: " + version + ".");
699
757
  if (version !== EFFECTS__namespace.version) {
700
758
  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!");