@galacean/effects-plugin-stats 2.4.8 → 2.5.0-alpha.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.
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.4.8
6
+ * Version: v2.5.0-alpha.1
7
7
  */
8
8
 
9
9
  'use strict';
@@ -194,6 +194,11 @@ function _inherits(subClass, superClass) {
194
194
  if (superClass) _set_prototype_of(subClass, superClass);
195
195
  }
196
196
 
197
+ function _assert_this_initialized(self) {
198
+ if (self === void 0) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
199
+ return self;
200
+ }
201
+
197
202
  /**
198
203
  * DrawCallHook
199
204
  */ var DrawCallHook = /*#__PURE__*/ function() {
@@ -345,14 +350,40 @@ function _inherits(subClass, superClass) {
345
350
  return ShaderHook;
346
351
  }();
347
352
 
348
- /**
349
- * TextureHook
350
- */ var TextureHook = /*#__PURE__*/ function() {
353
+ var TextureHook = /*#__PURE__*/ function() {
351
354
  function TextureHook(gl, debug) {
355
+ var _this = this;
352
356
  this.gl = gl;
353
357
  this.debug = debug;
354
358
  this.textures = 0;
355
359
  this.hooked = true;
360
+ this.hookedCreateTexture = function() {
361
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
362
+ args[_key] = arguments[_key];
363
+ }
364
+ var _this_realCreateTexture;
365
+ var texture = (_this_realCreateTexture = _this.realCreateTexture).call.apply(_this_realCreateTexture, [].concat([
366
+ _this.gl
367
+ ], args));
368
+ _this.textures++;
369
+ if (_this.debug) {
370
+ console.debug("CreateTexture: " + texture + ", textures: " + _this.textures + ".");
371
+ }
372
+ return texture;
373
+ };
374
+ this.hookedDeleteTexture = function() {
375
+ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
376
+ args[_key] = arguments[_key];
377
+ }
378
+ var _this_realDeleteTexture;
379
+ (_this_realDeleteTexture = _this.realDeleteTexture).call.apply(_this_realDeleteTexture, [].concat([
380
+ _this.gl
381
+ ], args));
382
+ _this.textures--;
383
+ if (_this.debug) {
384
+ console.debug("DeleteTexture, textures: " + _this.textures + ".");
385
+ }
386
+ };
356
387
  this.realCreateTexture = gl.createTexture;
357
388
  this.realDeleteTexture = gl.deleteTexture;
358
389
  gl.createTexture = this.hookedCreateTexture.bind(this);
@@ -362,21 +393,6 @@ function _inherits(subClass, superClass) {
362
393
  }
363
394
  }
364
395
  var _proto = TextureHook.prototype;
365
- _proto.hookedCreateTexture = function hookedCreateTexture() {
366
- var texture = this.realCreateTexture.call(this.gl);
367
- this.textures++;
368
- if (this.debug) {
369
- console.debug("CreateTexture: " + texture + ", textures: " + this.textures + ".");
370
- }
371
- return texture;
372
- };
373
- _proto.hookedDeleteTexture = function hookedDeleteTexture(texture) {
374
- this.realDeleteTexture.call(this.gl, texture);
375
- this.textures--;
376
- if (this.debug) {
377
- console.debug("DeleteTexture, textures: " + this.textures + ".");
378
- }
379
- };
380
396
  _proto.reset = function reset() {
381
397
  this.textures = 0;
382
398
  };
@@ -465,10 +481,15 @@ function _inherits(subClass, superClass) {
465
481
 
466
482
  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';
467
483
  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";
468
- var Monitor = /*#__PURE__*/ function() {
484
+ /**
485
+ * Performance monitor.
486
+ */ var Monitor = /*#__PURE__*/ function(EventEmitter) {
487
+ _inherits(Monitor, EventEmitter);
469
488
  function Monitor(gl, options) {
470
- this.options = options;
471
- this.items = [
489
+ var _this;
490
+ _this = EventEmitter.call(this) || this;
491
+ _this.options = options;
492
+ _this.items = [
472
493
  "fps",
473
494
  "memory",
474
495
  "drawCall",
@@ -478,7 +499,8 @@ var Monitor = /*#__PURE__*/ function() {
478
499
  "programs",
479
500
  "webglContext"
480
501
  ];
481
- this.data = {
502
+ _this.visible = false;
503
+ _this.data = {
482
504
  fps: 60,
483
505
  memory: 0,
484
506
  drawCall: 0,
@@ -490,15 +512,17 @@ var Monitor = /*#__PURE__*/ function() {
490
512
  programs: 0,
491
513
  webglContext: "2.0"
492
514
  };
493
- this.core = new Core(gl, options.debug);
494
- this.createContainer();
495
- this.update = this.update.bind(this);
515
+ _this.core = new Core(gl, options.debug);
516
+ _this.visible = options.visible;
517
+ _this.createContainer();
518
+ _this.update = _this.update.bind(_assert_this_initialized(_this));
519
+ return _this;
496
520
  }
497
521
  var _proto = Monitor.prototype;
498
522
  _proto.createContainer = function createContainer() {
499
523
  var container = document.createElement("div");
500
524
  this.container = container;
501
- if (!this.options.visible) {
525
+ if (!this.visible) {
502
526
  this.hide();
503
527
  }
504
528
  container.classList.add("gl-perf");
@@ -528,12 +552,22 @@ var Monitor = /*#__PURE__*/ function() {
528
552
  if (!data || data.triangles === 0) {
529
553
  return;
530
554
  }
555
+ this.emit("update", data);
556
+ if (!this.visible) {
557
+ return;
558
+ }
531
559
  for(var i = 0, l = this.items.length; i < l; i++)_this = this, _loop(i);
532
560
  };
533
- _proto.hide = function hide() {
561
+ /**
562
+ * Hide the monitor
563
+ */ _proto.hide = function hide() {
564
+ this.visible = false;
534
565
  this.container.style.display = "none";
535
566
  };
536
- _proto.show = function show() {
567
+ /**
568
+ * Show the monitor
569
+ */ _proto.show = function show() {
570
+ this.visible = true;
537
571
  this.container.style.display = "block";
538
572
  };
539
573
  /**
@@ -563,9 +597,11 @@ var Monitor = /*#__PURE__*/ function() {
563
597
  this.container.remove();
564
598
  };
565
599
  return Monitor;
566
- }();
600
+ }(EFFECTS.EventEmitter);
567
601
 
568
- var StatsComponent = /*#__PURE__*/ function(Behaviour) {
602
+ /**
603
+ * Stats 组件,用于监控渲染性能
604
+ */ var StatsComponent = /*#__PURE__*/ function(Behaviour) {
569
605
  _inherits(StatsComponent, Behaviour);
570
606
  function StatsComponent() {
571
607
  return Behaviour.apply(this, arguments);
@@ -576,10 +612,15 @@ var StatsComponent = /*#__PURE__*/ function(Behaviour) {
576
612
  var gl = renderer.pipelineContext.gl;
577
613
  this.monitor = new Monitor(gl, options);
578
614
  };
579
- _proto.onUpdate = function onUpdate(dt) {
615
+ /**
616
+ * 每帧更新
617
+ * @param dt
618
+ */ _proto.onUpdate = function onUpdate(dt) {
580
619
  this.monitor.update(dt);
581
620
  };
582
- _proto.dispose = function dispose() {
621
+ /**
622
+ * 释放资源
623
+ */ _proto.dispose = function dispose() {
583
624
  Behaviour.prototype.dispose.call(this);
584
625
  this.monitor.dispose();
585
626
  };
@@ -682,12 +723,40 @@ var json = {
682
723
  "compositionId": "2"
683
724
  };
684
725
  /**
726
+ * Stats plugin for the Player.
685
727
  *
728
+ * @example
729
+ * ```ts
730
+ * // Simple usage of Stats plugin
731
+ * import { Stats } from '@galacean/effects-plugin-stats';
732
+ *
733
+ * const stats = new Stats(player);
734
+ * // Hide the stats monitor
735
+ * stats.hide();
736
+ * // Show the stats monitor
737
+ * stats.show();
738
+ * ```
739
+ *
740
+ * @example
741
+ * ```ts
742
+ * // 自定义 Stats 组件的 onUpdate 方法
743
+ * // 可用于在每帧更新时获取性能数据
744
+ * const stats = new Stats(player, { visible: false });
745
+ * const [_, component] = await Promise.all([
746
+ * player.loadScene(json),
747
+ * stats.getComponent(),
748
+ * ]);
749
+ *
750
+ * component?.monitor.on('update', data => {
751
+ * console.info(data);
752
+ * });
753
+ * ```
686
754
  */ var Stats = /*#__PURE__*/ function() {
687
755
  function Stats(player, options) {
688
756
  this.player = player;
689
757
  this.options = options;
690
758
  this.disposed = false;
759
+ this.initialized = new EFFECTS.Deferred();
691
760
  void this.init();
692
761
  }
693
762
  var _proto = Stats.prototype;
@@ -701,8 +770,8 @@ var json = {
701
770
  _state.trys.push([
702
771
  0,
703
772
  2,
704
- ,
705
- 3
773
+ 3,
774
+ 4
706
775
  ]);
707
776
  return [
708
777
  4,
@@ -717,12 +786,17 @@ var json = {
717
786
  }
718
787
  return [
719
788
  3,
720
- 3
789
+ 4
721
790
  ];
722
791
  case 2:
723
792
  e = _state.sent();
724
793
  throw new Error("Failed to load stats scene: " + e.message + ".");
725
794
  case 3:
795
+ _this.initialized.resolve();
796
+ return [
797
+ 7
798
+ ];
799
+ case 4:
726
800
  return [
727
801
  2
728
802
  ];
@@ -730,15 +804,45 @@ var json = {
730
804
  });
731
805
  })();
732
806
  };
733
- _proto.hide = function hide() {
807
+ /**
808
+ * Get the stats component.
809
+ * @returns
810
+ */ _proto.getComponent = function getComponent() {
811
+ var _this = this;
812
+ return _async_to_generator(function() {
813
+ return __generator(this, function(_state) {
814
+ switch(_state.label){
815
+ case 0:
816
+ return [
817
+ 4,
818
+ _this.initialized.promise
819
+ ];
820
+ case 1:
821
+ _state.sent();
822
+ return [
823
+ 2,
824
+ _this.component
825
+ ];
826
+ }
827
+ });
828
+ })();
829
+ };
830
+ /**
831
+ * Hide the stats monitor.
832
+ */ _proto.hide = function hide() {
734
833
  var _this_component;
735
834
  (_this_component = this.component) == null ? void 0 : _this_component.monitor.hide();
736
835
  };
737
- _proto.show = function show() {
836
+ /**
837
+ * Show the stats monitor.
838
+ */ _proto.show = function show() {
738
839
  var _this_component;
739
840
  (_this_component = this.component) == null ? void 0 : _this_component.monitor.show();
740
841
  };
741
- _proto.dispose = function dispose() {
842
+ /**
843
+ * Dispose the stats component and release resources.
844
+ * @returns
845
+ */ _proto.dispose = function dispose() {
742
846
  var _this_component;
743
847
  if (this.disposed) {
744
848
  return;
@@ -752,7 +856,7 @@ var json = {
752
856
 
753
857
  /**
754
858
  * 插件版本号
755
- */ var version = "2.4.8";
859
+ */ var version = "2.5.0-alpha.1";
756
860
  EFFECTS.logger.info("Plugin stats version: " + version + ".");
757
861
  if (version !== EFFECTS__namespace.version) {
758
862
  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!");