UNPKG

bp-cloner

Version:

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

353 lines (352 loc) 14 kB
import { g as T, R as At, h as yt, I as pt, _ as Ft, c as H, F as U, i as M, C as g, j as dt } from "./index-7uRiSnMV.js"; function Ct(d) { const n = d.split("?")[0], e = n.lastIndexOf("."); return e > -1 ? n.substring(e).toLowerCase() : ""; } T.prototype._partialLoadFile = function(d, n, e, r, o = null) { const i = (t) => { e[n] = t, e._internalCount++, e._internalCount === 6 && r(e); }, u = (t, l) => { o && t && o(t.status + " " + t.statusText, l); }; this._loadFile(d, i, void 0, void 0, !0, u); }; T.prototype._cascadeLoadFiles = function(d, n, e, r = null) { const o = []; o._internalCount = 0; for (let i = 0; i < 6; i++) this._partialLoadFile(e[i], i, o, n, r); }; T.prototype._cascadeLoadImgs = function(d, n, e, r, o = null, i) { const u = []; u._internalCount = 0; for (let t = 0; t < 6; t++) this._partialLoadImg(r[t], t, u, d, n, e, o, i); }; T.prototype._partialLoadImg = function(d, n, e, r, o, i, u = null, t) { const l = At(); yt(d, (c) => { e[n] = c, e._internalCount++, r && r.removePendingData(l), e._internalCount === 6 && i && i(o, e); }, (c, f) => { r && r.removePendingData(l), u && u(c, f); }, r ? r.offlineProvider : null, t), r && r.addPendingData(l); }; T.prototype.createCubeTextureBase = function(d, n, e, r, o = null, i = null, u, t = null, l = !1, a = 0, s = 0, c = null, f = null, G = null, y = !1, p = null) { const A = c || new pt( this, 7 /* InternalTextureSource.Cube */ ); A.isCube = !0, A.url = d, A.generateMipMaps = !r, A._lodGenerationScale = a, A._lodGenerationOffset = s, A._useSRGBBuffer = !!y && this._caps.supportSRGBBuffers && (this.version > 1 || this.isWebGPU || !!r), A !== c && (A.label = d.substring(0, 60)), this._doNotHandleContextLost || (A._extension = t, A._files = e, A._buffer = p); const _ = d; this._transformTextureUrl && !c && (d = this._transformTextureUrl(d)); const B = t ?? Ct(d), P = Ft(B), w = (h, x) => { d === _ ? i && h && i(h.status + " " + h.statusText, x) : (H.Warn(`Failed to load ${d}, falling back to the ${_}`), this.createCubeTextureBase(_, n, e, !!r, o, i, u, t, l, a, s, A, f, G, y, p)); }; if (P) P.then((h) => { const x = (C) => { f && f(A, C), h.loadCubeData(C, A, l, o, i); }; p ? x(p) : e && e.length === 6 ? h.supportCascades ? this._cascadeLoadFiles(n, (C) => x(C.map((I) => new Uint8Array(I))), e, i) : i ? i("Textures type does not support cascades.") : H.Warn("Texture loader does not support cascades.") : this._loadFile(d, (C) => x(new Uint8Array(C)), void 0, void 0, !0, w); }); else { if (!e || e.length === 0) throw new Error("Cannot load cubemap because files were not defined, or the correct loader was not found."); this._cascadeLoadImgs(n, A, (h, x) => { G && G(h, x); }, e, i); } return this._internalTexturesCache.push(A), A; }; const _t = 542327876, Q = 131072, V = 512, Y = 4, Z = 64, S = 131072; function X(d) { return d.charCodeAt(0) + (d.charCodeAt(1) << 8) + (d.charCodeAt(2) << 16) + (d.charCodeAt(3) << 24); } function Gt(d) { return String.fromCharCode(d & 255, d >> 8 & 255, d >> 16 & 255, d >> 24 & 255); } const q = X("DXT1"), tt = X("DXT3"), et = X("DXT5"), z = X("DX10"), rt = 113, at = 116, nt = 2, st = 10, ht = 88, E = 31, mt = 0, xt = 1, ot = 2, ft = 3, $ = 4, it = 7, j = 20, lt = 21, bt = 22, Bt = 23, Rt = 24, Ot = 25, Ut = 26, gt = 28, wt = 32; class F { /** * Gets DDS information from an array buffer * @param data defines the array buffer view to read data from * @returns the DDS information */ static GetDDSInfo(n) { const e = new Int32Array(n.buffer, n.byteOffset, E), r = new Int32Array(n.buffer, n.byteOffset, E + 4); let o = 1; e[ot] & Q && (o = Math.max(1, e[it])); const i = e[lt], u = i === z ? r[wt] : 0; let t = 0; switch (i) { case rt: t = 2; break; case at: t = 1; break; case z: if (u === st) { t = 2; break; } if (u === nt) { t = 1; break; } } return { width: e[$], height: e[ft], mipmapCount: o, isFourCC: (e[j] & Y) === Y, isRGB: (e[j] & Z) === Z, isLuminance: (e[j] & S) === S, isCube: (e[gt] & V) === V, isCompressed: i === q || i === tt || i === et, dxgiFormat: u, textureType: t }; } static _GetHalfFloatAsFloatRGBAArrayBuffer(n, e, r, o, i, u) { const t = new Float32Array(o), l = new Uint16Array(i, r); let a = 0; for (let s = 0; s < e; s++) for (let c = 0; c < n; c++) { const f = (c + s * n) * 4; t[a] = U(l[f]), t[a + 1] = U(l[f + 1]), t[a + 2] = U(l[f + 2]), F.StoreLODInAlphaChannel ? t[a + 3] = u : t[a + 3] = U(l[f + 3]), a += 4; } return t; } static _GetHalfFloatRGBAArrayBuffer(n, e, r, o, i, u) { if (F.StoreLODInAlphaChannel) { const t = new Uint16Array(o), l = new Uint16Array(i, r); let a = 0; for (let s = 0; s < e; s++) for (let c = 0; c < n; c++) { const f = (c + s * n) * 4; t[a] = l[f], t[a + 1] = l[f + 1], t[a + 2] = l[f + 2], t[a + 3] = M(u), a += 4; } return t; } return new Uint16Array(i, r, o); } static _GetFloatRGBAArrayBuffer(n, e, r, o, i, u) { if (F.StoreLODInAlphaChannel) { const t = new Float32Array(o), l = new Float32Array(i, r); let a = 0; for (let s = 0; s < e; s++) for (let c = 0; c < n; c++) { const f = (c + s * n) * 4; t[a] = l[f], t[a + 1] = l[f + 1], t[a + 2] = l[f + 2], t[a + 3] = u, a += 4; } return t; } return new Float32Array(i, r, o); } static _GetFloatAsHalfFloatRGBAArrayBuffer(n, e, r, o, i, u) { const t = new Uint16Array(o), l = new Float32Array(i, r); let a = 0; for (let s = 0; s < e; s++) for (let c = 0; c < n; c++) t[a] = M(l[a]), t[a + 1] = M(l[a + 1]), t[a + 2] = M(l[a + 2]), F.StoreLODInAlphaChannel ? t[a + 3] = M(u) : t[a + 3] = M(l[a + 3]), a += 4; return t; } static _GetFloatAsUIntRGBAArrayBuffer(n, e, r, o, i, u) { const t = new Uint8Array(o), l = new Float32Array(i, r); let a = 0; for (let s = 0; s < e; s++) for (let c = 0; c < n; c++) { const f = (c + s * n) * 4; t[a] = g(l[f]) * 255, t[a + 1] = g(l[f + 1]) * 255, t[a + 2] = g(l[f + 2]) * 255, F.StoreLODInAlphaChannel ? t[a + 3] = u : t[a + 3] = g(l[f + 3]) * 255, a += 4; } return t; } static _GetHalfFloatAsUIntRGBAArrayBuffer(n, e, r, o, i, u) { const t = new Uint8Array(o), l = new Uint16Array(i, r); let a = 0; for (let s = 0; s < e; s++) for (let c = 0; c < n; c++) { const f = (c + s * n) * 4; t[a] = g(U(l[f])) * 255, t[a + 1] = g(U(l[f + 1])) * 255, t[a + 2] = g(U(l[f + 2])) * 255, F.StoreLODInAlphaChannel ? t[a + 3] = u : t[a + 3] = g(U(l[f + 3])) * 255, a += 4; } return t; } static _GetRGBAArrayBuffer(n, e, r, o, i, u, t, l, a) { const s = new Uint8Array(o), c = new Uint8Array(i, r); let f = 0; for (let G = 0; G < e; G++) for (let y = 0; y < n; y++) { const p = (y + G * n) * 4; s[f] = c[p + u], s[f + 1] = c[p + t], s[f + 2] = c[p + l], s[f + 3] = c[p + a], f += 4; } return s; } static _ExtractLongWordOrder(n) { return n === 0 || n === 255 || n === -16777216 ? 0 : 1 + F._ExtractLongWordOrder(n >> 8); } static _GetRGBArrayBuffer(n, e, r, o, i, u, t, l) { const a = new Uint8Array(o), s = new Uint8Array(i, r); let c = 0; for (let f = 0; f < e; f++) for (let G = 0; G < n; G++) { const y = (G + f * n) * 3; a[c] = s[y + u], a[c + 1] = s[y + t], a[c + 2] = s[y + l], c += 3; } return a; } static _GetLuminanceArrayBuffer(n, e, r, o, i) { const u = new Uint8Array(o), t = new Uint8Array(i, r); let l = 0; for (let a = 0; a < e; a++) for (let s = 0; s < n; s++) { const c = s + a * n; u[l] = t[c], l++; } return u; } /** * Uploads DDS Levels to a Babylon Texture * @internal */ static UploadDDSLevels(n, e, r, o, i, u, t = -1, l, a = !0) { let s = null; o.sphericalPolynomial && (s = []); const c = !!n.getCaps().s3tc; e.generateMipMaps = i; const f = new Int32Array(r.buffer, r.byteOffset, E); let G, y, p, A = 0, _, B, P, w, h = 0, x = 1; if (f[mt] !== _t) { H.Error("Invalid magic number in DDS header"); return; } if (!o.isFourCC && !o.isRGB && !o.isLuminance) { H.Error("Unsupported format, must contain a FourCC, RGB or LUMINANCE code"); return; } if (o.isCompressed && !c) { H.Error("Compressed textures are not supported on this platform."); return; } let C = f[bt]; _ = f[xt] + 4; let I = !1; if (o.isFourCC) switch (G = f[lt], G) { case q: x = 8, h = 33777; break; case tt: x = 16, h = 33778; break; case et: x = 16, h = 33779; break; case rt: I = !0, C = 64; break; case at: I = !0, C = 128; break; case z: { _ += 5 * 4; let b = !1; switch (o.dxgiFormat) { case st: I = !0, C = 64, b = !0; break; case nt: I = !0, C = 128, b = !0; break; case ht: o.isRGB = !0, o.isFourCC = !1, C = 32, b = !0; break; } if (b) break; } // eslint-disable-next-line no-fallthrough default: H.Error(["Unsupported FourCC code:", Gt(G)]); return; } const v = F._ExtractLongWordOrder(f[Bt]), J = F._ExtractLongWordOrder(f[Rt]), K = F._ExtractLongWordOrder(f[Ot]), ct = F._ExtractLongWordOrder(f[Ut]); I && (h = n._getRGBABufferInternalSizedFormat(o.textureType)), P = 1, f[ot] & Q && i !== !1 && (P = Math.max(1, f[it])); const ut = l || 0, k = n.getCaps(); for (let b = ut; b < u; b++) { for (y = f[$], p = f[ft], w = 0; w < P; ++w) { if (t === -1 || t === w) { const m = t === -1 ? w : 0; if (!o.isCompressed && o.isFourCC) { e.format = 5, A = y * p * 4; let R = null; if (n._badOS || n._badDesktopOS || !k.textureHalfFloat && !k.textureFloat) C === 128 ? (R = F._GetFloatAsUIntRGBAArrayBuffer(y, p, r.byteOffset + _, A, r.buffer, m), s && m == 0 && s.push(F._GetFloatRGBAArrayBuffer(y, p, r.byteOffset + _, A, r.buffer, m))) : C === 64 && (R = F._GetHalfFloatAsUIntRGBAArrayBuffer(y, p, r.byteOffset + _, A, r.buffer, m), s && m == 0 && s.push(F._GetHalfFloatAsFloatRGBAArrayBuffer(y, p, r.byteOffset + _, A, r.buffer, m))), e.type = 0; else { const D = k.textureFloat && (a && k.textureFloatLinearFiltering || !a), W = k.textureHalfFloat && (a && k.textureHalfFloatLinearFiltering || !a), N = (C === 128 || C === 64 && !W) && D ? 1 : (C === 64 || C === 128 && !D) && W ? 2 : 0; let L, O = null; switch (C) { case 128: { switch (N) { case 1: L = F._GetFloatRGBAArrayBuffer, O = null; break; case 2: L = F._GetFloatAsHalfFloatRGBAArrayBuffer, O = F._GetFloatRGBAArrayBuffer; break; case 0: L = F._GetFloatAsUIntRGBAArrayBuffer, O = F._GetFloatRGBAArrayBuffer; break; } break; } default: { switch (N) { case 1: L = F._GetHalfFloatAsFloatRGBAArrayBuffer, O = null; break; case 2: L = F._GetHalfFloatRGBAArrayBuffer, O = F._GetHalfFloatAsFloatRGBAArrayBuffer; break; case 0: L = F._GetHalfFloatAsUIntRGBAArrayBuffer, O = F._GetHalfFloatAsFloatRGBAArrayBuffer; break; } break; } } e.type = N, R = L(y, p, r.byteOffset + _, A, r.buffer, m), s && m == 0 && s.push(O ? O(y, p, r.byteOffset + _, A, r.buffer, m) : R); } R && n._uploadDataToTextureDirectly(e, R, b, m); } else if (o.isRGB) e.type = 0, C === 24 ? (e.format = 4, A = y * p * 3, B = F._GetRGBArrayBuffer(y, p, r.byteOffset + _, A, r.buffer, v, J, K), n._uploadDataToTextureDirectly(e, B, b, m)) : (e.format = 5, A = y * p * 4, B = F._GetRGBAArrayBuffer(y, p, r.byteOffset + _, A, r.buffer, v, J, K, ct), n._uploadDataToTextureDirectly(e, B, b, m)); else if (o.isLuminance) { const R = n._getUnpackAlignement(), D = y; A = Math.floor((y + R - 1) / R) * R * (p - 1) + D, B = F._GetLuminanceArrayBuffer(y, p, r.byteOffset + _, A, r.buffer), e.format = 1, e.type = 0, n._uploadDataToTextureDirectly(e, B, b, m); } else A = Math.max(4, y) / 4 * Math.max(4, p) / 4 * x, B = new Uint8Array(r.buffer, r.byteOffset + _, A), e.type = 0, n._uploadCompressedDataToTextureDirectly(e, h, y, p, B, b, m); } _ += C ? y * p * (C / 8) : A, y *= 0.5, p *= 0.5, y = Math.max(1, y), p = Math.max(1, p); } if (l !== void 0) break; } s && s.length > 0 ? o.sphericalPolynomial = dt.ConvertCubeMapToSphericalPolynomial({ size: f[$], right: s[0], left: s[1], up: s[2], down: s[3], front: s[4], back: s[5], format: 5, type: 1, gammaSpace: !1 }) : o.sphericalPolynomial = void 0; } } F.StoreLODInAlphaChannel = !1; export { F as DDSTools };