From cce076d324ca35d69cac2423a10e98843d9315dc Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Thu, 7 Nov 2024 22:21:55 +0900 Subject: [PATCH 1/2] Impl VRM10 material exporter in URP --- .../UnityShaderLab/Properties/MToon10Meta.cs | 9 ++++-- .../Export/BuiltInVrm10MaterialExporter.cs | 7 +++-- .../BuiltInVrm10MToonMaterialExporter.cs | 20 +++++++++++-- .../IO/Material/URP/Export/Materials.meta | 3 ++ .../UrpVrm10MToonMaterialExporter.cs | 29 +++++++++++++++++++ .../UrpVrm10MToonMaterialExporter.cs.meta | 3 ++ .../URP/Export/UrpVrm10MaterialExporter.cs | 20 ++++++++----- ProjectSettings/GraphicsSettings.asset | 5 ++-- 8 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 Assets/VRM10/Runtime/IO/Material/URP/Export/Materials.meta create mode 100644 Assets/VRM10/Runtime/IO/Material/URP/Export/Materials/UrpVrm10MToonMaterialExporter.cs create mode 100644 Assets/VRM10/Runtime/IO/Material/URP/Export/Materials/UrpVrm10MToonMaterialExporter.cs.meta diff --git a/Assets/VRM10/MToon10/Runtime/UnityShaderLab/Properties/MToon10Meta.cs b/Assets/VRM10/MToon10/Runtime/UnityShaderLab/Properties/MToon10Meta.cs index 399223743..c8a9bfb71 100644 --- a/Assets/VRM10/MToon10/Runtime/UnityShaderLab/Properties/MToon10Meta.cs +++ b/Assets/VRM10/MToon10/Runtime/UnityShaderLab/Properties/MToon10Meta.cs @@ -1,8 +1,13 @@ -namespace VRM10.MToon10 +using System; + +namespace VRM10.MToon10 { public static class MToon10Meta { public static readonly string UnityShaderName = "VRM10/MToon10"; - public static readonly string URPUnityShaderName = "VRM10/Universal Render Pipeline/MToon10"; + public static readonly string UnityUrpShaderName = "VRM10/Universal Render Pipeline/MToon10"; + + [Obsolete("Use UnityUrpShaderName instead")] + public static readonly string URPUnityShaderName = UnityUrpShaderName; } } \ No newline at end of file diff --git a/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Export/BuiltInVrm10MaterialExporter.cs b/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Export/BuiltInVrm10MaterialExporter.cs index fb0d3ac0e..6e0595728 100644 --- a/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Export/BuiltInVrm10MaterialExporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Export/BuiltInVrm10MaterialExporter.cs @@ -5,17 +5,18 @@ namespace UniVRM10 { public class BuiltInVrm10MaterialExporter : IMaterialExporter { - private readonly BuiltInGltfMaterialExporter _gltfExporter = new BuiltInGltfMaterialExporter(); + public BuiltInGltfMaterialExporter GltfExporter { get; set; } = new(); + public BuiltInVrm10MToonMaterialExporter MToonExporter { get; set; } = new(); public glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter, GltfExportSettings settings) { - if (BuiltInVrm10MToonMaterialExporter.TryExportMaterialAsMToon(m, textureExporter, out var dst)) + if (MToonExporter.TryExportMaterial(m, textureExporter, out var dst)) { return dst; } else { - return _gltfExporter.ExportMaterial(m, textureExporter, settings); + return GltfExporter.ExportMaterial(m, textureExporter, settings); } } } diff --git a/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Export/Materials/BuiltInVrm10MToonMaterialExporter.cs b/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Export/Materials/BuiltInVrm10MToonMaterialExporter.cs index 7bb4529e4..ebb315948 100644 --- a/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Export/Materials/BuiltInVrm10MToonMaterialExporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Export/Materials/BuiltInVrm10MToonMaterialExporter.cs @@ -7,11 +7,18 @@ using ColorSpace = UniGLTF.ColorSpace; namespace UniVRM10 { - public static class BuiltInVrm10MToonMaterialExporter + public class BuiltInVrm10MToonMaterialExporter { - public static bool TryExportMaterialAsMToon(Material src, ITextureExporter textureExporter, out glTFMaterial dst) + public Shader Shader { get; set; } + + public BuiltInVrm10MToonMaterialExporter(Shader shader = null) { - if (src.shader.name != MToon10Meta.UnityShaderName) + Shader = shader != null ? shader : Shader.Find(MToon10Meta.UnityShaderName); + } + + public bool TryExportMaterial(Material src, ITextureExporter textureExporter, out glTFMaterial dst) + { + if (src.shader == null || src.shader != Shader) { dst = null; return false; @@ -203,5 +210,12 @@ namespace UniVRM10 throw new ArgumentOutOfRangeException(nameof(mode), mode, null); } } + + [Obsolete("Use TryExportMaterial")] + public static bool TryExportMaterialAsMToon(Material src, ITextureExporter textureExporter, out glTFMaterial dst) + { + var exporter = new BuiltInVrm10MToonMaterialExporter(); + return exporter.TryExportMaterial(src, textureExporter, out dst); + } } } diff --git a/Assets/VRM10/Runtime/IO/Material/URP/Export/Materials.meta b/Assets/VRM10/Runtime/IO/Material/URP/Export/Materials.meta new file mode 100644 index 000000000..86227ff99 --- /dev/null +++ b/Assets/VRM10/Runtime/IO/Material/URP/Export/Materials.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f34870b7baf94fad98d958eea3368033 +timeCreated: 1730954293 \ No newline at end of file diff --git a/Assets/VRM10/Runtime/IO/Material/URP/Export/Materials/UrpVrm10MToonMaterialExporter.cs b/Assets/VRM10/Runtime/IO/Material/URP/Export/Materials/UrpVrm10MToonMaterialExporter.cs new file mode 100644 index 000000000..5cd98ea0f --- /dev/null +++ b/Assets/VRM10/Runtime/IO/Material/URP/Export/Materials/UrpVrm10MToonMaterialExporter.cs @@ -0,0 +1,29 @@ +using UniGLTF; +using UnityEngine; +using VRM10.MToon10; + +namespace UniVRM10 +{ + public class UrpVrm10MToonMaterialExporter + { + private readonly BuiltInVrm10MToonMaterialExporter _builtInExporter; + + public Shader Shader + { + get => _builtInExporter.Shader; + set => _builtInExporter.Shader = value; + } + + public UrpVrm10MToonMaterialExporter(Shader shader = null) + { + _builtInExporter = new BuiltInVrm10MToonMaterialExporter( + shader != null ? shader : Shader.Find(MToon10Meta.UnityUrpShaderName) + ); + } + + public bool TryExportMaterial(Material src, ITextureExporter textureExporter, out glTFMaterial dst) + { + return _builtInExporter.TryExportMaterial(src, textureExporter, out dst); + } + } +} \ No newline at end of file diff --git a/Assets/VRM10/Runtime/IO/Material/URP/Export/Materials/UrpVrm10MToonMaterialExporter.cs.meta b/Assets/VRM10/Runtime/IO/Material/URP/Export/Materials/UrpVrm10MToonMaterialExporter.cs.meta new file mode 100644 index 000000000..f00595be1 --- /dev/null +++ b/Assets/VRM10/Runtime/IO/Material/URP/Export/Materials/UrpVrm10MToonMaterialExporter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7d97f9c3e4a2454e829349d47f8bd1f7 +timeCreated: 1730954308 \ No newline at end of file diff --git a/Assets/VRM10/Runtime/IO/Material/URP/Export/UrpVrm10MaterialExporter.cs b/Assets/VRM10/Runtime/IO/Material/URP/Export/UrpVrm10MaterialExporter.cs index f4795e313..fce636cdb 100644 --- a/Assets/VRM10/Runtime/IO/Material/URP/Export/UrpVrm10MaterialExporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/URP/Export/UrpVrm10MaterialExporter.cs @@ -5,15 +5,21 @@ namespace UniVRM10 { public class UrpVrm10MaterialExporter : IMaterialExporter { + public UrpGltfMaterialExporter GltfExporter { get; set; } = new(); + public UrpVrm10MToonMaterialExporter MToonExporter { get; set; } = new(); + public glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter, GltfExportSettings settings) { - #if UNITY_EDITOR - return new glTFMaterial{ - name = "dummyForTest", - }; - #else - throw new System.NotImplementedException(); - #endif + Debug.Log(1); + if (MToonExporter.TryExportMaterial(m, textureExporter, out var dst)) + { + Debug.Log(2); + return dst; + } + else + { + return GltfExporter.ExportMaterial(m, textureExporter, settings); + } } } } diff --git a/ProjectSettings/GraphicsSettings.asset b/ProjectSettings/GraphicsSettings.asset index 2376bed17..62d55662a 100644 --- a/ProjectSettings/GraphicsSettings.asset +++ b/ProjectSettings/GraphicsSettings.asset @@ -39,8 +39,9 @@ GraphicsSettings: - {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0} - {fileID: 16001, guid: 0000000000000000f000000000000000, type: 0} - {fileID: 17000, guid: 0000000000000000f000000000000000, type: 0} - - {fileID: 46, guid: 0000000000000000f000000000000000, type: 0} - - {fileID: 46, guid: 0000000000000000f000000000000000, type: 0} + - {fileID: 4800000, guid: 933532a4fcc9baf4fa0491de14d08ed7, type: 3} + - {fileID: 4800000, guid: 8c17b56f4bf084c47872edcb95237e4a, type: 3} + - {fileID: 4800000, guid: 0781c64b7f2a1604ea6aa7e252080372, type: 3} m_PreloadedShaders: [] m_PreloadShadersBatchTimeLimit: -1 m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000, From a1249a79373b5839a55b41fc484b95bb43fa8ad2 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Thu, 7 Nov 2024 22:24:08 +0900 Subject: [PATCH 2/2] remove debug log --- .../Runtime/IO/Material/URP/Export/UrpVrm10MaterialExporter.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Assets/VRM10/Runtime/IO/Material/URP/Export/UrpVrm10MaterialExporter.cs b/Assets/VRM10/Runtime/IO/Material/URP/Export/UrpVrm10MaterialExporter.cs index fce636cdb..1651bc7fd 100644 --- a/Assets/VRM10/Runtime/IO/Material/URP/Export/UrpVrm10MaterialExporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/URP/Export/UrpVrm10MaterialExporter.cs @@ -10,10 +10,8 @@ namespace UniVRM10 public glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter, GltfExportSettings settings) { - Debug.Log(1); if (MToonExporter.TryExportMaterial(m, textureExporter, out var dst)) { - Debug.Log(2); return dst; } else