Skip to content

Commit

Permalink
Merge pull request vrm-c#1002 from ousttrue/feature/vertex_color_state
Browse files Browse the repository at this point in the history
VertexColorState を格上げ
  • Loading branch information
PoChang007 authored Jun 2, 2021
2 parents aca501f + ca8a04d commit 909ee48
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public virtual void CalcMeshSize(ref MeshExportInfo info,
sb.Append($") x {info.Mesh.vertexCount}");
switch (info.VertexColor)
{
case MeshExportInfo.VertexColorState.ExistsAndIsUsed:
case MeshExportInfo.VertexColorState.ExistsAndMixed: // エクスポートする
case VertexColorState.ExistsAndIsUsed:
case VertexColorState.ExistsAndMixed: // エクスポートする
sb.Insert(0, "[use vcolor]");
break;
case MeshExportInfo.VertexColorState.ExistsButNotUsed:
case VertexColorState.ExistsButNotUsed:
sb.Insert(0, "[remove vcolor]");
break;
}
Expand Down Expand Up @@ -150,7 +150,7 @@ bool TryGetMeshInfo(GameObject root, Renderer renderer, out MeshExportInfo info)
return false;
}

info.VertexColor = MeshExportInfo.DetectVertexColor(info.Mesh, info.Renderer.sharedMaterials);
info.VertexColor = VertexColorUtility.DetectVertexColor(info.Mesh, info.Renderer.sharedMaterials);

var relativePath = UniGLTF.UnityExtensions.RelativePathFrom(renderer.transform, root.transform);
CalcMeshSize(ref info, relativePath);
Expand Down
67 changes: 0 additions & 67 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,75 +21,8 @@ public struct MeshExportInfo

public bool HasSkinning => Mesh.boneWeights != null && Mesh.boneWeights.Length == Mesh.vertexCount;

/// <summary>
/// Mesh に頂点カラーが含まれているか。
/// 含まれている場合にマテリアルは Unlit.VColorMultiply になっているか?
/// </summary>
public enum VertexColorState
{
// VColorが存在しない
None,
// VColorが存在して使用している(UnlitはすべてVColorMultiply)
ExistsAndIsUsed,
// VColorが存在するが使用していない(UnlitはすべてVColorNone。もしくはUnlitが存在しない)
ExistsButNotUsed,
// VColorが存在して、Unlit.Multiply と Unlit.NotMultiply が混在している。 Unlit.NotMultiply を MToon か Standardに変更した方がよい
ExistsAndMixed,
}
public VertexColorState VertexColor;

static bool MaterialUseVertexColor(Material m)
{
if (m == null)
{
return false;
}
if (m.shader.name != UniGLTF.UniUnlit.Utils.ShaderName)
{
return false;
}
if (UniGLTF.UniUnlit.Utils.GetVColBlendMode(m) != UniGLTF.UniUnlit.UniUnlitVertexColorBlendOp.Multiply)
{
return false;
}
return true;
}

public static VertexColorState DetectVertexColor(Mesh mesh, Material[] materials)
{
if (mesh != null && mesh.colors != null && mesh.colors.Length == mesh.vertexCount)
{
// mesh が 頂点カラーを保持している
VertexColorState? state = default;
if (materials != null)
{
foreach (var m in materials)
{
var currentState = MaterialUseVertexColor(m)
? MeshExportInfo.VertexColorState.ExistsAndIsUsed
: MeshExportInfo.VertexColorState.ExistsButNotUsed
;
if (state.HasValue)
{
if (state.Value != currentState)
{
state = MeshExportInfo.VertexColorState.ExistsAndMixed;
break;
}
}
else
{
state = currentState;
}
}
}
return state.GetValueOrDefault(VertexColorState.None);
}
else
{
return VertexColorState.None;
}
}
public int VertexCount;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public static (glTFMesh, Dictionary<int, int> blendShapeIndexMap) Export(glTF gl

var colorAccessorIndex = -1;

var vColorState = MeshExportInfo.DetectVertexColor(mesh, materials);
if (vColorState == MeshExportInfo.VertexColorState.ExistsAndIsUsed // VColor使っている
|| vColorState == MeshExportInfo.VertexColorState.ExistsAndMixed // VColorを使っているところと使っていないところが混在(とりあえずExportする)
var vColorState = VertexColorUtility.DetectVertexColor(mesh, materials);
if (vColorState == VertexColorState.ExistsAndIsUsed // VColor使っている
|| vColorState == VertexColorState.ExistsAndMixed // VColorを使っているところと使っていないところが混在(とりあえずExportする)
)
{
// UniUnlit で Multiply 設定になっている
Expand Down
76 changes: 76 additions & 0 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/VertexColorState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using UnityEngine;

namespace UniGLTF
{
/// <summary>
/// Mesh に頂点カラーが含まれているか。
/// 含まれている場合にマテリアルは Unlit.VColorMultiply になっているか?
/// </summary>
public enum VertexColorState
{
// VColorが存在しない
None,
// VColorが存在して使用している(UnlitはすべてVColorMultiply)
ExistsAndIsUsed,
// VColorが存在するが使用していない(UnlitはすべてVColorNone。もしくはUnlitが存在しない)
ExistsButNotUsed,
// VColorが存在して、Unlit.Multiply と Unlit.NotMultiply が混在している。 Unlit.NotMultiply を MToon か Standardに変更した方がよい
ExistsAndMixed,
}

public static class VertexColorUtility
{
static bool MaterialUseVertexColor(Material m)
{
if (m == null)
{
return false;
}
if (m.shader.name != UniGLTF.UniUnlit.Utils.ShaderName)
{
return false;
}
if (UniGLTF.UniUnlit.Utils.GetVColBlendMode(m) != UniGLTF.UniUnlit.UniUnlitVertexColorBlendOp.Multiply)
{
return false;
}
return true;
}

public static VertexColorState DetectVertexColor(Mesh mesh, Material[] materials)
{
if (mesh != null && mesh.colors != null && mesh.colors.Length == mesh.vertexCount)
{
// mesh が 頂点カラーを保持している
VertexColorState? state = default;
if (materials != null)
{
foreach (var m in materials)
{
var currentState = MaterialUseVertexColor(m)
? VertexColorState.ExistsAndIsUsed
: VertexColorState.ExistsButNotUsed
;
if (state.HasValue)
{
if (state.Value != currentState)
{
state = VertexColorState.ExistsAndMixed;
break;
}
}
else
{
state = currentState;
}
}
}
return state.GetValueOrDefault(VertexColorState.None);
}
else
{
return VertexColorState.None;
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/VertexColorState.cs.meta

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

2 changes: 1 addition & 1 deletion Assets/VRM/Editor/Format/VRMExporterWizard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ protected override bool DoGUI(bool isValid)
{
switch (meshInfo.VertexColor)
{
case UniGLTF.MeshExportInfo.VertexColorState.ExistsAndMixed:
case UniGLTF.VertexColorState.ExistsAndMixed:
Validation.Warning($"{meshInfo.Renderer}: Both vcolor.multiply and not multiply unlit materials exist").DrawGUI();
break;
}
Expand Down

0 comments on commit 909ee48

Please sign in to comment.