From 68094b54aa3de3ed7ea2551b251296ac84cb8b0c Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 27 Sep 2021 21:00:26 +0900 Subject: [PATCH 1/2] Rename to UniUnlitUtil and Use UniUnlitUtil.ShaderName --- .../MaterialIO/GltfUnlitMaterialImporter.cs | 22 +++++++++---------- .../UniGLTF/IO/MaterialIO/MaterialExporter.cs | 8 +++---- .../UniGLTF/IO/MeshIO/VertexColorState.cs | 4 ++-- .../GLTF/IO/Runtime/MaterialFactory.cs | 2 +- .../GLTF/UniUnlit/Editor/UniUnlitEditor.cs | 22 +++++++++---------- .../Runtime/{Utils.cs => UniUnlitUtil.cs} | 2 +- .../UniUnlit/Runtime/UniUnlitUtil.cs.meta | 11 ++++++++++ .../GLTF/UniUnlit/Runtime/Utils.cs.meta | 3 --- 8 files changed, 40 insertions(+), 34 deletions(-) rename Assets/VRMShaders/GLTF/UniUnlit/Runtime/{Utils.cs => UniUnlitUtil.cs} (99%) create mode 100644 Assets/VRMShaders/GLTF/UniUnlit/Runtime/UniUnlitUtil.cs.meta delete mode 100644 Assets/VRMShaders/GLTF/UniUnlit/Runtime/Utils.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs index a49b0ed54c..a329b6ca16 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UniGLTF.UniUnlit; using VRMShaders; using ColorSpace = VRMShaders.ColorSpace; @@ -6,8 +6,6 @@ namespace UniGLTF { public static class GltfUnlitMaterialImporter { - public const string ShaderName = "UniGLTF/UniUnlit"; - public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor matDesc) { if (i < 0 || i >= data.GLTF.materials.Count) @@ -23,7 +21,7 @@ public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor m return false; } - matDesc = new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, src), ShaderName); + matDesc = new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, src), UniUnlitUtil.ShaderName); // texture if (src.pbrMetallicRoughness.baseColorTexture != null) @@ -46,41 +44,41 @@ public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor m { if (src.alphaMode == "OPAQUE") { - UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque); + UniUnlitUtil.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque); } else if (src.alphaMode == "BLEND") { - UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Transparent); + UniUnlitUtil.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Transparent); } else if (src.alphaMode == "MASK") { - UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Cutout); + UniUnlitUtil.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Cutout); material.SetFloat("_Cutoff", src.alphaCutoff); } else { // default OPAQUE - UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque); + UniUnlitUtil.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque); } // culling if (src.doubleSided) { - UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Off); + UniUnlitUtil.SetCullMode(material, UniUnlit.UniUnlitCullMode.Off); } else { - UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Back); + UniUnlitUtil.SetCullMode(material, UniUnlit.UniUnlitCullMode.Back); } // VColor var hasVertexColor = data.GLTF.MaterialHasVertexColor(i); if (hasVertexColor) { - UniUnlit.Utils.SetVColBlendMode(material, UniUnlit.UniUnlitVertexColorBlendOp.Multiply); + UniUnlitUtil.SetVColBlendMode(material, UniUnlit.UniUnlitVertexColorBlendOp.Multiply); } - UniUnlit.Utils.ValidateProperties(material, true); + UniUnlitUtil.ValidateProperties(material, true); }); return true; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs index 551fc79576..1fab2d3b19 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs @@ -220,7 +220,7 @@ public static bool IsUnlit(string shaderName) case "Unlit/Texture": case "Unlit/Transparent": case "Unlit/Transparent Cutout": - case "UniGLTF/UniUnlit": + case UniUnlit.UniUnlitUtil.ShaderName: return true; default: @@ -244,7 +244,7 @@ protected virtual glTFMaterial CreateMaterial(Material m) case "Unlit/Transparent Cutout": return Export_UnlitCutout(m); - case "UniGLTF/UniUnlit": + case UniUnlit.UniUnlitUtil.ShaderName: return Export_UniUnlit(m); default: @@ -285,7 +285,7 @@ private glTFMaterial Export_UniUnlit(Material m) { var material = glTF_KHR_materials_unlit.CreateDefault(); - var renderMode = UniUnlit.Utils.GetRenderMode(m); + var renderMode = UniUnlit.UniUnlitUtil.GetRenderMode(m); if (renderMode == UniUnlitRenderMode.Opaque) { material.alphaMode = glTFBlendMode.OPAQUE.ToString(); @@ -304,7 +304,7 @@ private glTFMaterial Export_UniUnlit(Material m) material.alphaMode = glTFBlendMode.OPAQUE.ToString(); } - var cullMode = UniUnlit.Utils.GetCullMode(m); + var cullMode = UniUnlit.UniUnlitUtil.GetCullMode(m); if (cullMode == UniUnlitCullMode.Off) { material.doubleSided = true; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/VertexColorState.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/VertexColorState.cs index c4e3a3c5e2..8ac9dfbfd8 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/VertexColorState.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/VertexColorState.cs @@ -26,11 +26,11 @@ static bool MaterialUseVertexColor(Material m) { return false; } - if (m.shader.name != UniGLTF.UniUnlit.Utils.ShaderName) + if (m.shader.name != UniGLTF.UniUnlit.UniUnlitUtil.ShaderName) { return false; } - if (UniGLTF.UniUnlit.Utils.GetVColBlendMode(m) != UniGLTF.UniUnlit.UniUnlitVertexColorBlendOp.Multiply) + if (UniGLTF.UniUnlit.UniUnlitUtil.GetVColBlendMode(m) != UniGLTF.UniUnlit.UniUnlitVertexColorBlendOp.Multiply) { return false; } diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/MaterialFactory.cs b/Assets/VRMShaders/GLTF/IO/Runtime/MaterialFactory.cs index a2ede5335c..ead7a2c544 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/MaterialFactory.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/MaterialFactory.cs @@ -24,7 +24,7 @@ public MaterialFactory(IReadOnlyDictionary externalMateri {"VRM/UnlitCutout", "Unlit/Transparent Cutout"}, // 互換性は無いがとりあえず、 {"VRM/UnlitTransparentZWrite", "VRM/MToon"}, - {"UniGLTF/StandardVColor", UniGLTF.UniUnlit.Utils.ShaderName}, + {"UniGLTF/StandardVColor", UniGLTF.UniUnlit.UniUnlitUtil.ShaderName}, }; public struct MaterialLoadInfo diff --git a/Assets/VRMShaders/GLTF/UniUnlit/Editor/UniUnlitEditor.cs b/Assets/VRMShaders/GLTF/UniUnlit/Editor/UniUnlitEditor.cs index 326f129a31..cdfb16e114 100644 --- a/Assets/VRMShaders/GLTF/UniUnlit/Editor/UniUnlitEditor.cs +++ b/Assets/VRMShaders/GLTF/UniUnlit/Editor/UniUnlitEditor.cs @@ -22,12 +22,12 @@ public class UniUnlitEditor : ShaderGUI public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties) { - _mainTex = FindProperty(Utils.PropNameMainTex, properties); - _color = FindProperty(Utils.PropNameColor, properties); - _cutoff = FindProperty(Utils.PropNameCutoff, properties); - _blendMode = FindProperty(Utils.PropNameBlendMode, properties); - _cullMode = FindProperty(Utils.PropNameCullMode, properties); - _vColBlendMode = FindProperty(Utils.PropNameVColBlendMode, properties); + _mainTex = FindProperty(UniUnlitUtil.PropNameMainTex, properties); + _color = FindProperty(UniUnlitUtil.PropNameColor, properties); + _cutoff = FindProperty(UniUnlitUtil.PropNameCutoff, properties); + _blendMode = FindProperty(UniUnlitUtil.PropNameBlendMode, properties); + _cullMode = FindProperty(UniUnlitUtil.PropNameCullMode, properties); + _vColBlendMode = FindProperty(UniUnlitUtil.PropNameVColBlendMode, properties); // _srcBlend = FindProperty(PropNameSrcBlend, properties); // _dstBlend = FindProperty(PropNameDstBlend, properties); // _zWrite = FindProperty(PropNameZWrite, properties); @@ -46,18 +46,18 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader) { var blendMode = UniUnlitRenderMode.Opaque; - if (material.HasProperty(Utils.PropNameStandardShadersRenderMode)) // from Standard shader + if (material.HasProperty(UniUnlitUtil.PropNameStandardShadersRenderMode)) // from Standard shader { - blendMode = (UniUnlitRenderMode) Math.Min(2f, material.GetFloat(Utils.PropNameStandardShadersRenderMode)); + blendMode = (UniUnlitRenderMode) Math.Min(2f, material.GetFloat(UniUnlitUtil.PropNameStandardShadersRenderMode)); } // assigns UniUnlit's properties... base.AssignNewShaderToMaterial(material, oldShader, newShader); // take over old value - material.SetFloat(Utils.PropNameBlendMode, (float) blendMode); + material.SetFloat(UniUnlitUtil.PropNameBlendMode, (float) blendMode); - Utils.ValidateProperties(material, isRenderModeChangedByUser: true); + UniUnlitUtil.ValidateProperties(material, isRenderModeChangedByUser: true); } private void DrawRenderingBox(MaterialEditor materialEditor, Material[] materials) @@ -152,7 +152,7 @@ private static void ModeChanged(Material[] materials, bool isRenderModeChangedBy { foreach (var material in materials) { - Utils.ValidateProperties(material, isRenderModeChangedByUser); + UniUnlitUtil.ValidateProperties(material, isRenderModeChangedByUser); } } } diff --git a/Assets/VRMShaders/GLTF/UniUnlit/Runtime/Utils.cs b/Assets/VRMShaders/GLTF/UniUnlit/Runtime/UniUnlitUtil.cs similarity index 99% rename from Assets/VRMShaders/GLTF/UniUnlit/Runtime/Utils.cs rename to Assets/VRMShaders/GLTF/UniUnlit/Runtime/UniUnlitUtil.cs index 74c474054b..b65680abae 100644 --- a/Assets/VRMShaders/GLTF/UniUnlit/Runtime/Utils.cs +++ b/Assets/VRMShaders/GLTF/UniUnlit/Runtime/UniUnlitUtil.cs @@ -24,7 +24,7 @@ public enum UniUnlitVertexColorBlendOp Multiply = 1, } - public static class Utils + public static class UniUnlitUtil { public const string ShaderName = "UniGLTF/UniUnlit"; public const string PropNameMainTex = "_MainTex"; diff --git a/Assets/VRMShaders/GLTF/UniUnlit/Runtime/UniUnlitUtil.cs.meta b/Assets/VRMShaders/GLTF/UniUnlit/Runtime/UniUnlitUtil.cs.meta new file mode 100644 index 0000000000..6a8bfea5fd --- /dev/null +++ b/Assets/VRMShaders/GLTF/UniUnlit/Runtime/UniUnlitUtil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 318c9e903f457f94589b2c5513d7d914 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRMShaders/GLTF/UniUnlit/Runtime/Utils.cs.meta b/Assets/VRMShaders/GLTF/UniUnlit/Runtime/Utils.cs.meta deleted file mode 100644 index 7824f085e0..0000000000 --- a/Assets/VRMShaders/GLTF/UniUnlit/Runtime/Utils.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: e96cbbd810384352a6799dd731533178 -timeCreated: 1537534399 \ No newline at end of file From a92890f371befa49d5e15ce28964e91e6861af8e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 27 Sep 2021 21:01:35 +0900 Subject: [PATCH 2/2] Implement VRMZWriteMaterialImporter --- .../IO/VRMMaterialDescriptorGenerator.cs | 45 ++++++---- .../Runtime/IO/VRMZWriteMaterialImporter.cs | 87 +++++++++++++++++++ .../IO/VRMZWriteMaterialImporter.cs.meta | 11 +++ 3 files changed, 125 insertions(+), 18 deletions(-) create mode 100644 Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs create mode 100644 Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs.meta diff --git a/Assets/VRM/Runtime/IO/VRMMaterialDescriptorGenerator.cs b/Assets/VRM/Runtime/IO/VRMMaterialDescriptorGenerator.cs index 83c30e42bf..69c293e9e7 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialDescriptorGenerator.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialDescriptorGenerator.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using UniGLTF; +using UniGLTF; using UnityEngine; using VRMShaders; @@ -16,24 +14,35 @@ public VRMMaterialDescriptorGenerator(glTF_VRM_extensions vrm) public MaterialDescriptor Get(GltfData data, int i) { + MaterialDescriptor matDesc; + + // legacy "VRM/UnlitTransparentZWrite" + if (VRMZWriteMaterialImporter.TryCreateParam(data, m_vrm, i, out matDesc)) + { + return matDesc; + } + // mtoon - if (!VRMMToonMaterialImporter.TryCreateParam(data, m_vrm, i, out MaterialDescriptor matDesc)) + if (VRMMToonMaterialImporter.TryCreateParam(data, m_vrm, i, out matDesc)) { - // unlit - if (!GltfUnlitMaterialImporter.TryCreateParam(data, i, out matDesc)) - { - // pbr - if (!GltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc)) - { - // fallback -#if VRM_DEVELOP - Debug.LogWarning($"material: {i} out of range. fallback"); -#endif - return new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, null), GltfPbrMaterialImporter.ShaderName); - } - } + return matDesc; } - return matDesc; + + // unlit + if (GltfUnlitMaterialImporter.TryCreateParam(data, i, out matDesc)) + { + return matDesc; + } + + // pbr + if (GltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc)) + { + return matDesc; + } + + // fallback + Debug.LogWarning($"fallback"); + return new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, null), GltfPbrMaterialImporter.ShaderName); } } } diff --git a/Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs new file mode 100644 index 0000000000..50f958982b --- /dev/null +++ b/Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs @@ -0,0 +1,87 @@ +using UniGLTF; +using UnityEngine; +using VRMShaders; + +namespace VRM +{ + public static class VRMZWriteMaterialImporter + { + public const string ShaderName = "VRM/UnlitTransparentZWrite"; + + public static bool TryCreateParam(GltfData data, glTF_VRM_extensions vrm, int materialIdx, out MaterialDescriptor matDesc) + { + if (vrm?.materialProperties == null || vrm.materialProperties.Count == 0) + { + matDesc = default; + return false; + } + if (materialIdx < 0 || materialIdx >= vrm.materialProperties.Count) + { + matDesc = default; + return false; + } + + var vrmMaterial = vrm.materialProperties[materialIdx]; + if (vrmMaterial.shader != ShaderName) + { + // fallback to gltf + matDesc = default; + return false; + } + + // use material.name, because material name may renamed in GltfParser. + var name = data.GLTF.materials[materialIdx].name; + + // + // import as MToon + // + matDesc = new MaterialDescriptor(name, MToon.Utils.ShaderName); + + matDesc.RenderQueue = vrmMaterial.renderQueue; + + if (vrmMaterial.textureProperties.ContainsKey(MToon.Utils.PropMainTex)) + { + if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial, MToon.Utils.PropMainTex, out var texture)) + { + matDesc.TextureSlots.Add(MToon.Utils.PropMainTex, texture.Item2); + matDesc.TextureSlots.Add(MToon.Utils.PropShadeTexture, texture.Item2); + } + } + + matDesc.Colors[MToon.Utils.PropColor] = Color.white; + matDesc.Colors[MToon.Utils.PropShadeColor] = Color.white; + + foreach (var kv in vrmMaterial.keywordMap) + { + if (kv.Value) + { + matDesc.Actions.Add(material => material.EnableKeyword(kv.Key)); + } + else + { + matDesc.Actions.Add(material => material.DisableKeyword(kv.Key)); + } + } + + foreach (var kv in vrmMaterial.tagMap) + { + matDesc.Actions.Add(material => material.SetOverrideTag(kv.Key, kv.Value)); + } + + if (vrmMaterial.shader == MToon.Utils.ShaderName) + { + // TODO: Material拡張にMToonの項目が追加されたら旧バージョンのshaderPropから変換をかける + // インポート時にUniVRMに含まれるMToonのバージョンに上書きする + matDesc.FloatValues[MToon.Utils.PropVersion] = MToon.Utils.VersionNumber; + } + + matDesc.Actions.Add(m => + { + m.SetFloat(MToon.Utils.PropBlendMode, (float)MToon.RenderMode.TransparentWithZWrite); + MToon.Utils.ValidateProperties(m, true); + }); + + return true; + } + } +} diff --git a/Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs.meta b/Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs.meta new file mode 100644 index 0000000000..0300100638 --- /dev/null +++ b/Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 51208fe94c21c4b4abb16fd89a3aa7f2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: