Skip to content

Commit

Permalink
Merge pull request #1578 from ousttrue/fix10/unknown_material_validation
Browse files Browse the repository at this point in the history
Fix10/unknown material validation
  • Loading branch information
ousttrue authored Mar 14, 2022
2 parents be51a91 + 143175b commit fa4b934
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace UniGLTF
{
/// <summary>
/// MeshExportValidator から使われる
/// </summary>
public interface IMaterialValidator
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public IEnumerable<Validation> Validate(GameObject ExportRoot)
var gltfMaterial = MaterialValidator.GetGltfMaterialTypeFromUnityShaderName(m.shader.name);
if (string.IsNullOrEmpty(gltfMaterial))
{
yield return Validation.Warning($"{m}: unknown shader: {m.shader.name} => export as gltf default");
yield return Validation.Warning($"{m}: unknown shader: {m.shader.name} => export as gltf default", ValidationContext.Create(m));
}

var used = new HashSet<Texture>();
Expand Down
36 changes: 0 additions & 36 deletions Assets/VRM/Editor/Format/VRMExporterWizard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,42 +121,6 @@ protected override void Clear()
m_meshes = null;
}

/// <summary>
/// VRM0
/// </summary>
class VRMMaterialValidator : DefaultMaterialValidator
{
public override string GetGltfMaterialTypeFromUnityShaderName(string shaderName)
{
var name = VRMMaterialExporter.VrmMaterialName(shaderName);
if (!string.IsNullOrEmpty(name))
{
return name;
}
return base.GetGltfMaterialTypeFromUnityShaderName(shaderName);
}

public override IEnumerable<(string propertyName, Texture texture)> EnumerateTextureProperties(Material m)
{
if (m.shader.name != "VRM/MToon")
{
foreach (var x in base.EnumerateTextureProperties(m))
{
yield return x;
}
}

var prop = UniGLTF.ShaderPropExporter.PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
foreach (var kv in prop.Properties)
{
if (kv.ShaderPropertyType == UniGLTF.ShaderPropExporter.ShaderPropertyType.TexEnv)
{
yield return (kv.Key, m.GetTexture(kv.Key));
}
}
}
}

protected override IEnumerable<Validator> ValidatorFactory()
{
// ヒエラルキー のチェック
Expand Down
58 changes: 58 additions & 0 deletions Assets/VRM/Editor/Format/VRMMaterialValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Collections.Generic;
using UnityEngine;

namespace VRM
{
/// <summary>
/// VRM0
/// </summary>
class VRMMaterialValidator : UniGLTF.DefaultMaterialValidator
{
public override string GetGltfMaterialTypeFromUnityShaderName(string shaderName)
{
var name = VRMMaterialExporter.VrmMaterialName(shaderName);
if (!string.IsNullOrEmpty(name))
{
return name;
}
return base.GetGltfMaterialTypeFromUnityShaderName(shaderName);
}

public override IEnumerable<(string propertyName, Texture texture)> EnumerateTextureProperties(Material m)
{
/// 歴史的経緯により処理ロジックが2種類ある
if (m.shader.name == "VRM/MToon")
{
// [Unity列挙]
// * UnityEditor.ShaderUtil により Shader の Property を列挙する。Editor専用。
// * あらかじめEditorで実行して property 一覧をハードコーディングしてある `Assets\VRMShaders\VRM\IO\Runtime\VRM\PreExportShaders_VRM.cs` 界隈。
// * 今は "VRM/MToon" のみ
//
// extensions.VRM.materialProperties に記録する
//
var prop = UniGLTF.ShaderPropExporter.PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
foreach (var kv in prop.Properties)
{
if (kv.ShaderPropertyType == UniGLTF.ShaderPropExporter.ShaderPropertyType.TexEnv)
{
yield return (kv.Key, m.GetTexture(kv.Key));
}
}
}
else
{
// [Shaderの定義から手で記述]
//
// PBR, Unlit
// * DefaultMaterialValidator.EnumerateTextureProperties
//
// glTF.materials に記録する
//
foreach (var textureProperty in base.EnumerateTextureProperties(m))
{
yield return textureProperty;
}
}
}
}
}

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

5 changes: 1 addition & 4 deletions Assets/VRM10/Editor/Components/VRM10Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ namespace UniVRM10
/// </summary>
public class VRM10Window : EditorWindow
{
const string MENU_KEY = VRMVersion.MENU + "/VRM1 Window";
const string WINDOW_TITLE = "VRM1 Window";

[MenuItem(MENU_KEY, false, 1)]
private static void ExportFromMenu()
public static void Open()
{
var window = (VRM10Window)GetWindow(typeof(VRM10Window));
window.titleContent = new GUIContent(WINDOW_TITLE);
Expand Down
41 changes: 41 additions & 0 deletions Assets/VRM10/Editor/VRM10MaterialValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Collections.Generic;
using UnityEngine;

namespace UniVRM10
{
/// <summary>
/// VRM0
/// </summary>
class VRM10MaterialValidator : UniGLTF.DefaultMaterialValidator
{
const string MTOON_SHADER_NAME = "VRM10/MToon10";

public override string GetGltfMaterialTypeFromUnityShaderName(string shaderName)
{
switch (shaderName)
{
case MTOON_SHADER_NAME:
return "VRMC_materials_mtoon";
}

// TODO: VRM-0.X

return base.GetGltfMaterialTypeFromUnityShaderName(shaderName);
}

public override IEnumerable<(string propertyName, Texture texture)> EnumerateTextureProperties(Material m)
{
if (m.shader.name == MTOON_SHADER_NAME)
{
// TODO
}
else
{
foreach (var x in base.EnumerateTextureProperties(m))
{
yield return x;
}
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/VRM10/Editor/VRM10MaterialValidator.cs.meta

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

9 changes: 5 additions & 4 deletions Assets/VRM10/Editor/Vrm10ExportDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ namespace UniVRM10
{
public class VRM10ExportDialog : ExportDialogBase
{
const string CONVERT_HUMANOID_KEY = VRMVersion.MENU + "/Export VRM-1.0";

[MenuItem(CONVERT_HUMANOID_KEY, false, 0)]
private static void ExportFromMenu()
public static void Open()
{
var window = (VRM10ExportDialog)GetWindow(typeof(VRM10ExportDialog));
window.titleContent = new GUIContent("VRM-1.0 Exporter");
Expand Down Expand Up @@ -136,6 +133,10 @@ protected override IEnumerable<Validator> ValidatorFactory()
yield break;
}

// Mesh/Renderer のチェック
m_meshes.MaterialValidator = new VRM10MaterialValidator();
yield return m_meshes.Validate;

yield return HumanoidValidator.Validate_TPose;

// MeshUtility.Validators.HumanoidValidator.EnableFreeze = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,8 @@ namespace UniVRM10
/// は SubModuleになった。 `$ git submodule update --init` しておくこと。
///
/// </summary>
public static class Menu
public static class Vrm10SerializerGenerator
{
#if VRM_DEVELOP
[MenuItem(UniVRM10.VRMVersion.MENU + "/Generate from JsonSchema")]
public static void Generate()
{
Run(false);
}

[MenuItem(UniVRM10.VRMVersion.MENU + "/Generate from JsonSchema(debug)")]
public static void Parse()
{
Run(true);
}
#endif

struct GenerateInfo
{
public string JsonSchema;
Expand All @@ -49,7 +35,7 @@ public GenerateInfo(string jsonSchema, string formatDir) : this(jsonSchema, form

const string SPEC_DIR = "vrm-specification/specification";

static void Run(bool debug)
public static void Run(bool debug)
{
var projectRoot = new DirectoryInfo(Path.GetFullPath(Path.Combine(Application.dataPath, "../")));

Expand Down
11 changes: 11 additions & 0 deletions Assets/VRM10/Editor/Vrm10SerializerGenerator.cs.meta

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

25 changes: 25 additions & 0 deletions Assets/VRM10/Editor/Vrm10TopMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using UnityEditor;

namespace UniVRM10
{
public static class Vrm10TopMenu
{
private const string UserMenuPrefix = VRMVersion.MENU;
private const string DevelopmentMenuPrefix = VRMVersion.MENU + "/Development";

const string CONVERT_HUMANOID_KEY = VRMVersion.MENU + "/Export VRM-1.0";
[MenuItem(UserMenuPrefix + "/Export VRM-1.0", priority = 1)]
static void OpenExportDialog() => VRM10ExportDialog.Open();

#if VRM_DEVELOP
[MenuItem(UserMenuPrefix + "/VRM1 Window", false, 2)]
static void OpenWindow() => VRM10Window.Open();

[MenuItem(DevelopmentMenuPrefix + "/Generate from JsonSchema")]
public static void Generate() => Vrm10SerializerGenerator.Run(false);

[MenuItem(DevelopmentMenuPrefix + "/Generate from JsonSchema(debug)")]
public static void Parse() => Vrm10SerializerGenerator.Run(true);
#endif
}
}
11 changes: 11 additions & 0 deletions Assets/VRM10/Editor/Vrm10TopMenu.cs.meta

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

0 comments on commit fa4b934

Please sign in to comment.