UNPKG

nanogl-gltf

Version:
62 lines (61 loc) 2.88 kB
import { isAllOnes } from '../lib/Utils'; import { Uniform } from 'nanogl-pbr/Input'; import { MetalnessSurface } from 'nanogl-pbr/PbrSurface'; import { ColorSpace } from 'nanogl-pbr/ColorSpace'; /** * The PbrMetallicRoughness element contains the base properties for a PBR material, using the metallic-roughness workflow. */ export default class PbrMetallicRoughness { /** * Parse the PbrMetallicRoughness data, filling the class properties. * * Is async as it may need to wait for baseColorTexture and metallicRoughnessTexture to be loaded. * @param gltfLoader GLTFLoader to use * @param data Data to parse */ async parse(gltfLoader, data) { var _a, _b; this.baseColorFactor = new Float32Array(data.baseColorFactor || [1, 1, 1, 1]); this.metallicFactor = (_a = data.metallicFactor) !== null && _a !== void 0 ? _a : 1; this.roughnessFactor = (_b = data.roughnessFactor) !== null && _b !== void 0 ? _b : 1; if (data.baseColorTexture !== undefined) { this.baseColorTexture = await gltfLoader._loadElement(data.baseColorTexture); } if (data.metallicRoughnessTexture !== undefined) { this.metallicRoughnessTexture = await gltfLoader._loadElement(data.metallicRoughnessTexture); } } /** * Setup the PbrMetallicRoughness data on a nanogl-pbr StandardPass by attaching baseColor * and metallicRoughness textures with samplers and their strength factors. It uses a nanogl-pbr MetalnessSurface. * @param pass Pass to setup */ setupPass(pass) { const surface = new MetalnessSurface(); pass.setSurface(surface); if (this.baseColorTexture) { const baseColorSampler = this.baseColorTexture.createSampler('basecolor'); baseColorSampler.colorspace = ColorSpace.SRGB; surface.baseColor.attach(baseColorSampler, 'rgb'); pass.alpha.attach(baseColorSampler, 'a'); } if (!isAllOnes(this.baseColorFactor)) { const cFactor = new Uniform('uBasecolorFactor', 4); cFactor.set(...this.baseColorFactor); surface.baseColorFactor.attach(cFactor, 'rgb'); pass.alphaFactor.attach(cFactor, 'a'); } if (this.metallicRoughnessTexture) { const mrSampler = this.metallicRoughnessTexture.createSampler('metalRough'); mrSampler.colorspace = ColorSpace.LINEAR; surface.metalness.attach(mrSampler, 'b'); surface.roughness.attach(mrSampler, 'g'); } if (this.metallicFactor !== 1) { surface.metalnessFactor.attachUniform('uMetalnessFactor').set(this.metallicFactor); } if (this.roughnessFactor !== 1) { surface.roughnessFactor.attachUniform('uRoughnessFactor').set(this.roughnessFactor); } } }