diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs index 0f33eb548..ab9a3bedd 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using UniGLTF.UniUnlit; using UnityEngine; @@ -65,7 +66,9 @@ namespace UniGLTF { Texture metallicSmoothTexture = default; float smoothness = 1.0f; - if (m.HasProperty("_MetallicGlossMap")) + + var textuerNames = m.GetTexturePropertyNames(); + if (textuerNames.Contains("_MetallicGlossMap")) { if (m.HasProperty("_GlossMapScale")) { @@ -76,7 +79,7 @@ namespace UniGLTF Texture occlusionTexture = default; var occlusionStrength = 1.0f; - if (m.HasProperty("_OcclusionMap")) + if (textuerNames.Contains("_OcclusionMap")) { occlusionTexture = m.GetTexture("_OcclusionMap"); if (occlusionTexture != null && m.HasProperty("_OcclusionStrength")) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 424b02de0..5aa7c151e 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -168,6 +168,11 @@ namespace UniGLTF return node; } + public virtual void ExportExtensions() + { + + } + public virtual void Export(MeshExportSettings meshExportSettings) { var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]); @@ -184,12 +189,6 @@ namespace UniGLTF var materialExporter = CreateMaterialExporter(); glTF.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureManager)).ToList(); - - for (int i = 0; i < TextureManager.Exported.Count; ++i) - { - var unityTexture = TextureManager.Exported[i]; - glTF.PushGltfTexture(bufferIndex, unityTexture); - } #endregion #region Meshes @@ -306,6 +305,15 @@ namespace UniGLTF } #endregion #endif + + ExportExtensions(); + + // Extension で Texture が増える場合があるので最後に呼ぶ + for (int i = 0; i < TextureManager.Exported.Count; ++i) + { + var unityTexture = TextureManager.Exported[i]; + glTF.PushGltfTexture(bufferIndex, unityTexture); + } } #endregion } diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index 1245c9196..c4d5c80bb 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -32,10 +32,8 @@ namespace VRM gltf.extensionsUsed.Add(glTF_VRM_extensions.ExtensionName); } - public override void Export(MeshExportSettings configuration) + public override void ExportExtensions() { - base.Export(configuration); - // avatar var animator = Copy.GetComponent(); if (animator != null) diff --git a/Assets/VRM/Tests/VRMLoadTests.cs b/Assets/VRM/Tests/VRMLoadTests.cs index d671ca3ec..49173e7f3 100644 --- a/Assets/VRM/Tests/VRMLoadTests.cs +++ b/Assets/VRM/Tests/VRMLoadTests.cs @@ -48,17 +48,25 @@ namespace VRM } } - static void Load(FileInfo gltf, DirectoryInfo root) + static GameObject Load(FileInfo gltf, DirectoryInfo root, byte[] bytes = null) { var parser = new GltfParser(); try { - parser.ParsePath(gltf.FullName); + if (bytes != null) + { + parser.Parse(gltf.FullName, bytes); + } + else + { + parser.ParsePath(gltf.FullName); + } } catch (Exception ex) { Debug.LogError($"ParseError: {gltf}"); Debug.LogException(ex); + return null; } try @@ -66,11 +74,13 @@ namespace VRM using (var importer = new VRMImporterContext(parser)) { importer.Load(); + return importer.DisposeOnGameObjectDestroyed().gameObject; } } catch (Exception ex) { Message(gltf.FullName.Substring(root.FullName.Length), ex); + return null; } } @@ -90,7 +100,23 @@ namespace VRM foreach (var gltf in EnumerateGltfFiles(root)) { - Load(gltf, root); + // import + var go = Load(gltf, root); + try + { + // export + var vrm = VRMExporter.Export(UniGLTF.MeshExportSettings.Default, go); + + // re import + if (vrm != null) + { + Load(gltf, root, vrm.ToGlbBytes()); + } + } + finally + { + GameObject.DestroyImmediate(go); + } } } }