From 1cee06e83ef3d9df48a8923c28cf0e2f22690a11 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 20 Apr 2021 13:49:21 +0900 Subject: [PATCH 1/3] add test --- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 63 ++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index efe9db5f4..01180fabd 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -1,3 +1,5 @@ +using System.IO; +using System.Linq; using NUnit.Framework; using UnityEngine; using VRMShaders; @@ -31,5 +33,66 @@ namespace UniGLTF Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.minFilter); Assert.AreEqual(glFilter.LINEAR_MIPMAP_LINEAR, sampler.magFilter); } + + static FileInfo Find(DirectoryInfo current, string target) + { + foreach (var child in current.EnumerateFiles()) + { + if (child.Name == target) + { + return child; + } + } + + foreach (var child in current.EnumerateDirectories()) + { + var found = Find(child, target); + if (found != null) + { + return found; + } + } + + return null; + } + + static FileInfo GetGltfTestModelPath(string name) + { + var env = System.Environment.GetEnvironmentVariable("GLTF_SAMPLE_MODELS"); + if (string.IsNullOrEmpty(env)) + { + return null; + } + var root = new DirectoryInfo($"{env}/2.0"); + if (!root.Exists) + { + return null; + } + + return Find(root, name); + } + + [Test] + public void TextureExtractTest() + { + var path = GetGltfTestModelPath("BoomBox.glb"); + if (path == null) + { + return; + } + + // parse + var parser = new GltfParser(); + parser.ParsePath(path.FullName); + + // load + var loader = new ImporterContext(parser); + loader.Load(); + + // extractor + var extractor = new TextureExtractor(parser, UnityPath.FromUnityPath(""), loader.TextureFactory.Textures.Select(x => (new SubAssetKey(typeof(Texture2D), x.Texture.name), x.Texture)).ToArray()); + var m = GltfTextureEnumerator.EnumerateTexturesForMaterial(parser, 0).FirstOrDefault(x => x.Item1.Name == "texture_1.standard"); + extractor.Extract(m.Item1, m.Item2); + } } } From 1cd4c3d951d1a91358bf8173b5004211503d4e94 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 20 Apr 2021 13:50:36 +0900 Subject: [PATCH 2/3] remove targetPath.ImportAsset. use EditorApplication.delayCall --- .../Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs index 8883b85d1..3ec448ba4 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs @@ -66,7 +66,7 @@ namespace UniGLTF var (_, subAsset) = m_subAssets.FirstOrDefault(x => x.Equals(key)); targetPath = m_textureDirectory.Child($"{key.Name}.png"); File.WriteAllBytes(targetPath.FullPath, subAsset.EncodeToPNG().ToArray()); - targetPath.ImportAsset(); + // targetPath.ImportAsset(); break; } @@ -75,7 +75,7 @@ namespace UniGLTF // write original bytes targetPath = m_textureDirectory.Child($"{key.Name}{param.Ext}"); File.WriteAllBytes(targetPath.FullPath, param.Index0().Result.ToArray()); - targetPath.ImportAsset(); + // targetPath.ImportAsset(); break; } } From 01241ac7be663920a7b3593f8aad14d571cd24a1 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 20 Apr 2021 14:03:06 +0900 Subject: [PATCH 3/3] =?UTF-8?q?StandardMap=20=E3=81=AE=20AssetKey=20?= =?UTF-8?q?=E3=81=AE=E6=AF=94=E8=BC=83=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UniGLTF/ScriptedImporter/TextureExtractor.cs | 10 +++++++--- Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs index 3ec448ba4..d474f79b8 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs @@ -63,10 +63,14 @@ namespace UniGLTF case TextureImportTypes.StandardMap: { // write converted texture - var (_, subAsset) = m_subAssets.FirstOrDefault(x => x.Equals(key)); + var (_, subAsset) = m_subAssets.FirstOrDefault(kv => kv.Key.Equals(key)); + if (subAsset == null) + { + throw new KeyNotFoundException(); + } targetPath = m_textureDirectory.Child($"{key.Name}.png"); File.WriteAllBytes(targetPath.FullPath, subAsset.EncodeToPNG().ToArray()); - // targetPath.ImportAsset(); + targetPath.ImportAsset(); break; } @@ -75,7 +79,7 @@ namespace UniGLTF // write original bytes targetPath = m_textureDirectory.Child($"{key.Name}{param.Ext}"); File.WriteAllBytes(targetPath.FullPath, param.Index0().Result.ToArray()); - // targetPath.ImportAsset(); + targetPath.ImportAsset(); break; } } diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs index 01180fabd..3b5eb1f65 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureTests.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Linq; using NUnit.Framework; @@ -92,7 +93,8 @@ namespace UniGLTF // extractor var extractor = new TextureExtractor(parser, UnityPath.FromUnityPath(""), loader.TextureFactory.Textures.Select(x => (new SubAssetKey(typeof(Texture2D), x.Texture.name), x.Texture)).ToArray()); var m = GltfTextureEnumerator.EnumerateTexturesForMaterial(parser, 0).FirstOrDefault(x => x.Item1.Name == "texture_1.standard"); - extractor.Extract(m.Item1, m.Item2); + + Assert.Catch(() => extractor.Extract(m.Item1, m.Item2)); } } }