From 9c355c91b9c992cf27ddab1f54913fffc1a72214 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 7 Nov 2018 13:22:49 +0900 Subject: [PATCH] Added TextureExportManager --- Core/Scripts/IO/MaterialExporter.cs | 56 ++++++------- Core/Scripts/IO/TextureConverter.cs | 2 +- Core/Scripts/IO/TextureExportManager.cs | 85 ++++++++++++++++++++ Core/Scripts/IO/TextureExportManager.cs.meta | 12 +++ Core/Scripts/IO/gltfExporter.cs | 14 +--- 5 files changed, 125 insertions(+), 44 deletions(-) create mode 100644 Core/Scripts/IO/TextureExportManager.cs create mode 100644 Core/Scripts/IO/TextureExportManager.cs.meta diff --git a/Core/Scripts/IO/MaterialExporter.cs b/Core/Scripts/IO/MaterialExporter.cs index 4d2a310..1424068 100644 --- a/Core/Scripts/IO/MaterialExporter.cs +++ b/Core/Scripts/IO/MaterialExporter.cs @@ -16,32 +16,27 @@ public enum glTFBlendMode public interface IMaterialExporter { - glTFMaterial ExportMaterial(Material m, List textures, List exportTextures); + glTFMaterial ExportMaterial(Material m, TextureExportManager textureManager); } public class MaterialExporter : IMaterialExporter { - public virtual glTFMaterial ExportMaterial(Material m, List textures, List exportTextures) + public virtual glTFMaterial ExportMaterial(Material m, TextureExportManager textureManager) { - if (textures == null) - { - throw new System.ArgumentNullException(); - } - var material = CreateMaterial(m); // common params material.name = m.name; - Export_Color(m, textures, exportTextures, material); - Export_Metallic(m, textures, exportTextures, material); - Export_Normal(m, textures, exportTextures, material); - Export_Occlusion(m, textures, exportTextures, material); - Export_Emission(m, textures, exportTextures, material); + 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); return material; } - static void Export_Color(Material m, List textures, List exportTextures, glTFMaterial material) + static void Export_Color(Material m, TextureExportManager textureManager, glTFMaterial material) { if (m.HasProperty("_Color")) { @@ -50,10 +45,9 @@ static void Export_Color(Material m, List textures, List expor if (m.HasProperty("_MainTex")) { - var index = textures.IndexOf(m.GetTexture("_MainTex")); - if (index != -1 && m.mainTexture != null) + var index = textureManager.CopyAndGetIndex(m.GetTexture("_MainTex"), RenderTextureReadWrite.sRGB); + if (index != -1) { - exportTextures[index] = TextureItem.CopyTexture(m.mainTexture, RenderTextureReadWrite.sRGB, null); material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo() { index = index, @@ -62,15 +56,14 @@ static void Export_Color(Material m, List textures, List expor } } - static void Export_Metallic(Material m, List textures, List exportTextures, glTFMaterial material) + static void Export_Metallic(Material m, TextureExportManager textureManager, glTFMaterial material) { int index = -1; if (m.HasProperty("_MetallicGlossMap")) { - index = textures.IndexOf(m.GetTexture("_MetallicGlossMap")); - if (index != -1 && m.HasProperty("_MetallicGlossMap")) + index = textureManager.ConvertAndGetIndex(m.GetTexture("_MetallicGlossMap"), new MetallicRoughnessConverter()); + if (index != -1) { - exportTextures[index] = (new MetallicRoughnessConverter()).GetExportTexture(textures[index] as Texture2D); material.pbrMetallicRoughness.metallicRoughnessTexture = new glTFMaterialMetallicRoughnessTextureInfo() { index = index, @@ -97,14 +90,13 @@ static void Export_Metallic(Material m, List textures, List ex } } - static void Export_Normal(Material m, List textures, List exportTextures, glTFMaterial material) + static void Export_Normal(Material m, TextureExportManager textureManager, glTFMaterial material) { if (m.HasProperty("_BumpMap")) { - var index = textures.IndexOf(m.GetTexture("_BumpMap")); - if (index != -1 && m.HasProperty("_BumpMap")) + var index = textureManager.ConvertAndGetIndex(m.GetTexture("_BumpMap"), new NormalConverter()); + if (index != -1) { - exportTextures[index] = (new NormalConverter()).GetExportTexture(textures[index] as Texture2D); material.normalTexture = new glTFMaterialNormalTextureInfo() { index = index, @@ -118,14 +110,13 @@ static void Export_Normal(Material m, List textures, List expo } } - static void Export_Occlusion(Material m, List textures, List exportTextures, glTFMaterial material) + static void Export_Occlusion(Material m, TextureExportManager textureManager, glTFMaterial material) { if (m.HasProperty("_OcclusionMap")) { - var index = textures.IndexOf(m.GetTexture("_OcclusionMap")); - if (index != -1 && m.HasProperty("_OcclusionMap")) + var index = textureManager.ConvertAndGetIndex(m.GetTexture("_OcclusionMap"), new OcclusionConverter()); + if (index != -1) { - exportTextures[index] = (new OcclusionConverter()).GetExportTexture(textures[index] as Texture2D); material.occlusionTexture = new glTFMaterialOcclusionTextureInfo() { index = index, @@ -139,7 +130,7 @@ static void Export_Occlusion(Material m, List textures, List e } } - static void Export_Emission(Material m, List textures, List exportTextures, glTFMaterial material) + static void Export_Emission(Material m, TextureExportManager textureManager, glTFMaterial material) { if (m.HasProperty("_EmissionColor")) { @@ -149,10 +140,9 @@ static void Export_Emission(Material m, List textures, List ex if (m.HasProperty("_EmissionMap")) { - var index = textures.IndexOf(m.GetTexture("_EmissionMap")); - if (index != -1 && m.HasProperty("_EmissionMap")) + var index = textureManager.CopyAndGetIndex(m.GetTexture("_EmissionMap"), RenderTextureReadWrite.sRGB); + if (index != -1) { - exportTextures[index] = TextureItem.CopyTexture(textures[index], RenderTextureReadWrite.sRGB, null); material.emissiveTexture = new glTFMaterialEmissiveTextureInfo() { index = index, @@ -256,7 +246,7 @@ static glTFMaterial Export_Standard(Material m) pbrMetallicRoughness = new glTFPbrMetallicRoughness(), }; - switch(m.GetTag("RenderType", true)) + switch (m.GetTag("RenderType", true)) { case "Transparent": material.alphaMode = glTFBlendMode.BLEND.ToString(); diff --git a/Core/Scripts/IO/TextureConverter.cs b/Core/Scripts/IO/TextureConverter.cs index de72a2c..b7fba39 100644 --- a/Core/Scripts/IO/TextureConverter.cs +++ b/Core/Scripts/IO/TextureConverter.cs @@ -7,7 +7,7 @@ namespace UniGLTF { - interface ITextureConverter + public interface ITextureConverter { Texture2D GetImportTexture(Texture2D texture); Texture2D GetExportTexture(Texture2D texture); diff --git a/Core/Scripts/IO/TextureExportManager.cs b/Core/Scripts/IO/TextureExportManager.cs new file mode 100644 index 0000000..2137af5 --- /dev/null +++ b/Core/Scripts/IO/TextureExportManager.cs @@ -0,0 +1,85 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + + +namespace UniGLTF +{ + public class TextureExportManager + { + List m_textures; + public List Textures + { + get { return m_textures; } + } + + List m_exportTextures; + public Texture GetExportTexture(int index) + { + if (index < 0 || index >= m_exportTextures.Count) + { + return null; + } + if (m_exportTextures[index] != null) + { + // コピー変換済み + return m_exportTextures[index]; + } + + // オリジナル + return m_textures[index]; + } + + public TextureExportManager(IEnumerable textures) + { + /* + if (textures == null) + { + throw new System.ArgumentNullException(); + } + */ + m_textures = textures.ToList(); + m_exportTextures = new List(Enumerable.Repeat(null, m_textures.Count)); + } + + public int CopyAndGetIndex(Texture texture, RenderTextureReadWrite readWrite) + { + if (texture == null) + { + return -1; + } + + var index = m_textures.IndexOf(texture); + if (index == -1) + { + // ありえない? + return -1; + } + + // ToDo: may already exists + m_exportTextures[index] = TextureItem.CopyTexture(texture, readWrite, null); + + return index; + } + + public int ConvertAndGetIndex(Texture texture, ITextureConverter converter) + { + if (texture == null) + { + return -1; + } + + var index = m_textures.IndexOf(texture); + if (index == -1) + { + // ありえない? + return -1; + } + + m_exportTextures[index] = converter.GetExportTexture(texture as Texture2D); + + return index; + } + } +} diff --git a/Core/Scripts/IO/TextureExportManager.cs.meta b/Core/Scripts/IO/TextureExportManager.cs.meta new file mode 100644 index 0000000..5048e04 --- /dev/null +++ b/Core/Scripts/IO/TextureExportManager.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 435499f173753ac418331fe0f967b789 +timeCreated: 1541561421 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Core/Scripts/IO/gltfExporter.cs b/Core/Scripts/IO/gltfExporter.cs index 37d8f2a..42106e1 100644 --- a/Core/Scripts/IO/gltfExporter.cs +++ b/Core/Scripts/IO/gltfExporter.cs @@ -90,11 +90,7 @@ public List Materials private set; } - public List Textures - { - get; - private set; - } + public TextureExportManager TextureManager; protected virtual IMaterialExporter CreateMaterialExporter() { @@ -445,17 +441,15 @@ public void FromGameObject(glTF gltf, GameObject go, bool useSparseAccessorForMo 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(); - - Textures = unityTextures.Select(y => y.Texture).ToList(); - List exportTextures = new List(Textures); + TextureManager = new TextureExportManager(unityTextures.Select(x => x.Texture)); var materialExporter = CreateMaterialExporter(); - gltf.materials = Materials.Select(x => materialExporter.ExportMaterial(x, Textures, exportTextures)).ToList(); + 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, exportTextures[i], unityTexture.TextureType); + TextureIO.ExportTexture(gltf, bufferIndex, TextureManager.GetExportTexture(i), unityTexture.TextureType); } #endregion