@deck.gl-community/layers
Version:
Add-on layers for deck.gl
64 lines • 2.49 kB
JavaScript
// deck.gl-community
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
const CUBE_FACES = ['+X', '-X', '+Y', '-Y', '+Z', '-Z'];
/**
* Normalizes loaders.gl cubemap load options so in-memory manifests can still
* resolve relative face URLs through `core.baseUrl`.
*/
export function createCubemapLoadOptions(cubemap, loadOptions) {
if (!loadOptions) {
return undefined;
}
if (typeof cubemap === 'string' || loadOptions.core?.baseUrl || !loadOptions.baseUrl) {
return loadOptions;
}
return {
...loadOptions,
core: {
...loadOptions.core,
baseUrl: loadOptions.baseUrl
}
};
}
/** Converts a loaders.gl cubemap result into luma.gl `TextureCubeData`. */
export function convertLoadedCubemapToTextureData(texture) {
if (texture.type !== 'cube' || !Array.isArray(texture.data) || texture.data.length !== 6) {
throw new Error('SkyboxLayer expected a cubemap texture with six faces.');
}
return Object.fromEntries(CUBE_FACES.map((face, index) => [face, normalizeTextureSlice(texture.data[index], face)]));
}
/** Normalizes a cubemap face that may contain one or more mip levels. */
function normalizeTextureSlice(faceData, face) {
if (Array.isArray(faceData)) {
if (faceData.length === 0) {
throw new Error(`SkyboxLayer received an empty mip chain for face ${face}.`);
}
return faceData.map((level, mipLevel) => normalizeTextureLevel(level, face, mipLevel));
}
return normalizeTextureLevel(faceData, face, 0);
}
/** Normalizes a single cubemap face mip level into luma.gl upload data. */
function normalizeTextureLevel(faceData, face, mipLevel) {
if (typeof ImageBitmap !== 'undefined' && faceData instanceof ImageBitmap) {
return faceData;
}
const level = faceData;
if (level?.imageBitmap) {
return level.imageBitmap;
}
if (level && ArrayBuffer.isView(level.data) && level.width && level.height) {
return {
data: level.data,
width: level.width,
height: level.height,
format: typeof level.textureFormat === 'string'
? level.textureFormat
: typeof level.format === 'string'
? level.format
: 'rgba8unorm'
};
}
throw new Error(`SkyboxLayer could not normalize cubemap face ${face} mip ${mipLevel}.`);
}
//# sourceMappingURL=cubemap-utils.js.map