public Texture2D ConvertTexture(string prop) { var convertedTexture = Converts.FirstOrDefault(x => x.Key == prop); if (convertedTexture.Value != null) { return(convertedTexture.Value); } if (prop == "_BumpMap") { if (Application.isPlaying) { var converted = new NormalConverter().GetImportTexture(Texture); m_converts.Add(prop, converted); return(converted); } else { #if UNITY_EDITOR var textureAssetPath = AssetDatabase.GetAssetPath(Texture); if (!string.IsNullOrEmpty(textureAssetPath)) { TextureIO.MarkTextureAssetAsNormalMap(textureAssetPath); } else { Debug.LogWarningFormat("no asset for {0}", Texture); } #endif return(Texture); } } if (prop == "_MetallicGlossMap") { var converted = new MetallicRoughnessConverter().GetImportTexture(Texture); m_converts.Add(prop, converted); return(converted); } if (prop == "_OcclusionMap") { var converted = new OcclusionConverter().GetImportTexture(Texture); m_converts.Add(prop, converted); return(converted); } return(null); }
public void ImportingColorTest() { { var roughnessFactor = 1.0f; var conv = new MetallicRoughnessConverter(roughnessFactor); Assert.That( conv.Import(new Color32(255, 255, 255, 255)), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) // a <- 0 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8) Is.EqualTo(new Color32(255, 0, 0, 0))); } { var roughnessFactor = 1.0f; var conv = new MetallicRoughnessConverter(roughnessFactor); Assert.That( conv.Import(new Color32(255, 63, 255, 255)), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) // a <- 128 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8) Is.EqualTo(new Color32(255, 0, 0, 128))); // smoothness 0.5 * src.a 1.0 } { var roughnessFactor = 0.5f; var conv = new MetallicRoughnessConverter(roughnessFactor); Assert.That( conv.Import(new Color32(255, 255, 255, 255)), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) // a <- 74 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8) Is.EqualTo(new Color32(255, 0, 0, 74))); } { var roughnessFactor = 0.0f; var conv = new MetallicRoughnessConverter(roughnessFactor); Assert.That( conv.Import(new Color32(255, 255, 255, 255)), // r <- 255 : Same metallic (src.r) // g <- 0 : (Unused) // b <- 0 : (Unused) // a <- 255 : ((1 - sqrt(src.g(as float) * roughnessFactor)))(as uint8) Is.EqualTo(new Color32(255, 0, 0, 255))); } }
static void Export_Metallic(Material m, TextureExportManager textureManager, glTFMaterial material) { int index = -1; if (m.HasProperty("_MetallicGlossMap")) { float smoothness = 0.0f; if (m.HasProperty("_GlossMapScale")) { smoothness = m.GetFloat("_GlossMapScale"); } // Bake smoothness values into a texture. var converter = new MetallicRoughnessConverter(smoothness); index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), converter); if (index != -1) { material.pbrMetallicRoughness.metallicRoughnessTexture = new glTFMaterialMetallicRoughnessTextureInfo() { index = index, }; Export_MainTextureTransform(m, material.pbrMetallicRoughness.metallicRoughnessTexture); } } if (index != -1) { material.pbrMetallicRoughness.metallicFactor = 1.0f; // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212. material.pbrMetallicRoughness.roughnessFactor = 1.0f; } else { if (m.HasProperty("_Metallic")) { material.pbrMetallicRoughness.metallicFactor = m.GetFloat("_Metallic"); } if (m.HasProperty("_Glossiness")) { material.pbrMetallicRoughness.roughnessFactor = 1.0f - m.GetFloat("_Glossiness"); } } }
public void ExportingColorTest() { { var smoothness = 1.0f; var conv = new MetallicRoughnessConverter(smoothness); Assert.That( conv.Export(new Color32(255, 255, 255, 255)), // r <- 0 : (Unused) // g <- 0 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8) // b <- 255 : Same metallic (src.r) // a <- 255 : (Unused) Is.EqualTo(new Color32(0, 0, 255, 255))); } { var smoothness = 0.5f; var conv = new MetallicRoughnessConverter(smoothness); Assert.That( conv.Export(new Color32(255, 255, 255, 255)), // r <- 0 : (Unused) // g <- 63 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8) // b <- 255 : Same metallic (src.r) // a <- 255 : (Unused) Is.EqualTo(new Color32(0, 63, 255, 255))); } { var smoothness = 0.0f; var conv = new MetallicRoughnessConverter(smoothness); Assert.That( conv.Export(new Color32(255, 255, 255, 255)), // r <- 0 : (Unused) // g <- 255 : ((1 - src.a(as float) * smoothness) ^ 2)(as uint8) // b <- 255 : Same metallic (src.r) // a <- 255 : (Unused) Is.EqualTo(new Color32(0, 255, 255, 255))); } }