Skip to content

Commit

Permalink
Merge pull request #1084 from Santarh/fixMatZero
Browse files Browse the repository at this point in the history
Fix error when material count was zero in VRM
  • Loading branch information
ousttrue authored Jun 30, 2021
2 parents 8b645b0 + 5bfa273 commit d504482
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
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

0 comments on commit d504482

Please sign in to comment.