|
1 | | -// Cube texture fallback for BN's NativeEngine. |
2 | | -// |
3 | | -// BN's NativeEngine.createCubeTexture only natively handles .env single-file |
4 | | -// cubemaps and 6-file face arrays. Snippets that load .dds (or .ktx) cubemaps |
5 | | -// without an explicit forcedExtension or 6-file array throw "Cannot load |
6 | | -// cubemap because 6 files were not defined". |
7 | | -// |
8 | | -// Babylon's standard environment cubemaps are hosted both as <name>.dds and |
9 | | -// <name>.env at the same path on assets.babylonjs.com and |
10 | | -// playground.babylonjs.com. This polyfill detects the failure pre-condition |
11 | | -// and transparently retries with the .env URL. If the .env counterpart 404s, |
12 | | -// it falls through to the original (which throws), preserving existing |
13 | | -// behavior. |
14 | | - |
| 1 | +#include <Babylon/Polyfills/CubeTexture.h> |
| 2 | + |
| 3 | +#include <napi/env.h> |
| 4 | + |
| 5 | +namespace Babylon::Polyfills::CubeTexture |
| 6 | +{ |
| 7 | + namespace |
| 8 | + { |
| 9 | + // Embedded polyfill source. Kept verbatim from the original Playground |
| 10 | + // cube_texture_polyfill.js (PR #1710 v0) so behaviour stays identical: |
| 11 | + // - Patches BABYLON.NativeEngine.prototype.createCubeTexture. |
| 12 | + // - Triggers only when the URL ends in .dds / .ktx / .ktx2 and no |
| 13 | + // forcedExtension, 6-file array, or raw buffer was supplied. |
| 14 | + // - Retries via NativeEngine._loadFile to fetch the .env counterpart; |
| 15 | + // on 404 falls back to the original (which throws), preserving the |
| 16 | + // existing failure mode. |
| 17 | + // - Idempotent via the __cubeTexturePolyfillInstalled marker on the |
| 18 | + // prototype. |
| 19 | + constexpr const char* JS_SOURCE = R"javascript( |
15 | 20 | (function () { |
16 | 21 | "use strict"; |
17 | 22 |
|
|
92 | 97 | }); |
93 | 98 | }; |
94 | 99 |
|
95 | | - var onEnvFailed = function (request, exception) { |
| 100 | + var onEnvFailed = function (/* request, exception */) { |
96 | 101 | settle(function () { |
97 | 102 | original.call(self, rootUrl, scene, files, noMipmap, onLoad, onError, format, forcedExtension, createPolynomials, lodScale, lodOffset, texture, loaderOptions, useSRGBBuffer, buffer); |
98 | 103 | }); |
|
108 | 113 | }; |
109 | 114 |
|
110 | 115 | if (typeof console !== "undefined" && console.log) { |
111 | | - console.log("[cube_texture_polyfill] NativeEngine.createCubeTexture patched with .env fallback"); |
| 116 | + console.log("[CubeTexture polyfill] NativeEngine.createCubeTexture patched with .env fallback"); |
112 | 117 | } |
113 | 118 | })(); |
| 119 | +)javascript"; |
| 120 | + |
| 121 | + constexpr const char* JS_SOURCE_URL = "babylon-native://polyfills/CubeTexture.js"; |
| 122 | + } |
| 123 | + |
| 124 | + void Initialize(Napi::Env env) |
| 125 | + { |
| 126 | + // The free function Napi::Eval(env, source, url) is declared by every |
| 127 | + // engine-specific <napi/env.h> across both N-API trees (Chakra, V8, |
| 128 | + // JavaScriptCore in Core/Node-API/Include/Engine/<X>/, and JSI in |
| 129 | + // Core/Node-API-JSI/Include/napi/). Using it uniformly avoids the |
| 130 | + // Shared-only env.RunScript() which is missing from the JSI tree. |
| 131 | + Napi::HandleScope scope{env}; |
| 132 | + Napi::Eval(env, JS_SOURCE, JS_SOURCE_URL); |
| 133 | + } |
| 134 | +} |
0 commit comments