public Dictionary <string, Material> LoadFromFile(string path) { Dictionary <string, Material> mats = new Dictionary <string, Material>(); string relativeLocation = Directory.GetParent(path).FullName; Gltf deserializedFile = Interface.LoadModel(path); foreach (glTFLoader.Schema.Material material in deserializedFile.Materials) { Material mat = new Material(); mat.MaterialName = material.Name ?? "NewMaterial"; int diffuseMap = material.PbrMetallicRoughness != null && material.PbrMetallicRoughness.BaseColorTexture != null ? material.PbrMetallicRoughness.BaseColorTexture.Index : -1; int normalMap = material.NormalTexture != null ? material.NormalTexture.Index : -1; int metallicRoughness = material.PbrMetallicRoughness != null && material.PbrMetallicRoughness.MetallicRoughnessTexture != null ? material.PbrMetallicRoughness.MetallicRoughnessTexture.Index : -1; int ambientMap = material.OcclusionTexture != null ? material.OcclusionTexture.Index : -1; mat.DiffuseMap = GetTexturePathOrDefault(deserializedFile, relativeLocation, diffuseMap); mat.NormalMap = GetTexturePathOrDefault(deserializedFile, relativeLocation, normalMap); mat.MetallicRoughness = GetTexturePathOrDefault(deserializedFile, relativeLocation, metallicRoughness); mat.AmbientMap = GetTexturePathOrDefault(deserializedFile, relativeLocation, ambientMap); mats.Add(mat.MaterialName, mat); } return(mats); }
public Dictionary <string, EngineMaterial> LoadFromFile(string path) { string relativeLocation = Directory.GetParent(path).FullName; Dictionary <string, EngineMaterial> mats = new Dictionary <string, EngineMaterial>(); List <Mesh> meshes = new List <Mesh>(); AssimpContext context = new AssimpContext(); Scene loadedAssimpScene = context.ImportFile(path, PostProcessSteps.OptimizeMeshes | PostProcessSteps.Triangulate | PostProcessSteps.FlipUVs); if (loadedAssimpScene == null || loadedAssimpScene.SceneFlags == SceneFlags.Incomplete) { Console.WriteLine("Scene error"); } List <AssimpMat> materials = loadedAssimpScene.Materials; foreach (var material in materials) { EngineMaterial _material = new EngineMaterial(); _material.MaterialName = material.Name; TextureSlot[] textureSlots = material.GetAllMaterialTextures(); if (material.HasTextureDiffuse) { _material.DiffuseMap = "C:/Users/kpbil/source/models/mira/textures/Mira_bc2.jpg"; //Path.Combine(relativeLocation, material.TextureDiffuse.FilePath); } if (material.HasTextureNormal) { _material.NormalMap = "C:/Users/kpbil/source/models/mira/textures/Mira_nm.jpg"; //Path.Combine(relativeLocation, material.TextureNormal.FilePath); } if (material.HasTextureAmbient) { _material.AmbientMap = "C:/Users/kpbil/source/models/mira/textures/Mira_ao.jpg";//Path.Combine(relativeLocation, material.TextureAmbient.FilePath); } try { _material.Metallic = "C:/Users/kpbil/source/models/mira/textures/Mira_metal.jpg";//Path.Combine(relativeLocation, textureSlots.Where(t => t.TextureType == TextureType.Unknown).FirstOrDefault().FilePath); } catch { //TODO: add handling } try { _material.Roughness = "C:/Users/kpbil/source/models/mira/textures/Mira_rou.jpg";//Path.Combine(relativeLocation, textureSlots.Where(t => t.TextureType == TextureType.Unknown).FirstOrDefault().FilePath); } catch { //TODO: add handling } mats.Add(_material.MaterialName, _material); } return(mats); }