UNPKG

nanogl-gltf

Version:
204 lines (203 loc) 7.11 kB
import Accessor from './Accessor'; import { IMaterial } from './Material'; import { GLContext } from 'nanogl/types'; import GLArrayBuffer from 'nanogl/arraybuffer'; import Program from 'nanogl/program'; import Vao from 'nanogl-vao'; import GLIndexBuffer from 'nanogl/indexbuffer'; import Gltf2 from '../types/Gltf2'; import GltfLoader from '../io/GltfLoader'; import GltfTypes from '../types/GltfTypes'; import { IElement } from '../types/Elements'; import Bounds from 'nanogl-pbr/Bounds'; /** * A helper class to manage a Primitive's attribute (linking an attribute name to its Accessor) */ export declare class Attribute { /** * The standard Semantic for this attribute (POSITION, NORMAL, TEXCOORDS_0, ...) */ semantic: string; /** * The accessor providing data for this attribute */ accessor: Accessor; /** * The glsl attribute name */ glslname: string; /** * @param semantic Attribute's semantic * @param accessor Attribute's Accessor */ constructor(semantic: string, accessor: Accessor); } /** * A helper class to manage a set of Attributes sharing the same BufferView */ export declare class BufferInfos { /** * The Attributes sharing the same BufferView */ attributes: Attribute[]; /** * The BufferView */ accessor: Accessor; constructor(accessor: Accessor); addAttribute(attribute: Attribute): void; } /** * A helper class to manage an array of Attributes */ export declare class AttributesSet { /** * The list of attributes */ _attributes: Attribute[]; constructor(); get length(): number; get attributes(): Attribute[]; /** * Add an attribute to the set * @param attribute Attribute to add */ add(attribute: Attribute): void; /** * Get an Attribute by its semantic (POSITION, NORMAL, TEXCOORD_0, ...) * @param semantic Semantic of the attribute to get */ getSemantic(semantic: string): Attribute; getBuffersViewSets(): BufferInfos[]; } /** * The Primitive element is a set of attributes defining a geometry to render with a given material and in a given mode. * It may be indexed, and may have morph targets. * * It's the most basic renderable element, as a Scene is made of Meshes, and a Mesh is made of Primitives. */ export default class Primitive implements IElement { readonly gltftype: GltfTypes.PRIMITIVE; name: undefined | string; extras: any; /** * The set of attributes for this Primitive (POSITION, NORMAL, TEXCOORD_0, ...) */ attributes: AttributesSet; /** * The rendering mode to use for this Primitive (TRIANGLES, LINES, POINTS, ...). * If not defined, the default mode is TRIANGLES. */ mode: Gltf2.MeshPrimitiveMode; /** * The Material to use for this Primitive. * If not defined, a default material with no effect will be used. */ material: IMaterial; /** * The indices to use to render this Primitive, if it is indexed. * If not indexed, the Primitive is rendered in vertices' order. */ indices: Accessor; /** * The Morph Targets to use to render this Primitive, if it is morphed. */ targets: AttributesSet[]; /** * The VAOs for this Primitive, one per Program used. * A VAO is a WebGLVertexArrayObject, containing the Primitive's attributes and indices, useful to bind/unbind them all at once. */ _vaoMap: Map<string, Vao>; /** * The current VAOs for this Primitive, used for rendering. */ _currentVao: Vao; /** * The GLArrayBuffers for this Primitive's attributes (basic & morph target) */ buffers: GLArrayBuffer[]; /** * The GLIndexBuffer for this Primitive's indices, if it is indexed. */ indexBuffer: GLIndexBuffer; /** * The bounding box of this Primitive */ readonly bounds: Bounds; /** * Calculate the bounding box of this Primitive, based on its POSITION attribute. */ _calculaterBounds(): void; /** * Parse the Primitive data, load the attributes, indices, material and morph targets, and calculate the bounding box. * * Is async as it needs to wait for the Accessors, and possible Material, to be created. * @param gltfLoader GLTFLoader to use * @param data Data to parse */ parse(gltfLoader: GltfLoader, data: Gltf2.IMeshPrimitive): Promise<any>; /** * Parse the attributes of a Primitive and store it in an AttributesSet. * * Is async as it needs to wait for the Attribute's Accessors to be loaded, or created if not yet loaded. * @param gltfLoader GLTFLoader to use to load the Accessors * @param aset AttributesSet to fill * @param data Data to parse */ parseAttributeSet(gltfLoader: GltfLoader, aset: AttributesSet, data: any): Promise<void>; /** * Parse the attributes of a Primitive's Morph Target and store it in an AttributesSet. * * Is async as it needs to wait for the Attribute's Accessors to be loaded, or created if not yet loaded. * @param gltfLoader GLTFLoader to use to load the Accessors * @param aset AttributesSet to fill * @param data Data to parse * @param morphIndex Index of the Morph Target */ parseMorphAttributeSet(gltfLoader: GltfLoader, aset: AttributesSet, data: any, morphIndex: number): Promise<void>; /** * Create the GLArrayBuffers for Primitive's attributes (basic & morph target) and GLIndexBuffer for Primitive's indices. * After that, the Primitive is ready to be rendered. * @param gl GL context to use */ allocateGl(gl: GLContext): void; /** * Create a GLArrayBuffer from an attribute (or a set of attributes sharing the same BufferView) * @param gl GL context to use * @param set Set of attributes to create the array buffer from */ createArrayBuffer(gl: GLContext, set: BufferInfos): GLArrayBuffer; /** * Create an attribute definition from an Attribute, to be used in a GLArrayBuffer * @param attribute Attribute to create the definition from */ createAttributeDefinition(attribute: Attribute): { name: string; type: Gltf2.AccessorComponentType; size: number; normalize: boolean; offset: number; stride: number; }; /** * Get the Primitive's VAO linked to a specific Program. If it doesn't exist yet, create it. * @param prg */ getVao(prg: Program): Vao; /** * Bind the VAO containing the Primitive's attributes and indices, ready to be drawn. * Called by the MeshRenderer just before rendering the Primitive. * @param prg */ bindVao(prg: Program): void; /** * Draw the Primitive, using its indices if it is indexed, or its vertices' order if it is not. * Called by the MeshRenderer. */ render(): void; /** * Unbind the Primitive's VAO. * Called by the MeshRenderer just after rendering the Primitive. */ unbindVao(): void; }