@babylonjs/viewer
Version:
The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.
157 lines (154 loc) • 6.25 kB
JavaScript
import { o as TU, p as U8, ay as getBilinearSampler, q as getOrCreateSampler } from './index-DMbDahsc.esm.js';
async function rebuildTexture2D(engine, tex) {
const source = tex._recoverySource;
if (!source) {
return;
}
if (source.kind === "url") {
const rebuilt = await rebuildUrlTexture2D(engine, source.url, source.opts);
tex.texture = rebuilt.texture;
tex.view = rebuilt.view;
tex.sampler = rebuilt.sampler;
tex.width = rebuilt.width;
tex.height = rebuilt.height;
tex._recoverySource = source;
return;
}
if (source.kind === "solid") {
const texture2 = engine._device.createTexture({ size: { width: 1, height: 1 }, format: "rgba8unorm", usage: TU.TEXTURE_BINDING | TU.COPY_DST });
const data = new U8(source.rgba.map((v) => Math.round(v * 255)));
engine._device.queue.writeTexture({ texture: texture2 }, data, { bytesPerRow: 4, rowsPerImage: 1 }, { width: 1, height: 1 });
tex.texture = texture2;
tex.view = texture2.createView();
tex.sampler = getBilinearSampler(engine);
tex.width = 1;
tex.height = 1;
return;
}
if (source.kind === "dynamic") {
const { rebuildDynamicTexture2D } = await import('./dynamic-texture-recovery-BGY1ewGU.esm.js');
await rebuildDynamicTexture2D(engine, tex);
return;
}
if (source.kind === "pixels") {
const options = source.options;
const texture2 = engine._device.createTexture({
size: { width: source.width, height: source.height },
format: options.srgb ? "rgba8unorm-srgb" : "rgba8unorm",
usage: TU.TEXTURE_BINDING | TU.COPY_DST
});
engine._device.queue.writeTexture(
{ texture: texture2 },
source.data,
{ bytesPerRow: source.width * 4, rowsPerImage: source.height },
{ width: source.width, height: source.height }
);
tex.texture = texture2;
tex.view = texture2.createView();
tex.sampler = getOrCreateSampler(engine, {
addressModeU: options.addressModeU ?? "clamp-to-edge",
addressModeV: options.addressModeV ?? "clamp-to-edge",
minFilter: options.minFilter ?? "nearest",
magFilter: options.magFilter ?? "nearest"
});
tex.width = source.width;
tex.height = source.height;
return;
}
if (source.kind === "render") {
const texture2 = engine._device.createTexture({
size: { width: source.width, height: source.height },
format: source.format,
usage: TU.TEXTURE_BINDING | TU.RENDER_ATTACHMENT | TU.COPY_DST
});
tex.texture = texture2;
tex.view = texture2.createView();
tex.sampler = getOrCreateSampler(engine, source.samplerDesc);
tex.width = source.width;
tex.height = source.height;
return;
}
const width = source.bitmap?.width ?? 1;
const height = source.bitmap?.height ?? 1;
const format = source.srgb ? "rgba8unorm-srgb" : "rgba8unorm";
const mipLevelCount = source.mipMaps ? Math.floor(Math.log2(Math.max(width, height))) + 1 : 1;
const texture = engine._device.createTexture({
size: { width, height },
format,
mipLevelCount,
usage: TU.TEXTURE_BINDING | TU.COPY_DST | TU.COPY_SRC | TU.RENDER_ATTACHMENT
});
if (source.bitmap) {
engine._device.queue.copyExternalImageToTexture({ source: source.bitmap }, { texture, premultipliedAlpha: false }, { width, height });
if (source.mipMaps && mipLevelCount > 1) {
const { generateMipmaps } = await import('./generate-mipmaps-BpLaf4fV.esm.js');
generateMipmaps(engine, texture);
}
} else {
engine._device.queue.writeTexture({ texture }, source.fallback ?? new U8([255, 255, 255, 255]), { bytesPerRow: 4 }, { width: 1, height: 1 });
}
tex.texture = texture;
tex.view = texture.createView();
const samplerDescriptors = engine._deviceLostRecovery?._samplerDescriptors;
const capturedSamplerDesc = samplerDescriptors?.get(tex.sampler);
const samplerDesc = capturedSamplerDesc ?? {
addressModeU: "repeat",
addressModeV: "repeat",
minFilter: "linear",
magFilter: "linear",
mipmapFilter: "linear",
maxAnisotropy: 4
};
const sampler = samplerDesc.lodMaxClamp === 0 ? engine._device.createSampler(samplerDesc) : getOrCreateSampler(engine, samplerDesc);
if (capturedSamplerDesc) {
samplerDescriptors.set(sampler, capturedSamplerDesc);
}
tex.sampler = sampler;
tex.width = width;
tex.height = height;
}
async function rebuildUrlTexture2D(engine, url, opts) {
const mipMaps = opts.mipMaps ?? true;
const addressModeU = opts.addressModeU ?? "repeat";
const addressModeV = opts.addressModeV ?? "repeat";
const invertY = opts.invertY ?? true;
const srgb = opts.srgb ?? false;
const premultiplyAlpha = opts.premultiplyAlpha ?? false;
const format = srgb ? "rgba8unorm-srgb" : "rgba8unorm";
const response = await fetch(url);
const blob = await response.blob();
const imageBitmap = await createImageBitmap(blob, {
premultiplyAlpha: premultiplyAlpha ? "premultiply" : "none",
colorSpaceConversion: "none"
});
const width = imageBitmap.width;
const height = imageBitmap.height;
const mipLevelCount = mipMaps ? Math.floor(Math.log2(Math.max(width, height))) + 1 : 1;
const texture = engine._device.createTexture({
size: { width, height },
format,
mipLevelCount,
usage: TU.TEXTURE_BINDING | TU.COPY_DST | TU.RENDER_ATTACHMENT
});
engine._device.queue.copyExternalImageToTexture({ source: imageBitmap, flipY: invertY }, { texture, premultipliedAlpha: premultiplyAlpha }, { width, height });
imageBitmap.close();
if (mipMaps && mipLevelCount > 1) {
const { generateMipmaps } = await import('./generate-mipmaps-BpLaf4fV.esm.js');
generateMipmaps(engine, texture);
}
const minF = opts.minFilter ?? "linear";
const magF = opts.magFilter ?? "linear";
const mipF = mipMaps ? "linear" : "nearest";
const allLinear = minF === "linear" && magF === "linear" && mipF === "linear";
const sampler = getOrCreateSampler(engine, {
addressModeU,
addressModeV,
minFilter: minF,
magFilter: magF,
mipmapFilter: mipF,
maxAnisotropy: allLinear ? 4 : 1
});
return { texture, view: texture.createView(), sampler, width, height };
}
export { rebuildTexture2D };
//# sourceMappingURL=texture-recovery-DOZbz_TC.esm.js.map