/// <summary> /// /// </summary> /// <param name="mat"></param> /// <returns>roughnessFactor</returns> private static double ConvertTraditional2MetallicRoughness(WaveFront.Material mat) { // Transform from 0-1000 range to 0-1 range. Then invert. //var roughnessFactor = mat.SpecularExponent; // options.metallicRoughness ? 1.0 : 0.0; //roughnessFactor = roughnessFactor / 1000.0; var roughnessFactor = 1.0 - mat.SpecularExponent / 1000.0; roughnessFactor = Clamp(roughnessFactor, 0.0, 1.0); if (mat.Specular == null || mat.Specular.Color == null) { mat.Specular = new Reflectivity(new Color()); return(roughnessFactor); } // Translate the blinn-phong model to the pbr metallic-roughness model // Roughness factor is a combination of specular intensity and shininess // Metallic factor is 0.0 // Textures are not converted for now var specularIntensity = Luminance(mat.Specular.Color); // Low specular intensity values should produce a rough material even if shininess is high. if (specularIntensity < 0.1) { roughnessFactor *= (1.0 - specularIntensity); } var metallicFactor = 0.0; mat.Specular = new Reflectivity(new Color(metallicFactor)); return(roughnessFactor); }
private int AddMaterial(WaveFront.Material mat) { Gltf.Material gMat = null; if (mat == null) { gMat = GetDefault(); } else { var roughnessFactor = ConvertTraditional2MetallicRoughness(mat); gMat = new Gltf.Material { Name = mat.Name, AlphaMode = AlphaMode.OPAQUE }; var hasTexture = !String.IsNullOrEmpty(mat.DiffuseTextureFile); var alpha = mat.GetAlpha(); var metallicFactor = 0.0; if (mat.Specular != null && mat.Specular.Color != null) { metallicFactor = mat.Specular.Color.Red; } gMat.PbrMetallicRoughness = new PbrMetallicRoughness { RoughnessFactor = roughnessFactor, MetallicFactor = metallicFactor }; if (mat.Diffuse != null) { gMat.PbrMetallicRoughness.BaseColorFactor = mat.Diffuse.Color.ToArray(alpha); } else if (mat.Ambient != null) { gMat.PbrMetallicRoughness.BaseColorFactor = mat.Ambient.Color.ToArray(alpha); } else { gMat.PbrMetallicRoughness.BaseColorFactor = new double[] { 0.7, 0.7, 0.7, alpha }; } if (hasTexture) { int index = -1; for (var i = 0; i < _model.Textures.Count; i++) { if (mat.DiffuseTextureFile == _model.Textures[i].Name) { index = i; break; } } if (index == -1) { index = AddTexture(mat.DiffuseTextureFile); } gMat.PbrMetallicRoughness.BaseColorTexture = new Info { Index = index }; } if (mat.Emissive != null && mat.Emissive.Color != null) { gMat.EmissiveFactor = mat.Emissive.Color.ToArray(); } if (alpha < 1.0) { gMat.AlphaMode = AlphaMode.BLEND; gMat.DoubleSided = true; } } var matIndex = _model.Materials.Count; _model.Materials.Add(gMat); return(matIndex); }