import Check from "../Core/Check.js"; import Frozen from "../Core/Frozen.js"; import defined from "../Core/defined.js"; import hasExtension from "./hasExtension.js"; import { MeshoptDecoder } from "meshoptimizer"; import ResourceLoader from "./ResourceLoader.js"; import ResourceLoaderState from "./ResourceLoaderState.js"; /** * Loads a glTF buffer view. *
* Implements the {@link ResourceLoader} interface. *
* * @private */ class GltfBufferViewLoader extends ResourceLoader { /** * @param {object} options Object with the following properties: * @param {ResourceCache} options.resourceCache The {@link ResourceCache} (to avoid circular dependencies). * @param {object} options.gltf The glTF JSON. * @param {number} options.bufferViewId The buffer view ID. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to. * @param {string} [options.cacheKey] The cache key of the resource. */ constructor(options) { super(); options = options ?? Frozen.EMPTY_OBJECT; const resourceCache = options.resourceCache; const gltf = options.gltf; const bufferViewId = options.bufferViewId; const gltfResource = options.gltfResource; const baseResource = options.baseResource; const cacheKey = options.cacheKey; //>>includeStart('debug', pragmas.debug); Check.typeOf.func("options.resourceCache", resourceCache); Check.typeOf.object("options.gltf", gltf); Check.typeOf.number("options.bufferViewId", bufferViewId); Check.typeOf.object("options.gltfResource", gltfResource); Check.typeOf.object("options.baseResource", baseResource); //>>includeEnd('debug'); const bufferView = gltf.bufferViews[bufferViewId]; let bufferId = bufferView.buffer; let byteOffset = bufferView.byteOffset; let byteLength = bufferView.byteLength; let hasMeshopt = false; let meshoptByteStride; let meshoptCount; let meshoptMode; let meshoptFilter; if (hasExtension(bufferView, "EXT_meshopt_compression")) { const meshopt = bufferView.extensions.EXT_meshopt_compression; bufferId = meshopt.buffer; byteOffset = meshopt.byteOffset ?? 0; byteLength = meshopt.byteLength; hasMeshopt = true; meshoptByteStride = meshopt.byteStride; meshoptCount = meshopt.count; meshoptMode = meshopt.mode; meshoptFilter = meshopt.filter ?? "NONE"; } const buffer = gltf.buffers[bufferId]; this._hasMeshopt = hasMeshopt; this._meshoptByteStride = meshoptByteStride; this._meshoptCount = meshoptCount; this._meshoptMode = meshoptMode; this._meshoptFilter = meshoptFilter; this._resourceCache = resourceCache; this._gltfResource = gltfResource; this._baseResource = baseResource; this._buffer = buffer; this._bufferId = bufferId; this._byteOffset = byteOffset; this._byteLength = byteLength; this._cacheKey = cacheKey; this._bufferLoader = undefined; this._typedArray = undefined; this._state = ResourceLoaderState.UNLOADED; this._promise = undefined; } /** * The cache key of the resource. * * * @type {string} * @readonly * @private */ get cacheKey() { return this._cacheKey; } /** * The typed array containing buffer view data. * * * @type {Uint8Array} * @readonly * @private */ get typedArray() { return this._typedArray; } /** * Loads the resource. * @returns {Promise