From a92890f371befa49d5e15ce28964e91e6861af8e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 27 Sep 2021 21:01:35 +0900 Subject: [PATCH] Implement VRMZWriteMaterialImporter --- .../IO/VRMMaterialDescriptorGenerator.cs | 47 ++++++---- .../Runtime/IO/VRMZWriteMaterialImporter.cs | 87 +++++++++++++++++++ .../IO/VRMZWriteMaterialImporter.cs.meta | 11 +++ 3 files changed, 126 insertions(+), 19 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 83c30e42b..69c293e9e 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 @@ namespace VRM public MaterialDescriptor Get(GltfData data, int i) { - // mtoon - if (!VRMMToonMaterialImporter.TryCreateParam(data, m_vrm, i, out MaterialDescriptor matDesc)) + MaterialDescriptor matDesc; + + // legacy "VRM/UnlitTransparentZWrite" + if (VRMZWriteMaterialImporter.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; + + // mtoon + if (VRMMToonMaterialImporter.TryCreateParam(data, m_vrm, i, out 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 000000000..50f958982 --- /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 000000000..030010063 --- /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: