@galacean/engine-rhi-webgl 2.0.0-alpha.38 → 2.0.0-alpha.39

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/module.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Canvas, Scene, Engine, BufferUsage, BufferBindFlag, SetDataOptions, GLCapabilityType, Logger, TextureUtils, TextureUsage, TextureWrapMode, TextureFormat, TextureFilterMode, TextureDepthCompareFunction, Texture, TextureCube, CameraClearFlags, ColorWriteMask, SystemInfo, Platform } from '@galacean/engine-core';
2
- import { Vector2, MathUtil, Vector4, Color } from '@galacean/engine-math';
2
+ import { MathUtil, Vector4, Color } from '@galacean/engine-math';
3
3
 
4
4
  /**
5
5
  * Smoothing plug-in.
@@ -69,24 +69,6 @@ import { Vector2, MathUtil, Vector4, Color } from '@galacean/engine-math';
69
69
  return GLCompressedTextureInternalFormat;
70
70
  }({});
71
71
 
72
- function _defineProperties(target, props) {
73
- for (var i = 0; i < props.length; i++) {
74
- var descriptor = props[i];
75
- descriptor.enumerable = descriptor.enumerable || false;
76
- descriptor.configurable = true;
77
-
78
- if ("value" in descriptor) descriptor.writable = true;
79
-
80
- Object.defineProperty(target, descriptor.key, descriptor);
81
- }
82
- }
83
- function _create_class(Constructor, protoProps, staticProps) {
84
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
85
- if (staticProps) _defineProperties(Constructor, staticProps);
86
-
87
- return Constructor;
88
- }
89
-
90
72
  function _set_prototype_of(o, p) {
91
73
  _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
92
74
  o.__proto__ = p;
@@ -119,69 +101,134 @@ function _instanceof(left, right) {
119
101
  _inherits(WebCanvas, Canvas);
120
102
  function WebCanvas(webCanvas) {
121
103
  var _this;
122
- _this = Canvas.call(this) || this, _this._scale = new Vector2();
123
- var width = webCanvas.width;
124
- var height = webCanvas.height;
104
+ _this = Canvas.call(this) || this, _this._autoResolutionScale = 1, _this._resolutionDirty = false, _this._pendingDevicePixelWidth = 0, _this._pendingDevicePixelHeight = 0;
125
105
  _this._webCanvas = webCanvas;
126
- _this.width = width;
127
- _this.height = height;
106
+ _this._setResolution(webCanvas.width, webCanvas.height);
128
107
  return _this;
129
108
  }
130
109
  var _proto = WebCanvas.prototype;
131
110
  /**
132
- * Resize the rendering size according to the clientWidth and clientHeight of the canvas.
133
- * @param pixelRatio - Pixel ratio
134
- */ _proto.resizeByClientSize = function resizeByClientSize(pixelRatio) {
135
- if (pixelRatio === void 0) pixelRatio = window.devicePixelRatio;
136
- var webCanvas = this._webCanvas;
137
- if (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas)) {
138
- var exportWidth = webCanvas.clientWidth * pixelRatio;
139
- var exportHeight = webCanvas.clientHeight * pixelRatio;
140
- this.width = exportWidth;
141
- this.height = exportHeight;
111
+ * @inheritdoc
112
+ *
113
+ * @remarks
114
+ * The canvas must have a CSS-constrained display size; otherwise auto-resolution disables itself.
115
+ */ _proto.setAutoResolution = function setAutoResolution(scale) {
116
+ var _this = this;
117
+ if (scale === void 0) scale = 1;
118
+ if (!Number.isFinite(scale) || scale <= 0) {
119
+ throw new Error("WebCanvas.setAutoResolution: invalid scale " + scale);
120
+ }
121
+ // OffscreenCanvas has no display size to follow
122
+ if (this._isOffscreenCanvas()) return;
123
+ this._autoResolutionScale = scale;
124
+ if (this._resizeObserver) {
125
+ this._resolutionDirty = true;
126
+ } else if (typeof ResizeObserver !== "undefined") {
127
+ // observe() fires an initial entry, which sets the pending device pixel size
128
+ this._resizeObserver = new ResizeObserver(function(entries) {
129
+ var _entries__devicePixelContentBoxSize;
130
+ var box = (_entries__devicePixelContentBoxSize = entries[entries.length - 1].devicePixelContentBoxSize) == null ? void 0 : _entries__devicePixelContentBoxSize[0];
131
+ _this._pendingDevicePixelWidth = box ? box.inlineSize : 0;
132
+ _this._pendingDevicePixelHeight = box ? box.blockSize : 0;
133
+ _this._resolutionDirty = true;
134
+ });
135
+ // Observe exact device pixels where supported, otherwise the content-box and size from the client size
136
+ if (WebCanvas._supportsDevicePixelContentBox()) {
137
+ this._resizeObserver.observe(this._webCanvas, {
138
+ box: "device-pixel-content-box"
139
+ });
140
+ } else {
141
+ this._resizeObserver.observe(this._webCanvas);
142
+ }
143
+ } else {
144
+ // ResizeObserver is unavailable (e.g. mini-programs, iOS Safari < 13.4): size once from the client size
145
+ console.warn("ResizeObserver is not supported in this environment. Falling back to one-time clientWidth × devicePixelRatio × scale sizing");
146
+ this._setResolutionByClientSizeFallback(scale);
142
147
  }
143
148
  };
144
149
  /**
145
- * Set scale.
146
- * @param x - Scale along the X axis
147
- * @param y - Scale along the Y axis
148
- */ _proto.setScale = function setScale(x, y) {
149
- this._scale.set(x, y);
150
- this.scale = this._scale;
150
+ * @internal
151
+ */ _proto._isOffscreenCanvas = function _isOffscreenCanvas() {
152
+ return typeof OffscreenCanvas !== "undefined" && _instanceof(this._webCanvas, OffscreenCanvas);
151
153
  };
152
- _proto._onWidthChanged = function _onWidthChanged(value) {
153
- this._webCanvas.width = value;
154
+ /**
155
+ * @internal
156
+ */ _proto._pumpPendingResolution = function _pumpPendingResolution() {
157
+ if (!this._resolutionDirty) return;
158
+ var webCanvas = this._webCanvas;
159
+ if (webCanvas.clientWidth === 0 || webCanvas.clientHeight === 0) return;
160
+ this._resolutionDirty = false;
161
+ var scale = this._autoResolutionScale;
162
+ // Snapshot the display size to detect a layout feedback loop after applying the buffer size below
163
+ var prevClientWidth = webCanvas.clientWidth;
164
+ var prevClientHeight = webCanvas.clientHeight;
165
+ if (this._pendingDevicePixelWidth > 0 && this._pendingDevicePixelHeight > 0) {
166
+ // Device pixels already fold in the device pixel ratio, so only the scale is applied
167
+ this._setResolution(Math.round(this._pendingDevicePixelWidth * scale), Math.round(this._pendingDevicePixelHeight * scale));
168
+ } else {
169
+ this._setResolutionByClientSizeFallback(scale);
170
+ }
171
+ // If setting the buffer moved the display size, the canvas has no CSS size constraint and would grow
172
+ // unboundedly; exit auto-resolution instead
173
+ if (webCanvas.clientWidth !== prevClientWidth || webCanvas.clientHeight !== prevClientHeight) {
174
+ console.warn("Auto-resolution disabled: the canvas has no CSS width/height, so sizing the buffer grows its display size. " + "Set a CSS width/height on the canvas, then call setResolution() or setAutoResolution().");
175
+ this._exitAutoResolution();
176
+ }
154
177
  };
155
- _proto._onHeightChange = function _onHeightChange(value) {
156
- this._webCanvas.height = value;
178
+ _proto._setResolutionByClientSizeFallback = function _setResolutionByClientSizeFallback(scale) {
179
+ var webCanvas = this._webCanvas;
180
+ if (webCanvas.clientWidth <= 0 || webCanvas.clientHeight <= 0) return;
181
+ var pixelRatio = window.devicePixelRatio * scale;
182
+ this._setResolution(Math.round(webCanvas.clientWidth * pixelRatio), Math.round(webCanvas.clientHeight * pixelRatio));
157
183
  };
158
- _create_class(WebCanvas, [
159
- {
160
- key: "scale",
161
- get: /**
162
- * The scale of canvas, the value is visible width/height divide the render width/height.
163
- * @remarks Need to re-assign after modification to ensure that the modification takes effect.
164
- */ function get() {
165
- var webCanvas = this._webCanvas;
166
- if (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas)) {
167
- this._scale.set(webCanvas.clientWidth * devicePixelRatio / webCanvas.width, webCanvas.clientHeight * devicePixelRatio / webCanvas.height);
168
- }
169
- return this._scale;
170
- },
171
- set: function set(value) {
172
- var webCanvas = this._webCanvas;
173
- if (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas)) {
174
- webCanvas.style.transformOrigin = "left top";
175
- webCanvas.style.transform = "scale(" + value.x + ", " + value.y + ")";
176
- }
177
- }
184
+ /**
185
+ * @internal
186
+ */ _proto._destroy = function _destroy() {
187
+ this._exitAutoResolution();
188
+ };
189
+ _proto._exitAutoResolution = function _exitAutoResolution() {
190
+ if (this._resizeObserver) {
191
+ this._resizeObserver.disconnect();
192
+ this._resizeObserver = undefined;
178
193
  }
179
- ]);
194
+ this._resolutionDirty = false;
195
+ this._pendingDevicePixelWidth = 0;
196
+ this._pendingDevicePixelHeight = 0;
197
+ };
198
+ _proto._onResolutionChanged = function _onResolutionChanged(width, height) {
199
+ var webCanvas = this._webCanvas;
200
+ webCanvas.width = width;
201
+ webCanvas.height = height;
202
+ };
203
+ WebCanvas._supportsDevicePixelContentBox = function _supportsDevicePixelContentBox() {
204
+ var _this__devicePixelContentBoxSupported;
205
+ // Passing { box: "device-pixel-content-box" } to observe() throws on browsers whose
206
+ // ResizeObserverBoxOptions enum lacks that value (Safari, Firefox < 93), so callers must gate on this
207
+ return (_this__devicePixelContentBoxSupported = this._devicePixelContentBoxSupported) != null ? _this__devicePixelContentBoxSupported : this._devicePixelContentBoxSupported = typeof ResizeObserverEntry !== "undefined" && "devicePixelContentBoxSize" in ResizeObserverEntry.prototype;
208
+ };
180
209
  return WebCanvas;
181
210
  }(Canvas);
182
211
 
212
+ function _defineProperties(target, props) {
213
+ for (var i = 0; i < props.length; i++) {
214
+ var descriptor = props[i];
215
+ descriptor.enumerable = descriptor.enumerable || false;
216
+ descriptor.configurable = true;
217
+
218
+ if ("value" in descriptor) descriptor.writable = true;
219
+
220
+ Object.defineProperty(target, descriptor.key, descriptor);
221
+ }
222
+ }
223
+ function _create_class(Constructor, protoProps, staticProps) {
224
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
225
+ if (staticProps) _defineProperties(Constructor, staticProps);
226
+
227
+ return Constructor;
228
+ }
229
+
183
230
  /**
184
- * WebGL platform engine,support includes WebGL1.0 and WebGL2.0.
231
+ * WebGL platform engine, supports WebGL 1.0 and WebGL 2.0.
185
232
  */ var WebGLEngine = /*#__PURE__*/ function(Engine) {
186
233
  _inherits(WebGLEngine, Engine);
187
234
  function WebGLEngine() {
@@ -196,6 +243,7 @@ function _instanceof(left, right) {
196
243
  var webCanvas = new WebCanvas(typeof canvas === "string" ? document.getElementById(canvas) : canvas);
197
244
  var webGLGraphicDevice = new WebGLGraphicDevice(configuration.graphicDeviceOptions);
198
245
  var engine = new WebGLEngine(webCanvas, webGLGraphicDevice, configuration);
246
+ webCanvas.setAutoResolution();
199
247
  // @ts-ignore
200
248
  var promise = engine._initialize(configuration);
201
249
  return promise.then(function() {
@@ -207,7 +255,7 @@ function _instanceof(left, right) {
207
255
  {
208
256
  key: "canvas",
209
257
  get: /**
210
- * Web canvas.
258
+ * The web canvas the engine renders to.
211
259
  */ function get() {
212
260
  // @ts-ignore
213
261
  return this._canvas;
@@ -2045,7 +2093,7 @@ function _create_for_of_iterator_helper_loose(o, allowArrayLike) {
2045
2093
  var gl;
2046
2094
  if (webGLMode == 0 || webGLMode == 1) {
2047
2095
  gl = webCanvas.getContext("webgl2", webGLOptions);
2048
- if (!gl && (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas))) {
2096
+ if (!gl && !canvas._isOffscreenCanvas()) {
2049
2097
  gl = webCanvas.getContext("experimental-webgl2", webGLOptions);
2050
2098
  }
2051
2099
  this._isWebGL2 = true;
@@ -2057,7 +2105,7 @@ function _create_for_of_iterator_helper_loose(o, allowArrayLike) {
2057
2105
  if (!gl) {
2058
2106
  if (webGLMode == 0 || webGLMode == 2) {
2059
2107
  gl = webCanvas.getContext("webgl", webGLOptions);
2060
- if (!gl && (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas))) {
2108
+ if (!gl && !canvas._isOffscreenCanvas()) {
2061
2109
  gl = webCanvas.getContext("experimental-webgl", webGLOptions);
2062
2110
  }
2063
2111
  this._isWebGL2 = false;