private int AddMaterial(Model gltf, string name, WaveFront.Material mtl) { var mat = new Material(); mat.Name = name; mat.PbrMetallicRoughness = new PbrMetallicRoughness(); var matIndex = gltf.Materials.Count; float metallicFactor = 0.0f; float?roughnessFactor = null; // convertTraditionalToMetallicRoughness float[] specular = null; var specularShininess = mtl.SpecularExponent; if (mtl.Specular != null) { specular = mtl.Specular.ToRGB(); metallicFactor = specular[0]; // if from blinn-phong model var specularIntensity = Luminance(specular); roughnessFactor = (mtl.SpecularExponent ?? 0.0f) / 1000; roughnessFactor = 1.0f - roughnessFactor; roughnessFactor = Clamp(roughnessFactor ?? 1.0f, 0.0f, 1.0f); metallicFactor = 0.0f; specular = new[] { metallicFactor, metallicFactor, metallicFactor, 1.0f }; specularShininess = roughnessFactor; } var emissiveTexture = mtl.EmissiveMap; var normalTexture = mtl.BumpMap; var occlusionTexture = mtl.AmbientMap; var baseColorTexture = mtl.DiffuseMap; var alphaTexture = mtl.DissolveMap; var metallicTexture = mtl.SpecularMap; var roughnessTexture = String.Empty; //var metallicRoughnessTexture = createMetallicRoughnessTexture( //var diffuseAlphaTexture var emissiveFactor = mtl.Emissive?.ToRGB(); var baseColorFactor = mtl.Diffuse?.ToRGB()?.ToList() ?? new List <float> { 0.315f, 0.315f, 0.315f }; if (IsValidTexture(emissiveTexture)) { var tIndex = ResolveTexture(gltf, emissiveTexture); if (tIndex != -1) { emissiveFactor = new float[] { 1.0f, 1.0f, 1.0f }; mat.EmissiveTexture = new TextureInfo { Index = tIndex }; } } if (IsValidTexture(baseColorTexture)) { var tIndex = ResolveTexture(gltf, baseColorTexture); if (tIndex != -1) { baseColorFactor = new List <float> { 1.0f, 1.0f, 1.0f }; mat.PbrMetallicRoughness.BaseColorTexture = new TextureInfo { Index = tIndex }; } } if (IsValidTexture(metallicTexture)) { metallicFactor = 1.0f; var tIndex = ResolveTexture(gltf, metallicTexture); mat.PbrMetallicRoughness.MetallicRoughnessTexture = new TextureInfo { Index = tIndex }; } if (IsValidTexture(roughnessTexture)) { roughnessFactor = 1.0f; } if (IsValidTexture(normalTexture)) { var tIndex = ResolveTexture(gltf, normalTexture); if (tIndex != -1) { mat.NormalTexture = new NormalTexture { Index = tIndex, Scale = 1 }; } } if (IsValidTexture(occlusionTexture)) { var tIndex = ResolveTexture(gltf, occlusionTexture); if (tIndex != -1) { mat.OcclusionTexture = new OcclusionTexture { Index = tIndex }; } } baseColorFactor.Add(1.0f); bool transparent; if (IsValidTexture(alphaTexture)) { transparent = true; } else { var alpha = mtl.Dissolve ?? 1.0f; baseColorFactor[3] = alpha; transparent = alpha < 1.0; } mat.DoubleSided = transparent; mat.AlphaMode = new CheckedValue <AlphaMode, string>(transparent ? AlphaMode.BLEND : AlphaMode.OPAQUE); mat.PbrMetallicRoughness.BaseColorFactor = baseColorFactor.ToArray(); mat.PbrMetallicRoughness.MetallicFactor = metallicFactor; mat.PbrMetallicRoughness.RoughnessFactor = roughnessFactor; mat.EmissiveFactor = emissiveFactor; gltf.Materials.Add(mat); return(matIndex); }
/// <summary> /// run converter /// </summary> /// <returns></returns> public Model Run() { var model = new Model { Asset = new Asset() }; // parse obj files var objModel = ObjParser.Parse(_objFile); var mtlModels = new List <MtlModel>(); foreach (var mtl in objModel.MaterialLibaries) { var mtlM = MtlParser.Parse(Path.Combine(_objFolder, mtl)); mtlModels.Add(mtlM); foreach (var m in mtlM.Materials) { var matName = m.Key; var mm = m.Value; var matIndex = AddMaterial(model, matName, mm); _matDict.Add(matName, matIndex); } } if (_matDict.Count == 0) { // add default material var mat = new Material(); mat.Name = "default"; mat.PbrMetallicRoughness = new PbrMetallicRoughness(); mat.DoubleSided = true; mat.PbrMetallicRoughness.BaseColorFactor = new float[] { 0.3f, 0.3f, 0.3f, 1.0f }; model.Materials.Add(mat); _matDict.Add("default", 0); } AddNodes(model, objModel); if (_options.GLB) { model.BinBuffers = new List <byte>(); if (_buffers.ImageBuffers.Count > 0) { model.BinBuffers.AddRange(_buffers.ImageBuffers.SelectMany(c => c)); } model.BinBuffers.AddRange(_buffers.Positions.SelectMany(c => c)); if (_buffers.Normals.Count > 0) { model.BinBuffers.AddRange(_buffers.Normals.SelectMany(c => c)); } if (_buffers.Uvs.Count > 0) { model.BinBuffers.AddRange(_buffers.Uvs.SelectMany(c => c)); } model.BinBuffers.AddRange(_buffers.Indices.SelectMany(c => c)); } if (model.Images.Count > 0) { model.Samplers.Add(new Sampler { MagFilter = new CheckedValue <MagFilter, int>(MagFilter.Linear), MinFilter = new CheckedValue <MinFilter, int>(MinFilter.NearestMipmapLinear), WrapS = new CheckedValue <WrappingMode, int>(WrappingMode.Repeat), WrapT = new CheckedValue <WrappingMode, int>(WrappingMode.Repeat) }); } var doc = Document.Create(); doc.WriteModel(model, _gltfFile); return(model); }