Merge pull request #890 from ousttrue/fix/texture_extract_subassetkey

Fix/texture extract subassetkey
This commit is contained in:
PoChang007
2021-04-20 14:05:08 +09:00
committed by GitHub
2 changed files with 70 additions and 1 deletions

View File

@@ -63,7 +63,11 @@ 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();

View File

@@ -1,3 +1,6 @@
using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
using VRMShaders;
@@ -31,5 +34,67 @@ 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");
Assert.Catch<NotImplementedException>(() => extractor.Extract(m.Item1, m.Item2));
}
}
}