Material variants
Material variants switch authored surface appearances while preserving the scene hierarchy and model identities.
API​
MaterialVariants from @luma.gl/engine/scenegraph accepts named variants and explicit per-model material/parameter mappings. See scene controllers for the source-independent construction API.
For imported assets, createScenegraphsFromGLTF() exposes a variants controller. Read names to discover choices, call selectVariant(name) to apply one, and resetVariant() to restore authored defaults. The controller updates materials and pipeline state on existing models.
Configuration​
import {MaterialVariants} from '@luma.gl/engine/scenegraph';
// modelNode is an existing ModelNode; paintedMaterial is a compatible Material.
const variants = new MaterialVariants({
variants: [{name: 'paint', index: 0}],
bindings: [{
modelNode,
defaultMaterial: modelNode.model.material,
defaultParameters: {...modelNode.model.parameters},
mappings: new Map([[0, {
material: paintedMaterial,
parameters: {...modelNode.model.parameters}
}]])
}]
});
variants.selectVariant('paint');
variants.resetVariant();
Alternate materials must remain compatible with the original shader feature layout. Selecting a variant does not rebuild the model shader to add previously absent texture or alpha-cutoff defines.
Imported variants​
The importer supplies the same engine controller with source variant names and resolved primitive mappings:
import {createScenegraphsFromGLTF} from '@luma.gl/engine/gltf';
const scenegraphs = createScenegraphsFromGLTF(device, gltf);
const variantName = scenegraphs.variants.names[0];
if (variantName) {
scenegraphs.variants.selectVariant(variantName);
}
// Restore the original appearance.
scenegraphs.variants.resetVariant();
Remarks​
KHR_materials_variantsmaps primitives to named materials. Unmapped primitives return to their default material when switching variants.