@combos-fun/engine 0.1.14-beta.0 → 0.1.14-beta.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.
@@ -891,7 +891,7 @@ class Scene extends GameObject {
891
891
  }
892
892
 
893
893
  /** Generated at build from package.json */
894
- const version = "0.1.14-beta.0";
894
+ const version = "0.1.14-beta.1";
895
895
 
896
896
  /**
897
897
  * Sent to `window.parent` after each `System.init` completes during `Game.addSystem`
@@ -1710,6 +1710,108 @@ function normalizeResourceSrc(src, resourceType, resourceName = '(unnamed)') {
1710
1710
  return out;
1711
1711
  }
1712
1712
 
1713
+ function rasterType(type, url) {
1714
+ if (type && type !== 'ktx2')
1715
+ return type;
1716
+ const path = url.split('?')[0].split('#')[0];
1717
+ const dot = path.lastIndexOf('.');
1718
+ const ext = dot >= 0 ? path.slice(dot + 1).toLowerCase() : '';
1719
+ if (ext === 'png' || ext === 'jpg' || ext === 'jpeg' || ext === 'webp')
1720
+ return ext;
1721
+ return 'png';
1722
+ }
1723
+ function preferImageSlot(slot, supported) {
1724
+ const ktx2 = compressedTextureUrl(slot);
1725
+ if (!ktx2)
1726
+ return;
1727
+ if (supported) {
1728
+ if (slot.type !== 'ktx2' && slot.url && slot.url !== ktx2 && !imageFallbackUrl(slot)) {
1729
+ slot.fallback = { type: slot.type, url: slot.url };
1730
+ }
1731
+ slot.type = 'ktx2';
1732
+ slot.url = ktx2;
1733
+ return;
1734
+ }
1735
+ if (slot.type !== 'ktx2')
1736
+ return;
1737
+ const fallback = imageFallbackUrl(slot);
1738
+ if (!fallback)
1739
+ return;
1740
+ slot.type = rasterType(slot.fallback?.type, fallback);
1741
+ slot.url = fallback;
1742
+ }
1743
+ /** Point a failed KTX2 image slot at its raster fallback. Returns false when there is nothing to load. */
1744
+ function useRasterImageFallback(slot) {
1745
+ if (!slot || slot.type !== 'ktx2')
1746
+ return false;
1747
+ const fallback = imageFallbackUrl(slot);
1748
+ if (!fallback || fallback === slot.url)
1749
+ return false;
1750
+ slot.type = rasterType(slot.fallback?.type, fallback);
1751
+ slot.url = fallback;
1752
+ return true;
1753
+ }
1754
+ /**
1755
+ * Choose the image file for this load. A supported GPU downloads `ktx2`
1756
+ * and keeps the raster URL as `fallback`. Otherwise the raster URL stays
1757
+ * the file that will be downloaded.
1758
+ */
1759
+ function applyCompressedImagePreference(src, supported) {
1760
+ if (src.image)
1761
+ preferImageSlot(src.image, supported);
1762
+ }
1763
+
1764
+ const WEBGL_COMPRESSED_EXTENSIONS = [
1765
+ 'WEBGL_compressed_texture_astc',
1766
+ 'EXT_texture_compression_bptc',
1767
+ 'WEBGL_compressed_texture_s3tc',
1768
+ 'WEBGL_compressed_texture_etc',
1769
+ ];
1770
+ const WEBGPU_COMPRESSED_FEATURES = [
1771
+ 'texture-compression-astc',
1772
+ 'texture-compression-bc',
1773
+ 'texture-compression-etc2',
1774
+ ];
1775
+ let supportPromise = null;
1776
+ /** True when the GPU can take ASTC, BC7, BC3, or ETC2. Cached for the page. */
1777
+ function gpuSupportsCompressedTexture() {
1778
+ if (!supportPromise)
1779
+ supportPromise = probeCompressedTextureSupport();
1780
+ return supportPromise;
1781
+ }
1782
+ async function probeCompressedTextureSupport() {
1783
+ if (probeWebGL())
1784
+ return true;
1785
+ return probeWebGPU();
1786
+ }
1787
+ function probeWebGL() {
1788
+ if (typeof document === 'undefined')
1789
+ return false;
1790
+ const canvas = document.createElement('canvas');
1791
+ const gl = canvas.getContext('webgl2') || canvas.getContext('webgl');
1792
+ if (!gl)
1793
+ return false;
1794
+ const supported = WEBGL_COMPRESSED_EXTENSIONS.some((name) => gl.getExtension(name));
1795
+ const lose = gl.getExtension('WEBGL_lose_context');
1796
+ lose?.loseContext?.();
1797
+ return supported;
1798
+ }
1799
+ async function probeWebGPU() {
1800
+ const gpu = (typeof navigator === 'undefined' ? undefined : navigator.gpu);
1801
+ if (!gpu?.requestAdapter)
1802
+ return false;
1803
+ try {
1804
+ const adapter = await gpu.requestAdapter();
1805
+ const has = adapter?.features?.has;
1806
+ if (!has)
1807
+ return false;
1808
+ return WEBGPU_COMPRESSED_FEATURES.some((name) => has.call(adapter.features, name));
1809
+ }
1810
+ catch {
1811
+ return false;
1812
+ }
1813
+ }
1814
+
1713
1815
  const resourceLoader = {
1714
1816
  AbstractLoadStrategy: resourceLoader$1.AbstractLoadStrategy,
1715
1817
  AudioLoadStrategy: resourceLoader$1.AudioLoadStrategy,
@@ -2007,14 +2109,38 @@ class Resource extends EventEmitter__default.default {
2007
2109
  if (!unLoadNames.length)
2008
2110
  return;
2009
2111
  const resolves = {};
2010
- const loader = this.getLoader(preload);
2011
2112
  unLoadNames.forEach(name => {
2012
2113
  this.promiseMap[name] = new Promise(r => (resolves[name] = r));
2114
+ });
2115
+ void this.beginLoad(unLoadNames, resolves, preload);
2116
+ }
2117
+ async beginLoad(names, resolves, preload) {
2118
+ let supported = false;
2119
+ try {
2120
+ supported = await gpuSupportsCompressedTexture();
2121
+ }
2122
+ catch {
2123
+ supported = false;
2124
+ }
2125
+ const loader = this.getLoader(preload);
2126
+ for (const name of names) {
2013
2127
  const res = this.resourcesMap[name];
2014
- for (const handler of this.preProcessResourceHandlers) {
2015
- handler(res);
2128
+ if (!res) {
2129
+ resolves[name]?.({});
2130
+ continue;
2131
+ }
2132
+ try {
2133
+ for (const handler of this.preProcessResourceHandlers) {
2134
+ handler(res);
2135
+ }
2136
+ res.src = normalizeResourceSrc(res.src, String(res.type), res.name);
2137
+ applyCompressedImagePreference(res.src, supported);
2138
+ }
2139
+ catch (err) {
2140
+ console.error(err);
2141
+ resolves[name]?.({});
2142
+ continue;
2016
2143
  }
2017
- res.src = normalizeResourceSrc(res.src, String(res.type), res.name);
2018
2144
  for (const key in res.src) {
2019
2145
  const resourceType = res.src[key].type;
2020
2146
  if (resourceType === 'data') {
@@ -2034,7 +2160,7 @@ class Resource extends EventEmitter__default.default {
2034
2160
  });
2035
2161
  }
2036
2162
  }
2037
- });
2163
+ }
2038
2164
  loader.load();
2039
2165
  }
2040
2166
  async doComplete(name, resolve, preload = false) {
@@ -2070,11 +2196,12 @@ class Resource extends EventEmitter__default.default {
2070
2196
  return Array.from(Object.keys(res.src)).every(resourceKey => res.data[resourceKey]);
2071
2197
  }
2072
2198
  getLoader(preload = false) {
2073
- let loader = this.loaders.find(({ loading }) => !loading);
2199
+ let loader = this.loaders.find((item) => !item.loading && !reservedLoaders.has(item));
2074
2200
  if (!loader) {
2075
2201
  loader = new resourceLoader$1.Loader();
2076
2202
  this.loaders.push(loader);
2077
2203
  }
2204
+ reservedLoaders.add(loader);
2078
2205
  if (preload) {
2079
2206
  loader.onStart.once(() => {
2080
2207
  this.progress.onStart();
@@ -2087,6 +2214,7 @@ class Resource extends EventEmitter__default.default {
2087
2214
  this.onError({ errMsg, resource, preload });
2088
2215
  });
2089
2216
  loader.onComplete.once(() => {
2217
+ reservedLoaders.delete(loader);
2090
2218
  loader.onLoad.detachAll();
2091
2219
  loader.onError.detachAll();
2092
2220
  loader.reset();
@@ -2100,7 +2228,20 @@ class Resource extends EventEmitter__default.default {
2100
2228
  this.doComplete(name, resolves[name], preload);
2101
2229
  }
2102
2230
  async onError({ errMsg, preload = false, resource }) {
2103
- const { metadata: { name, resolves }, } = resource;
2231
+ const { metadata: { key, name, resolves }, } = resource;
2232
+ const res = this.resourcesMap[name];
2233
+ if (key === 'image' && useRasterImageFallback(res?.src?.image)) {
2234
+ const slot = res.src.image;
2235
+ const loader = this.getLoader(preload);
2236
+ loader.add({
2237
+ url: slot.url,
2238
+ name: `${name}_image_fallback`,
2239
+ strategy: RESOURCE_TYPE_STRATEGY[slot.type],
2240
+ metadata: { key, name, resolves },
2241
+ });
2242
+ loader.load();
2243
+ return;
2244
+ }
2104
2245
  this._destroy(name, true);
2105
2246
  resolves[name]({});
2106
2247
  if (preload) {
@@ -2115,6 +2256,7 @@ class Resource extends EventEmitter__default.default {
2115
2256
  }
2116
2257
  }
2117
2258
  /** Resource manager single instance */
2259
+ const reservedLoaders = new WeakSet();
2118
2260
  const resource = new Resource();
2119
2261
  resource.registerInstance(exports.RESOURCE_TYPE.SHADER, (res) => ({
2120
2262
  vertex: typeof res.data?.vertex === 'string' ? res.data.vertex : '',
@@ -2242,9 +2384,11 @@ exports.IDEProp = IDEProp;
2242
2384
  exports.RESOURCE_TYPE_STRATEGY = RESOURCE_TYPE_STRATEGY;
2243
2385
  exports.Scene = Scene;
2244
2386
  exports.System = System;
2387
+ exports.applyCompressedImagePreference = applyCompressedImagePreference;
2245
2388
  exports.componentObserver = componentObserver;
2246
2389
  exports.compressedTextureUrl = compressedTextureUrl;
2247
2390
  exports.decorators = decorators;
2391
+ exports.gpuSupportsCompressedTexture = gpuSupportsCompressedTexture;
2248
2392
  exports.imageFallbackUrl = imageFallbackUrl;
2249
2393
  exports.isAllowedMessageOrigin = isAllowedMessageOrigin;
2250
2394
  exports.mergeAllowedMessageOrigins = mergeAllowedMessageOrigins;