UNPKG

nanogl-gltf

Version:
51 lines (50 loc) 1.98 kB
import GltfTypes from '../types/GltfTypes'; import Gltf from '../Gltf'; import { Sampler } from 'nanogl-pbr/Input'; import TexCoord from 'nanogl-pbr/TexCoord'; /** * The BaseTextureInfo element contains the base properties and methods for TextureInfo, NormalTextureInfo and OcclusionTextureInfo. * It contains the texture and the texCoord to use to sample the texture, and a createSampler() method to create a nanogl-pbr Sampler object. */ export class BaseTextureInfo { constructor() { /** * nanogl-pbr Sampler object, used to sample the texture. */ this._sampler = null; } /** * Create a nanogl-pbr Sampler object, used to sample the texture. * @param id ID or name to use for the sampler, to differentiate multiple samplers on the same Program */ createSampler(id) { var _a; if (this._sampler === null) { const attrib = Gltf.getSemantics().getAttributeName(`TEXCOORD_${this.texCoord}`); this._sampler = new Sampler(`tex_${(_a = this.name) !== null && _a !== void 0 ? _a : ''}${id}`, TexCoord.create(attrib)); this.texture.glTexturePromise.then((t) => this._sampler.set(t)); } return this._sampler; } /** * Parse the BaseTextureInfo data, fill the texture and texCoord properties. * * Is async as it needs to wait for the texture to be created. * @param gltfLoader GLTFLoader to use * @param data Data to parse */ async parse(gltfLoader, data) { var _a; this.texture = await gltfLoader.getElement(GltfTypes.TEXTURE, data.index); this.texCoord = (_a = data.texCoord) !== null && _a !== void 0 ? _a : 0; } } /** * The TextureInfo element contains the texture and the texCoord to use to sample the texture. */ export default class TextureInfo extends BaseTextureInfo { constructor() { super(...arguments); this.gltftype = GltfTypes.TEXTURE_INFO; } }