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

[1.0] thumbnail の名前が無い時に SubAssetKey を作るのに失敗するのを修正 #1193

Merged
merged 5 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 16 additions & 2 deletions Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UniGLTF;
using UnityEngine;
Expand Down Expand Up @@ -68,6 +69,8 @@ public TextureDescriptorSet Get()
}
}

public const string THUMBNAIL_NAME = "__VRM10_thumbnail__";

/// <summary>
/// VRM-1 の thumbnail テクスチャー。gltf.textures ではなく gltf.images の参照であることに注意(sampler等の設定が無い)
/// </summary>
Expand All @@ -81,14 +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);

// data.GLTF.textures は前処理によりユニーク性がある
// unique な名前を振り出す
var used = new HashSet<string>(data.GLTF.textures.Select(x => x.name));
var imageName = gltfImage.name;
if (string.IsNullOrEmpty(imageName))
{
imageName = THUMBNAIL_NAME;
}
var uniqueName = GlbLowLevelParser.FixNameUnique(used, imageName);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FixNameUnique で thumbnail 用の名前を作り出す !

Copy link
Contributor

Choose a reason for hiding this comment

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

Specified な実装だが、致し方なし


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
);
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM10/Runtime/Migration/MigrationVrm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static byte[] Migrate(JsonNode json, ArraySegment<byte> 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"]);

Expand Down
18 changes: 16 additions & 2 deletions Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -54,7 +54,21 @@ public static UniGLTF.Extensions.VRMC_vrm.Meta Migrate(JsonNode vrm0)
case "author": meta.Authors = new List<string>() { kv.Value.GetString() }; break;
case "contactInformation": meta.ContactInformation = kv.Value.GetString(); break;
case "reference": meta.References = new List<string>() { 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();
if (textureIndex == -1)
{
meta.ThumbnailImage = -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

サムネがない

}
else
{
var gltfTexture = gltf.textures[textureIndex];
meta.ThumbnailImage = gltfTexture.source;
Copy link
Contributor

Choose a reason for hiding this comment

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

サムネがある

}
break;
}

case "allowedUserName":
{
Expand Down
29 changes: 2 additions & 27 deletions Assets/VRM10/Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.IO;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
Expand All @@ -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<VRM10Controller>();
}

}

[Test]
public void DuplicatedMaterialColorBindings()
{
var controller = Load();
var controller = TestAsset.LoadAlicia();

var src = controller.Vrm.Expression.Aa.MaterialColorBindings.ToList();

Expand Down Expand Up @@ -63,7 +38,7 @@ public void DuplicatedMaterialColorBindings()
[Test]
public void DuplicatedMaterialUVBindings()
{
var controller = Load();
var controller = TestAsset.LoadAlicia();

var renderers = controller.GetComponentsInChildren<Renderer>();
var name = renderers[0].sharedMaterials[0].name;
Expand Down
23 changes: 23 additions & 0 deletions Assets/VRM10/Tests/LoadTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

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

image の name が null でもロードできる


using (var loader = new Vrm10Importer(vrm))
{
loader.LoadAsync(new VRMShaders.ImmediateCaller()).Wait();
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/VRM10/Tests/LoadTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Assets/VRM10/Tests/TestAsset.cs
Original file line number Diff line number Diff line change
@@ -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<VRM10Controller>();
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/VRM10/Tests/TestAsset.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.