From 3ad42a262e96a80fc0681f3eefbb4abb4be7c771 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 5 Dec 2022 17:05:21 +0900 Subject: [PATCH 1/2] fix --- .../Import/Materials/BuiltInVrmMToonMaterialImporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInVrmMToonMaterialImporter.cs b/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInVrmMToonMaterialImporter.cs index 954dd5d3b..190b353fc 100644 --- a/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInVrmMToonMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInVrmMToonMaterialImporter.cs @@ -12,7 +12,7 @@ namespace VRM /// /// 過去バージョンに含まれていたが、廃止・統合された Shader のフォールバック情報 /// - public static Dictionary FallbackShaders = new Dictionary + public static readonly Dictionary FallbackShaders = new Dictionary { {"VRM/UnlitTexture", "Unlit/Texture"}, {"VRM/UnlitTransparent", "Unlit/Transparent"}, From 0a7ac08dac655b65b347b29027339cee1652b757 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 5 Dec 2022 17:21:20 +0900 Subject: [PATCH 2/2] specify a Shader instead of a string in MaterialDescriptor. --- .../BuiltInGltfMaterialDescriptorGenerator.cs | 2 +- .../Materials/BuiltInGltfDefaultMaterialImporter.cs | 2 +- .../Materials/BuiltInGltfPbrMaterialImporter.cs | 4 ++-- .../Materials/BuiltInGltfUnlitMaterialImporter.cs | 4 +++- .../Materials/UrpGltfDefaultMaterialImporter.cs | 2 +- .../Import/Materials/UrpGltfPbrMaterialImporter.cs | 7 +++++-- .../Import/UrpGltfMaterialDescriptorGenerator.cs | 2 +- Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs | 2 +- .../Import/BuiltInVrmMaterialDescriptorGenerator.cs | 2 +- .../Materials/BuiltInVrmMToonMaterialImporter.cs | 3 ++- ...ltInVrmUnlitTransparentZWriteMaterialImporter.cs | 5 ++++- .../URP/Import/UrpVrmMaterialDescriptorGenerator.cs | 2 +- Assets/VRM/Tests/MToonTest.cs | 2 +- .../BuiltInVrm10MaterialDescriptorGenerator.cs | 4 +++- .../Materials/BuiltInVrm10MToonMaterialImporter.cs | 2 +- .../Import/UrpVrm10MaterialDescriptorGenerator.cs | 3 ++- Assets/VRM10/Tests/MaterialImportTests.cs | 2 +- .../Runtime/Material/Importer/MaterialDescriptor.cs | 6 +++--- .../IO/Runtime/Material/Importer/MaterialFactory.cs | 13 +++---------- 19 files changed, 37 insertions(+), 32 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/BuiltInGltfMaterialDescriptorGenerator.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/BuiltInGltfMaterialDescriptorGenerator.cs index 00e2a304e..c91d60401 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/BuiltInGltfMaterialDescriptorGenerator.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/BuiltInGltfMaterialDescriptorGenerator.cs @@ -23,7 +23,7 @@ namespace UniGLTF return new MaterialDescriptor( GltfMaterialImportUtils.ImportMaterialName(i, null), - BuiltInGltfPbrMaterialImporter.ShaderName, + BuiltInGltfPbrMaterialImporter.Shader, null, new Dictionary(), new Dictionary(), diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfDefaultMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfDefaultMaterialImporter.cs index 63bc109f0..ca2d4acc9 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfDefaultMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfDefaultMaterialImporter.cs @@ -15,7 +15,7 @@ namespace UniGLTF // FIXME return new MaterialDescriptor( "__default__", - "Standard", + BuiltInGltfPbrMaterialImporter.Shader, default, new Dictionary(), new Dictionary(), diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfPbrMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfPbrMaterialImporter.cs index bbc665005..8e19db3f5 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfPbrMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfPbrMaterialImporter.cs @@ -45,7 +45,7 @@ namespace UniGLTF private static readonly int DstBlend = Shader.PropertyToID("_DstBlend"); private static readonly int ZWrite = Shader.PropertyToID("_ZWrite"); private static readonly int Cutoff = Shader.PropertyToID("_Cutoff"); - public const string ShaderName = "Standard"; + public static Shader Shader => Shader.Find("Standard"); private enum BlendMode { @@ -205,7 +205,7 @@ namespace UniGLTF matDesc = new MaterialDescriptor( GltfMaterialImportUtils.ImportMaterialName(i, src), - ShaderName, + Shader, null, textureSlots, floatValues, diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfUnlitMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfUnlitMaterialImporter.cs index 651df6d59..4bc74d2ce 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfUnlitMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInGltfUnlitMaterialImporter.cs @@ -11,6 +11,8 @@ namespace UniGLTF { private static readonly int Cutoff = Shader.PropertyToID("_Cutoff"); + public static Shader Shader => Shader.Find(UniUnlitUtil.ShaderName); + public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor matDesc) { if (i < 0 || i >= data.GLTF.materials.Count) @@ -48,7 +50,7 @@ namespace UniGLTF matDesc = new MaterialDescriptor( GltfMaterialImportUtils.ImportMaterialName(i, src), - UniUnlitUtil.ShaderName, + Shader, null, textureSlots, new Dictionary(), 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 154c6d75c..2235009de 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 @@ -15,7 +15,7 @@ namespace UniGLTF // FIXME return new MaterialDescriptor( "__default__", - UrpGltfPbrMaterialImporter.ShaderName, + UrpGltfPbrMaterialImporter.Shader, default, new Dictionary(), new Dictionary(), 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 66d14310c..c72e5b884 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 @@ -13,11 +13,14 @@ namespace UniGLTF /// public static class UrpGltfPbrMaterialImporter { + public const string ShaderName = "Universal Render Pipeline/Lit"; + private static readonly int SrcBlend = Shader.PropertyToID("_SrcBlend"); private static readonly int DstBlend = Shader.PropertyToID("_DstBlend"); private static readonly int ZWrite = Shader.PropertyToID("_ZWrite"); private static readonly int Cutoff = Shader.PropertyToID("_Cutoff"); - public const string ShaderName = "Universal Render Pipeline/Lit"; + + public static Shader Shader => Shader.Find(ShaderName); private enum BlendMode { @@ -182,7 +185,7 @@ namespace UniGLTF matDesc = new MaterialDescriptor( GltfMaterialImportUtils.ImportMaterialName(i, src), - ShaderName, + Shader, null, textureSlots, floatValues, 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 4ab8d7e9d..b0a24cf9e 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/UrpGltfMaterialDescriptorGenerator.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Import/UrpGltfMaterialDescriptorGenerator.cs @@ -24,7 +24,7 @@ namespace UniGLTF return new MaterialDescriptor( GltfMaterialImportUtils.ImportMaterialName(i, null), - BuiltInGltfPbrMaterialImporter.ShaderName, + UrpGltfPbrMaterialImporter.Shader, null, new Dictionary(), new Dictionary(), diff --git a/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs b/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs index 118f3316b..5c76b650d 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs @@ -227,7 +227,7 @@ namespace UniGLTF using (var data = new AutoGltfFileParser(path).Parse()) { var matDesc = new BuiltInGltfMaterialDescriptorGenerator().Get(data, 0); - Assert.AreEqual("Standard", matDesc.ShaderName); + Assert.AreEqual("Standard", matDesc.Shader.name); Assert.AreEqual(5, matDesc.TextureSlots.Count); var (key, value) = matDesc.EnumerateSubAssetKeyValue().First(); Assert.AreEqual(new SubAssetKey(typeof(Texture2D), "texture_0"), key); diff --git a/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/BuiltInVrmMaterialDescriptorGenerator.cs b/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/BuiltInVrmMaterialDescriptorGenerator.cs index 7f112c065..91c4cfd9c 100644 --- a/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/BuiltInVrmMaterialDescriptorGenerator.cs +++ b/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/BuiltInVrmMaterialDescriptorGenerator.cs @@ -45,7 +45,7 @@ namespace VRM Debug.LogWarning($"fallback"); return new MaterialDescriptor( GltfMaterialImportUtils.ImportMaterialName(i, null), - BuiltInGltfPbrMaterialImporter.ShaderName, + BuiltInGltfPbrMaterialImporter.Shader, null, new Dictionary(), new Dictionary(), diff --git a/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInVrmMToonMaterialImporter.cs b/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInVrmMToonMaterialImporter.cs index 190b353fc..c855693f9 100644 --- a/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInVrmMToonMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInVrmMToonMaterialImporter.cs @@ -67,6 +67,7 @@ namespace VRM { shaderName = FallbackShaders[shaderName]; } + var shader = Shader.Find(shaderName); var textureSlots = new Dictionary(); var floatValues = new Dictionary(); @@ -75,7 +76,7 @@ namespace VRM var actions = new List>(); matDesc = new MaterialDescriptor( name, - shaderName, + shader, vrmMaterial.renderQueue, textureSlots, floatValues, diff --git a/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInVrmUnlitTransparentZWriteMaterialImporter.cs b/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInVrmUnlitTransparentZWriteMaterialImporter.cs index 4ab36a434..4efb4533d 100644 --- a/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInVrmUnlitTransparentZWriteMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/MaterialIO/BuiltInRP/Import/Materials/BuiltInVrmUnlitTransparentZWriteMaterialImporter.cs @@ -158,7 +158,10 @@ namespace VRM unityMaterial.renderQueue = vrmMaterial.renderQueue; }); - matDesc = new MaterialDescriptor(name, Utils.ShaderName, null, + matDesc = new MaterialDescriptor( + name, + Shader.Find(Utils.ShaderName), + null, textureSlots, floatValues, colors, diff --git a/Assets/VRM/Runtime/IO/MaterialIO/URP/Import/UrpVrmMaterialDescriptorGenerator.cs b/Assets/VRM/Runtime/IO/MaterialIO/URP/Import/UrpVrmMaterialDescriptorGenerator.cs index 3c20c7c4f..ac88061a7 100644 --- a/Assets/VRM/Runtime/IO/MaterialIO/URP/Import/UrpVrmMaterialDescriptorGenerator.cs +++ b/Assets/VRM/Runtime/IO/MaterialIO/URP/Import/UrpVrmMaterialDescriptorGenerator.cs @@ -29,7 +29,7 @@ namespace VRM } return new MaterialDescriptor( GltfMaterialImportUtils.ImportMaterialName(i, null), - BuiltInGltfPbrMaterialImporter.ShaderName, + UrpGltfPbrMaterialImporter.Shader, null, new Dictionary(), new Dictionary(), diff --git a/Assets/VRM/Tests/MToonTest.cs b/Assets/VRM/Tests/MToonTest.cs index ec5c7adbd..c9d91a63c 100644 --- a/Assets/VRM/Tests/MToonTest.cs +++ b/Assets/VRM/Tests/MToonTest.cs @@ -72,7 +72,7 @@ namespace VRM { var vrmImporter = new VRMImporterContext(new VRMData(data), null); var materialParam = new BuiltInVrmMaterialDescriptorGenerator(vrmImporter.VRM).Get(data, 0); - Assert.AreEqual("VRM/MToon", materialParam.ShaderName); + Assert.AreEqual("VRM/MToon", materialParam.Shader.name); Assert.AreEqual("Alicia_body", materialParam.TextureSlots["_MainTex"].UnityObjectName); var (key, value) = materialParam.EnumerateSubAssetKeyValue().First(); diff --git a/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Import/BuiltInVrm10MaterialDescriptorGenerator.cs b/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Import/BuiltInVrm10MaterialDescriptorGenerator.cs index 5a25a5a94..4e947844b 100644 --- a/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Import/BuiltInVrm10MaterialDescriptorGenerator.cs +++ b/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Import/BuiltInVrm10MaterialDescriptorGenerator.cs @@ -16,13 +16,15 @@ namespace UniVRM10 if (BuiltInGltfUnlitMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc; // pbr if (BuiltInGltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc; + // fallback if (Symbols.VRM_DEVELOP) { Debug.LogWarning($"material: {i} out of range. fallback"); } return new MaterialDescriptor( - GltfMaterialImportUtils.ImportMaterialName(i, null), BuiltInGltfPbrMaterialImporter.ShaderName, + GltfMaterialImportUtils.ImportMaterialName(i, null), + BuiltInGltfPbrMaterialImporter.Shader, null, new Dictionary(), new Dictionary(), diff --git a/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Import/Materials/BuiltInVrm10MToonMaterialImporter.cs b/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Import/Materials/BuiltInVrm10MToonMaterialImporter.cs index 982461d7b..b7ba7a563 100644 --- a/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Import/Materials/BuiltInVrm10MToonMaterialImporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/BuiltInRP/Import/Materials/BuiltInVrm10MToonMaterialImporter.cs @@ -33,7 +33,7 @@ namespace UniVRM10 // use material.name, because material name may renamed in GltfParser. matDesc = new MaterialDescriptor( m.name, - MToon10Meta.UnityShaderName, + Shader.Find(MToon10Meta.UnityShaderName), null, Vrm10MToonTextureImporter.EnumerateAllTextures(data, m, mtoon).ToDictionary(tuple => tuple.key, tuple => tuple.Item2.Item2), TryGetAllFloats(m, mtoon).ToDictionary(tuple => tuple.key, tuple => tuple.value), diff --git a/Assets/VRM10/Runtime/IO/Material/URP/Import/UrpVrm10MaterialDescriptorGenerator.cs b/Assets/VRM10/Runtime/IO/Material/URP/Import/UrpVrm10MaterialDescriptorGenerator.cs index fdf8c0a0f..8cba77b28 100644 --- a/Assets/VRM10/Runtime/IO/Material/URP/Import/UrpVrm10MaterialDescriptorGenerator.cs +++ b/Assets/VRM10/Runtime/IO/Material/URP/Import/UrpVrm10MaterialDescriptorGenerator.cs @@ -14,11 +14,12 @@ namespace UniVRM10 if (BuiltInGltfUnlitMaterialImporter.TryCreateParam(data, i, out MaterialDescriptor matDesc)) return matDesc; // pbr if (UrpGltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc; + // fallback Debug.LogWarning($"material: {i} out of range. fallback"); return new MaterialDescriptor( GltfMaterialImportUtils.ImportMaterialName(i, null), - BuiltInGltfPbrMaterialImporter.ShaderName, + UrpGltfPbrMaterialImporter.Shader, null, new Dictionary(), new Dictionary(), diff --git a/Assets/VRM10/Tests/MaterialImportTests.cs b/Assets/VRM10/Tests/MaterialImportTests.cs index 1af246409..a1737c3c4 100644 --- a/Assets/VRM10/Tests/MaterialImportTests.cs +++ b/Assets/VRM10/Tests/MaterialImportTests.cs @@ -27,7 +27,7 @@ namespace UniVRM10 var matDesc = new BuiltInVrm10MaterialDescriptorGenerator().Get(data, 0); Assert.AreEqual("Alicia_body", matDesc.Name); - Assert.AreEqual("VRM10/MToon10", matDesc.ShaderName); + Assert.AreEqual("VRM10/MToon10", matDesc.Shader.name); Assert.AreEqual("Alicia_body", matDesc.TextureSlots["_MainTex"].UnityObjectName); Assert.AreEqual("Alicia_body", matDesc.TextureSlots["_ShadeTex"].UnityObjectName); diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Material/Importer/MaterialDescriptor.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Material/Importer/MaterialDescriptor.cs index e17e9bdde..8d51b12ec 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Material/Importer/MaterialDescriptor.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Material/Importer/MaterialDescriptor.cs @@ -7,7 +7,7 @@ namespace VRMShaders public sealed class MaterialDescriptor { public readonly string Name; - public readonly string ShaderName; + public readonly Shader Shader; public readonly int? RenderQueue; public readonly IReadOnlyDictionary TextureSlots; public readonly IReadOnlyDictionary FloatValues; @@ -19,7 +19,7 @@ namespace VRMShaders public MaterialDescriptor( string name, - string shaderName, + Shader shader, int? renderQueue, IReadOnlyDictionary textureSlots, IReadOnlyDictionary floatValues, @@ -28,7 +28,7 @@ namespace VRMShaders IReadOnlyList> actions) { Name = name; - ShaderName = shaderName; + Shader = shader; RenderQueue = renderQueue; TextureSlots = textureSlots; FloatValues = floatValues; diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Material/Importer/MaterialFactory.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Material/Importer/MaterialFactory.cs index e968c3194..882818fbb 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Material/Importer/MaterialFactory.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Material/Importer/MaterialFactory.cs @@ -101,19 +101,12 @@ namespace VRMShaders getTexture = (x, y) => Task.FromResult(null); } - var shaderName = matDesc.ShaderName; - if (String.IsNullOrEmpty(shaderName)) + if (matDesc.Shader == null) { - throw new Exception("no shader name"); + throw new ArgumentNullException(nameof(matDesc.Shader)); } - var shader = Shader.Find(shaderName); - if (shader == null) - { - throw new Exception($"shader: {shaderName} not found"); - } - - material = new Material(shader); + material = new Material(matDesc.Shader); material.name = matDesc.SubAssetKey.Name; foreach (var kv in matDesc.TextureSlots)