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); + } } }