UNPKG

nanogl-gltf

Version:
178 lines (177 loc) 6.84 kB
import BufferView from './BufferView'; import Gltf2 from '../types/Gltf2'; import GltfLoader from '../io/GltfLoader'; import GltfTypes from '../types/GltfTypes'; import AccessorSparse from './AccessorSparse'; import { IElement } from '../types/Elements'; import { TypedArrayConstructor, TypedArray } from '../types/TypedArray'; export declare type normalizeFunc = (n: number) => number; /** * GLSL name for Accessor types */ export declare enum AccessorGlslType { float = "float", vec2 = "vec2", vec3 = "vec3", vec4 = "vec4", mat2 = "mat2", mat3 = "mat3", mat4 = "mat4" } /** * Get the TypedArray constructor for the given component type * @param t Component type */ export declare function getArrayForDataType(t: Gltf2.AccessorComponentType): TypedArrayConstructor; /** * Base type for Accessor, AccessorSparseIndices and AccessorSparseValues data */ export declare type BaseAccessorData = Gltf2.IAccessor | Gltf2.IAccessorSparseIndices | Gltf2.IAccessorSparseValues; /** * Base class for Accessor, AccessorSparseIndices and AccessorSparseValues, as they share a lot of attributes and methods and only differ in the way they are parsed. */ export declare class BaseAccessor { /** * Whether the Element to hold is normalized or not */ normalized: boolean; /** * Bytes offset in the BufferView */ byteOffset: number; /** * Number of elements in the BufferView */ count: number; /** * Stride in bytes between values, if different values are interleaved in the BufferView */ _stride: number; /** * Stride in elements count between values, if different values are interleaved in the BufferView */ _strideElem: number; /** * Type of each value in this Accessor's BufferView (UNSIGNED_BYTE, FLOAT, ...) */ componentType: Gltf2.AccessorComponentType; /** * Type of each element in this Accessor (SCALAR, VEC3, ...) */ type: Gltf2.AccessorType; /** * Maximum value of each element in this Accessor */ max?: number[]; /** * Minimum value of each element in this Accessor */ min?: number[]; /** * BufferView element containing the data */ bufferView: BufferView; /** * Empty TypedArray used to store the value of a normalized element when calling getValue() */ _valueHolder: TypedArray; /** * TypedArray containing the data, the slice of BufferView that is needed */ _array: TypedArray; /** * Function to normalize the data, based on the componentType */ _normalizeFunc: normalizeFunc; /** * Sparse element containing the sparse data, if any */ sparse: AccessorSparse | null; /** * Number of components for this accessor type (1 for SCALAR, 3 for VEC3, 16 for MAT4, ...). */ get numComps(): 1 | 2 | 3 | 4 | 9 | 16; /** * Number of bytes for each element in this accessor, based on the componentType. */ get bytesPerElem(): number; /** * GLSL type for this accessor, based on the accessor type. */ get glslType(): AccessorGlslType; [Symbol.iterator](): Generator<TypedArray, void, unknown>; /** * Create an empty TypedArray for this accessor, to hold 1 element. Will be a Float32Array if normalized. * Useful when we need to create an array but we don't have the data yet. * @param normalized Whether the Element to hold is normalized or not. Default to this.normalized. */ createElementHolder(normalized?: boolean): TypedArray; /** * Create an empty TypedArray of a certain size for this accessor, to hold multiple elements. Will be a Float32Array if normalized. * Useful when we need to create an array but we don't have the data yet. * @param size Size of the array to create * @param normalized Whether the Element to hold is normalized or not. Default to this.normalized. */ createElementHolderArray(size: number, normalized?: boolean): TypedArray; /** * Copy accessor value at the given index to output array * @param index Index of the value to copy * @param normalized Whether the value should be normalized or not before returning it */ getScalar(index: number, normalized?: boolean): number; /** * Copy accessor value at the given index to output array. Skip sparse resolve * @param index Index of the value to copy */ getRawScalar(index: number): number; /** * Copy accessor value of a whole element at the given index to output array (for example, if it's a VEC3, copy 3 values) * @param out TypedArray to store the value in * @param index Index of the value to get * @param normalized Whether the value should be normalized or not before returning it */ getValue(out: TypedArray, index: number, normalized?: boolean): void; /** * Copy multiple elements of the accessor to the output array * @param out TypedArray to store the values in * @param index Index of the first value to get * @param size Number of values to get */ getValues(out: TypedArray, index: number, size: number): void; /** * Copy accessor value of a whole element at the given index to output array (for example, if it's a VEC3, copy 3 values). Skip sparse resolve * @param out TypedArray to store the value in * @param index Index of the value to get */ getRawValue(out: TypedArray, index: number): void; /** * Copy multiple elements of the accessor to the output array. Skip sparse resolve * @param out TypedArray to store the values in * @param index Index of the first value to get * @param size Number of values to get */ getRawValues(out: TypedArray, index: number, size: number): void; /** * Normalize the given raw value and store it in out * @param out TypedArray to store the value in * @param raw TypedArray to normalize */ _normalize(out: TypedArray, raw: TypedArray): void; } /** * The Accessor element refers to a BufferView and describe the layout of data in it (type, length, max, min, ...). */ export default class Accessor extends BaseAccessor implements IElement { readonly gltftype: GltfTypes.ACCESSOR; name: undefined | string; extras: any; /** * Parse the Accessor data, load the BufferView element and store only the part that is needed in _array attribute. * If the Accessor contains sparse data, load the Sparse element and store it in sparse attribute. * * Is async as it needs to wait for the BufferView to be created, if needed. * @param gltfLoader GLTFLoader to use * @param data Data to parse */ parse(gltfLoader: GltfLoader, data: Gltf2.IAccessor): Promise<any>; }