Customize Non-PBR to PBR Materials Conversion before Saving 3D Scenes to GLTF 2.0 Format in C#

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:


 // 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 = delegate(Material material)


    PhongMaterial m = (PhongMaterial) material;

    return new PbrMaterial() {Albedo = new Vector3(m.DiffuseColor.x, m.DiffuseColor.y, m.DiffuseColor.z)};


// save in GLTF 2.0 format

s.Save("test.gltf", opt);