UNPKG

nanogl-gltf

Version:
73 lines (72 loc) 2.92 kB
import { IExtensionFactory, IExtensionInstance } from "./IExtension"; import GltfLoader from "../io/GltfLoader"; import Gltf2 from "../types/Gltf2"; import Material from "nanogl-pbr/Material"; import Primitive from "../elements/Primitive"; import Node from "../elements/Node"; import Gltf from "../Gltf"; import MaterialPass from "nanogl-pbr/MaterialPass"; /** * Context object passed to material and pass override factories, if needed when overriding a material or pass */ export declare type MaterialOverrideFactoryContext = { /** * The gltf material data to override */ data: Gltf2.IMaterial; /** * The gltf object containing the material */ gltf: Gltf; /** * The node containing the primitive */ node: Node; /** * The primitive which will use the material */ primitive: Primitive; }; /** * Simple material or material override factory function returning a Material */ export declare type MaterialOverrideFactory = Material | ((ctx: MaterialOverrideFactoryContext) => Material); /** * Simple pass or pass override factory function returning a MaterialPass */ export declare type PassOverrideFactory = MaterialPass | ((ctx: MaterialOverrideFactoryContext, material: Material) => MaterialPass | null); /** * This extension allows all materials and their passes to be overriden by custom code at load time. * Useful to add custom shading, completely replace a material, add Chunk effect to every materials of a GLTF, ... */ export default class MaterialOverrideExtension implements IExtensionFactory { readonly name: string; /** * Map of material override factories */ readonly materials: Map<string, MaterialOverrideFactory>; /** * Map of pass override factories */ readonly passes: Map<string, PassOverrideFactory>; /** * Add Material Override. * Gltf material with the given name will be replaced by the one created by the given material factory * @param name The name of the gltf material to override, if empty string the given material will be used as default override * @param m Material factory to use to create the override material */ overrideMaterial(name: string | "", m: MaterialOverrideFactory): void; /** * Add Pass Override. * The Color pass of the overriden material will be replaced by the one created by the given pass factory * @param name The name of the material to override, if empty string is given, the pass will be applied to all materials * @param p Pass factory to use to create the override pass */ overridePass(name: string | "", p: PassOverrideFactory): void; /** * Check that the given name is not already used by a material or pass override * @param name Material or pass name to check */ private assertNotExist; createInstance(gltfLoader: GltfLoader): IExtensionInstance; }