Strict SubAssetKey constructor

This commit is contained in:
Masataka SUMI 2021-05-25 15:44:20 +09:00
parent effc63255c
commit b7593254d9
9 changed files with 58 additions and 87 deletions

View File

@ -35,7 +35,12 @@ namespace UniGLTF
//
// Import(create unity objects)
//
using (var loader = new ImporterContext(parser, GetExternalTextures(scriptedImporter, parser)))
// 2 回目以降の Asset Import において、 Importer の設定で Extract した UnityEngine.Object が入る
var extractedObjects = scriptedImporter.GetExternalObjectMap()
.ToDictionary(kv => new SubAssetKey(kv.Value.GetType(), kv.Value.name), kv => kv.Value);
using (var loader = new ImporterContext(parser, extractedObjects))
{
// settings TextureImporters
foreach (var (key, textureInfo) in GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser))
@ -60,55 +65,5 @@ namespace UniGLTF
});
}
}
private static IReadOnlyDictionary<SubAssetKey, Object> GetExternalTextures(ScriptedImporter scriptedImporter, GltfParser parser)
{
// 2 回目以降の Asset Import において、 Importer の設定で Extract した Textures が入る
var externalTextures = scriptedImporter.GetExternalObjectMap()
.Where(kv => kv.Value is Texture)
.ToDictionary(kv => kv.Value.TextureSubAssetKey(), kv => kv.Value);
// return externalTextures;
var assetDirectoryPath = UnityPath.FromUnityPath(scriptedImporter.assetPath).Parent;
foreach (var (key, obj) in EnumerateExternalTexturesReferencedByMaterials(parser, assetDirectoryPath))
{
if (!externalTextures.ContainsKey(key))
{
externalTextures.Add(key, obj);
}
}
return externalTextures;
}
/// <summary>
/// glTF image の uri が参照する外部テクスチャファイルのうち、存在するもの
/// </summary>
private static IEnumerable<(SubAssetKey, Object)> EnumerateExternalTexturesReferencedByMaterials(GltfParser parser, UnityPath dir)
{
var used = new HashSet<Texture2D>();
foreach (var (key, texParam) in GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser))
{
if (string.IsNullOrEmpty(texParam.Uri)) continue;
if (texParam.Uri.StartsWith("data:")) continue;
var texPath = dir.Child(texParam.Uri);
var asset = AssetDatabase.LoadAssetAtPath<Texture2D>(texPath.Value);
if (asset == null)
{
throw new System.IO.FileNotFoundException($"{texPath}");
}
if (used.Add(asset))
{
yield return (key, asset);
}
}
}
private static SubAssetKey TextureSubAssetKey(this Object obj)
{
return new SubAssetKey(typeof(Texture), obj.name);
}
}
}

View File

@ -78,7 +78,7 @@ namespace VRM
//
var map = texturePaths
.Select(x => x.LoadAsset<Texture2D>())
.ToDictionary(SubAssetKey.FromTexture, x => x as Object);
.ToDictionary(x => new SubAssetKey(x), x => x as Object);
using (var context = new VRMImporterContext(parser, map))
{

View File

@ -54,7 +54,7 @@ namespace VRM
{
var map = texturePaths
.Select(x => x.LoadAsset<Texture>())
.ToDictionary(SubAssetKey.FromTexture, x => x as UnityEngine.Object);
.ToDictionary(x => new SubAssetKey(x), x => x as UnityEngine.Object);
using (var context = new VRMImporterContext(parser, map))
{

View File

@ -77,7 +77,7 @@ namespace VRM
Assert.AreEqual("Alicia_body", materialParam.TextureSlots["_MainTex"].UnityObjectName);
var (key, value) = materialParam.EnumerateSubAssetKeyValue().First();
Assert.AreEqual(new SubAssetKey(typeof(Texture2D), "Alicia_body"), key);
Assert.AreEqual(new SubAssetKey(typeof(Texture), "Alicia_body"), key);
}
}
}

View File

@ -95,9 +95,10 @@ namespace UniVRM10
//
// Import(create unity objects)
//
var externalObjectMap = scriptedImporter.GetExternalObjectMap().ToDictionary(kv => new SubAssetKey(kv.Key.type, kv.Key.name), kv => kv.Value);
var extractedObjects = scriptedImporter.GetExternalObjectMap()
.ToDictionary(kv => new SubAssetKey(kv.Value.GetType(), kv.Value.name), kv => kv.Value);
using (var loader = new Vrm10Importer(parser, externalObjectMap))
using (var loader = new Vrm10Importer(parser, extractedObjects))
{
// settings TextureImporters
foreach (var (key, textureInfo) in Vrm10TextureEnumerator.EnumerateAllTexturesDistinct(parser))

View File

@ -19,7 +19,7 @@ namespace UniVRM10
}
var usedTextures = new HashSet<SubAssetKey>();
// Thumbnail Texture referenced by VRM Meta.
if (TryGetMetaThumbnailTextureImportParam(parser, vrm, out (SubAssetKey key, TextureImportParam) thumbnail))
{
@ -41,7 +41,7 @@ namespace UniVRM10
}
}
}
/// <summary>
/// VRM-1 の thumbnail テクスチャー。gltf.textures ではなく gltf.images の参照であることに注意(sampler等の設定が無い)
/// </summary>
@ -66,11 +66,10 @@ namespace UniVRM10
getThumbnailImageBytesAsync, default, default,
default, default, default
);
var key = new SubAssetKey(typeof(Texture2D), name);
value = (key, param);
value = (param.SubAssetKey, param);
return true;
}
/// <summary>
/// Material によって参照されている Texture を Enumerate する.
/// まず VRM Material だと仮定して処理し, 失敗すれば glTF Material だとして処理する.
@ -95,4 +94,4 @@ namespace UniVRM10
}
}
}
}
}

View File

@ -16,7 +16,7 @@ namespace VRMShaders
public int? RenderQueue;
public readonly List<Action<Material>> Actions = new List<Action<Material>>();
public SubAssetKey SubAssetKey => SubAssetKey.FromMaterial(Name);
public SubAssetKey SubAssetKey => new SubAssetKey(SubAssetKey.MaterialType, Name);
public MaterialImportParam(string name, string shaderName)
{

View File

@ -18,38 +18,54 @@ namespace VRMShaders
/// の呼び出し時に、identifier.name と externalObject.name が同じでない運用にしてみる。
///
/// </summary>
public struct SubAssetKey : IEquatable<SubAssetKey>
public readonly struct SubAssetKey : IEquatable<SubAssetKey>
{
public static SubAssetKey FromTexture(Texture texture)
{
return FromTexture(texture.name);
}
public static SubAssetKey FromTexture(string textureName)
{
return new SubAssetKey(typeof(Texture2D), textureName);
}
public static SubAssetKey FromMaterial(Material material)
{
return FromMaterial(material.name);
}
public static SubAssetKey FromMaterial(string materialName)
{
return new SubAssetKey(typeof(Material), materialName);
}
public static readonly Type TextureType = typeof(Texture);
public static readonly Type MaterialType = typeof(Material);
public readonly Type Type;
public readonly string Name;
public SubAssetKey(Type t, string name)
public SubAssetKey(Texture obj)
{
if (string.IsNullOrEmpty(name))
if (obj == null || string.IsNullOrEmpty(obj.name))
{
throw new System.ArgumentNullException();
}
Type = t;
Type = TextureType;
Name = obj.name;
}
public SubAssetKey(Material obj)
{
if (obj == null || string.IsNullOrEmpty(obj.name))
{
throw new System.ArgumentNullException();
}
Type = MaterialType;
Name = obj.name;
}
public SubAssetKey(Type type, string name)
{
if (type == null || string.IsNullOrEmpty(name))
{
throw new System.ArgumentNullException();
}
if (!type.IsSubclassOf(typeof(UnityEngine.Object)))
{
throw new System.ArgumentException($"{type}:{name}");
}
if (type.IsSubclassOf(TextureType))
{
type = TextureType;
}
Type = type;
Name = name;
}

View File

@ -50,7 +50,7 @@ namespace VRMShaders
/// <summary>
/// この Texture が Unity に実アセットとして存在する際の一意な Key
/// </summary>
public SubAssetKey SubAssetKey => SubAssetKey.FromTexture(UnityObjectName);
public SubAssetKey SubAssetKey => new SubAssetKey(SubAssetKey.TextureType, UnityObjectName);
public TextureImportParam(string name, string ext, string uri, Vector2 offset, Vector2 scale, SamplerParam sampler, TextureImportTypes textureType, float metallicFactor, float roughnessFactor,
GetTextureBytesAsync i0,