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