@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.
61 lines (58 loc) • 3.28 kB
JavaScript
import { k as getOrCreateSampler, an as uploadBaseColorFactorTexture, ao as uploadOrmFactorTexture } from './index-By0tcgYN.esm.js';
function gltfTexSamplerDesc(json, texInfo) {
const s = json.textures?.[texInfo.index]?.sampler != null ? json.samplers?.[json.textures[texInfo.index].sampler] : void 0;
const wrap = (m) => m === 33071 ? "clamp-to-edge" : m === 33648 ? "mirror-repeat" : "repeat";
const minF = s?.minFilter;
const minNearest = !!minF && minF % 2 === 0;
const mipNearest = minF === 9984 || minF === 9985;
const noMip = minF === 9728 || minF === 9729;
const magLinear = s?.magFilter !== 9728;
return {
magFilter: magLinear ? "linear" : "nearest",
minFilter: minNearest ? "nearest" : "linear",
mipmapFilter: mipNearest ? "nearest" : "linear",
addressModeU: wrap(s?.wrapS),
addressModeV: wrap(s?.wrapT),
...noMip ? { lodMaxClamp: 0 } : void 0,
// WebGPU forbids anisotropy unless mag/min/mip filters are ALL linear; gate on
// every filter (incl. mipNearest, e.g. glTF LINEAR_MIPMAP_NEAREST) or createSampler throws.
// Also disable it for the clamped-mip path (single LOD → anisotropy is meaningless).
maxAnisotropy: magLinear && !minNearest && !mipNearest && !noMip ? 4 : 1
};
}
function makeSamplerFor(engine, json, defaultSampler) {
return (texInfo) => {
if (texInfo == null) {
return defaultSampler;
}
const desc = gltfTexSamplerDesc(json, texInfo);
const sampler = desc.lodMaxClamp === 0 ? engine._device.createSampler(desc) : getOrCreateSampler(engine, desc);
engine._deviceLostRecovery?._samplerDescriptors.set(sampler, desc);
return sampler;
};
}
function buildSampledPbrTextures(engine, mat, defaultSampler, generateMipmaps, samplerFor, getCachedTex) {
const def = mat._rawMatDef ?? {};
const pbr = def.pbrMetallicRoughness ?? {};
const cached = (bitmap, srgb, texInfo) => {
const s = samplerFor(texInfo);
const tex = getCachedTex(bitmap, srgb);
return s === defaultSampler ? tex : { ...tex, sampler: s };
};
const baseColorTexture = mat._baseColorImage ? cached(mat._baseColorImage, true, pbr.baseColorTexture) : uploadBaseColorFactorTexture(engine, mat._baseColorFactor, defaultSampler, generateMipmaps);
const normalTexture = mat._normalImage ? cached(mat._normalImage, false, def.normalTexture) : void 0;
const emissiveTexture = mat._emissiveImage ? cached(mat._emissiveImage, true, def.emissiveTexture) : void 0;
const single = mat._metallicRoughnessImage ?? mat._occlusionImage;
const ormTexInfo = mat._metallicRoughnessImage ? pbr.metallicRoughnessTexture : def.occlusionTexture;
let ormTexture;
if (single && (!mat._metallicRoughnessImage || !mat._occlusionImage || mat._metallicRoughnessImage === mat._occlusionImage)) {
ormTexture = cached(single, false, ormTexInfo);
} else if (!single) {
ormTexture = uploadOrmFactorTexture(engine, mat._roughnessFactor, mat._metallicFactor, defaultSampler, generateMipmaps);
} else {
ormTexture = cached(mat._metallicRoughnessImage, false, pbr.metallicRoughnessTexture);
}
return { baseColorTexture, ormTexture, normalTexture, emissiveTexture };
}
export { buildSampledPbrTextures, makeSamplerFor };
//# sourceMappingURL=gltf-sampler-desc-D6urE3ea.esm.js.map