vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
458 stars 72 forks source link

How to build PBR material #132

Closed Misterblue closed 1 year ago

Misterblue commented 2 years ago

I am using SharpGLTF for output of meshes that have been built programmatically. I am trying to build PBR materials for the meshes but I cannot see how to set baseColorTexture and some other properties.

For instance, the class MaterialPBRMetallicRoughness has the property _baseColorTexture but it is private and it looks like the only way to set it is via reading in a GLTF/GLB file. So I end up not sure how to create a Material with the correct PBR parameters.

I would like to do something like:

mat = modelRoot.CreateMaterial(matName)
.WithPBMMetallicRoughness()
.WithMetallicFactor(0.8)
.WithRoughnessFactor(0.2)
.WithBaseColorFactor(diffuseColor)
.WithBaseColorTexture(modelRoot.CreateTextureInfo(...))
.WithMetallicRoughnessTexture(modelRoot.CreateTextureInfo(...))
.WithDoubleSided(false)

So, the basic question is, is there an example to show how to create a complete PBR based Material without using MaterialBuilder? Or, missing an example, how should it be done?

vpenades commented 2 years ago

PBR materials are complex.

MaterialBuilder might seem complex to you, but trying to create a material directly with the low level API is even more complex due to the awkward architecture of glTF regarding materials. Also the low level API is more prone to change over time as the glTF standard keeps evolving and forces me to do API changes to keep with it.

In the low level API, everything is exposed via Channels.

There's plenty of examples in the unit tests for both the low level API and the MaterialBuilder API