From 61415a927d43418fec709d3a4a4b013e509f9f20 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Fri, 22 Oct 2021 16:06:45 +0900 Subject: [PATCH 1/6] =?UTF-8?q?TransparentZwrite=20=E3=81=AE=E3=83=95?= =?UTF-8?q?=E3=82=A9=E3=83=BC=E3=83=AB=E3=83=90=E3=83=83=E3=82=AF=E6=99=82?= =?UTF-8?q?=E3=81=AE=E6=8C=87=E5=AE=9A=E3=82=92=E6=98=8E=E7=A4=BA=E7=9A=84?= =?UTF-8?q?=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...MUnlitTransparentZWriteMaterialImporter.cs | 126 +++++++++++++----- 1 file changed, 92 insertions(+), 34 deletions(-) diff --git a/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs index 72fdfc727..28bf33f53 100644 --- a/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs @@ -9,6 +9,7 @@ namespace VRM public static class VRMUnlitTransparentZWriteMaterialImporter { public const string ShaderName = "VRM/UnlitTransparentZWrite"; + public const string MainTexturePropName = "_MainTex"; public static bool TryCreateParam(GltfData data, glTF_VRM_extensions vrm, int materialIdx, out MaterialDescriptor matDesc) { @@ -26,7 +27,6 @@ namespace VRM var vrmMaterial = vrm.materialProperties[materialIdx]; if (vrmMaterial.shader != ShaderName) { - // fallback to gltf matDesc = default; return false; } @@ -34,50 +34,108 @@ namespace VRM // 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 = MToon.Utils.GetRenderQueueRequirement(RenderMode.TransparentWithZWrite).DefaultValue; - - // NOTE: Unlit のフォールバックなので、 Lit/Shade 色は黒とし、Emissive Factor に設定する. - // また、元のシェーダのうちユーザが設定できるプロパティは Texture のみ. - matDesc.Colors[MToon.Utils.PropColor] = Color.black; - matDesc.Colors[MToon.Utils.PropShadeColor] = Color.black; - matDesc.Colors[MToon.Utils.PropEmissionColor] = Color.white; - - if (vrmMaterial.textureProperties.ContainsKey(MToon.Utils.PropMainTex)) + if (vrmMaterial.textureProperties.ContainsKey(MainTexturePropName)) { - if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial, MToon.Utils.PropMainTex, out var texture)) + if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial, MainTexturePropName, out var texture)) { - matDesc.TextureSlots.Add(MToon.Utils.PropEmissionMap, texture.Item2); + matDesc.TextureSlots.Add(MToon.Utils.PropMainTex, texture.Item2); } } matDesc.Actions.Add(unityMaterial => { - // NOTE: ZWrite などの属性は util に設定させる. - var parameter = MToon.Utils.GetMToonParametersFromMaterial(unityMaterial); - parameter.Rendering.CullMode = CullMode.Back; - parameter.Rendering.RenderMode = RenderMode.TransparentWithZWrite; - parameter.Rendering.RenderQueueOffsetNumber = 0; - MToon.Utils.SetMToonParametersToMaterial(unityMaterial, parameter); - }); - - 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); + var mainTexture = (Texture2D) unityMaterial.GetTexture(MToon.Utils.PropMainTex); + + // NOTE: Unlit のフォールバックなので + // Lit/Shade 色は黒として、Alpha のために Lit にテクスチャを設定する. + // Emissive 色は白として、テクスチャを設定する. + // また、元のシェーダのうちユーザが設定できるプロパティは Texture のみ. + var def = new MToonDefinition + { + Meta = new MetaDefinition + { + Implementation = MToon.Utils.Implementation, + VersionNumber = MToon.Utils.VersionNumber, + }, + Rendering = new RenderingDefinition + { + RenderMode = RenderMode.TransparentWithZWrite, + CullMode = CullMode.Back, + RenderQueueOffsetNumber = 0, + }, + Color = new ColorDefinition + { + LitColor = Color.black, + LitMultiplyTexture = mainTexture, + ShadeColor = Color.black, + ShadeMultiplyTexture = default, + CutoutThresholdValue = 0.5f, + }, + Lighting = new LightingDefinition + { + LitAndShadeMixing = new LitAndShadeMixingDefinition + { + ShadingShiftValue = 0, + ShadingToonyValue = 1, + ShadowReceiveMultiplierValue = 1, + ShadowReceiveMultiplierMultiplyTexture = default, + LitAndShadeMixingMultiplierValue = 1, + LitAndShadeMixingMultiplierMultiplyTexture = default, + }, + LightingInfluence = new LightingInfluenceDefinition + { + LightColorAttenuationValue = 0, + GiIntensityValue = 0.1f, + }, + Normal = new NormalDefinition + { + NormalTexture = default, + NormalScaleValue = 1, + }, + }, + Emission = new EmissionDefinition + { + EmissionColor = Color.white, + EmissionMultiplyTexture = mainTexture, + }, + MatCap = new MatCapDefinition + { + AdditiveTexture = default, + }, + Rim = new RimDefinition + { + RimColor = Color.black, + RimMultiplyTexture = default, + RimLightingMixValue = 1, + RimLiftValue = 0, + RimFresnelPowerValue = 1, + }, + Outline = new OutlineDefinition + { + OutlineWidthMode = OutlineWidthMode.None, + OutlineWidthValue = 0, + OutlineWidthMultiplyTexture = default, + OutlineScaledMaxDistanceValue = 1, + OutlineColorMode = OutlineColorMode.FixedColor, + OutlineColor = Color.black, + OutlineLightingMixValue = 1, + }, + TextureOption = new TextureUvCoordsDefinition + { + MainTextureLeftBottomOriginScale = new Vector2(1, 1), + MainTextureLeftBottomOriginOffset = new Vector2(0, 0), + UvAnimationMaskTexture = default, + UvAnimationScrollXSpeedValue = 0, + UvAnimationScrollYSpeedValue = 0, + UvAnimationRotationSpeedValue = 0, + }, + }; + MToon.Utils.SetMToonParametersToMaterial(unityMaterial, def); }); + Debug.LogWarning($"fallback: {ShaderName} => {MToon.Utils.ShaderName}"); return true; } } From bd982eae8fe5488330d769c3c2a5d55ddb6a2a82 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Fri, 22 Oct 2021 16:31:58 +0900 Subject: [PATCH 2/6] rename --- .../IO/VRMUnlitTransparentZWriteMaterialImporter.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs index 28bf33f53..9c1a494f0 100644 --- a/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs @@ -8,8 +8,8 @@ namespace VRM { public static class VRMUnlitTransparentZWriteMaterialImporter { - public const string ShaderName = "VRM/UnlitTransparentZWrite"; - public const string MainTexturePropName = "_MainTex"; + public const string UnlitTransparentZWriteShaderName = "VRM/UnlitTransparentZWrite"; + public const string UnlitTransparentZWriteMainTexturePropName = "_MainTex"; public static bool TryCreateParam(GltfData data, glTF_VRM_extensions vrm, int materialIdx, out MaterialDescriptor matDesc) { @@ -25,7 +25,7 @@ namespace VRM } var vrmMaterial = vrm.materialProperties[materialIdx]; - if (vrmMaterial.shader != ShaderName) + if (vrmMaterial.shader != UnlitTransparentZWriteShaderName) { matDesc = default; return false; @@ -36,9 +36,9 @@ namespace VRM matDesc = new MaterialDescriptor(name, MToon.Utils.ShaderName); - if (vrmMaterial.textureProperties.ContainsKey(MainTexturePropName)) + if (vrmMaterial.textureProperties.ContainsKey(UnlitTransparentZWriteMainTexturePropName)) { - if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial, MainTexturePropName, out var texture)) + if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial, UnlitTransparentZWriteMainTexturePropName, out var texture)) { matDesc.TextureSlots.Add(MToon.Utils.PropMainTex, texture.Item2); } @@ -135,7 +135,7 @@ namespace VRM MToon.Utils.SetMToonParametersToMaterial(unityMaterial, def); }); - Debug.LogWarning($"fallback: {ShaderName} => {MToon.Utils.ShaderName}"); + Debug.LogWarning($"fallback: {UnlitTransparentZWriteShaderName} => {MToon.Utils.ShaderName}"); return true; } } From 4b0cfe972e110f6b8eb839b8437a01c9c6af2e79 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Fri, 22 Oct 2021 16:38:40 +0900 Subject: [PATCH 3/6] comment --- .../VRMUnlitTransparentZWriteMaterialImporter.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs index 9c1a494f0..6afbf1c4f 100644 --- a/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs @@ -61,15 +61,18 @@ namespace VRM }, Rendering = new RenderingDefinition { + // NOTE: Transparent ZWrite RenderMode = RenderMode.TransparentWithZWrite, CullMode = CullMode.Back, RenderQueueOffsetNumber = 0, }, Color = new ColorDefinition { - LitColor = Color.black, + // NOTE: Unlit なので、RGB 値は黒とする。 + // NOTE: Alpha は使うので、テクスチャを設定する. + LitColor = new Color(0, 0, 0, 1), LitMultiplyTexture = mainTexture, - ShadeColor = Color.black, + ShadeColor = new Color(0, 0, 0, 1), ShadeMultiplyTexture = default, CutoutThresholdValue = 0.5f, }, @@ -77,6 +80,7 @@ namespace VRM { LitAndShadeMixing = new LitAndShadeMixingDefinition { + // NOTE: default value ShadingShiftValue = 0, ShadingToonyValue = 1, ShadowReceiveMultiplierValue = 1, @@ -86,26 +90,31 @@ namespace VRM }, LightingInfluence = new LightingInfluenceDefinition { + // NOTE: default value LightColorAttenuationValue = 0, GiIntensityValue = 0.1f, }, Normal = new NormalDefinition { + // NOTE: default value NormalTexture = default, NormalScaleValue = 1, }, }, Emission = new EmissionDefinition { + // NOTE: Unlit なので Emission にテクスチャを設定する. EmissionColor = Color.white, EmissionMultiplyTexture = mainTexture, }, MatCap = new MatCapDefinition { + // NOTE: default value AdditiveTexture = default, }, Rim = new RimDefinition { + // NOTE: default value RimColor = Color.black, RimMultiplyTexture = default, RimLightingMixValue = 1, @@ -114,6 +123,7 @@ namespace VRM }, Outline = new OutlineDefinition { + // NOTE: default value OutlineWidthMode = OutlineWidthMode.None, OutlineWidthValue = 0, OutlineWidthMultiplyTexture = default, @@ -124,6 +134,7 @@ namespace VRM }, TextureOption = new TextureUvCoordsDefinition { + // NOTE: default value MainTextureLeftBottomOriginScale = new Vector2(1, 1), MainTextureLeftBottomOriginOffset = new Vector2(0, 0), UvAnimationMaskTexture = default, From fe25feaed6516547eedb6fe61caee41aa6505ed2 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Fri, 22 Oct 2021 16:39:23 +0900 Subject: [PATCH 4/6] refactor --- .../Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs index 6afbf1c4f..691aca340 100644 --- a/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs @@ -52,7 +52,7 @@ namespace VRM // Lit/Shade 色は黒として、Alpha のために Lit にテクスチャを設定する. // Emissive 色は白として、テクスチャを設定する. // また、元のシェーダのうちユーザが設定できるプロパティは Texture のみ. - var def = new MToonDefinition + MToon.Utils.SetMToonParametersToMaterial(unityMaterial, new MToonDefinition { Meta = new MetaDefinition { @@ -142,8 +142,7 @@ namespace VRM UvAnimationScrollYSpeedValue = 0, UvAnimationRotationSpeedValue = 0, }, - }; - MToon.Utils.SetMToonParametersToMaterial(unityMaterial, def); + }); }); Debug.LogWarning($"fallback: {UnlitTransparentZWriteShaderName} => {MToon.Utils.ShaderName}"); From efbb5aca0c54d134187ed7c02ae54eb10e3c4e4f Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Fri, 22 Oct 2021 17:09:52 +0900 Subject: [PATCH 5/6] =?UTF-8?q?renderQueue=20=E3=82=92=E5=BE=A9=E5=B8=B0?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs index 691aca340..dee6fd95f 100644 --- a/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMUnlitTransparentZWriteMaterialImporter.cs @@ -143,6 +143,9 @@ namespace VRM UvAnimationRotationSpeedValue = 0, }, }); + + // NOTE: MToon として正しくはないが、やむをえず renderQueue を元の値で復帰する. + unityMaterial.renderQueue = vrmMaterial.renderQueue; }); Debug.LogWarning($"fallback: {UnlitTransparentZWriteShaderName} => {MToon.Utils.ShaderName}"); From 4d6bf7d61402d559313ca9d1a08bba772bc66a8e Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Fri, 22 Oct 2021 17:10:13 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E3=83=95?= =?UTF-8?q?=E3=82=A9=E3=83=BC=E3=83=AB=E3=83=90=E3=83=83=E3=82=AF=E3=81=AB?= =?UTF-8?q?=E3=81=AA=E3=81=A3=E3=81=9F=E3=81=AE=E3=81=A7=E5=89=8A=E9=99=A4?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GLTF/IO/Runtime/Material/Importer/MaterialFactory.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Material/Importer/MaterialFactory.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Material/Importer/MaterialFactory.cs index ead7a2c54..f8f0011ac 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Material/Importer/MaterialFactory.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Material/Importer/MaterialFactory.cs @@ -17,13 +17,12 @@ namespace VRMShaders m_externalMap = externalMaterialMap; } + // TODO: UniVRM 0.x の方に処理を移したい static Dictionary s_fallbackShaders = new Dictionary { {"VRM/UnlitTexture", "Unlit/Texture"}, {"VRM/UnlitTransparent", "Unlit/Transparent"}, {"VRM/UnlitCutout", "Unlit/Transparent Cutout"}, - // 互換性は無いがとりあえず、 - {"VRM/UnlitTransparentZWrite", "VRM/MToon"}, {"UniGLTF/StandardVColor", UniGLTF.UniUnlit.UniUnlitUtil.ShaderName}, };