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

Fix error when material count was zero in VRM #1084

Merged
merged 2 commits into from
Jun 30, 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
13 changes: 12 additions & 1 deletion Assets/VRM/Runtime/IO/VRMMToonMaterialImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ public static class VRMMToonMaterialImporter
{
public static bool TryCreateParam(GltfData data, glTF_VRM_extensions vrm, int materialIdx, out MaterialDescriptor matDesc)
{
if (vrm?.materialProperties == null || vrm.materialProperties.Count == 0)
{
matDesc = default;
return false;
}
if (materialIdx < 0 || materialIdx >= vrm.materialProperties.Count)
{
matDesc = default;
return false;
}

var vrmMaterial = vrm.materialProperties[materialIdx];
if (vrmMaterial.shader == VRM.glTF_VRM_Material.VRM_USE_GLTFSHADER)
{
Expand Down Expand Up @@ -42,7 +53,7 @@ public static bool TryCreateParam(GltfData data, glTF_VRM_extensions vrm, int ma

foreach (var kv in vrmMaterial.textureProperties)
{
if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrm, materialIdx, kv.Key, out var texture))
if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial, kv.Key, out var texture))
{
matDesc.TextureSlots.Add(kv.Key, texture.Item2);
}
Expand Down
12 changes: 4 additions & 8 deletions Assets/VRM/Runtime/IO/VRMMToonTextureImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@ public static class VRMMToonTextureImporter
var vrmMaterial = vrm.materialProperties[materialIdx];
foreach (var kv in vrmMaterial.textureProperties)
{
if (TryGetTextureFromMaterialProperty(data, vrm, materialIdx, kv.Key, out var texture))
if (TryGetTextureFromMaterialProperty(data, vrmMaterial, kv.Key, out var texture))
{
yield return texture;
}
}
}

public static bool TryGetTextureFromMaterialProperty(GltfData data, glTF_VRM_extensions vrm, int materialIdx, string textureKey, out (SubAssetKey, TextureDescriptor) texture)
public static bool TryGetTextureFromMaterialProperty(GltfData data, glTF_VRM_Material vrmMaterial, string textureKey, out (SubAssetKey, TextureDescriptor) texture)
{
var vrmMaterial = vrm.materialProperties[materialIdx];
// 任意の shader の import を許容する
if (/*vrmMaterial.shader == MToon.Utils.ShaderName &&*/ vrmMaterial.textureProperties.TryGetValue(textureKey, out var textureIdx))
{
var (offset, scale) = (new Vector2(0, 0), new Vector2(1, 1));
if (TryGetTextureOffsetAndScale(vrm, materialIdx, textureKey, out var os))
if (TryGetTextureOffsetAndScale(vrmMaterial, textureKey, out var os))
{
offset = os.offset;
scale = os.scale;
Expand All @@ -48,10 +46,8 @@ public static bool TryGetTextureFromMaterialProperty(GltfData data, glTF_VRM_ext
return false;
}

public static bool TryGetTextureOffsetAndScale(glTF_VRM_extensions vrm, int materialIdx, string unityTextureKey, out (Vector2 offset, Vector2 scale) os)
public static bool TryGetTextureOffsetAndScale(glTF_VRM_Material vrmMaterial, string unityTextureKey, out (Vector2 offset, Vector2 scale) os)
{
var vrmMaterial = vrm.materialProperties[materialIdx];

if (vrmMaterial.vectorProperties.TryGetValue(unityTextureKey, out var vector))
{
os = (new Vector2(vector[0], vector[1]), new Vector2(vector[2], vector[3]));
Expand Down