public virtual glTFMaterial ExportMaterial(Material m, TextureExportManager textureManager) { var material = CreateMaterial(m); // common params material.name = m.name; Export_Color(m, textureManager, material); Export_Metallic(m, textureManager, material); Export_Normal(m, textureManager, material); Export_Occlusion(m, textureManager, material); Export_Emission(m, textureManager, material); if ((material.pbrMetallicRoughness != null) && (material.pbrMetallicRoughness.baseColorFactor == null) && (material.pbrMetallicRoughness.baseColorTexture == null) && (material.pbrMetallicRoughness.metallicFactor == -1.0f) && (material.pbrMetallicRoughness.roughnessFactor == -1.0f) && (material.pbrMetallicRoughness.baseColorFactor == null) && (material.pbrMetallicRoughness.extensions == null) && (material.pbrMetallicRoughness.extras == null)) { material.pbrMetallicRoughness = null; } return(material); }
static void Export_Emission(Material m, TextureExportManager textureManager, glTFMaterial material) { if (m.IsKeywordEnabled("_EMISSION") == false) { return; } if (m.HasProperty("_EmissionColor")) { var color = m.GetColor("_EmissionColor"); if (color.maxColorComponent > 1) { color /= color.maxColorComponent; } material.emissiveFactor = new float[] { color.r, color.g, color.b }; } if (m.HasProperty("_EmissionMap")) { var index = textureManager.CopyAndGetIndex(m.GetTexture("_EmissionMap"), RenderTextureReadWrite.sRGB); if (index != -1) { material.emissiveTexture = new glTFMaterialEmissiveTextureInfo() { index = index, }; } } }
protected virtual void FromGameObjectMaterialsAndTextures(glTF gltf, GameObject go, int bufferIndex) { Materials = Nodes.SelectMany(x => x.GetSharedMaterials()).Where(x => x != null).Distinct().ToList(); var unityTextures = Materials.SelectMany(x => TextureIO.GetTextures(x)).Where(x => x.Texture != null).Distinct().ToList(); TextureManager = new TextureExportManager(unityTextures.Select(x => x.Texture)); var materialExporter = CreateMaterialExporter(); gltf.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList(); for (int i = 0; i < unityTextures.Count; ++i) { var unityTexture = unityTextures[i]; TextureIO.ExportTexture(gltf, bufferIndex, TextureManager.GetExportTexture(i), unityTexture.TextureType); } }
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, }; } } 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"); } } }
static void Export_Color(Material m, TextureExportManager textureManager, glTFMaterial material) { if (m.HasProperty("_Color")) { material.pbrMetallicRoughness.baseColorFactor = m.color.linear.ToArray(); } if (m.HasProperty("_MainTex")) { var index = textureManager.CopyAndGetIndex(m.GetTexture("_MainTex"), RenderTextureReadWrite.sRGB); if (index != -1) { material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo() { index = index, }; } } }
static void Export_Occlusion(Material m, TextureExportManager textureManager, glTFMaterial material) { if (m.HasProperty("_OcclusionMap")) { var index = textureManager.ConvertAndGetIndex(m.GetTexture("_OcclusionMap"), new OcclusionConverter()); if (index != -1) { material.occlusionTexture = new glTFMaterialOcclusionTextureInfo() { index = index, }; } if (index != -1 && m.HasProperty("_OcclusionStrength")) { material.occlusionTexture.strength = m.GetFloat("_OcclusionStrength"); } } }
static void Export_Normal(Material m, TextureExportManager textureManager, glTFMaterial material) { if (m.HasProperty("_BumpMap")) { var index = textureManager.ConvertAndGetIndex(m.GetTexture("_BumpMap"), new NormalConverter()); if (index != -1) { material.normalTexture = new glTFMaterialNormalTextureInfo() { index = index, }; } if (index != -1 && m.HasProperty("_BumpScale")) { material.normalTexture.scale = m.GetFloat("_BumpScale"); } } }