@combos-fun/engine 0.1.12 → 0.1.14-beta.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.
@@ -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.12";
894
+ const version = "0.1.14-beta.0";
895
895
 
896
896
  /**
897
897
  * Sent to `window.parent` after each `System.init` completes during `Game.addSystem`
@@ -1577,6 +1577,7 @@ const EXT_TO_TYPE = {
1577
1577
  jpg: 'jpg',
1578
1578
  jpeg: 'jpeg',
1579
1579
  webp: 'webp',
1580
+ ktx2: 'ktx2',
1580
1581
  json: 'json',
1581
1582
  tex: 'tex',
1582
1583
  ske: 'ske',
@@ -1645,6 +1646,38 @@ function wrapSlot(slot, value, resourceName) {
1645
1646
  url,
1646
1647
  };
1647
1648
  }
1649
+ function nonemptyUrl(url) {
1650
+ if (typeof url !== 'string')
1651
+ return undefined;
1652
+ const trimmed = url.trim();
1653
+ return trimmed || undefined;
1654
+ }
1655
+ /** KTX2 URL for one image slot: the slot itself, `ktx2`, or a `texture` entry. */
1656
+ function compressedTextureUrl(slot) {
1657
+ if (!slot)
1658
+ return undefined;
1659
+ if (slot.type === 'ktx2') {
1660
+ const own = nonemptyUrl(slot.url);
1661
+ if (own)
1662
+ return own;
1663
+ }
1664
+ const companion = nonemptyUrl(slot.ktx2);
1665
+ if (companion)
1666
+ return companion;
1667
+ const textures = Array.isArray(slot.texture) ? slot.texture : slot.texture ? [slot.texture] : [];
1668
+ for (const item of textures) {
1669
+ if (item?.type !== 'ktx2')
1670
+ continue;
1671
+ const url = nonemptyUrl(item.url);
1672
+ if (url)
1673
+ return url;
1674
+ }
1675
+ return undefined;
1676
+ }
1677
+ /** Raster URL used when KTX2 transcode has no compressed GPU target. */
1678
+ function imageFallbackUrl(slot) {
1679
+ return nonemptyUrl(slot?.fallback?.url);
1680
+ }
1648
1681
  function missingUrlError(resourceName, slot) {
1649
1682
  return new Error(`Resource "${resourceName}" src.${slot}.url is required. ` +
1650
1683
  `Use { type, url } (e.g. { type: "webp", url: "https://..." }). ` +
@@ -1818,11 +1851,13 @@ resourceLoader$1.XhrLoadStrategy.setExtensionXhrType('dae', resourceLoader$1.Xhr
1818
1851
  resourceLoader$1.XhrLoadStrategy.setExtensionXhrType('vert', resourceLoader$1.XhrResponseType.Text);
1819
1852
  resourceLoader$1.XhrLoadStrategy.setExtensionXhrType('frag', resourceLoader$1.XhrResponseType.Text);
1820
1853
  resourceLoader$1.XhrLoadStrategy.setExtensionXhrType('glsl', resourceLoader$1.XhrResponseType.Text);
1854
+ resourceLoader$1.XhrLoadStrategy.setExtensionXhrType('ktx2', resourceLoader$1.XhrResponseType.Buffer);
1821
1855
  const RESOURCE_TYPE_STRATEGY = {
1822
1856
  png: resourceLoader$1.ImageLoadStrategy,
1823
1857
  jpg: resourceLoader$1.ImageLoadStrategy,
1824
1858
  jpeg: resourceLoader$1.ImageLoadStrategy,
1825
1859
  webp: resourceLoader$1.ImageLoadStrategy,
1860
+ ktx2: resourceLoader$1.XhrLoadStrategy,
1826
1861
  json: resourceLoader$1.XhrLoadStrategy,
1827
1862
  tex: resourceLoader$1.XhrLoadStrategy,
1828
1863
  ske: resourceLoader$1.XhrLoadStrategy,
@@ -2086,6 +2121,107 @@ resource.registerInstance(exports.RESOURCE_TYPE.SHADER, (res) => ({
2086
2121
  fragment: typeof res.data?.fragment === 'string' ? res.data.fragment : '',
2087
2122
  }));
2088
2123
 
2124
+ const DEFAULT_MAX_PIXEL_RATIO = 2;
2125
+ const ADAPTIVE_WARMUP_MS = 3000;
2126
+ const ADAPTIVE_SLOW_FRAME_MS = 20;
2127
+ const ADAPTIVE_FAST_FRAME_MS = 12;
2128
+ const ADAPTIVE_SLOW_FRAME_COUNT = 30;
2129
+ const ADAPTIVE_FAST_FRAME_COUNT = 120;
2130
+ const ADAPTIVE_STEP = 0.5;
2131
+ const MIN_PIXEL_RATIO = 1;
2132
+ function isPositive(value) {
2133
+ return typeof value === 'number' && Number.isFinite(value) && value > 0;
2134
+ }
2135
+ function roundToHalf(value) {
2136
+ return Math.round(value * 2) / 2;
2137
+ }
2138
+ function readDevicePixelRatio(devicePixelRatio) {
2139
+ if (isPositive(devicePixelRatio))
2140
+ return devicePixelRatio;
2141
+ if (typeof window !== 'undefined' && isPositive(window.devicePixelRatio)) {
2142
+ return window.devicePixelRatio;
2143
+ }
2144
+ return 1;
2145
+ }
2146
+ function resolveRenderQuality(input = {}, defaults = {}) {
2147
+ const maxPixelRatio = isPositive(input.maxPixelRatio) ? input.maxPixelRatio : DEFAULT_MAX_PIXEL_RATIO;
2148
+ const explicit = isPositive(input.resolution)
2149
+ ? input.resolution
2150
+ : isPositive(input.pixelRatio)
2151
+ ? input.pixelRatio
2152
+ : undefined;
2153
+ const devicePixelRatio = readDevicePixelRatio(input.devicePixelRatio ?? defaults.devicePixelRatio);
2154
+ const pixelRatio = explicit ?? Math.min(devicePixelRatio, maxPixelRatio);
2155
+ return {
2156
+ pixelRatio,
2157
+ pixelRatioCeiling: pixelRatio,
2158
+ antialias: typeof input.antialias === 'boolean' ? input.antialias : pixelRatio < 2,
2159
+ pauseWhenHidden: typeof input.pauseWhenHidden === 'boolean' ? input.pauseWhenHidden : true,
2160
+ adaptivePixelRatio: typeof input.adaptivePixelRatio === 'boolean'
2161
+ ? input.adaptivePixelRatio
2162
+ : defaults.adaptivePixelRatio ?? false,
2163
+ powerPreference: input.powerPreference ?? 'high-performance',
2164
+ autoDensity: typeof input.autoDensity === 'boolean' ? input.autoDensity : true,
2165
+ explicitPixelRatio: explicit != null,
2166
+ };
2167
+ }
2168
+ function shouldPauseRendering(pauseWhenHidden) {
2169
+ return pauseWhenHidden && typeof document !== 'undefined' && document.hidden === true;
2170
+ }
2171
+ /**
2172
+ * After a warmup, a run of slow frames steps the pixel ratio down by 0.5
2173
+ * (floor 1). A longer run of fast frames steps it back up toward the ceiling.
2174
+ */
2175
+ class AdaptivePixelRatio {
2176
+ constructor(options) {
2177
+ this.elapsedMs = 0;
2178
+ this.slowFrames = 0;
2179
+ this.fastFrames = 0;
2180
+ this.pixelRatio = options.pixelRatio;
2181
+ this.ceiling = options.ceiling;
2182
+ this.enabled = options.enabled;
2183
+ }
2184
+ /** Returns the new ratio when it changes, otherwise null. */
2185
+ sample(deltaMs) {
2186
+ if (!this.enabled)
2187
+ return null;
2188
+ if (!(deltaMs > 0) || !Number.isFinite(deltaMs))
2189
+ return null;
2190
+ this.elapsedMs += deltaMs;
2191
+ if (this.elapsedMs < ADAPTIVE_WARMUP_MS)
2192
+ return null;
2193
+ if (deltaMs > ADAPTIVE_SLOW_FRAME_MS) {
2194
+ this.fastFrames = 0;
2195
+ this.slowFrames += 1;
2196
+ if (this.slowFrames >= ADAPTIVE_SLOW_FRAME_COUNT && this.pixelRatio > MIN_PIXEL_RATIO) {
2197
+ this.slowFrames = 0;
2198
+ const next = Math.max(MIN_PIXEL_RATIO, roundToHalf(this.pixelRatio - ADAPTIVE_STEP));
2199
+ if (next < this.pixelRatio) {
2200
+ this.pixelRatio = next;
2201
+ return next;
2202
+ }
2203
+ }
2204
+ return null;
2205
+ }
2206
+ if (deltaMs < ADAPTIVE_FAST_FRAME_MS) {
2207
+ this.slowFrames = 0;
2208
+ this.fastFrames += 1;
2209
+ if (this.fastFrames >= ADAPTIVE_FAST_FRAME_COUNT && this.pixelRatio < this.ceiling) {
2210
+ this.fastFrames = 0;
2211
+ const next = Math.min(this.ceiling, roundToHalf(this.pixelRatio + ADAPTIVE_STEP));
2212
+ if (next > this.pixelRatio) {
2213
+ this.pixelRatio = next;
2214
+ return next;
2215
+ }
2216
+ }
2217
+ return null;
2218
+ }
2219
+ this.slowFrames = 0;
2220
+ this.fastFrames = 0;
2221
+ return null;
2222
+ }
2223
+ }
2224
+
2089
2225
  /** Decorators util */
2090
2226
  const decorators = {
2091
2227
  IDEProp,
@@ -2093,6 +2229,7 @@ const decorators = {
2093
2229
  };
2094
2230
  console.log(`@combos-fun/engine version: ${version}`);
2095
2231
 
2232
+ exports.AdaptivePixelRatio = AdaptivePixelRatio;
2096
2233
  exports.COMBOS_GAME_PLUGIN_INIT_SUCCESS = COMBOS_GAME_PLUGIN_INIT_SUCCESS;
2097
2234
  exports.COMBOS_GAME_READY = COMBOS_GAME_READY;
2098
2235
  exports.COMBOS_GAME_SET_PLAYING = COMBOS_GAME_SET_PLAYING;
@@ -2106,7 +2243,9 @@ exports.RESOURCE_TYPE_STRATEGY = RESOURCE_TYPE_STRATEGY;
2106
2243
  exports.Scene = Scene;
2107
2244
  exports.System = System;
2108
2245
  exports.componentObserver = componentObserver;
2246
+ exports.compressedTextureUrl = compressedTextureUrl;
2109
2247
  exports.decorators = decorators;
2248
+ exports.imageFallbackUrl = imageFallbackUrl;
2110
2249
  exports.isAllowedMessageOrigin = isAllowedMessageOrigin;
2111
2250
  exports.mergeAllowedMessageOrigins = mergeAllowedMessageOrigins;
2112
2251
  exports.normalizeResourceSrc = normalizeResourceSrc;
@@ -2115,7 +2254,9 @@ exports.parseSetPlayingMessage = parseSetPlayingMessage;
2115
2254
  exports.postParentGameReady = postParentGameReady;
2116
2255
  exports.postParentGameState = postParentGameState;
2117
2256
  exports.postParentPluginInitSuccess = postParentPluginInitSuccess;
2257
+ exports.resolveRenderQuality = resolveRenderQuality;
2118
2258
  exports.resource = resource;
2119
2259
  exports.resourceLoader = resourceLoader;
2260
+ exports.shouldPauseRendering = shouldPauseRendering;
2120
2261
  exports.version = version;
2121
2262
  //# sourceMappingURL=engine.cjs.js.map