UNPKG

bp-cloner

Version:

More info and explanations here - https://doc.babylonjs.com/communityExtensions/clonerSystem

237 lines (236 loc) 8.96 kB
import { c as B, a as z, V as m, I as O, B as S, T as G, d as V, P as k } from "./index-7uRiSnMV.js"; import "./dumpTools-RKIlzSDD.js"; const R = "image/png", F = 2, C = [134, 22, 135, 150, 246, 214, 150, 54]; function H(e) { const r = new DataView(e.buffer, e.byteOffset, e.byteLength); let n = 0; for (let i = 0; i < C.length; i++) if (r.getUint8(n++) !== C[i]) return B.Error("Not a babylon environment map"), null; let t = "", o = 0; for (; o = r.getUint8(n++); ) t += String.fromCharCode(o); let a = JSON.parse(t); return a = A(a), a.binaryDataPosition = n, a.specular && (a.specular.lodGenerationScale = a.specular.lodGenerationScale || 0.8), a; } function A(e) { if (e.version > F) throw new Error(`Unsupported babylon environment map version "${e.version}". Latest supported version is "${F}".`); return e.version === 2 || (e = { ...e, version: 2, imageType: R }), e; } function E(e, r) { r = A(r); const n = r.specular; let t = Math.log2(r.width); if (t = Math.round(t) + 1, n.mipmaps.length !== 6 * t) throw new Error(`Unsupported specular mipmaps number "${n.mipmaps.length}"`); const o = new Array(t); for (let a = 0; a < t; a++) { o[a] = new Array(6); for (let i = 0; i < 6; i++) { const s = n.mipmaps[a * 6 + i]; o[a][i] = new Uint8Array(e.buffer, e.byteOffset + r.binaryDataPosition + s.position, s.length); } } return o; } function N(e, r) { var o; r = A(r); const n = new Array(6), t = (o = r.irradiance) == null ? void 0 : o.irradianceTexture; if (t) { if (t.faces.length !== 6) throw new Error(`Incorrect irradiance texture faces number "${t.faces.length}"`); for (let a = 0; a < 6; a++) { const i = t.faces[a]; n[a] = new Uint8Array(e.buffer, e.byteOffset + r.binaryDataPosition + i.position, i.length); } } return n; } function P(e, r, n) { var s; n = A(n); const t = n.specular; if (!t) return Promise.resolve([]); e._lodGenerationScale = t.lodGenerationScale; const o = [], a = E(r, n); o.push($(e, a, n.imageType)); const i = (s = n.irradiance) == null ? void 0 : s.irradianceTexture; if (i) { const d = N(r, n); o.push(Y(e, d, i.size, n.imageType)); } return Promise.all(o); } function D(e, r, n, t, o, a, i, s, d, p, w) { return new Promise((y, I) => { if (n) { const g = r.createTexture(null, !0, !0, null, 1, null, (l) => { I(l); }, e); t == null || t.onEffectCreatedObservable.addOnce((l) => { l.executeWhenCompiled(() => { t.externalTextureSamplerBinding = !0, t.onApply = (c) => { c._bindTexture("textureSampler", g), c.setFloat2("scale", 1, r._features.needsInvertingBitmap && e instanceof ImageBitmap ? -1 : 1); }, r.scenes.length && (r.scenes[0].postProcessManager.directRender([t], p, !0, a, i), r.restoreDefaultFramebuffer(), g.dispose(), URL.revokeObjectURL(o), y()); }); }); } else { if (r._uploadImageToTexture(w, e, a, i), s) { const g = d[i]; g && r._uploadImageToTexture(g._texture, e, a, 0); } y(); } }); } async function $(e, r, n = R) { const t = e.getEngine(); e.format = 5, e.type = 0, e.generateMipMaps = !0, e._cachedAnisotropicFilteringLevel = null, t.updateTextureSamplingMode(3, e), await U(e, r, !0, n), e.isReady = !0; } async function Y(e, r, n, t = R) { const o = e.getEngine(), a = new O( o, 5 /* InternalTextureSource.RenderTarget */ ), i = new S(o, a); e._irradianceTexture = i, a.isCube = !0, a.format = 5, a.type = 0, a.generateMipMaps = !0, a._cachedAnisotropicFilteringLevel = null, a.generateMipMaps = !0, a.width = n, a.height = n, o.updateTextureSamplingMode(3, a), await U(a, [r], !1, t), o.generateMipMapsForCubemap(a), a.isReady = !0; } async function U(e, r, n, t = R) { if (!G.IsExponentOfTwo(e.width)) throw new Error("Texture size must be a power of two"); const o = V(e.width) + 1, a = e.getEngine(); let i = !1, s = !1, d = null, p = null, w = null; const y = a.getCaps(); y.textureLOD ? a._features.supportRenderAndCopyToLodForFloatTextures ? y.textureHalfFloatRender && y.textureHalfFloatLinearFiltering ? (i = !0, e.type = 2) : y.textureFloatRender && y.textureFloatLinearFiltering && (i = !0, e.type = 1) : i = !1 : (i = !1, s = n); let I = 0; if (i) a.isWebGPU ? (I = 1, await import("./rgbdDecode.fragment-D0W-aLrq.js")) : await import("./rgbdDecode.fragment-B17E8fhA.js"), d = new k("rgbdDecode", "rgbdDecode", null, null, 1, null, 3, a, !1, void 0, e.type, void 0, null, !1, void 0, I), e._isRGBD = !1, e.invertY = !1, p = a.createRenderTargetCubeTexture(e.width, { generateDepthBuffer: !1, generateMipMaps: !0, generateStencilBuffer: !1, samplingMode: 3, type: e.type, format: 5 }); else if (e._isRGBD = !0, e.invertY = !0, s) { w = {}; const c = e._lodGenerationScale, h = e._lodGenerationOffset; for (let u = 0; u < 3; u++) { const b = 1 - u / 2, f = h, L = (o - 1) * c + h, M = f + (L - f) * b, x = Math.round(Math.min(Math.max(M, 0), L)), v = new O( a, 2 /* InternalTextureSource.Temp */ ); v.isCube = !0, v.invertY = !0, v.generateMipMaps = !1, a.updateTextureSamplingMode(2, v); const _ = new S(null); switch (_._isCube = !0, _._texture = v, w[x] = _, u) { case 0: e._lodTextureLow = _; break; case 1: e._lodTextureMid = _; break; case 2: e._lodTextureHigh = _; break; } } } const g = []; for (let l = 0; l < r.length; l++) for (let c = 0; c < 6; c++) { const h = r[l][c], u = new Blob([h], { type: t }), T = URL.createObjectURL(u); let b; if (a._features.forceBitmapOverHTMLImageElement) b = a.createImageBitmap(u, { premultiplyAlpha: "none" }).then((f) => D(f, a, i, d, T, c, l, s, w, p, e)); else { const f = new Image(); f.src = T, b = new Promise((L, M) => { f.onload = () => { D(f, a, i, d, T, c, l, s, w, p, e).then(() => L()).catch((x) => { M(x); }); }, f.onerror = (x) => { M(x); }; }); } g.push(b); } if (await Promise.all(g), r.length < o) { let l; const c = Math.pow(2, o - 1 - r.length), h = c * c * 4; switch (e.type) { case 0: { l = new Uint8Array(h); break; } case 2: { l = new Uint16Array(h); break; } case 1: { l = new Float32Array(h); break; } } for (let u = r.length; u < o; u++) for (let T = 0; T < 6; T++) a._uploadArrayBufferViewToTexture((p == null ? void 0 : p.texture) || e, l, T, u); } if (p) { const l = e._irradianceTexture; e._irradianceTexture = null, a._releaseTexture(e), p._swapAndDie(e), e._irradianceTexture = l; } d && d.dispose(), s && (e._lodTextureHigh && e._lodTextureHigh._texture && (e._lodTextureHigh._texture.isReady = !0), e._lodTextureMid && e._lodTextureMid._texture && (e._lodTextureMid._texture.isReady = !0), e._lodTextureLow && e._lodTextureLow._texture && (e._lodTextureLow._texture.isReady = !0)); } function W(e, r) { r = A(r); const n = r.irradiance; if (!n) return; const t = new z(); m.FromArrayToRef(n.x, 0, t.x), m.FromArrayToRef(n.y, 0, t.y), m.FromArrayToRef(n.z, 0, t.z), m.FromArrayToRef(n.xx, 0, t.xx), m.FromArrayToRef(n.yy, 0, t.yy), m.FromArrayToRef(n.zz, 0, t.zz), m.FromArrayToRef(n.yz, 0, t.yz), m.FromArrayToRef(n.zx, 0, t.zx), m.FromArrayToRef(n.xy, 0, t.xy), e._sphericalPolynomial = t; } class q { constructor() { this.supportCascades = !1; } /** * Uploads the cube texture data to the WebGL texture. It has already been bound. * @param data contains the texture data * @param texture defines the BabylonJS internal texture * @param createPolynomials will be true if polynomials have been requested * @param onLoad defines the callback to trigger once the texture is ready * @param onError defines the callback to trigger in case of error */ loadCubeData(r, n, t, o, a) { if (Array.isArray(r)) return; const i = H(r); if (i) { n.width = i.width, n.height = i.width; try { W(n, i), P(n, r, i).then(() => { n.isReady = !0, n.onLoadedObservable.notifyObservers(n), n.onLoadedObservable.clear(), o && o(); }, (s) => { a == null || a("Can not upload environment levels", s); }); } catch (s) { a == null || a("Can not upload environment file", s); } } else a && a("Can not parse the environment file", null); } /** * Uploads the 2D texture data to the WebGL texture. It has already been bound once in the callback. */ loadData() { throw ".env not supported in 2d."; } } export { q as _ENVTextureLoader };