MeshExportValidator.Validate

This commit is contained in:
ousttrue 2021-05-06 14:12:59 +09:00
parent 3bae81d3d2
commit 8629531284
6 changed files with 144 additions and 41 deletions

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniGLTF.M17N;
using UnityEngine;
namespace UniGLTF
@ -9,6 +10,12 @@ namespace UniGLTF
[Serializable]
public class MeshExportValidator : ScriptableObject
{
public enum Messages
{
MATERIALS_CONTAINS_NULL,
UNKNOWN_SHADER,
}
public static Mesh GetMesh(Renderer r)
{
if (r is SkinnedMeshRenderer smr)
@ -30,10 +37,6 @@ namespace UniGLTF
public int ExpectedExportByteSize => Meshes.Where(x => x.IsRendererActive).Sum(x => x.ExportByteSize);
List<Validation> m_validations = new List<Validation>();
public IEnumerable<Validation> Validations => m_validations;
public MeshExportSettings Settings;
public virtual bool UseBlendShape(int index, string relativePath) => true;
@ -164,7 +167,6 @@ namespace UniGLTF
public void SetRoot(GameObject ExportRoot, MeshExportSettings settings)
{
Settings = settings;
m_validations.Clear();
Meshes.Clear();
if (ExportRoot == null)
{
@ -179,5 +181,49 @@ namespace UniGLTF
}
}
}
public Func<string, string> GltfMaterialFromUnityShaderName = DefaultGltfMaterialType;
public static string DefaultGltfMaterialType(string shaderName)
{
if (shaderName == "Standard")
{
return "pbr";
}
if (MaterialExporter.IsUnlit(shaderName))
{
return "unlit";
}
return null;
}
public IEnumerable<Validation> Validate(GameObject ExportRoot)
{
foreach (var info in Meshes)
{
// invalid materials.len
// material null
if (info.Renderer.sharedMaterials.Any(x => x == null))
{
yield return Validation.Error($"{info.Renderer}: {Messages.MATERIALS_CONTAINS_NULL.Msg()}");
}
}
foreach (var m in Meshes.SelectMany(x => x.Renderer.sharedMaterials).Distinct())
{
if (m == null)
{
continue;
}
var gltfMaterial = GltfMaterialFromUnityShaderName(m.shader.name);
if (string.IsNullOrEmpty(gltfMaterial))
{
yield return Validation.Warning($"{m}: unknown shader: {m.shader.name} => export as gltf default");
}
}
yield break;
}
}
}

View File

@ -196,6 +196,22 @@ namespace UniGLTF
}
}
public static bool IsUnlit(string shaderName)
{
switch (shaderName)
{
case "Unlit/Color":
case "Unlit/Texture":
case "Unlit/Transparent":
case "Unlit/Transparent Cutout":
case "UniGLTF/UniUnlit":
return true;
default:
return false;
}
}
protected virtual glTFMaterial CreateMaterial(Material m)
{
switch (m.shader.name)

View File

@ -216,7 +216,7 @@ namespace UniGLTF
public virtual void ExportExtensions(Func<Texture2D, (byte[], string)> getTextureBytes)
{
// do nothing
}
public virtual void Export(MeshExportSettings meshExportSettings, Func<Texture, bool> useAsset, Func<Texture2D, (byte[], string)> getTextureBytes)

View File

@ -124,18 +124,37 @@ namespace VRM
protected override IEnumerable<Validator> ValidatorFactory()
{
HumanoidValidator.MeshInformations = m_meshes.Meshes;
HumanoidValidator.EnableFreeze = m_settings.PoseFreeze;
VRMExporterValidator.ReduceBlendshape = m_settings.ReduceBlendshape;
// ヒエラルキー のチェック
yield return HierarchyValidator.Validate;
if (!State.ExportRoot)
{
// Root が無い
yield break;
}
// Mesh/Renderer のチェック
m_meshes.GltfMaterialFromUnityShaderName = (string shaderName) =>
{
var name = VRMMaterialExporter.VrmMaterialName(shaderName);
if (!string.IsNullOrEmpty(name))
{
return name;
}
return MeshExportValidator.DefaultGltfMaterialType(shaderName);
};
yield return m_meshes.Validate;
// Humanoid のチェック
HumanoidValidator.MeshInformations = m_meshes.Meshes;
HumanoidValidator.EnableFreeze = m_settings.PoseFreeze;
yield return HumanoidValidator.Validate;
//
// VRM のチェック
//
VRMExporterValidator.ReduceBlendshape = m_settings.ReduceBlendshape;
yield return VRMExporterValidator.Validate;
yield return VRMSpringBoneValidator.Validate;
var firstPerson = State.ExportRoot.GetComponent<VRMFirstPerson>();
@ -160,6 +179,8 @@ namespace VRM
m_meshes.SetRoot(State.ExportRoot, m_settings);
}
static bool s_foldT = true;
protected override bool DoGUI(bool isValid)
{
if (State.ExportRoot == null)
@ -174,42 +195,46 @@ namespace VRM
{
var backup = GUI.enabled;
GUI.enabled = State.ExportRoot.scene.IsValid();
if (GUI.enabled)
{
EditorGUILayout.HelpBox(EnableTPose.ENALBE_TPOSE_BUTTON.Msg(), MessageType.Info);
}
else
{
EditorGUILayout.HelpBox(EnableTPose.DISABLE_TPOSE_BUTTON.Msg(), MessageType.Warning);
}
//
// T-Pose
//
if (GUILayout.Button(VRMExportSettingsEditor.Options.DO_TPOSE.Msg()))
if (s_foldT = EditorGUILayout.Foldout(s_foldT, "T-Pose"))
{
if (State.ExportRoot != null)
if (GUI.enabled)
{
// fallback
Undo.RecordObjects(State.ExportRoot.GetComponentsInChildren<Transform>(), "tpose");
VRMBoneNormalizer.EnforceTPose(State.ExportRoot);
Repaint();
EditorGUILayout.HelpBox(EnableTPose.ENALBE_TPOSE_BUTTON.Msg(), MessageType.Info);
}
}
if (GUILayout.Button(VRMExportSettingsEditor.Options.DO_TPOSE.Msg() + "(unity internal)"))
{
if (State.ExportRoot != null)
else
{
Undo.RecordObjects(State.ExportRoot.GetComponentsInChildren<Transform>(), "tpose.internal");
if (InternalTPose.TryMakePoseValid(State.ExportRoot))
EditorGUILayout.HelpBox(EnableTPose.DISABLE_TPOSE_BUTTON.Msg(), MessageType.Warning);
}
//
// T-Pose
//
if (GUILayout.Button(VRMExportSettingsEditor.Options.DO_TPOSE.Msg()))
{
if (State.ExportRoot != null)
{
// done
// fallback
Undo.RecordObjects(State.ExportRoot.GetComponentsInChildren<Transform>(), "tpose");
VRMBoneNormalizer.EnforceTPose(State.ExportRoot);
Repaint();
}
else
}
if (GUILayout.Button(VRMExportSettingsEditor.Options.DO_TPOSE.Msg() + "(unity internal)"))
{
if (State.ExportRoot != null)
{
Debug.LogWarning("not found");
Undo.RecordObjects(State.ExportRoot.GetComponentsInChildren<Transform>(), "tpose.internal");
if (InternalTPose.TryMakePoseValid(State.ExportRoot))
{
// done
Repaint();
}
else
{
Debug.LogWarning("not found");
}
}
}
}
@ -222,9 +247,6 @@ namespace VRM
return false;
}
EditorGUILayout.HelpBox($"Mesh size: {m_meshes.ExpectedExportByteSize / 1000000.0f:0.0} MByte", MessageType.Info);
//
// GUI
//
@ -285,6 +307,7 @@ namespace VRM
break;
case Tabs.Mesh:
EditorGUILayout.HelpBox($"Mesh size: {m_meshes.ExpectedExportByteSize / 1000000.0f:0.0} MByte", MessageType.Info);
m_meshesInspector.OnInspectorGUI();
break;

View File

@ -10,6 +10,24 @@ namespace VRM
{
public class VRMMaterialExporter : MaterialExporter
{
public static string VrmMaterialName(string shaderName)
{
switch (shaderName)
{
case "VRM/UnlitTexture":
case "VRM/UnlitTransparent":
case "VRM/UnlitCutout":
case "VRM/UnlitTransparentZWrite":
return "KHR_materials_unlit";
case "VRM/MToon":
return "MToon";
default:
return null;
}
}
protected override glTFMaterial CreateMaterial(Material m)
{
switch (m.shader.name)

View File

@ -57,7 +57,7 @@ namespace UniVRM10
{
foreach (var material in renderer.sharedMaterials)
{
if (!materialNameMap.ContainsKey(material.name))
if (material != null && !materialNameMap.ContainsKey(material.name))
{
materialNameMap.Add(material.name, material);
}