UNPKG

nanogl-gltf

Version:
191 lines (190 loc) 7.65 kB
import Gltf from "../Gltf"; import IOInterface from "./IOInterface"; import { ExtensionList } from "../extensions/Registry"; import Gltf2 from "../types/Gltf2"; import { ElementOfType, PropertyType, AnyElement, PromiseElementForProperty } from "../types/Elements"; import GltfTypes from "../types/GltfTypes"; import "../extensions/DefaultExtension"; import { GltfLoaderOptions } from "./GltfLoaderOptions"; import { IMaterial } from "../elements/Material"; import { AbortSignal } from "@azure/abort-controller"; export declare class PendingElement { readonly data: Gltf2.IProperty; readonly promise: Promise<AnyElement>; constructor(data: Gltf2.IProperty, element: Promise<AnyElement>); } /** * This class is used to load a Gltf file and its resources, including extensions processing. */ export default class GltfLoader { /** * Gltf object which will be filled with the data from the Gltf file */ gltf: Gltf; /** * Gltf file path */ _url: string; /** * Resolved base directory of the Gltf file */ _baseUrl: string; /** * Implementation of IOInterface made for GLTFs */ gltfIO: IOInterface; /** * Gltf data file content, parsed as JSON from the GLTF/GLB file */ _data: Gltf2.IGLTF; /** * If the loaded file is a GLB file (binary GLTF), data such as images or shaders can be stored as base64-encoded binary data directly in the GLB file. * This buffer contains the decoded data. */ _glbData?: ArrayBuffer; /** * List of extensions activated on this loader */ _extensions: ExtensionList; /** * Map of all created elements from the Gltf data file, by uuid */ _elements: Map<string, Promise<AnyElement>>; /** * List of elements that are being created, waiting to be resolved and added to the Gltf object */ _pendingElements: PendingElement[]; /** * Map of all elements of the Gltf data file, ordered by type */ _byType: Map<GltfTypes, Promise<AnyElement>[]>; /** * Map of all properties of the Gltf data file, ordered by type */ _propertyMaps: Map<GltfTypes, Gltf2.Property[]>; /** * Abort signal if you want to be able to cancel the request at any time */ readonly abortSignal: AbortSignal; /** * Texture's minFilter to use when there is no specified one in this loader's options or in textures' samplers * @default GL_LINEAR */ readonly defaultTextureFilter: GLenum; /** * @param gltfIO Implementation of IOInterface made for GLTFs * @param url Gltf file path * @param options Options for Gltf loader */ constructor(gltfIO: IOInterface, url: string, options?: GltfLoaderOptions); /** * Load and return the gltf file */ load(): Promise<Gltf>; /** * Parse the buffer then prepare it for loading by adding uuids, types and indexes * If the buffer represents a GLTF file (plain JSON), il will be parsed directly. * If the buffer represents a GLB file (binary), it will be first decoded, then parsed. * @param buffer Buffer to unpack */ unpack(buffer: ArrayBuffer): void; /** * Decode a buffer representing a binary GLB file, then parse it and prepare it for loading * @param buffer Buffer to unpack */ unpackGlb(buffer: ArrayBuffer): void; /** * Resolve an absolute file path relative to this loader base directory * @param uri URI to resolve */ resolveUri(uri: string): string; /** * Load a buffer from an URI, if no URI is provided the _glbData buffer will be returned * @param uri URI to load */ loadBufferUri: (uri?: string) => Promise<ArrayBuffer>; /** * If element has no name or extras, give it the ones from the data * @param data Data to add * @param element Element to add data on */ parseCommonGltfProperty<P extends Gltf2.Property>(data: P, element: ElementOfType<PropertyType<P>>): void; /** * Create a Gltf element from its raw data, with extension handling if needed. * @param data Element data coming from the .gltf file */ _createElement<P extends Gltf2.Property>(data: P): PromiseElementForProperty<P>; /** * Create a Gltf element from its raw data, passing it through all the loader's extensions. * If no extension handles this specific element, a basic corresponding element will be created. * @param data Element data coming from the .gltf file */ _createElementInstance<P extends Gltf2.Property>(data: P): PromiseElementForProperty<P>; /** * Some extensions may want to modify the element after it has been created, this method will call all the extensions that want to do so. * @param data Element data coming from the .gltf file * @param element Corresponding element already created */ _extensionsAccept<P extends Gltf2.Property>(data: P, element: ElementOfType<PropertyType<P>>): PromiseElementForProperty<P>; /** * Load an element from its data, if the element is already loaded it will be returned directly. * @param data Data to load */ _loadElement<P extends Gltf2.Property>(data: P): PromiseElementForProperty<P>; /** * Provide a default material if needed. Used for Primitives that don't have a material. */ loadDefaultMaterial(): Promise<IMaterial>; /** * Get the array of elements of a given type. If the array doesn't exist, it will be created. * @param type Type of elements to get */ private _getElementHolder; /** * Get an element of a given type and index. If the element doesn't exist, it will be created by retrieving its data from the Gltf data file. * @param type Element's type * @param index Element's index */ getElement<T extends GltfTypes>(type: T, index: number): Promise<ElementOfType<T>>; /** * Parse all prepared elements of the Gltf data file, create them and add them to the Gltf object */ parseAll(): Promise<void>; /** * Load multiple elements from their data * @param dataList Array of elements to load */ private _loadElements; /** * Wait for all pending elements creation to complete and register them in Gltf object */ resolveElements(): Promise<void>; /** * Parse the Gltf data and prepare it for loading * @param gltfData Gltf data to parse */ prepareGltfDatas(gltfData: Gltf2.IGLTF): void; /** * Prepare multiple Gltf properties for loading, having the same type and parent * @param elementsData Elements to prepare * @param type Elements' type * @param parent Elements' parent */ prepareGltfProperties(elementsData: Gltf2.Property[], type: GltfTypes, parent: Gltf2.Property): void; /** * Prepare multiple Gltf root properties for loading, having the same type and parent. * A root property is a property that doesn't have a parent in the Gltf data. * @param elementsData Elements to prepare * @param type Elements' type * @param parent Elements' parent */ prepareGltfRootProperties(elementsData: Gltf2.Property[], type: GltfTypes, parent: Gltf2.Property): void; /** * Prepare Gltf property for loading by adding him a uuid, a type, an index and a parent * @param element Element to prepare * @param type Element's type * @param index Element's index * @param parent Element's parent */ prepareGltfProperty(element: Gltf2.Property, type: GltfTypes, index: number, parent: Gltf2.Property): void; }