From 543fb0b8bba4cb9478702d3db51ba94e6c38e722 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Sep 2021 14:58:27 +0900 Subject: [PATCH 1/5] add test --- Assets/VRM10/Tests/ExpressionTests.cs | 29 ++----------------------- Assets/VRM10/Tests/LoadTests.cs | 23 ++++++++++++++++++++ Assets/VRM10/Tests/LoadTests.cs.meta | 11 ++++++++++ Assets/VRM10/Tests/TestAsset.cs | 31 +++++++++++++++++++++++++++ Assets/VRM10/Tests/TestAsset.cs.meta | 11 ++++++++++ 5 files changed, 78 insertions(+), 27 deletions(-) create mode 100644 Assets/VRM10/Tests/LoadTests.cs create mode 100644 Assets/VRM10/Tests/LoadTests.cs.meta create mode 100644 Assets/VRM10/Tests/TestAsset.cs create mode 100644 Assets/VRM10/Tests/TestAsset.cs.meta diff --git a/Assets/VRM10/Tests/ExpressionTests.cs b/Assets/VRM10/Tests/ExpressionTests.cs index 5d2a865928..2095f44380 100644 --- a/Assets/VRM10/Tests/ExpressionTests.cs +++ b/Assets/VRM10/Tests/ExpressionTests.cs @@ -1,4 +1,3 @@ -using System.IO; using System.Linq; using NUnit.Framework; using UnityEngine; @@ -7,34 +6,10 @@ namespace UniVRM10.Test { public class ExpressionTests { - static string AliciaPath - { - get - { - return Path.GetFullPath(Application.dataPath + "/../Tests/Models/Alicia_vrm-0.51/AliciaSolid_vrm-0.51.vrm") - .Replace("\\", "/"); - } - } - - static VRM10Controller Load() - { - Vrm10Data.TryParseOrMigrate(AliciaPath, true, out Vrm10Data vrm); - using (var loader = new Vrm10Importer(vrm)) - { - var task = loader.LoadAsync(new VRMShaders.ImmediateCaller()); - task.Wait(); - - var instance = task.Result; - - return instance.GetComponent(); - } - - } - [Test] public void DuplicatedMaterialColorBindings() { - var controller = Load(); + var controller = TestAsset.LoadAlicia(); var src = controller.Vrm.Expression.Aa.MaterialColorBindings.ToList(); @@ -63,7 +38,7 @@ public void DuplicatedMaterialColorBindings() [Test] public void DuplicatedMaterialUVBindings() { - var controller = Load(); + var controller = TestAsset.LoadAlicia(); var renderers = controller.GetComponentsInChildren(); var name = renderers[0].sharedMaterials[0].name; diff --git a/Assets/VRM10/Tests/LoadTests.cs b/Assets/VRM10/Tests/LoadTests.cs new file mode 100644 index 0000000000..4f88785efb --- /dev/null +++ b/Assets/VRM10/Tests/LoadTests.cs @@ -0,0 +1,23 @@ +using NUnit.Framework; + +namespace UniVRM10.Test +{ + public class LoadTests + { + [Test] + public void EmptyThumbnailName() + { + Assert.True(Vrm10Data.TryParseOrMigrate(TestAsset.AliciaPath, true, out Vrm10Data vrm)); + + var index = vrm.VrmExtension.Meta.ThumbnailImage.Value; + + // empty thumbnail name + vrm.Data.GLTF.images[index].name = null; + + using (var loader = new Vrm10Importer(vrm)) + { + loader.LoadAsync(new VRMShaders.ImmediateCaller()).Wait(); + } + } + } +} diff --git a/Assets/VRM10/Tests/LoadTests.cs.meta b/Assets/VRM10/Tests/LoadTests.cs.meta new file mode 100644 index 0000000000..2dc13ac5f3 --- /dev/null +++ b/Assets/VRM10/Tests/LoadTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c9a9a3ce2deae9c4791d816aa189df3d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Tests/TestAsset.cs b/Assets/VRM10/Tests/TestAsset.cs new file mode 100644 index 0000000000..a9ef5e72c6 --- /dev/null +++ b/Assets/VRM10/Tests/TestAsset.cs @@ -0,0 +1,31 @@ +using System.IO; +using UnityEngine; + +namespace UniVRM10 +{ + public static class TestAsset + { + public static string AliciaPath + { + get + { + return Path.GetFullPath(Application.dataPath + "/../Tests/Models/Alicia_vrm-0.51/AliciaSolid_vrm-0.51.vrm") + .Replace("\\", "/"); + } + } + + public static VRM10Controller LoadAlicia() + { + Vrm10Data.TryParseOrMigrate(AliciaPath, true, out Vrm10Data vrm); + using (var loader = new Vrm10Importer(vrm)) + { + var task = loader.LoadAsync(new VRMShaders.ImmediateCaller()); + task.Wait(); + + var instance = task.Result; + + return instance.GetComponent(); + } + } + } +} diff --git a/Assets/VRM10/Tests/TestAsset.cs.meta b/Assets/VRM10/Tests/TestAsset.cs.meta new file mode 100644 index 0000000000..f9145dda79 --- /dev/null +++ b/Assets/VRM10/Tests/TestAsset.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b18f57dcd7d3c19469aa4842b2ee6ae1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From f0aedd454e0ed48b68096a670d692f026a62eff0 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Sep 2021 15:00:11 +0900 Subject: [PATCH 2/5] fallback default name --- .../Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs b/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs index 53fa5b0d11..293fe09801 100644 --- a/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs +++ b/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs @@ -82,6 +82,10 @@ public static bool TryGetMetaThumbnailTextureImportParam(GltfData data, UniGLTF. var imageIndex = vrm.Meta.ThumbnailImage.Value; var gltfImage = data.GLTF.images[imageIndex]; var name = TextureImportName.GetUnityObjectName(TextureImportTypes.sRGB, gltfImage.name, gltfImage.uri); + if (string.IsNullOrEmpty(name)) + { + name = "thumbnail"; + } GetTextureBytesAsync getThumbnailImageBytesAsync = () => { From c365dd41fc2cb778a50eb78ca0a9bc084936cdee Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Sep 2021 15:06:51 +0900 Subject: [PATCH 3/5] update thumbnail migration --- .../VRM10/Runtime/Migration/MigrationVrm.cs | 2 +- .../Runtime/Migration/MigrationVrmMeta.cs | 23 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrm.cs b/Assets/VRM10/Runtime/Migration/MigrationVrm.cs index da0bdfe142..5d027fa91e 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrm.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrm.cs @@ -38,7 +38,7 @@ public static byte[] Migrate(JsonNode json, ArraySegment bin) var vrm1 = new UniGLTF.Extensions.VRMC_vrm.VRMC_vrm(); // meta (required) - vrm1.Meta = MigrationVrmMeta.Migrate(vrm0["meta"]); + vrm1.Meta = MigrationVrmMeta.Migrate(gltf, vrm0["meta"]); // humanoid (required) vrm1.Humanoid = MigrationVrmHumanoid.Migrate(vrm0["humanoid"]); diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs b/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs index db803c9eaa..d2975f420b 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs @@ -27,7 +27,7 @@ namespace UniVRM10 // }, public static class MigrationVrmMeta { - public static UniGLTF.Extensions.VRMC_vrm.Meta Migrate(JsonNode vrm0) + public static UniGLTF.Extensions.VRMC_vrm.Meta Migrate(UniGLTF.glTF gltf, JsonNode vrm0) { var meta = new UniGLTF.Extensions.VRMC_vrm.Meta { @@ -54,7 +54,26 @@ public static UniGLTF.Extensions.VRMC_vrm.Meta Migrate(JsonNode vrm0) case "author": meta.Authors = new List() { kv.Value.GetString() }; break; case "contactInformation": meta.ContactInformation = kv.Value.GetString(); break; case "reference": meta.References = new List() { kv.Value.GetString() }; break; - case "texture": meta.ThumbnailImage = kv.Value.GetInt32(); break; + case "texture": + { + // vrm0x use texture. vrm10 use image + var textureIndex = kv.Value.GetInt32(); + var gltfTexture = gltf.textures[textureIndex]; + meta.ThumbnailImage = gltfTexture.source; + + var gltfImage = gltf.images[gltfTexture.source]; + if (string.IsNullOrEmpty(gltfImage.name)) + { + // use texture name + gltfImage.name = gltfTexture.name; + if (string.IsNullOrEmpty(gltfImage.name)) + { + // fall back default name + gltfImage.name = "__VRM10_thumbnail__"; + } + } + break; + } case "allowedUserName": { From 4aca030dd5039fee8a4feaaffd859efcd53b3be2 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Sep 2021 15:08:31 +0900 Subject: [PATCH 4/5] MigrationVrmMeta.THUMBNAIL_NAME --- .../Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs | 2 +- Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs b/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs index 293fe09801..d1bce792d2 100644 --- a/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs +++ b/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs @@ -84,7 +84,7 @@ public static bool TryGetMetaThumbnailTextureImportParam(GltfData data, UniGLTF. var name = TextureImportName.GetUnityObjectName(TextureImportTypes.sRGB, gltfImage.name, gltfImage.uri); if (string.IsNullOrEmpty(name)) { - name = "thumbnail"; + name = MigrationVrmMeta.THUMBNAIL_NAME; } GetTextureBytesAsync getThumbnailImageBytesAsync = () => diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs b/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs index d2975f420b..656e8f4ed4 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs @@ -27,6 +27,8 @@ namespace UniVRM10 // }, public static class MigrationVrmMeta { + public const string THUMBNAIL_NAME = "__VRM10_thumbnail__"; + public static UniGLTF.Extensions.VRMC_vrm.Meta Migrate(UniGLTF.glTF gltf, JsonNode vrm0) { var meta = new UniGLTF.Extensions.VRMC_vrm.Meta @@ -69,7 +71,7 @@ public static UniGLTF.Extensions.VRMC_vrm.Meta Migrate(UniGLTF.glTF gltf, JsonNo if (string.IsNullOrEmpty(gltfImage.name)) { // fall back default name - gltfImage.name = "__VRM10_thumbnail__"; + gltfImage.name = THUMBNAIL_NAME; } } break; From aad1b1252ea2df389d641b31a87c4dcea11ddc47 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Sep 2021 18:21:40 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=E6=96=B0=E3=81=97=E3=81=8F=20FixNameUnique?= =?UTF-8?q?=20=E3=81=A7=E5=90=8D=E5=89=8D=E3=82=92=E4=BD=9C=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Vrm10TextureDescriptorGenerator.cs | 18 ++++++++++++---- .../Runtime/Migration/MigrationVrmMeta.cs | 21 +++++++------------ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs b/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs index d1bce792d2..f8d7826f53 100644 --- a/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs +++ b/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using UniGLTF; using UnityEngine; @@ -68,6 +69,8 @@ public TextureDescriptorSet Get() } } + public const string THUMBNAIL_NAME = "__VRM10_thumbnail__"; + /// /// VRM-1 の thumbnail テクスチャー。gltf.textures ではなく gltf.images の参照であることに注意(sampler等の設定が無い) /// @@ -81,18 +84,25 @@ public static bool TryGetMetaThumbnailTextureImportParam(GltfData data, UniGLTF. var imageIndex = vrm.Meta.ThumbnailImage.Value; var gltfImage = data.GLTF.images[imageIndex]; - var name = TextureImportName.GetUnityObjectName(TextureImportTypes.sRGB, gltfImage.name, gltfImage.uri); - if (string.IsNullOrEmpty(name)) + + // data.GLTF.textures は前処理によりユニーク性がある + // unique な名前を振り出す + var used = new HashSet(data.GLTF.textures.Select(x => x.name)); + var imageName = gltfImage.name; + if (string.IsNullOrEmpty(imageName)) { - name = MigrationVrmMeta.THUMBNAIL_NAME; + imageName = THUMBNAIL_NAME; } + var uniqueName = GlbLowLevelParser.FixNameUnique(used, imageName); + + var objectName = TextureImportName.GetUnityObjectName(TextureImportTypes.sRGB, uniqueName, gltfImage.uri); GetTextureBytesAsync getThumbnailImageBytesAsync = () => { var bytes = data.GLTF.GetImageBytes(data.Storage, imageIndex); return Task.FromResult(GltfTextureImporter.ToArray(bytes)); }; - var texDesc = new TextureDescriptor(name, gltfImage.GetExt(), gltfImage.uri, Vector2.zero, Vector2.one, default, TextureImportTypes.sRGB, default, default, + var texDesc = new TextureDescriptor(objectName, gltfImage.GetExt(), gltfImage.uri, Vector2.zero, Vector2.one, default, TextureImportTypes.sRGB, default, default, getThumbnailImageBytesAsync, default, default, default, default, default ); diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs b/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs index 656e8f4ed4..5756bc275c 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs @@ -27,8 +27,6 @@ namespace UniVRM10 // }, public static class MigrationVrmMeta { - public const string THUMBNAIL_NAME = "__VRM10_thumbnail__"; - public static UniGLTF.Extensions.VRMC_vrm.Meta Migrate(UniGLTF.glTF gltf, JsonNode vrm0) { var meta = new UniGLTF.Extensions.VRMC_vrm.Meta @@ -60,19 +58,14 @@ public static UniGLTF.Extensions.VRMC_vrm.Meta Migrate(UniGLTF.glTF gltf, JsonNo { // vrm0x use texture. vrm10 use image var textureIndex = kv.Value.GetInt32(); - var gltfTexture = gltf.textures[textureIndex]; - meta.ThumbnailImage = gltfTexture.source; - - var gltfImage = gltf.images[gltfTexture.source]; - if (string.IsNullOrEmpty(gltfImage.name)) + if (textureIndex == -1) + { + meta.ThumbnailImage = -1; + } + else { - // use texture name - gltfImage.name = gltfTexture.name; - if (string.IsNullOrEmpty(gltfImage.name)) - { - // fall back default name - gltfImage.name = THUMBNAIL_NAME; - } + var gltfTexture = gltf.textures[textureIndex]; + meta.ThumbnailImage = gltfTexture.source; } break; }