Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle SubAssetKey instead of string strictly #974

Merged
merged 8 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Strict SubAssetKey constructor
  • Loading branch information
Santarh committed May 25, 2021
commit b7593254d90030f138e0d3822f5b10a5c2403671
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ public static void Import(ScriptedImporter scriptedImporter, AssetImportContext
//
// 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))
Expand All @@ -60,55 +65,5 @@ public static void Import(ScriptedImporter scriptedImporter, AssetImportContext
});
}
}

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);
}
}
}
2 changes: 1 addition & 1 deletion Assets/VRM/Editor/Format/VRMImporterMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static void ImportAsset(string path, UnityPath prefabPath)
//
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))
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void ImportVrm(UnityPath vrmPath)
{
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))
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM/Tests/MToonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void MaterialImporterTest()
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ public static void Import(ScriptedImporter scriptedImporter, AssetImportContext
//
// 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))
Expand Down
11 changes: 5 additions & 6 deletions Assets/VRM10/Runtime/IO/Texture/Vrm10TextureEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class Vrm10TextureEnumerator
}

var usedTextures = new HashSet<SubAssetKey>();

// Thumbnail Texture referenced by VRM Meta.
if (TryGetMetaThumbnailTextureImportParam(parser, vrm, out (SubAssetKey key, TextureImportParam) thumbnail))
{
Expand All @@ -41,7 +41,7 @@ public static class Vrm10TextureEnumerator
}
}
}

/// <summary>
/// VRM-1 の thumbnail テクスチャー。gltf.textures ではなく gltf.images の参照であることに注意(sampler等の設定が無い)
/// </summary>
Expand All @@ -66,11 +66,10 @@ public static bool TryGetMetaThumbnailTextureImportParam(GltfParser parser, UniG
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 だとして処理する.
Expand All @@ -95,4 +94,4 @@ public static bool TryGetMetaThumbnailTextureImportParam(GltfParser parser, UniG
}
}
}
}
}
2 changes: 1 addition & 1 deletion Assets/VRMShaders/GLTF/IO/Runtime/MaterialImportParam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class MaterialImportParam
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)
{
Expand Down
54 changes: 35 additions & 19 deletions Assets/VRMShaders/GLTF/IO/Runtime/SubAssetKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 readonly Type TextureType = typeof(Texture);
public static readonly Type MaterialType = typeof(Material);

public static SubAssetKey FromTexture(string textureName)
{
return new SubAssetKey(typeof(Texture2D), textureName);
}
public readonly Type Type;
public readonly string Name;

public static SubAssetKey FromMaterial(Material material)
public SubAssetKey(Texture obj)
{
return FromMaterial(material.name);
if (obj == null || string.IsNullOrEmpty(obj.name))
{
throw new System.ArgumentNullException();
}

Type = TextureType;
Name = obj.name;
}

public static SubAssetKey FromMaterial(string materialName)
public SubAssetKey(Material obj)
{
return new SubAssetKey(typeof(Material), materialName);
}
if (obj == null || string.IsNullOrEmpty(obj.name))
{
throw new System.ArgumentNullException();
}

public readonly Type Type;
public readonly string Name;
Type = MaterialType;
Name = obj.name;
}

public SubAssetKey(Type t, string name)
public SubAssetKey(Type type, string name)
{
if (string.IsNullOrEmpty(name))
if (type == null || string.IsNullOrEmpty(name))
{
throw new System.ArgumentNullException();
}
Type = t;

if (!type.IsSubclassOf(typeof(UnityEngine.Object)))
{
throw new System.ArgumentException($"{type}:{name}");
}

if (type.IsSubclassOf(TextureType))
{
type = TextureType;
}

Type = type;
Name = name;
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/VRMShaders/GLTF/IO/Runtime/TextureImportParam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public struct TextureImportParam
/// <summary>
/// この Texture が Unity に実アセットとして存在する際の一意な Key
/// </summary>
public SubAssetKey SubAssetKey => SubAssetKey.FromTexture(UnityObjectName);
public SubAssetKey SubAssetKey => new SubAssetKey(SubAssetKey.TextureType, UnityObjectName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


public TextureImportParam(string name, string ext, string uri, Vector2 offset, Vector2 scale, SamplerParam sampler, TextureImportTypes textureType, float metallicFactor, float roughnessFactor,
GetTextureBytesAsync i0,
Expand Down