Customize Non-PBR to PBR Materials Conversion before Saving 3D Scenes to GLTF 2.0 Format in C#
Contents
[
Hide
]
The
Scene
class of the Aspose.3D API represents a 3D scene. Developers can already build a 3D scene by adding various entities. GLTF 2.0 only supports PBR (Physically Based Rendering) materials, Aspose.3D API internally converts non-PBR materials into PBR materials before exporting into GLTF 2.0 (the materials in the scene will remain unchanged during the export), and the developers can provide custom convert function to override the default behavior.
Non-PBR to PBR Material Conversion
This C# code example demonstrates how to convert material to PBR material, and then saves 3D scene in the GLTF format with C# 3D file manipulation and conversion API:
C#
// initialize a new 3D scene
var s = new Scene();
var box = new Box();
s.RootNode.CreateChildNode("box1", box).Material = new PhongMaterial() {DiffuseColor = new Vector3(1, 0, 1)};
GLTFSaveOptions opt = new GLTFSaveOptions(FileFormat.GLTF2);
//Custom material converter to convert PhongMaterial to PbrMaterial
opt.MaterialConverter = (Material material) => {
var pbr = PbrMaterial.FromMaterial(material);
//customize your own PBR material here, you can get the original OBJ's material from the parameter mat.
//to create a compatible material with obj2gltf, use following definition:
pbr.MetallicFactor = 0;
pbr.RoughnessFactor = 0.98;
return pbr;
};
// save in GLTF 2.0 format
s.Save("test.gltf", opt);