From 14ea4befceb5d4e6cfeb355558e2ca4a8ed2363c Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 16 Jul 2021 17:38:51 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E6=95=B4=E7=90=86?= =?UTF-8?q?=E3=80=82IMaterialValidator.EnumerateTextureProperties=20?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Exporter がテクスチャー列挙という形式をとらずに全部展開してするという手法を取っているため、それほど共通化されない。 --- .../UniGLTF/ExportDialog/MaterialValidator.cs | 58 +++++++++++++++ .../ExportDialog/MaterialValidator.cs.meta | 11 +++ .../ExportDialog/MeshExportValidator.cs | 70 ++++++------------- .../UniGLTF/IO/MaterialIO/MaterialExporter.cs | 28 +++++--- Assets/VRM/Editor/Format/VRMExporterWizard.cs | 23 ++++++ Assets/VRM/Editor/VRM.Editor.asmdef | 3 +- 6 files changed, 131 insertions(+), 62 deletions(-) create mode 100644 Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MaterialValidator.cs create mode 100644 Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MaterialValidator.cs.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MaterialValidator.cs b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MaterialValidator.cs new file mode 100644 index 000000000..cd285f2b6 --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MaterialValidator.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace UniGLTF +{ + public interface IMaterialValidator + { + /// + /// shaderName から glTF マテリアルタイプ を得る + /// + /// shaderName が エクスポートできるものでないときは null を返す(gltfデフォルトの pbr として処理される) + /// + /// + /// + string GetGltfMaterialTypeFromUnityShaderName(string shaderName); + + /// + /// テクスチャーを使うプロパティを列挙する + /// + /// + /// + /// + IEnumerable<(string propertyName, Texture texture)> EnumerateTextureProperties(Material m); + } + + public class DefaultMaterialValidator : IMaterialValidator + { + public virtual string GetGltfMaterialTypeFromUnityShaderName(string shaderName) + { + if (shaderName == "Standard") + { + return "pbr"; + } + if (MaterialExporter.IsUnlit(shaderName)) + { + return "unlit"; + } + return null; + } + + public virtual IEnumerable<(string propertyName, Texture texture)> EnumerateTextureProperties(Material m) + { + // main color + yield return (MaterialExporter.COLOR_TEXTURE_PROP, m.GetTexture(MaterialExporter.COLOR_TEXTURE_PROP)); + + if (GetGltfMaterialTypeFromUnityShaderName(m.shader.name) == "unlit") + { + yield break; + } + + // PBR + yield return (MaterialExporter.METALLIC_TEX_PROP, m.GetTexture(MaterialExporter.METALLIC_TEX_PROP)); + yield return (MaterialExporter.NORMAL_TEX_PROP, m.GetTexture(MaterialExporter.NORMAL_TEX_PROP)); + yield return (MaterialExporter.EMISSION_TEX_PROP, m.GetTexture(MaterialExporter.EMISSION_TEX_PROP)); + yield return (MaterialExporter.OCCLUSION_TEX_PROP, m.GetTexture(MaterialExporter.OCCLUSION_TEX_PROP)); + } + } +} diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MaterialValidator.cs.meta b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MaterialValidator.cs.meta new file mode 100644 index 000000000..d46f5c32a --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MaterialValidator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6ba5723a5618eac438268641047d9549 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MeshExportValidator.cs b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MeshExportValidator.cs index af1e37f18..976a39ff4 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MeshExportValidator.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MeshExportValidator.cs @@ -7,34 +7,6 @@ using UnityEngine; namespace UniGLTF { - public interface IMaterialValidator - { - /// - /// shaderName から glTF マテリアルタイプ を得る - /// - /// shaderName が エクスポートできるものでないときは null を返す(gltfデフォルトの pbr として処理される) - /// - /// - /// - string GetGltfMaterialTypeFromUnityShaderName(string shaderName); - } - - public class DefaultMaterialValidator : IMaterialValidator - { - public virtual string GetGltfMaterialTypeFromUnityShaderName(string shaderName) - { - if (shaderName == "Standard") - { - return "pbr"; - } - if (MaterialExporter.IsUnlit(shaderName)) - { - return "unlit"; - } - return null; - } - } - [Serializable] public class MeshExportValidator : ScriptableObject { @@ -116,34 +88,32 @@ namespace UniGLTF yield return Validation.Warning($"{m}: unknown shader: {m.shader.name} => export as gltf default"); } - var count = ShaderUtil.GetPropertyCount(m.shader); - for (int i = 0; i < count; ++i) + var used = new HashSet(); + foreach (var (propName, texture) in MaterialValidator.EnumerateTextureProperties(m)) { - var propType = ShaderUtil.GetPropertyType(m.shader, i); - if (propType == ShaderUtil.ShaderPropertyType.TexEnv) + if (texture == null) { - var propName = ShaderUtil.GetPropertyName(m.shader, i); - var tex = m.GetTexture(propName); - if (tex != null) + continue; + } + var assetPath = AssetDatabase.GetAssetPath(texture); + if (!string.IsNullOrEmpty(assetPath)) + { + if (AssetImporter.GetAtPath(assetPath) is TextureImporter textureImporter) { - var assetPath = AssetDatabase.GetAssetPath(tex); - if (!string.IsNullOrEmpty(assetPath)) + switch (textureImporter.textureType) { - if (AssetImporter.GetAtPath(assetPath) is TextureImporter textureImporter) - { - switch (textureImporter.textureType) - { - case TextureImporterType.Default: - case TextureImporterType.NormalMap: - break; + case TextureImporterType.Default: + case TextureImporterType.NormalMap: + break; - default: - // EditorTextureSerializer throw Exception - // エクスポート未実装 - yield return Validation.Error($"{tex}: unknown texture type: {textureImporter.textureType}", ValidationContext.Create(tex)); - break; + default: + // EditorTextureSerializer throw Exception + // エクスポート未実装 + if (used.Add(texture)) + { + yield return Validation.Error($"{texture}: unknown texture type: {textureImporter.textureType}", ValidationContext.Create(texture)); } - } + break; } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs index 8711d43e4..14a14cec3 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs @@ -30,6 +30,12 @@ namespace UniGLTF return material; } + public const string COLOR_TEXTURE_PROP = "_MainTex"; + public const string METALLIC_TEX_PROP = "_MetallicGlossMap"; + public const string NORMAL_TEX_PROP = "_BumpMap"; + public const string EMISSION_TEX_PROP = "_EmissionMap"; + public const string OCCLUSION_TEX_PROP = "_OcclusionMap"; + static void Export_Color(Material m, ITextureExporter textureManager, glTFMaterial material) { if (m.HasProperty("_Color")) @@ -37,12 +43,12 @@ namespace UniGLTF material.pbrMetallicRoughness.baseColorFactor = m.GetColor("_Color").ToFloat4(ColorSpace.sRGB, ColorSpace.Linear); } - if (m.HasProperty("_MainTex")) + if (m.HasProperty(COLOR_TEXTURE_PROP)) { // Don't export alpha channel if material was OPAQUE var unnecessaryAlpha = string.Equals(material.alphaMode, "OPAQUE", StringComparison.Ordinal); - var index = textureManager.RegisterExportingAsSRgb(m.GetTexture("_MainTex"), !unnecessaryAlpha); + var index = textureManager.RegisterExportingAsSRgb(m.GetTexture(COLOR_TEXTURE_PROP), !unnecessaryAlpha); if (index != -1) { material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo() @@ -67,20 +73,20 @@ namespace UniGLTF float smoothness = 1.0f; var textuerNames = m.GetTexturePropertyNames(); - if (textuerNames.Contains("_MetallicGlossMap")) + if (textuerNames.Contains(METALLIC_TEX_PROP)) { if (m.HasProperty("_GlossMapScale")) { smoothness = m.GetFloat("_GlossMapScale"); } - metallicSmoothTexture = m.GetTexture("_MetallicGlossMap"); + metallicSmoothTexture = m.GetTexture(METALLIC_TEX_PROP); } Texture occlusionTexture = default; var occlusionStrength = 1.0f; - if (textuerNames.Contains("_OcclusionMap")) + if (textuerNames.Contains(OCCLUSION_TEX_PROP)) { - occlusionTexture = m.GetTexture("_OcclusionMap"); + occlusionTexture = m.GetTexture(OCCLUSION_TEX_PROP); if (occlusionTexture != null && m.HasProperty("_OcclusionStrength")) { occlusionStrength = m.GetFloat("_OcclusionStrength"); @@ -128,9 +134,9 @@ namespace UniGLTF static void Export_Normal(Material m, ITextureExporter textureExporter, glTFMaterial material) { - if (m.HasProperty("_BumpMap")) + if (m.HasProperty(NORMAL_TEX_PROP)) { - var index = textureExporter.RegisterExportingAsNormal(m.GetTexture("_BumpMap")); + var index = textureExporter.RegisterExportingAsNormal(m.GetTexture(NORMAL_TEX_PROP)); if (index != -1) { material.normalTexture = new glTFMaterialNormalTextureInfo() @@ -174,9 +180,9 @@ namespace UniGLTF material.emissiveFactor = color.ToFloat3(ColorSpace.Linear, ColorSpace.Linear); } - if (m.HasProperty("_EmissionMap")) + if (m.HasProperty(EMISSION_TEX_PROP)) { - var index = textureExporter.RegisterExportingAsSRgb(m.GetTexture("_EmissionMap"), needsAlpha: false); + var index = textureExporter.RegisterExportingAsSRgb(m.GetTexture(EMISSION_TEX_PROP), needsAlpha: false); if (index != -1) { material.emissiveTexture = new glTFMaterialEmissiveTextureInfo() @@ -191,7 +197,7 @@ namespace UniGLTF static void Export_MainTextureTransform(Material m, glTFTextureInfo textureInfo) { - Export_TextureTransform(m, textureInfo, "_MainTex"); + Export_TextureTransform(m, textureInfo, COLOR_TEXTURE_PROP); } static void Export_TextureTransform(Material m, glTFTextureInfo textureInfo, string propertyName) diff --git a/Assets/VRM/Editor/Format/VRMExporterWizard.cs b/Assets/VRM/Editor/Format/VRMExporterWizard.cs index b06e9fd0f..db6b5a9d6 100644 --- a/Assets/VRM/Editor/Format/VRMExporterWizard.cs +++ b/Assets/VRM/Editor/Format/VRMExporterWizard.cs @@ -124,6 +124,9 @@ namespace VRM m_meshes = null; } + /// + /// VRM0 + /// class VRMMaterialValidator : DefaultMaterialValidator { public override string GetGltfMaterialTypeFromUnityShaderName(string shaderName) @@ -135,6 +138,26 @@ namespace VRM } 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 ValidatorFactory() diff --git a/Assets/VRM/Editor/VRM.Editor.asmdef b/Assets/VRM/Editor/VRM.Editor.asmdef index ebcd2e1c1..38a926c9d 100644 --- a/Assets/VRM/Editor/VRM.Editor.asmdef +++ b/Assets/VRM/Editor/VRM.Editor.asmdef @@ -6,7 +6,8 @@ "VRMShaders.GLTF.IO.Runtime", "VRMShaders.GLTF.IO.Editor", "UniGLTF", - "UniGLTF.Editor" + "UniGLTF.Editor", + "VRMShaders.VRM.IO.Runtime" ], "optionalUnityReferences": [], "includePlatforms": [