|
| 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 | + |
| 15 | +(function () { |
| 16 | + "use strict"; |
| 17 | + |
| 18 | + if (typeof BABYLON === "undefined") { |
| 19 | + return; |
| 20 | + } |
| 21 | + if (!BABYLON.NativeEngine || !BABYLON.NativeEngine.prototype) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + var proto = BABYLON.NativeEngine.prototype; |
| 26 | + if (proto.__cubeTexturePolyfillInstalled) { |
| 27 | + return; |
| 28 | + } |
| 29 | + proto.__cubeTexturePolyfillInstalled = true; |
| 30 | + |
| 31 | + var original = proto.createCubeTexture; |
| 32 | + if (typeof original !== "function") { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + var FALLBACK_EXTS = [".dds", ".ktx", ".ktx2"]; |
| 37 | + |
| 38 | + function getExtension(url, forced) { |
| 39 | + if (forced) { |
| 40 | + return forced.toLowerCase(); |
| 41 | + } |
| 42 | + var dot = url.lastIndexOf("."); |
| 43 | + if (dot < 0) { |
| 44 | + return ""; |
| 45 | + } |
| 46 | + var ext = url.substring(dot).toLowerCase(); |
| 47 | + var q = ext.indexOf("?"); |
| 48 | + if (q >= 0) { |
| 49 | + ext = ext.substring(0, q); |
| 50 | + } |
| 51 | + return ext; |
| 52 | + } |
| 53 | + |
| 54 | + function replaceExt(url, oldExt) { |
| 55 | + return url.substring(0, url.length - oldExt.length) + ".env"; |
| 56 | + } |
| 57 | + |
| 58 | + proto.createCubeTexture = function (rootUrl, scene, files, noMipmap, onLoad, onError, format, forcedExtension, createPolynomials, lodScale, lodOffset, fallback, loaderOptions, useSRGBBuffer, buffer) { |
| 59 | + var ext = getExtension(rootUrl, forcedExtension); |
| 60 | + var hasFiles = files && files.length === 6; |
| 61 | + var canFallback = !buffer && !forcedExtension && !hasFiles && FALLBACK_EXTS.indexOf(ext) >= 0; |
| 62 | + |
| 63 | + if (!canFallback) { |
| 64 | + return original.apply(this, arguments); |
| 65 | + } |
| 66 | + |
| 67 | + var self = this; |
| 68 | + var envUrl = replaceExt(rootUrl, ext); |
| 69 | + var texture = fallback || new BABYLON.InternalTexture(self, 7 /* Cube */); |
| 70 | + texture.isCube = true; |
| 71 | + texture.url = rootUrl; |
| 72 | + |
| 73 | + var settled = false; |
| 74 | + var settle = function (action) { |
| 75 | + if (settled) { |
| 76 | + return; |
| 77 | + } |
| 78 | + settled = true; |
| 79 | + try { |
| 80 | + action(); |
| 81 | + } catch (e) { |
| 82 | + if (onError) { |
| 83 | + onError(e && e.message ? e.message : String(e), e); |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + var onEnvLoaded = function (data) { |
| 89 | + settle(function () { |
| 90 | + var buf = (data && data.byteLength !== undefined && !(data instanceof Uint8Array)) ? new Uint8Array(data, 0, data.byteLength) : data; |
| 91 | + original.call(self, envUrl, scene, files, noMipmap, onLoad, onError, format, ".env", createPolynomials, lodScale || 0, lodOffset || 0, texture, loaderOptions, useSRGBBuffer || false, buf); |
| 92 | + }); |
| 93 | + }; |
| 94 | + |
| 95 | + var onEnvFailed = function (request, exception) { |
| 96 | + settle(function () { |
| 97 | + original.call(self, rootUrl, scene, files, noMipmap, onLoad, onError, format, forcedExtension, createPolynomials, lodScale, lodOffset, texture, loaderOptions, useSRGBBuffer, buffer); |
| 98 | + }); |
| 99 | + }; |
| 100 | + |
| 101 | + try { |
| 102 | + self._loadFile(envUrl, onEnvLoaded, undefined, undefined, true, onEnvFailed); |
| 103 | + } catch (e) { |
| 104 | + onEnvFailed(null, e); |
| 105 | + } |
| 106 | + |
| 107 | + return texture; |
| 108 | + }; |
| 109 | + |
| 110 | + if (typeof console !== "undefined" && console.log) { |
| 111 | + console.log("[cube_texture_polyfill] NativeEngine.createCubeTexture patched with .env fallback"); |
| 112 | + } |
| 113 | +})(); |
0 commit comments