diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs
index 6e508d83f..068cb105a 100644
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs
+++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/OcclusionMetallicRoughnessConverter.cs
@@ -90,11 +90,36 @@ namespace UniGLTF
return dst;
}
- // public static Texture2D Export(Texture texture, float smoothness)
- // {
- // var converted = TextureConverter.Convert(texture, glTFTextureTypes.OcclusionMetallicRoughness, x => ExportPixel(x, smoothness), null);
- // return converted;
- // }
+ public static Texture2D Export(Texture metallicSmoothTexture, float smoothness, Texture occlusionTexture)
+ {
+ if (metallicSmoothTexture != null && occlusionTexture != null)
+ {
+ if (metallicSmoothTexture != occlusionTexture)
+ {
+ throw new NotImplementedException();
+ }
+ else
+ {
+ throw new NotImplementedException();
+ }
+ }
+ else if (metallicSmoothTexture)
+ {
+ var copyTexture = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
+ copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(x, smoothness, default)).ToArray());
+ copyTexture.Apply();
+ copyTexture.name = metallicSmoothTexture.name;
+ return copyTexture;
+ }
+ else if (occlusionTexture)
+ {
+ throw new NotImplementedException();
+ }
+ else
+ {
+ throw new NotImplementedException();
+ }
+ }
public static Color32 ExportPixel(Color32 metallicSmooth, float smoothness, Color32 occlusion)
{
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs
index 6e1100946..d321b6e00 100644
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs
+++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter/TextureExportManager.cs
@@ -1,129 +1,165 @@
using System;
-using System.Collections;
using System.Collections.Generic;
-using System.Linq;
using UnityEngine;
namespace UniGLTF
{
+ ///
+ /// glTF にエクスポートする Texture2D を蓄えて index を確定させる
+ ///
public class TextureExportManager
{
+ struct ExportKey
+ {
+ public readonly Texture Src;
+ public readonly glTFTextureTypes TextureType;
+
+ public ExportKey(Texture src, glTFTextureTypes type)
+ {
+ if (src == null)
+ {
+ throw new ArgumentNullException();
+ }
+ Src = src;
+ TextureType = type;
+ }
+ }
+ Dictionary m_exportMap = new Dictionary();
+ List m_exported = new List();
+
+ ///
+ /// Texture の export index を得る
+ ///
+ ///
+ ///
+ ///
public int GetTextureIndex(Texture src, glTFTextureTypes textureType)
{
- throw new NotImplementedException();
+ return m_exportMap[new ExportKey(src, textureType)];
}
+ ///
+ /// sRGBなテクスチャーを処理する
+ ///
+ ///
+ ///
public int ExportSRGB(Texture src)
{
- throw new NotImplementedException();
- }
+ if (src == null)
+ {
+ throw new ArgumentNullException();
+ }
- public int ExportMetallicSmoothnessOcclusion(Texture metallicSmoothTexture, float smoothness, Texture occlusionTexture)
- {
- if (metallicSmoothTexture != null && occlusionTexture != null)
+ // cache
+ if (m_exportMap.TryGetValue(new ExportKey(src, glTFTextureTypes.SRGB), out var index))
{
- if (metallicSmoothTexture != occlusionTexture)
- {
- throw new NotImplementedException();
- }
- else
- {
- throw new NotImplementedException();
- }
+ return index;
}
- else if (metallicSmoothTexture)
+
+ // get Texture2D
+ index = m_exported.Count;
+ if (src is Texture2D texture2D)
{
- throw new NotImplementedException();
- }
- else if (occlusionTexture)
- {
- throw new NotImplementedException();
+ // do nothing
}
else
{
- throw new NotImplementedException();
+ texture2D = TextureConverter.CopyTexture(src, glTFTextureTypes.SRGB, null);
}
+ m_exported.Add(texture2D);
+ m_exportMap.Add(new ExportKey(src, glTFTextureTypes.SRGB), index);
+
+ return index;
}
- public int ExportNormal(Texture normalTexture)
+ ///
+ /// Standard の Metallic, Smoothness, Occlusion をまとめる
+ ///
+ ///
+ ///
+ ///
+ ///
+ public int ExportMetallicSmoothnessOcclusion(Texture metallicSmoothTexture, float smoothness, Texture occlusionTexture)
{
- throw new NotImplementedException();
+ if (metallicSmoothTexture == null && occlusionTexture == null)
+ {
+ throw new ArgumentNullException();
+ }
+
+ // cache
+ if (m_exportMap.TryGetValue(new ExportKey(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness), out var index))
+ {
+ return index;
+ }
+ if (m_exportMap.TryGetValue(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), out index))
+ {
+ return index;
+ }
+
+ //
+ // Unity と glTF で互換性が無いので必ず変換が必用
+ //
+ index = m_exported.Count;
+ var texture2D = OcclusionMetallicRoughnessConverter.Export(metallicSmoothTexture, smoothness, occlusionTexture);
+
+ m_exported.Add(texture2D);
+ m_exportMap.Add(new ExportKey(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness), index);
+ m_exportMap.Add(new ExportKey(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness), index);
+
+ return index;
}
- // 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];
- // }
+ static bool UseNormalAsset(Texture src, out Texture2D texture2D)
+ {
+#if UNITY_EDITOR
+ // asset として存在して textureImporter.textureType = TextureImporterType.NormalMap
+ texture2D = src as Texture2D;
+ if (texture2D != null && !string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(src)))
+ {
+ return true;
+ }
+#endif
- // // オリジナル
- // return m_textures[index];
- // }
+ texture2D = default;
+ return false;
+ }
- // public TextureExportManager(IEnumerable textures)
- // {
- // if (textures == null)
- // {
- // // empty list for UnitTest
- // textures = new Texture[] { };
- // }
- // m_textures = textures.ToList();
- // m_exportTextures = new List(Enumerable.Repeat(null, m_textures.Count));
- // }
+ ///
+ /// Normal のテクスチャを変換する
+ ///
+ ///
+ ///
+ public int ExportNormal(Texture src)
+ {
+ if (src == null)
+ {
+ throw new ArgumentNullException();
+ }
- // public int CopyAndGetIndex(Texture texture, glTFTextureTypes readWrite)
- // {
- // if (texture == null)
- // {
- // return -1;
- // }
+ // cache
+ if (m_exportMap.TryGetValue(new ExportKey(src, glTFTextureTypes.Normal), out var index))
+ {
+ return index;
+ }
- // var index = m_textures.IndexOf(texture);
- // if (index == -1)
- // {
- // // ありえない?
- // return -1;
- // }
+ // get Texture2D
+ index = m_exported.Count;
+ Texture2D texture2D = default;
+ if (UseNormalAsset(src, out texture2D))
+ {
+ // EditorAsset を使うので変換不要
+ }
+ else
+ {
+ // 後で Bitmap を使うために変換する
+ texture2D = NormalConverter.Export(src);
+ }
- // #if UNITY_EDITOR
- // if (!string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture)))
- // {
- // m_exportTextures[index] = texture;
- // return index;
- // }
- // #endif
+ m_exported.Add(texture2D);
+ m_exportMap.Add(new ExportKey(src, glTFTextureTypes.Normal), index);
- // // ToDo: may already exists
- // m_exportTextures[index] = TextureConverter.CopyTexture(texture, readWrite, null);
-
- // return index;
- // }
-
- // public int ConvertAndGetIndex(Texture texture, Func converter)
- // {
- // if (texture == null)
- // {
- // return -1;
- // }
-
- // var index = m_textures.IndexOf(texture);
- // if (index == -1)
- // {
- // // ありえない?
- // return -1;
- // }
-
- // m_exportTextures[index] = converter(texture);
-
- // return index;
- // }
+ return index;
+ }
}
}
diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
index ce38ededf..abd51a5cb 100644
--- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
+++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs
@@ -206,11 +206,6 @@ namespace UniGLTF
var materialExporter = CreateMaterialExporter();
glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList();
-
- foreach (var material in Materials)
- {
- materialExporter.ExportMaterial(material, TextureManager);
- }
#endregion
#region Meshes