Toolkit material API

May 25, 2020 ยท View on GitHub

Overview

glTF materials specification has a rather complex architecture, with a number of specialized objects, plus a good number of extensions.

In order to streamline and simplify access to materials, a Material Build API is provided.

By default, SharpGLTF supports the default PBRMetallicRoughness shader style, plus Unlit and PBRSpecularGlossiness extensions. Each shader style has its own unique list of channels.

Each channel has a Texture, and a Vector4<X,Y,Z,W> parameter, where every element has a different meaning, depending on the kind of channel:

ChannelShader StyleXYZW
NormalAllScale
OcclussionAllStrength
EmissiveAllRedGreenBlue
BaseColorMetallic Roughness & UnlitRedGreenBlueAlpha
MetallicRoughnessMetallic RoughnessMetallic FactorRoughness Factor
DiffuseSpecular GlossinessDiffuse RedDiffuse GreenDiffuse BlueAlpha
SpecularGlossinessSpecular GlossinessSpecular RedSpecular GreenSpecular BlueGlossiness

Implementation

To create new materials, there's two ways of doing it; accessing directly to the glTF materials namespace, or using the MaterialBuilder class.

The advantage of MaterialBuilder is that it allows to create stand alone materials that can be easily edited and used to create glTF materials at any time.

Creating a standard material can be done like this:

var material = new Materials.MaterialBuilder("material1")
                .WithDoubleSide(true)
                .WithMetallicRoughnessShader()
                .WithChannelImage("Normal", "WaterBottle_normal.png")
                .WithChannelImage("Emissive", "WaterBottle_emissive.png")
                .WithChannelImage("Occlusion", "WaterBottle_occlusion.png")
                .WithChannelImage("BaseColor", "WaterBottle_baseColor.png")
                .WithChannelImage("MetallicRoughness", "WaterBottle_roughnessMetallic.png");

MaterialBuilder also supports a fallback material that will be used in case the main material is not supported by the rendering engine. But due to glTF limitations, this feature is restricted only to a main material using SpecularGlossiness shader, and the fallback material using MetallicRoughness shader.