From 9667f2cfb82c69a1ff5be3d922f9e8254b2d88b2 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Fri, 2 Aug 2024 01:19:50 +0900 Subject: [PATCH] Implements UrpGltfDefaultMaterialImporter --- .../UrpGltfDefaultMaterialImporter.cs | 52 ++++++++++++++++--- .../Materials/UrpGltfPbrMaterialImporter.cs | 15 ++++-- .../UrpGltfMaterialDescriptorGenerator.cs | 27 +++------- .../UrpVrmMaterialDescriptorGenerator.cs | 23 +++----- .../UrpVrm10MaterialDescriptorGenerator.cs | 27 ++++------ 5 files changed, 82 insertions(+), 62 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/Materials/UrpGltfDefaultMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/Materials/UrpGltfDefaultMaterialImporter.cs index b6e3cd90d..260a0c1b0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/Materials/UrpGltfDefaultMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/Materials/UrpGltfDefaultMaterialImporter.cs @@ -1,27 +1,65 @@ using System; using System.Collections.Generic; using UnityEngine; +using UnityEngine.Rendering; namespace UniGLTF { /// /// Generate the descriptor of the glTF default material. + /// + /// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#default-material /// - public static class UrpGltfDefaultMaterialImporter + public class UrpGltfDefaultMaterialImporter { - public static MaterialDescriptor CreateParam() + public Shader Shader { get; set; } + + public UrpGltfDefaultMaterialImporter(Shader shader = null) + { + Shader = shader != null ? shader : Shader.Find("Universal Render Pipeline/Lit"); + } + + public MaterialDescriptor CreateParam(string materialName) { - // FIXME return new MaterialDescriptor( - "__default__", - UrpGltfPbrMaterialImporter.Shader, - default, + string.IsNullOrEmpty(materialName) ? "__default__" : materialName, + Shader, + null, new Dictionary(), new Dictionary(), new Dictionary(), new Dictionary(), - new List>() + new List> { GenerateDefaultMaterial } ); } + + public static void GenerateDefaultMaterial(Material dst) + { + var context = new UrpLitContext(dst); + context.UnsafeEditMode = true; + + context.SurfaceType = UrpLitSurfaceType.Opaque; + context.IsAlphaClipEnabled = false; + context.CullMode = CullMode.Back; + + context.BaseColorSrgb = new Color(1, 1, 1, 1); + context.BaseTexture = null; + + context.WorkflowType = UrpLitWorkflowType.Metallic; + context.SmoothnessTextureChannel = UrpLitSmoothnessMapChannel.SpecularMetallicAlpha; + context.Metallic = 1f; + context.Smoothness = 0f; + context.MetallicGlossMap = null; + + context.OcclusionTexture = null; + + context.BumpMap = null; + + context.IsEmissionEnabled = false; + context.EmissionColorLinear = new Color(0, 0, 0, 0); + context.EmissionTexture = null; + + context.Validate(); + } } } \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/Materials/UrpGltfPbrMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/Materials/UrpGltfPbrMaterialImporter.cs index d39185d6d..11586d04d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/Materials/UrpGltfPbrMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/Materials/UrpGltfPbrMaterialImporter.cs @@ -11,13 +11,19 @@ namespace UniGLTF /// /// see: https://github.com/Unity-Technologies/Graphics/blob/v7.5.3/com.unity.render-pipelines.universal/Editor/UniversalRenderPipelineMaterialUpgrader.cs#L354-L379 /// - public static class UrpGltfPbrMaterialImporter + public class UrpGltfPbrMaterialImporter { - public const string ShaderName = "Universal Render Pipeline/Lit"; + /// + /// Universal Render Pipeline/Lit とプロパティやキーワードに互換があるカスタムシェーダに置換可能。 + /// + public Shader Shader { get; set; } - public static Shader Shader => Shader.Find(ShaderName); + public UrpGltfPbrMaterialImporter(Shader shader = null) + { + Shader = shader != null ? shader : Shader.Find("Universal Render Pipeline/Lit"); + } - public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor matDesc) + public bool TryCreateParam(GltfData data, int i, out MaterialDescriptor matDesc) { if (i < 0 || i >= data.GLTF.materials.Count) { @@ -44,7 +50,6 @@ namespace UniGLTF public static async Task GenerateMaterialAsync(GltfData data, glTFMaterial src, Material dst, GetTextureAsyncFunc getTextureAsync, IAwaitCaller awaitCaller) { - dst.shader = Shader; var context = new UrpLitContext(dst); context.UnsafeEditMode = true; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/UrpGltfMaterialDescriptorGenerator.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/UrpGltfMaterialDescriptorGenerator.cs index b1dcdff1e..a760ba344 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/UrpGltfMaterialDescriptorGenerator.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/UrpGltfMaterialDescriptorGenerator.cs @@ -1,6 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; using UnityEngine; namespace UniGLTF @@ -10,30 +7,22 @@ namespace UniGLTF /// public sealed class UrpGltfMaterialDescriptorGenerator : IMaterialDescriptorGenerator { + public UrpGltfPbrMaterialImporter PbrMaterialImporter { get; } + public UrpGltfDefaultMaterialImporter DefaultMaterialImporter { get; } + public MaterialDescriptor Get(GltfData data, int i) { if (BuiltInGltfUnlitMaterialImporter.TryCreateParam(data, i, out var param)) return param; - if (UrpGltfPbrMaterialImporter.TryCreateParam(data, i, out param)) return param; - // fallback + if (PbrMaterialImporter.TryCreateParam(data, i, out param)) return param; + + // NOTE: Fallback to default material if (Symbols.VRM_DEVELOP) { Debug.LogWarning($"material: {i} out of range. fallback"); } - - return new MaterialDescriptor( - GltfMaterialImportUtils.ImportMaterialName(i, null), - UrpGltfPbrMaterialImporter.Shader, - null, - new Dictionary(), - new Dictionary(), - new Dictionary(), - new Dictionary(), - new Collection>()); + return GetGltfDefault(GltfMaterialImportUtils.ImportMaterialName(i, null)); } - public MaterialDescriptor GetGltfDefault() - { - return UrpGltfDefaultMaterialImporter.CreateParam(); - } + public MaterialDescriptor GetGltfDefault(string materialName = null) => DefaultMaterialImporter.CreateParam(materialName); } } \ No newline at end of file diff --git a/Assets/VRM/Runtime/IO/MaterialIO/URP/Import/UrpVrmMaterialDescriptorGenerator.cs b/Assets/VRM/Runtime/IO/MaterialIO/URP/Import/UrpVrmMaterialDescriptorGenerator.cs index 85f1fae2d..a0aa70f6a 100644 --- a/Assets/VRM/Runtime/IO/MaterialIO/URP/Import/UrpVrmMaterialDescriptorGenerator.cs +++ b/Assets/VRM/Runtime/IO/MaterialIO/URP/Import/UrpVrmMaterialDescriptorGenerator.cs @@ -9,6 +9,9 @@ namespace VRM { private readonly glTF_VRM_extensions _vrm; + public UrpGltfPbrMaterialImporter PbrMaterialImporter { get; } + public UrpGltfDefaultMaterialImporter DefaultMaterialImporter { get; } + public UrpVrmMaterialDescriptorGenerator(glTF_VRM_extensions vrm) { _vrm = vrm; @@ -20,26 +23,16 @@ namespace VRM // unlit "UniUnlit" work in URP if (BuiltInGltfUnlitMaterialImporter.TryCreateParam(data, i, out var matDesc)) return matDesc; // pbr "Standard" to "Universal Render Pipeline/Lit" - if (UrpGltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc; - // fallback + if (PbrMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc; + + // NOTE: Fallback to default material if (Symbols.VRM_DEVELOP) { Debug.LogWarning($"material: {i} out of range. fallback"); } - return new MaterialDescriptor( - GltfMaterialImportUtils.ImportMaterialName(i, null), - UrpGltfPbrMaterialImporter.Shader, - null, - new Dictionary(), - new Dictionary(), - new Dictionary(), - new Dictionary(), - new Action[]{}); + return GetGltfDefault(GltfMaterialImportUtils.ImportMaterialName(i, null)); } - public MaterialDescriptor GetGltfDefault() - { - return UrpGltfDefaultMaterialImporter.CreateParam(); - } + public MaterialDescriptor GetGltfDefault(string materialName = null) => DefaultMaterialImporter.CreateParam(materialName); } } diff --git a/Assets/VRM10/Runtime/IO/Material/URP/Import/UrpVrm10MaterialDescriptorGenerator.cs b/Assets/VRM10/Runtime/IO/Material/URP/Import/UrpVrm10MaterialDescriptorGenerator.cs index b0cf9d20b..4717b195f 100644 --- a/Assets/VRM10/Runtime/IO/Material/URP/Import/UrpVrm10MaterialDescriptorGenerator.cs +++ b/Assets/VRM10/Runtime/IO/Material/URP/Import/UrpVrm10MaterialDescriptorGenerator.cs @@ -7,6 +7,9 @@ namespace UniVRM10 { public sealed class UrpVrm10MaterialDescriptorGenerator : IMaterialDescriptorGenerator { + public UrpGltfPbrMaterialImporter PbrMaterialImporter { get; } + public UrpGltfDefaultMaterialImporter DefaultMaterialImporter { get; } + public MaterialDescriptor Get(GltfData data, int i) { // mtoon @@ -14,24 +17,16 @@ namespace UniVRM10 // unlit if (BuiltInGltfUnlitMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc; // pbr - if (UrpGltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc; + if (PbrMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc; - // fallback - Debug.LogWarning($"material: {i} out of range. fallback"); - return new MaterialDescriptor( - GltfMaterialImportUtils.ImportMaterialName(i, null), - UrpGltfPbrMaterialImporter.Shader, - null, - new Dictionary(), - new Dictionary(), - new Dictionary(), - new Dictionary(), - new Action[]{}); + // NOTE: Fallback to default material + if (Symbols.VRM_DEVELOP) + { + Debug.LogWarning($"material: {i} out of range. fallback"); + } + return GetGltfDefault(GltfMaterialImportUtils.ImportMaterialName(i, null)); } - public MaterialDescriptor GetGltfDefault() - { - return UrpGltfDefaultMaterialImporter.CreateParam(); - } + public MaterialDescriptor GetGltfDefault(string materialName = null) => DefaultMaterialImporter.CreateParam(materialName); } }