diff --git a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs index 407ea4515..c7ecde86b 100644 --- a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs @@ -1,12 +1,10 @@ using System; -using MToon; using UniGLTF; using UniGLTF.Extensions.VRMC_materials_mtoon; using UnityEngine; using VRMShaders; +using VRMShaders.VRM10.MToon10.Runtime; using ColorSpace = VRMShaders.ColorSpace; -using OutlineWidthMode = MToon.OutlineWidthMode; -using RenderMode = MToon.RenderMode; namespace UniVRM10 { @@ -16,48 +14,79 @@ namespace UniVRM10 { try { - if (src.shader.name != MToon.Utils.ShaderName) + if (src.shader.name != MToon10Meta.UnityShaderName) { dst = null; return false; } - // convert MToon intermediate value from UnityEngine.Material - var def = MToon.Utils.GetMToonParametersFromMaterial(src); + // Get MToon10 Context + var context = new MToon10Context(src); + context.Validate(); // base material dst = glTF_KHR_materials_unlit.CreateDefault(); dst.name = src.name; + // vrmc_materials_mtoon ext + var mtoon = new UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon(); + mtoon.Version = ""; + // Rendering - dst.doubleSided = def.Rendering.CullMode == CullMode.Off; - dst.alphaMode = ExportAlphaMode(def.Rendering.RenderMode); - dst.alphaCutoff = Mathf.Max(def.Color.CutoutThresholdValue, 0); + dst.alphaMode = ExportAlphaMode(context.AlphaMode); + mtoon.TransparentWithZWrite = context.TransparentWithZWriteMode == MToon10TransparentWithZWriteMode.On; + dst.alphaCutoff = Mathf.Max(0, context.AlphaCutoff); + mtoon.RenderQueueOffsetNumber = context.RenderQueueOffsetNumber; + dst.doubleSided = context.DoubleSidedMode == MToon10DoubleSidedMode.On; // Lighting - dst.pbrMetallicRoughness = new glTFPbrMetallicRoughness + dst.pbrMetallicRoughness = new glTFPbrMetallicRoughness(); + dst.pbrMetallicRoughness.baseColorFactor = context.BaseColorFactorSrgb.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear); + var baseColorTextureIndex = textureExporter.ExportAsSRgb(context.BaseColorTexture); + if (baseColorTextureIndex != -1) { - baseColorFactor = def.Color.LitColor.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear), - baseColorTexture = new glTFMaterialBaseColorTextureInfo + dst.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo { - index = textureExporter.ExportAsSRgb(def.Color.LitMultiplyTexture), - }, - }; - - // Normal - var normalTextureIndex = textureExporter.ExportAsNormal(def.Lighting.Normal.NormalTexture); + index = baseColorTextureIndex, + }; + } + mtoon.ShadeColorFactor = context.ShadeColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear); + var shadeColorTextureIndex = textureExporter.ExportAsSRgb(context.ShadeColorTexture); + if (shadeColorTextureIndex != -1) + { + mtoon.ShadeMultiplyTexture = new TextureInfo + { + Index = shadeColorTextureIndex, + }; + } + var normalTextureIndex = textureExporter.ExportAsNormal(context.NormalTexture); if (normalTextureIndex != -1) { dst.normalTexture = new glTFMaterialNormalTextureInfo { index = normalTextureIndex, - scale = def.Lighting.Normal.NormalScaleValue, + scale = context.NormalTextureScale, }; } + mtoon.ShadingShiftFactor = context.ShadingShiftFactor; + var shadingShiftTextureIndex = textureExporter.ExportAsLinear(context.ShadingShiftTexture); + if (shadingShiftTextureIndex != -1) + { + mtoon.ShadingShiftTexture = new ShadingShiftTextureInfo + { + Index = shadingShiftTextureIndex, + Scale = context.ShadingShiftTextureScale, + }; + } + mtoon.ShadingToonyFactor = context.ShadingToonyFactor; + + // GI + // TODO: update schema + mtoon.GiIntensityFactor = context.GiEqualizationFactor; // Emission - dst.emissiveFactor = def.Emission.EmissionColor.ToFloat3(ColorSpace.Linear, ColorSpace.Linear); - var emissiveTextureIndex = textureExporter.ExportAsSRgb(def.Emission.EmissionMultiplyTexture); + dst.emissiveFactor = context.EmissiveFactorLinear.ToFloat3(ColorSpace.Linear, ColorSpace.Linear); + var emissiveTextureIndex = textureExporter.ExportAsSRgb(context.EmissiveTexture); if (emissiveTextureIndex != -1) { dst.emissiveTexture = new glTFMaterialEmissiveTextureInfo @@ -66,72 +95,67 @@ namespace UniVRM10 }; } - const float centimeterToMeter = 0.01f; - const float invertY = -1f; - - // VRMC_materials_mtoon - var mtoon = new UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon + // Rim Lighting + var matcapTextureIndex = textureExporter.ExportAsSRgb(context.MatcapTexture); + if (matcapTextureIndex != -1) { - Version = "", - - // Rendering - TransparentWithZWrite = def.Rendering.RenderMode == RenderMode.TransparentWithZWrite, - RenderQueueOffsetNumber = ExportRenderQueueOffset(def.Rendering.RenderMode, def.Rendering.RenderQueueOffsetNumber), - - // Lighting - ShadeColorFactor = def.Color.ShadeColor.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear), - ShadeMultiplyTexture = new TextureInfo + mtoon.MatcapTexture = new TextureInfo { - Index = textureExporter.ExportAsSRgb(def.Color.ShadeMultiplyTexture), - }, - ShadingToonyFactor = def.Lighting.LitAndShadeMixing.ShadingToonyValue, - ShadingShiftFactor = def.Lighting.LitAndShadeMixing.ShadingShiftValue, - ShadingShiftTexture = null, - - // Global Illumination - GiIntensityFactor = def.Lighting.LightingInfluence.GiIntensityValue, - - // Rim Lighting - MatcapTexture = new TextureInfo + Index = matcapTextureIndex, + }; + } + mtoon.ParametricRimColorFactor = context.ParametricRimColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear); + mtoon.ParametricRimFresnelPowerFactor = context.ParametricRimFresnelPowerFactor; + mtoon.ParametricRimLiftFactor = context.ParametricRimLiftFactor; + var rimMultiplyTextureIndex = textureExporter.ExportAsSRgb(context.RimMultiplyTexture); + if (rimMultiplyTextureIndex != -1) + { + mtoon.RimMultiplyTexture = new TextureInfo { - Index = textureExporter.ExportAsSRgb(def.MatCap.AdditiveTexture), - }, - ParametricRimColorFactor = def.Rim.RimColor.ToFloat3(ColorSpace.Linear, ColorSpace.Linear), - ParametricRimFresnelPowerFactor = def.Rim.RimFresnelPowerValue, - ParametricRimLiftFactor = def.Rim.RimLiftValue, - RimMultiplyTexture = new TextureInfo - { - Index = textureExporter.ExportAsSRgb(def.Rim.RimMultiplyTexture), - }, - RimLightingMixFactor = def.Rim.RimLightingMixValue, + Index = rimMultiplyTextureIndex, + }; + } + mtoon.RimLightingMixFactor = context.RimLightingMixFactor; - // Outline - OutlineWidthMode = ExportOutlineWidthMode(def.Outline.OutlineWidthMode), - OutlineWidthFactor = def.Outline.OutlineWidthValue * centimeterToMeter, - OutlineWidthMultiplyTexture = new TextureInfo + // Outline + mtoon.OutlineWidthMode = ExportOutlineWidthMode(context.OutlineWidthMode); + mtoon.OutlineWidthFactor = context.OutlineWidthFactor; + var outlineWidthMultiplyTextureIndex = textureExporter.ExportAsLinear(context.OutlineWidthMultiplyTexture); + if (outlineWidthMultiplyTextureIndex != -1) + { + mtoon.OutlineWidthMultiplyTexture = new TextureInfo { - Index = textureExporter.ExportAsLinear(def.Outline.OutlineWidthMultiplyTexture), - }, - OutlineColorFactor = def.Outline.OutlineColor.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear), - OutlineLightingMixFactor = ExportOutlineLightingMixFactor(def.Outline.OutlineColorMode, def.Outline.OutlineLightingMixValue), + Index = outlineWidthMultiplyTextureIndex, + }; + } + mtoon.OutlineColorFactor = context.OutlineColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear); + mtoon.OutlineLightingMixFactor = context.OutlineLightingMixFactor; - // UV Anim - UvAnimationMaskTexture = new TextureInfo + // UV Animation + var uvAnimationMaskTextureIndex = textureExporter.ExportAsLinear(context.UvAnimationMaskTexture); + if (uvAnimationMaskTextureIndex != -1) + { + mtoon.UvAnimationMaskTexture = new TextureInfo { - Index = textureExporter.ExportAsLinear(def.TextureOption.UvAnimationMaskTexture), - }, - UvAnimationScrollXSpeedFactor = def.TextureOption.UvAnimationScrollXSpeedValue, - UvAnimationScrollYSpeedFactor = def.TextureOption.UvAnimationScrollYSpeedValue * invertY, - UvAnimationRotationSpeedFactor = def.TextureOption.UvAnimationRotationSpeedValue, - }; + Index = uvAnimationMaskTextureIndex, + }; + } + mtoon.UvAnimationScrollXSpeedFactor = context.UvAnimationScrollXSpeedFactor; + { + // Coordinate Conversion + const float invertY = -1f; + mtoon.UvAnimationScrollYSpeedFactor = context.UvAnimationScrollYSpeedFactor * invertY; + } + mtoon.UvAnimationRotationSpeedFactor = context.UvAnimationRotationSpeedFactor; // Texture Transforms - var scale = def.TextureOption.MainTextureLeftBottomOriginScale; - var offset = def.TextureOption.MainTextureLeftBottomOriginOffset; + var scale = context.TextureScale; + var offset = context.TextureOffset; ExportTextureTransform(dst.pbrMetallicRoughness.baseColorTexture, scale, offset); ExportTextureTransform(dst.emissiveTexture, scale, offset); ExportTextureTransform(dst.normalTexture, scale, offset); ExportTextureTransform(mtoon.ShadeMultiplyTexture, scale, offset); + ExportTextureTransform(mtoon.ShadingShiftTexture, scale, offset); ExportTextureTransform(mtoon.MatcapTexture, scale, offset); ExportTextureTransform(mtoon.RimMultiplyTexture, scale, offset); ExportTextureTransform(mtoon.OutlineWidthMultiplyTexture, scale, offset); @@ -148,70 +172,36 @@ namespace UniVRM10 } } - private static string ExportAlphaMode(MToon.RenderMode renderMode) + private static string ExportAlphaMode(MToon10AlphaMode alphaMode) { - switch (renderMode) + switch (alphaMode) { - case RenderMode.Opaque: + case MToon10AlphaMode.Opaque: return "OPAQUE"; - case RenderMode.Cutout: + case MToon10AlphaMode.Cutout: return "MASK"; - case RenderMode.Transparent: - return "BLEND"; - case RenderMode.TransparentWithZWrite: + case MToon10AlphaMode.Transparent: return "BLEND"; default: - throw new ArgumentOutOfRangeException(nameof(renderMode), renderMode, null); + throw new ArgumentOutOfRangeException(nameof(alphaMode), alphaMode, null); } } - private static int ExportRenderQueueOffset(MToon.RenderMode renderMode, int offset) - { - switch (renderMode) - { - case RenderMode.Opaque: - return 0; - case RenderMode.Cutout: - return 0; - case RenderMode.Transparent: - return Mathf.Clamp(offset, -9, 0); - case RenderMode.TransparentWithZWrite: - return Mathf.Clamp(offset, 0, +9); - default: - throw new ArgumentOutOfRangeException(nameof(renderMode), renderMode, null); - } - } - - private static UniGLTF.Extensions.VRMC_materials_mtoon.OutlineWidthMode ExportOutlineWidthMode( - MToon.OutlineWidthMode mode) + private static OutlineWidthMode ExportOutlineWidthMode(MToon10OutlineMode mode) { switch (mode) { - case OutlineWidthMode.None: - return UniGLTF.Extensions.VRMC_materials_mtoon.OutlineWidthMode.none; - case OutlineWidthMode.WorldCoordinates: - return UniGLTF.Extensions.VRMC_materials_mtoon.OutlineWidthMode.worldCoordinates; - case OutlineWidthMode.ScreenCoordinates: - return UniGLTF.Extensions.VRMC_materials_mtoon.OutlineWidthMode.screenCoordinates; + case MToon10OutlineMode.None: + return OutlineWidthMode.none; + case MToon10OutlineMode.World: + return OutlineWidthMode.worldCoordinates; + case MToon10OutlineMode.Screen: + return OutlineWidthMode.screenCoordinates; default: throw new ArgumentOutOfRangeException(nameof(mode), mode, null); } } - private static float ExportOutlineLightingMixFactor(OutlineColorMode mode, float mixValue) - { - switch (mode) - { - case OutlineColorMode.FixedColor: - return 0; - case OutlineColorMode.MixedLighting: - return mixValue; - default: - throw new ArgumentOutOfRangeException(nameof(mode), mode, null); - } - } - - private static void ExportTextureTransform(glTFTextureInfo textureInfo, Vector2 unityScale, Vector2 unityOffset) { if (textureInfo == null) @@ -234,6 +224,16 @@ namespace UniVRM10 textureInfo.Extensions = gltfTextureInfo.extensions; } + private static void ExportTextureTransform(ShadingShiftTextureInfo textureInfo, Vector2 unityScale, Vector2 unityOffset) + { + // Generate extension to empty holder. + var gltfTextureInfo = new EmptyGltfTextureInfo(); + ExportTextureTransform(gltfTextureInfo, unityScale, unityOffset); + + // Copy extension from empty holder. + textureInfo.Extensions = gltfTextureInfo.extensions; + } + private sealed class EmptyGltfTextureInfo : glTFTextureInfo { diff --git a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialImporter.cs b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialImporter.cs index 762f45b05..a871a9d88 100644 --- a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialImporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialImporter.cs @@ -4,6 +4,7 @@ using UniGLTF; using UniGLTF.Extensions.VRMC_materials_mtoon; using UnityEngine; using VRMShaders; +using VRMShaders.VRM10.MToon10.Runtime; using ColorSpace = VRMShaders.ColorSpace; using OutlineWidthMode = UniGLTF.Extensions.VRMC_materials_mtoon.OutlineWidthMode; @@ -29,7 +30,7 @@ namespace UniVRM10 } // use material.name, because material name may renamed in GltfParser. - matDesc = new MaterialDescriptor(m.name, MToon.Utils.ShaderName); + matDesc = new MaterialDescriptor(m.name, MToon10Meta.UnityShaderName); foreach (var (key, (subAssetKey, value)) in Vrm10MToonTextureImporter.EnumerateAllTextures(parser, m, mtoon)) { @@ -51,12 +52,10 @@ namespace UniVRM10 matDesc.Vectors.Add(key, value); } - matDesc.RenderQueue = TryGetRenderQueue(m, mtoon); - matDesc.Actions.Add(material => { // Set hidden properties, keywords from float properties. - MToon.Utils.ValidateProperties(material, isBlendModeChangedByUser: false); + new MToonValidator(material).Validate(); }); return true; @@ -66,148 +65,169 @@ namespace UniVRM10 { const ColorSpace gltfColorSpace = ColorSpace.Linear; + // Rendering var baseColor = material?.pbrMetallicRoughness?.baseColorFactor?.ToColor4(gltfColorSpace, ColorSpace.sRGB); if (baseColor.HasValue) { - yield return (MToon.Utils.PropColor, baseColor.Value); - } - - var emissionColor = material?.emissiveFactor?.ToColor3(gltfColorSpace, ColorSpace.Linear); - if (emissionColor.HasValue) - { - yield return (MToon.Utils.PropEmissionColor, emissionColor.Value); + yield return (MToon10Prop.BaseColorFactor.ToUnityShaderLabName(), baseColor.Value); } + // Lighting var shadeColor = mToon?.ShadeColorFactor?.ToColor3(gltfColorSpace, ColorSpace.sRGB); if (shadeColor.HasValue) { - yield return (MToon.Utils.PropShadeColor, shadeColor.Value); + yield return (MToon10Prop.ShadeColorFactor.ToUnityShaderLabName(), shadeColor.Value); } + // GI + + // Emission + var emissionColor = material?.emissiveFactor?.ToColor3(gltfColorSpace, ColorSpace.Linear); + if (emissionColor.HasValue) + { + yield return (MToon10Prop.EmissiveFactor.ToUnityShaderLabName(), emissionColor.Value); + } + + // Rim Lighting var rimColor = mToon?.ParametricRimColorFactor?.ToColor3(gltfColorSpace, ColorSpace.Linear); if (rimColor.HasValue) { - yield return (MToon.Utils.PropRimColor, rimColor.Value); + yield return (MToon10Prop.ParametricRimColorFactor.ToUnityShaderLabName(), rimColor.Value); } + // Outline var outlineColor = mToon?.OutlineColorFactor?.ToColor3(gltfColorSpace, ColorSpace.sRGB); if (outlineColor.HasValue) { - yield return (MToon.Utils.PropOutlineColor, outlineColor.Value); + yield return (MToon10Prop.OutlineColorFactor.ToUnityShaderLabName(), outlineColor.Value); } + + // UV Animation } public static IEnumerable<(string key, float value)> TryGetAllFloats(glTFMaterial material, VRMC_materials_mtoon mToon) { - var renderMode = GetMToonRenderMode(material, mToon); + // Rendering + var alphaMode = GetMToon10AlphaMode(material); { - yield return (MToon.Utils.PropBlendMode, (float) renderMode); + yield return (MToon10Prop.AlphaMode.ToUnityShaderLabName(), (float) alphaMode); } - var cullMode = GetMToonCullMode(material, mToon); + var transparentWithZWrite = GetMToon10TransparentWithZWriteMode(material, mToon); { - yield return (MToon.Utils.PropCullMode, (float) cullMode); - } - - var outlineMode = GetMToonOutlineWidthMode(material, mToon); - { - yield return (MToon.Utils.PropOutlineWidthMode, (float) outlineMode); - - // In case of VRM 1.0 MToon, outline color mode is always MixedLighting. - yield return (MToon.Utils.PropOutlineColorMode, (float) MToon.OutlineColorMode.MixedLighting); + yield return (MToon10Prop.TransparentWithZWrite.ToUnityShaderLabName(), (float) transparentWithZWrite); } var cutoff = material?.alphaCutoff; if (cutoff.HasValue) { - yield return (MToon.Utils.PropCutoff, cutoff.Value); + yield return (MToon10Prop.AlphaCutoff.ToUnityShaderLabName(), cutoff.Value); } + var renderQueueOffset = mToon?.RenderQueueOffsetNumber; + if (renderQueueOffset.HasValue) + { + yield return (MToon10Prop.RenderQueueOffsetNumber.ToUnityShaderLabName(), (float) renderQueueOffset); + } + + var doubleSidedMode = GetMToon10DoubleSidedMode(material, mToon); + { + yield return (MToon10Prop.DoubleSided.ToUnityShaderLabName(), (float) doubleSidedMode); + } + + // Lighting var normalScale = material?.normalTexture?.scale; if (normalScale.HasValue) { - yield return ("_BumpScale", normalScale.Value); + yield return (MToon10Prop.NormalTextureScale.ToUnityShaderLabName(), normalScale.Value); } var shadingShift = mToon?.ShadingShiftFactor; if (shadingShift.HasValue) { - yield return (MToon.Utils.PropShadeShift, shadingShift.Value); + yield return (MToon10Prop.ShadingShiftFactor.ToUnityShaderLabName(), shadingShift.Value); } var shadingShiftTextureScale = mToon?.ShadingShiftTexture?.Scale; if (shadingShiftTextureScale.HasValue) { - Debug.LogWarning("Need VRM 1.0 MToon implementation."); - yield return ("_NEED_IMPLEMENTATION_MTOON_1_0_shadingShiftTextureScale", shadingShiftTextureScale.Value); + yield return (MToon10Prop.ShadingShiftTextureScale.ToUnityShaderLabName(), shadingShiftTextureScale.Value); } var shadingToony = mToon?.ShadingToonyFactor; if (shadingToony.HasValue) { - yield return (MToon.Utils.PropShadeToony, shadingToony.Value); + yield return (MToon10Prop.ShadingToonyFactor.ToUnityShaderLabName(), shadingToony.Value); } - var giIntensity = mToon?.GiIntensityFactor; - if (giIntensity.HasValue) + // GI + var giEqualization = mToon?.GiIntensityFactor; + if (giEqualization.HasValue) { - yield return (MToon.Utils.PropIndirectLightIntensity, giIntensity.Value); + // TODO: Update schema + yield return (MToon10Prop.GiEqualizationFactor.ToUnityShaderLabName(), giEqualization.Value); } - var rimLightMix = mToon?.RimLightingMixFactor; - if (rimLightMix.HasValue) - { - yield return (MToon.Utils.PropRimLightingMix, rimLightMix.Value); - } + // Emission + // Rim Lighting var rimFresnelPower = mToon?.ParametricRimFresnelPowerFactor; if (rimFresnelPower.HasValue) { - yield return (MToon.Utils.PropRimFresnelPower, rimFresnelPower.Value); + yield return (MToon10Prop.ParametricRimFresnelPowerFactor.ToUnityShaderLabName(), rimFresnelPower.Value); } var rimLift = mToon?.ParametricRimLiftFactor; if (rimLift.HasValue) { - yield return (MToon.Utils.PropRimLift, rimLift.Value); + yield return (MToon10Prop.ParametricRimLiftFactor.ToUnityShaderLabName(), rimLift.Value); + } + + var rimLightMix = mToon?.RimLightingMixFactor; + if (rimLightMix.HasValue) + { + yield return (MToon10Prop.RimLightingMixFactor.ToUnityShaderLabName(), rimLightMix.Value); + } + + // Outline + var outlineMode = GetMToon10OutlineWidthMode(material, mToon); + { + yield return (MToon10Prop.OutlineWidthMode.ToUnityShaderLabName(), (float) outlineMode); } - // Unit conversion. - // Because Unity implemented MToon uses centimeter unit in width parameter. var outlineWidth = mToon?.OutlineWidthFactor; if (outlineWidth.HasValue) { - const float meterToCentimeter = 100f; - - yield return (MToon.Utils.PropOutlineWidth, outlineWidth.Value * meterToCentimeter); + yield return (MToon10Prop.OutlineWidthFactor.ToUnityShaderLabName(), outlineWidth.Value); } var outlineLightMix = mToon?.OutlineLightingMixFactor; if (outlineLightMix.HasValue) { - yield return (MToon.Utils.PropOutlineLightingMix, outlineLightMix.Value); + yield return (MToon10Prop.OutlineLightingMixFactor.ToUnityShaderLabName(), outlineLightMix.Value); } + // UV Animation var uvAnimSpeedScrollX = mToon?.UvAnimationScrollXSpeedFactor; if (uvAnimSpeedScrollX.HasValue) { - yield return (MToon.Utils.PropUvAnimScrollX, uvAnimSpeedScrollX.Value); + yield return (MToon10Prop.UvAnimationScrollXSpeedFactor.ToUnityShaderLabName(), uvAnimSpeedScrollX.Value); } - // UV coords conversion. - // glTF (top-left origin) to Unity (bottom-left origin) var uvAnimSpeedScrollY = mToon?.UvAnimationScrollYSpeedFactor; if (uvAnimSpeedScrollY.HasValue) { + // UV coords conversion. + // glTF (top-left origin) to Unity (bottom-left origin) const float invertY = -1f; - yield return (MToon.Utils.PropUvAnimScrollY, uvAnimSpeedScrollY.Value * invertY); + yield return (MToon10Prop.UvAnimationScrollYSpeedFactor.ToUnityShaderLabName(), uvAnimSpeedScrollY.Value * invertY); } var uvAnimSpeedRotation = mToon?.UvAnimationRotationSpeedFactor; if (uvAnimSpeedRotation.HasValue) { - yield return (MToon.Utils.PropUvAnimRotation, uvAnimSpeedRotation.Value); + yield return (MToon10Prop.UvAnimationRotationSpeedFactor.ToUnityShaderLabName(), uvAnimSpeedRotation.Value); } } @@ -216,77 +236,59 @@ namespace UniVRM10 yield break; } - public static int? TryGetRenderQueue(glTFMaterial material, VRMC_materials_mtoon mToon) - { - var renderQueueOffset = mToon?.RenderQueueOffsetNumber; - if (renderQueueOffset.HasValue) - { - var renderMode = GetMToonRenderMode(material, mToon); - return MToon.Utils.GetRenderQueueRequirement(renderMode).DefaultValue + - renderQueueOffset.Value; - } - return default; - } - - - private static MToon.RenderMode GetMToonRenderMode(glTFMaterial material, VRMC_materials_mtoon mToon) + private static MToon10AlphaMode GetMToon10AlphaMode(glTFMaterial material) { switch (material?.alphaMode) { case "OPAQUE": - return MToon.RenderMode.Opaque; + return MToon10AlphaMode.Opaque; case "MASK": - return MToon.RenderMode.Cutout; + return MToon10AlphaMode.Cutout; case "BLEND": - var mToonZWrite = mToon?.TransparentWithZWrite; - if (mToonZWrite.HasValue) - { - if (mToonZWrite.Value) - { - return MToon.RenderMode.TransparentWithZWrite; - } - else - { - return MToon.RenderMode.Transparent; - } - } - else - { - // Invalid - return MToon.RenderMode.Transparent; - } + return MToon10AlphaMode.Transparent; default: - // Invalid - return MToon.RenderMode.Opaque; + Debug.LogWarning($"Invalid AlphaMode"); + return MToon10AlphaMode.Opaque; } } - private static MToon.CullMode GetMToonCullMode(glTFMaterial material, VRMC_materials_mtoon mToon) + private static MToon10TransparentWithZWriteMode GetMToon10TransparentWithZWriteMode(glTFMaterial material, VRMC_materials_mtoon mtoon) { - var doubleSided = material?.doubleSided; - if (doubleSided.HasValue && doubleSided.Value) + if (mtoon?.TransparentWithZWrite == true) { - return MToon.CullMode.Off; + return MToon10TransparentWithZWriteMode.On; } else { - return MToon.CullMode.Back; + return MToon10TransparentWithZWriteMode.Off; + } + } + private static MToon10DoubleSidedMode GetMToon10DoubleSidedMode(glTFMaterial material, VRMC_materials_mtoon mToon) + { + if (material?.doubleSided == true) + { + return MToon10DoubleSidedMode.On; + } + else + { + return MToon10DoubleSidedMode.Off; } } - private static MToon.OutlineWidthMode GetMToonOutlineWidthMode(glTFMaterial material, VRMC_materials_mtoon mToon) + private static MToon10OutlineMode GetMToon10OutlineWidthMode(glTFMaterial material, VRMC_materials_mtoon mToon) { - switch (mToon.OutlineWidthMode) + switch (mToon?.OutlineWidthMode) { case OutlineWidthMode.none: - return MToon.OutlineWidthMode.None; + return MToon10OutlineMode.None; case OutlineWidthMode.worldCoordinates: - return MToon.OutlineWidthMode.WorldCoordinates; + return MToon10OutlineMode.World; case OutlineWidthMode.screenCoordinates: - return MToon.OutlineWidthMode.ScreenCoordinates; + return MToon10OutlineMode.Screen; default: // Invalid - return MToon.OutlineWidthMode.None; + Debug.LogWarning("Invalid outlineWidthMode"); + return MToon10OutlineMode.None; } } } diff --git a/Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs b/Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs index bbdeb3531..28c2cf446 100644 --- a/Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs +++ b/Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs @@ -4,6 +4,7 @@ using UniGLTF; using UniGLTF.Extensions.VRMC_materials_mtoon; using UnityEngine; using VRMShaders; +using VRMShaders.VRM10.MToon10.Runtime; namespace UniVRM10 { @@ -13,48 +14,47 @@ namespace UniVRM10 { if (TryGetBaseColorTexture(parser, material, out var litTex)) { - yield return (MToon.Utils.PropMainTex, litTex); + yield return (MToon10Prop.BaseColorTexture.ToUnityShaderLabName(), litTex); } if (TryGetEmissiveTexture(parser, material, out var emissiveTex)) { - yield return (MToon.Utils.PropEmissionMap, emissiveTex); + yield return (MToon10Prop.EmissiveTexture.ToUnityShaderLabName(), emissiveTex); } if (TryGetNormalTexture(parser, material, out var normalTex)) { - yield return ("_BumpMap", normalTex); + yield return (MToon10Prop.NormalTexture.ToUnityShaderLabName(), normalTex); } if (TryGetShadeMultiplyTexture(parser, mToon, out var shadeTex)) { - yield return (MToon.Utils.PropShadeTexture, shadeTex); + yield return (MToon10Prop.ShadeColorTexture.ToUnityShaderLabName(), shadeTex); } if (TryGetShadingShiftTexture(parser, mToon, out var shadeShiftTex)) { - Debug.LogWarning("Need VRM 1.0 MToon implementation."); - yield return ("_NEED_IMPLEMENTATION_MTOON_1_0_shadingShiftTexture", shadeShiftTex); + yield return (MToon10Prop.ShadingShiftTexture.ToUnityShaderLabName(), shadeShiftTex); } if (TryGetMatcapTexture(parser, mToon, out var matcapTex)) { - yield return (MToon.Utils.PropSphereAdd, matcapTex); + yield return (MToon10Prop.MatcapTexture.ToUnityShaderLabName(), matcapTex); } if (TryGetRimMultiplyTexture(parser, mToon, out var rimTex)) { - yield return (MToon.Utils.PropRimTexture, rimTex); + yield return (MToon10Prop.RimMultiplyTexture.ToUnityShaderLabName(), rimTex); } if (TryGetOutlineWidthMultiplyTexture(parser, mToon, out var outlineTex)) { - yield return (MToon.Utils.PropOutlineWidthTexture, outlineTex); + yield return (MToon10Prop.OutlineWidthMultiplyTexture.ToUnityShaderLabName(), outlineTex); } if (TryGetUvAnimationMaskTexture(parser, mToon, out var uvAnimMaskTex)) { - yield return (MToon.Utils.PropUvAnimMaskTexture, uvAnimMaskTex); + yield return (MToon10Prop.UvAnimationMaskTexture.ToUnityShaderLabName(), uvAnimMaskTex); } } diff --git a/Assets/VRM10/Runtime/Migration/MigrationMToon.cs b/Assets/VRM10/Runtime/Migration/MigrationMToon.cs index 514499b14..a5605ceb7 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationMToon.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationMToon.cs @@ -420,12 +420,12 @@ namespace UniVRM10 } // Light - dst.GiIntensityFactor = MToonMigrator.MigrateToGiEqualization(mtoon.Definition.Lighting.LightingInfluence.GiIntensityValue); - dst.ShadingToonyFactor = MToonMigrator.MigrateToShadingToony( + dst.GiIntensityFactor = MToon10Migrator.MigrateToGiEqualization(mtoon.Definition.Lighting.LightingInfluence.GiIntensityValue); + dst.ShadingToonyFactor = MToon10Migrator.MigrateToShadingToony( mtoon.Definition.Lighting.LitAndShadeMixing.ShadingToonyValue, mtoon.Definition.Lighting.LitAndShadeMixing.ShadingShiftValue ); - dst.ShadingShiftFactor = MToonMigrator.MigrateToShadingShift( + dst.ShadingShiftFactor = MToon10Migrator.MigrateToShadingShift( mtoon.Definition.Lighting.LitAndShadeMixing.ShadingToonyValue, mtoon.Definition.Lighting.LitAndShadeMixing.ShadingShiftValue ); diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs index d88057edb..7bf96ec6e 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs @@ -21,24 +21,24 @@ namespace VRMShaders.VRM10.MToon10.Editor using (new LabelScope("Rendering")) { - PopupEnum("Alpha Mode", props[MToon10Prop.AlphaMode], materialEditor); - var alphaMode = (AlphaMode) (int) props[MToon10Prop.AlphaMode].floatValue; + PopupEnum("Alpha Mode", props[MToon10Prop.AlphaMode], materialEditor); + var alphaMode = (MToon10AlphaMode) (int) props[MToon10Prop.AlphaMode].floatValue; - if (alphaMode == AlphaMode.Transparent) + if (alphaMode == MToon10AlphaMode.Transparent) { - PopupEnum( + PopupEnum( "Transparent With ZWrite Mode", props[MToon10Prop.TransparentWithZWrite], materialEditor ); } - if (alphaMode == AlphaMode.Cutout) + if (alphaMode == MToon10AlphaMode.Cutout) { materialEditor.ShaderProperty(props[MToon10Prop.AlphaCutoff], "Cutoff"); } - PopupEnum("Double Sided", props[MToon10Prop.DoubleSided], materialEditor); + PopupEnum("Double Sided", props[MToon10Prop.DoubleSided], materialEditor); materialEditor.ShaderProperty(props[MToon10Prop.RenderQueueOffsetNumber], "RenderQueue Offset"); } @@ -117,7 +117,7 @@ namespace VRMShaders.VRM10.MToon10.Editor using (new LabelScope("Outline")) { - PopupEnum("Outline Mode", props[MToon10Prop.OutlineWidthMode], materialEditor); + PopupEnum("Outline Mode", props[MToon10Prop.OutlineWidthMode], materialEditor); materialEditor.TexturePropertySingleLine( new GUIContent("Outline Width", "Outline Width (G) [meter]"), props[MToon10Prop.OutlineWidthMultiplyTexture], diff --git a/Assets/VRMShaders/VRM10/MToon10/Runtime/Extensions/MaterialExtensions.cs b/Assets/VRMShaders/VRM10/MToon10/Runtime/Extensions/MaterialExtensions.cs index 7143ce4fd..99124bed1 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Runtime/Extensions/MaterialExtensions.cs +++ b/Assets/VRMShaders/VRM10/MToon10/Runtime/Extensions/MaterialExtensions.cs @@ -31,5 +31,25 @@ namespace VRMShaders.VRM10.MToon10.Runtime { return mat.GetTexture(prop.ToUnityShaderLabName()); } + + public static Vector2 GetTextureScale(this Material mat, MToon10Prop prop) + { + return mat.GetTextureScale(prop.ToUnityShaderLabName()); + } + + public static Vector2 GetTextureOffset(this Material mat, MToon10Prop prop) + { + return mat.GetTextureOffset(prop.ToUnityShaderLabName()); + } + + public static float GetFloat(this Material mat, MToon10Prop prop) + { + return mat.GetFloat(prop.ToUnityShaderLabName()); + } + + public static Color GetColor(this Material mat, MToon10Prop prop) + { + return mat.GetColor(prop.ToUnityShaderLabName()); + } } } \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Runtime/MToon10Context.cs b/Assets/VRMShaders/VRM10/MToon10/Runtime/MToon10Context.cs new file mode 100644 index 000000000..a6267fcd9 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Runtime/MToon10Context.cs @@ -0,0 +1,201 @@ +using UnityEngine; + +namespace VRMShaders.VRM10.MToon10.Runtime +{ + public sealed class MToon10Context + { + private readonly Material _material; + + public MToon10Context(Material material) + { + _material = material; + } + + public void Validate() + { + new MToonValidator(_material).Validate(); + } + + // Rendering + public MToon10AlphaMode AlphaMode + { + get => (MToon10AlphaMode) _material.GetInt(MToon10Prop.AlphaMode); + } + + public MToon10TransparentWithZWriteMode TransparentWithZWriteMode + { + get => (MToon10TransparentWithZWriteMode) _material.GetInt(MToon10Prop.TransparentWithZWrite); + } + + public float AlphaCutoff + { + get => _material.GetFloat(MToon10Prop.AlphaCutoff); + } + + public int RenderQueueOffsetNumber + { + get => _material.GetInt(MToon10Prop.RenderQueueOffsetNumber); + } + + public MToon10DoubleSidedMode DoubleSidedMode + { + get => (MToon10DoubleSidedMode) _material.GetInt(MToon10Prop.DoubleSided); + } + + // Lighting + public Color BaseColorFactorSrgb + { + get => _material.GetColor(MToon10Prop.BaseColorFactor); + } + + public Texture BaseColorTexture + { + get => _material.GetTexture(MToon10Prop.BaseColorTexture); + } + + public Color ShadeColorFactorSrgb + { + get => _material.GetColor(MToon10Prop.ShadeColorFactor); + } + + public Texture ShadeColorTexture + { + get => _material.GetTexture(MToon10Prop.ShadeColorTexture); + } + + public Texture NormalTexture + { + get => _material.GetTexture(MToon10Prop.NormalTexture); + } + + public float NormalTextureScale + { + get => _material.GetFloat(MToon10Prop.NormalTextureScale); + } + + public float ShadingShiftFactor + { + get => _material.GetFloat(MToon10Prop.ShadingShiftFactor); + } + + public Texture ShadingShiftTexture + { + get => _material.GetTexture(MToon10Prop.ShadingShiftTexture); + } + + public float ShadingShiftTextureScale + { + get => _material.GetFloat(MToon10Prop.ShadingShiftTextureScale); + } + + public float ShadingToonyFactor + { + get => _material.GetFloat(MToon10Prop.ShadingToonyFactor); + } + + // GI + public float GiEqualizationFactor + { + get => _material.GetFloat(MToon10Prop.GiEqualizationFactor); + } + + // Emission + public Color EmissiveFactorLinear + { + get => _material.GetColor(MToon10Prop.EmissiveFactor).linear; + } + + public Texture EmissiveTexture + { + get => _material.GetTexture(MToon10Prop.EmissiveTexture); + } + // Rim Lighting + public Texture MatcapTexture + { + get => _material.GetTexture(MToon10Prop.MatcapTexture); + } + + public Color ParametricRimColorFactorSrgb + { + get => _material.GetColor(MToon10Prop.ParametricRimColorFactor); + } + + public float ParametricRimFresnelPowerFactor + { + get => _material.GetFloat(MToon10Prop.ParametricRimFresnelPowerFactor); + } + + public float ParametricRimLiftFactor + { + get => _material.GetFloat(MToon10Prop.ParametricRimLiftFactor); + } + + public Texture RimMultiplyTexture + { + get => _material.GetTexture(MToon10Prop.RimMultiplyTexture); + } + + public float RimLightingMixFactor + { + get => _material.GetFloat(MToon10Prop.RimLightingMixFactor); + } + + // Outline + public MToon10OutlineMode OutlineWidthMode + { + get => (MToon10OutlineMode) _material.GetInt(MToon10Prop.OutlineWidthMode); + } + + public float OutlineWidthFactor + { + get => _material.GetFloat(MToon10Prop.OutlineWidthFactor); + } + + public Texture OutlineWidthMultiplyTexture + { + get => _material.GetTexture(MToon10Prop.OutlineWidthMultiplyTexture); + } + + public Color OutlineColorFactorSrgb + { + get => _material.GetColor(MToon10Prop.OutlineColorFactor); + } + + public float OutlineLightingMixFactor + { + get => _material.GetFloat(MToon10Prop.OutlineLightingMixFactor); + } + + // UV Animation + public Texture UvAnimationMaskTexture + { + get => _material.GetTexture(MToon10Prop.UvAnimationMaskTexture); + } + + public float UvAnimationScrollXSpeedFactor + { + get => _material.GetFloat(MToon10Prop.UvAnimationScrollXSpeedFactor); + } + + public float UvAnimationScrollYSpeedFactor + { + get => _material.GetFloat(MToon10Prop.UvAnimationScrollYSpeedFactor); + } + + public float UvAnimationRotationSpeedFactor + { + get => _material.GetFloat(MToon10Prop.UvAnimationRotationSpeedFactor); + } + + // etc + public Vector2 TextureScale + { + get => _material.GetTextureScale(MToon10Prop.BaseColorTexture); + } + + public Vector2 TextureOffset + { + get => _material.GetTextureOffset(MToon10Prop.BaseColorTexture); + } + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Runtime/MToon10Context.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Runtime/MToon10Context.cs.meta new file mode 100644 index 000000000..6e90c419b --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Runtime/MToon10Context.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1f07ab1da80c4891938ee36c6f2042a2 +timeCreated: 1623312159 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Runtime/MToonValidator.cs b/Assets/VRMShaders/VRM10/MToon10/Runtime/MToonValidator.cs index b14d7fc82..81c0c420d 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Runtime/MToonValidator.cs +++ b/Assets/VRMShaders/VRM10/MToon10/Runtime/MToonValidator.cs @@ -79,10 +79,10 @@ namespace VRMShaders.VRM10.MToon10.Runtime switch (doubleSidedMode) { case MToon10DoubleSidedMode.Off: - material.SetInt(MToon10Prop.UnityCullMode, (int) CullMode.Back); + material.SetInt(MToon10Prop.UnityCullMode, (int) UnityCullMode.Back); break; case MToon10DoubleSidedMode.On: - material.SetInt(MToon10Prop.UnityCullMode, (int) CullMode.Off); + material.SetInt(MToon10Prop.UnityCullMode, (int) UnityCullMode.Off); break; default: throw new ArgumentOutOfRangeException(nameof(doubleSidedMode), doubleSidedMode, null); diff --git a/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/ShaderLabDefinedValues/UnityCullMode.cs b/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/ShaderLabDefinedValues/UnityCullMode.cs new file mode 100644 index 000000000..fcfff41e1 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/ShaderLabDefinedValues/UnityCullMode.cs @@ -0,0 +1,9 @@ +namespace VRMShaders.VRM10.MToon10.Runtime +{ + public enum UnityCullMode + { + Off = 0, + // Front = 1, + Back = 2, + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/ShaderLabDefinedValues/UnityCullMode.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/ShaderLabDefinedValues/UnityCullMode.cs.meta new file mode 100644 index 000000000..947c22d98 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/ShaderLabDefinedValues/UnityCullMode.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 03ac85ab52a547019ff7e377f1778225 +timeCreated: 1623308821 \ No newline at end of file