From 949265226fc952dfd5761621b88010a09ce5f6fe Mon Sep 17 00:00:00 2001 From: mkc1370 Date: Wed, 26 Oct 2022 15:30:26 +0900 Subject: [PATCH] Make unsupported shaders visible --- .../Expression/Preview/PreviewMaterialUtil.cs | 23 +++++++++++--- .../Expression/Preview/PreviewSceneManager.cs | 9 +++++- .../Expression/PreviewMaterialItem.cs | 31 +++++++++++-------- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMaterialUtil.cs b/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMaterialUtil.cs index cbec604ac..bfa7cb9dd 100644 --- a/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMaterialUtil.cs +++ b/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMaterialUtil.cs @@ -9,9 +9,9 @@ namespace UniVRM10 { public static class PreviewMaterialUtil { - public static PreviewMaterialItem CreateForPreview(Material material) + public static bool TryCreateForPreview(Material material, out PreviewMaterialItem item) { - var item = new PreviewMaterialItem(material); + item = new PreviewMaterialItem(material); var propNames = new List(); for (int i = 0; i < ShaderUtil.GetPropertyCount(material.shader); ++i) @@ -24,11 +24,24 @@ namespace UniVRM10 case ShaderUtil.ShaderPropertyType.Color: // 色 { - var bindType = PreviewMaterialItem.GetBindType(name); + if (!PreviewMaterialItem.TryGetBindType(name, out var bindType)) + { + Debug.LogError($"{material.shader.name}.{name} is unsupported property name"); + item = null; + return false; + } + + if (!Enum.TryParse(propType.ToString(), true, out ShaderPropertyType propertyType)) + { + Debug.LogError($"{material.shader.name}.{propertyType.ToString()} is unsupported property type"); + item = null; + return false; + } + item.PropMap.Add(bindType, new PropItem { Name = name, - PropertyType = (UniVRM10.ShaderPropertyType)Enum.Parse(typeof(UniVRM10.ShaderPropertyType), propType.ToString(), true), + PropertyType = propertyType, DefaultValues = material.GetColor(name), }); propNames.Add(name); @@ -40,7 +53,7 @@ namespace UniVRM10 } } item.PropNames = propNames.ToArray(); - return item; + return true; } } } diff --git a/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewSceneManager.cs b/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewSceneManager.cs index f7aa1ce82..ba163dd6b 100644 --- a/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewSceneManager.cs +++ b/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewSceneManager.cs @@ -94,10 +94,17 @@ namespace UniVRM10 { dst = new Material(src); map.Add(src, dst); + + if (!PreviewMaterialUtil.TryCreateForPreview(dst, out var previewMaterialItem)) + { + // Return cloned material for preview + return dst; + } + + m_materialMap.Add(src.name, previewMaterialItem); //Debug.LogFormat("add material {0}", src.name); materialNames.Add(src.name); - m_materialMap.Add(src.name, PreviewMaterialUtil.CreateForPreview(dst)); } return dst; }; diff --git a/Assets/VRM10/Runtime/Components/Expression/PreviewMaterialItem.cs b/Assets/VRM10/Runtime/Components/Expression/PreviewMaterialItem.cs index 38d86deee..460f14809 100644 --- a/Assets/VRM10/Runtime/Components/Expression/PreviewMaterialItem.cs +++ b/Assets/VRM10/Runtime/Components/Expression/PreviewMaterialItem.cs @@ -85,34 +85,39 @@ namespace UniVRM10 public static readonly string SHADE_COLOR_PROPERTY = MToon10Prop.ShadeColorFactor.ToUnityShaderLabName(); public static readonly string MATCAP_COLOR_PROPERTY = MToon10Prop.MatcapColorFactor.ToUnityShaderLabName(); - public static MaterialColorType GetBindType(string property) + public static bool TryGetBindType(string property, out MaterialColorType type) { if (property == COLOR_PROPERTY) { - return MaterialColorType.color; + type = MaterialColorType.color; } - if (property == EMISSION_COLOR_PROPERTY) + else if (property == EMISSION_COLOR_PROPERTY) { - return MaterialColorType.emissionColor; + type = MaterialColorType.emissionColor; } - if (property == RIM_COLOR_PROPERTY) + else if (property == RIM_COLOR_PROPERTY) { - return MaterialColorType.rimColor; + type = MaterialColorType.rimColor; } - if (property == OUTLINE_COLOR_PROPERTY) + else if (property == OUTLINE_COLOR_PROPERTY) { - return MaterialColorType.outlineColor; + type = MaterialColorType.outlineColor; } - if (property == SHADE_COLOR_PROPERTY) + else if (property == SHADE_COLOR_PROPERTY) { - return MaterialColorType.shadeColor; + type = MaterialColorType.shadeColor; } - if (property == MATCAP_COLOR_PROPERTY) + else if (property == MATCAP_COLOR_PROPERTY) { - return MaterialColorType.matcapColor; + type = MaterialColorType.matcapColor; + } + else + { + type = default; + return false; } - throw new NotImplementedException(); + return true; } ///