From 2367e9dbc94fdd60e892144b9dbb8974c593357a Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 24 Aug 2021 20:19:39 +0900 Subject: [PATCH 1/2] null check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * try による握りつぶしをやめて Error は失敗させる --- .../IO/Material/Vrm10MToonMaterialExporter.cs | 306 +++++++++--------- 1 file changed, 153 insertions(+), 153 deletions(-) diff --git a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs index 4c2f3dcd8..7f50a6b55 100644 --- a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs @@ -14,163 +14,155 @@ namespace UniVRM10 public static bool TryExportMaterialAsMToon(Material src, ITextureExporter textureExporter, out glTFMaterial dst) { - try - { - if (src.shader.name != MToon10Meta.UnityShaderName) - { - dst = null; - return false; - } - - // 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.SpecVersion = MTOON_SPEC_VERSION; - - // Rendering - 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.baseColorFactor = context.BaseColorFactorSrgb.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear); - var baseColorTextureIndex = textureExporter.RegisterExportingAsSRgb(context.BaseColorTexture, context.AlphaMode != MToon10AlphaMode.Opaque); - if (baseColorTextureIndex != -1) - { - dst.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo - { - index = baseColorTextureIndex, - }; - } - mtoon.ShadeColorFactor = context.ShadeColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear); - var shadeColorTextureIndex = textureExporter.RegisterExportingAsSRgb(context.ShadeColorTexture, needsAlpha: false); - if (shadeColorTextureIndex != -1) - { - mtoon.ShadeMultiplyTexture = new TextureInfo - { - Index = shadeColorTextureIndex, - }; - } - var normalTextureIndex = textureExporter.RegisterExportingAsNormal(context.NormalTexture); - if (normalTextureIndex != -1) - { - dst.normalTexture = new glTFMaterialNormalTextureInfo - { - index = normalTextureIndex, - scale = context.NormalTextureScale, - }; - } - mtoon.ShadingShiftFactor = context.ShadingShiftFactor; - var shadingShiftTextureIndex = textureExporter.RegisterExportingAsLinear(context.ShadingShiftTexture, needsAlpha: false); - if (shadingShiftTextureIndex != -1) - { - mtoon.ShadingShiftTexture = new ShadingShiftTextureInfo - { - Index = shadingShiftTextureIndex, - Scale = context.ShadingShiftTextureScale, - }; - } - mtoon.ShadingToonyFactor = context.ShadingToonyFactor; - - // GI - mtoon.GiEqualizationFactor = context.GiEqualizationFactor; - - // Emission - dst.emissiveFactor = context.EmissiveFactorLinear.ToFloat3(ColorSpace.Linear, ColorSpace.Linear); - var emissiveTextureIndex = textureExporter.RegisterExportingAsSRgb(context.EmissiveTexture, needsAlpha: false); - if (emissiveTextureIndex != -1) - { - dst.emissiveTexture = new glTFMaterialEmissiveTextureInfo - { - index = emissiveTextureIndex, - }; - } - - // Rim Lighting - var matcapTextureIndex = textureExporter.RegisterExportingAsSRgb(context.MatcapTexture, needsAlpha: false); - if (matcapTextureIndex != -1) - { - mtoon.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.RegisterExportingAsSRgb(context.RimMultiplyTexture, needsAlpha: false); - if (rimMultiplyTextureIndex != -1) - { - mtoon.RimMultiplyTexture = new TextureInfo - { - Index = rimMultiplyTextureIndex, - }; - } - mtoon.RimLightingMixFactor = context.RimLightingMixFactor; - - // Outline - mtoon.OutlineWidthMode = ExportOutlineWidthMode(context.OutlineWidthMode); - mtoon.OutlineWidthFactor = context.OutlineWidthFactor; - var outlineWidthMultiplyTextureIndex = textureExporter.RegisterExportingAsLinear(context.OutlineWidthMultiplyTexture, needsAlpha: false); - if (outlineWidthMultiplyTextureIndex != -1) - { - mtoon.OutlineWidthMultiplyTexture = new TextureInfo - { - Index = outlineWidthMultiplyTextureIndex, - }; - } - mtoon.OutlineColorFactor = context.OutlineColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear); - mtoon.OutlineLightingMixFactor = context.OutlineLightingMixFactor; - - // UV Animation - var uvAnimationMaskTextureIndex = textureExporter.RegisterExportingAsLinear(context.UvAnimationMaskTexture, needsAlpha: false); - if (uvAnimationMaskTextureIndex != -1) - { - mtoon.UvAnimationMaskTexture = new TextureInfo - { - 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 = 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); - ExportTextureTransform(mtoon.UvAnimationMaskTexture, scale, offset); - - UniGLTF.Extensions.VRMC_materials_mtoon.GltfSerializer.SerializeTo(ref dst.extensions, mtoon); - - return true; - } - catch (Exception) + if (src.shader.name != MToon10Meta.UnityShaderName) { dst = null; return false; } + + // 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.SpecVersion = MTOON_SPEC_VERSION; + + // Rendering + 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.baseColorFactor = context.BaseColorFactorSrgb.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear); + var baseColorTextureIndex = textureExporter.RegisterExportingAsSRgb(context.BaseColorTexture, context.AlphaMode != MToon10AlphaMode.Opaque); + if (baseColorTextureIndex != -1) + { + dst.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo + { + index = baseColorTextureIndex, + }; + } + mtoon.ShadeColorFactor = context.ShadeColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear); + var shadeColorTextureIndex = textureExporter.RegisterExportingAsSRgb(context.ShadeColorTexture, needsAlpha: false); + if (shadeColorTextureIndex != -1) + { + mtoon.ShadeMultiplyTexture = new TextureInfo + { + Index = shadeColorTextureIndex, + }; + } + var normalTextureIndex = textureExporter.RegisterExportingAsNormal(context.NormalTexture); + if (normalTextureIndex != -1) + { + dst.normalTexture = new glTFMaterialNormalTextureInfo + { + index = normalTextureIndex, + scale = context.NormalTextureScale, + }; + } + mtoon.ShadingShiftFactor = context.ShadingShiftFactor; + var shadingShiftTextureIndex = textureExporter.RegisterExportingAsLinear(context.ShadingShiftTexture, needsAlpha: false); + if (shadingShiftTextureIndex != -1) + { + mtoon.ShadingShiftTexture = new ShadingShiftTextureInfo + { + Index = shadingShiftTextureIndex, + Scale = context.ShadingShiftTextureScale, + }; + } + mtoon.ShadingToonyFactor = context.ShadingToonyFactor; + + // GI + mtoon.GiEqualizationFactor = context.GiEqualizationFactor; + + // Emission + dst.emissiveFactor = context.EmissiveFactorLinear.ToFloat3(ColorSpace.Linear, ColorSpace.Linear); + var emissiveTextureIndex = textureExporter.RegisterExportingAsSRgb(context.EmissiveTexture, needsAlpha: false); + if (emissiveTextureIndex != -1) + { + dst.emissiveTexture = new glTFMaterialEmissiveTextureInfo + { + index = emissiveTextureIndex, + }; + } + + // Rim Lighting + var matcapTextureIndex = textureExporter.RegisterExportingAsSRgb(context.MatcapTexture, needsAlpha: false); + if (matcapTextureIndex != -1) + { + mtoon.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.RegisterExportingAsSRgb(context.RimMultiplyTexture, needsAlpha: false); + if (rimMultiplyTextureIndex != -1) + { + mtoon.RimMultiplyTexture = new TextureInfo + { + Index = rimMultiplyTextureIndex, + }; + } + mtoon.RimLightingMixFactor = context.RimLightingMixFactor; + + // Outline + mtoon.OutlineWidthMode = ExportOutlineWidthMode(context.OutlineWidthMode); + mtoon.OutlineWidthFactor = context.OutlineWidthFactor; + var outlineWidthMultiplyTextureIndex = textureExporter.RegisterExportingAsLinear(context.OutlineWidthMultiplyTexture, needsAlpha: false); + if (outlineWidthMultiplyTextureIndex != -1) + { + mtoon.OutlineWidthMultiplyTexture = new TextureInfo + { + Index = outlineWidthMultiplyTextureIndex, + }; + } + mtoon.OutlineColorFactor = context.OutlineColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear); + mtoon.OutlineLightingMixFactor = context.OutlineLightingMixFactor; + + // UV Animation + var uvAnimationMaskTextureIndex = textureExporter.RegisterExportingAsLinear(context.UvAnimationMaskTexture, needsAlpha: false); + if (uvAnimationMaskTextureIndex != -1) + { + mtoon.UvAnimationMaskTexture = new TextureInfo + { + 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 = 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); + ExportTextureTransform(mtoon.UvAnimationMaskTexture, scale, offset); + + UniGLTF.Extensions.VRMC_materials_mtoon.GltfSerializer.SerializeTo(ref dst.extensions, mtoon); + + return true; } private static string ExportAlphaMode(MToon10AlphaMode alphaMode) @@ -217,6 +209,10 @@ namespace UniVRM10 public static void ExportTextureTransform(TextureInfo textureInfo, Vector2 unityScale, Vector2 unityOffset) { + if (textureInfo == null) + { + return; + } // Generate extension to empty holder. var gltfTextureInfo = new EmptyGltfTextureInfo(); ExportTextureTransform(gltfTextureInfo, unityScale, unityOffset); @@ -227,6 +223,10 @@ namespace UniVRM10 public static void ExportTextureTransform(ShadingShiftTextureInfo textureInfo, Vector2 unityScale, Vector2 unityOffset) { + if (textureInfo == null) + { + return; + } // Generate extension to empty holder. var gltfTextureInfo = new EmptyGltfTextureInfo(); ExportTextureTransform(gltfTextureInfo, unityScale, unityOffset); From 584f9c67a41a5b60834dd3be556febf01860958c Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 24 Aug 2021 20:25:35 +0900 Subject: [PATCH 2/2] =?UTF-8?q?importer=20=E3=81=AE=20matcap=20texture=20?= =?UTF-8?q?=E3=81=AE=E9=96=93=E9=81=95=E3=81=84=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs b/Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs index 10c46f0f2..7390ded09 100644 --- a/Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs +++ b/Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs @@ -128,7 +128,7 @@ namespace UniVRM10 private static bool TryGetMatcapTexture(GltfData data, VRMC_materials_mtoon mToon, out (SubAssetKey, TextureDescriptor) pair) { - return TryGetSRGBTexture(data, new Vrm10TextureInfo(mToon.ShadingShiftTexture), out pair); + return TryGetSRGBTexture(data, new Vrm10TextureInfo(mToon.MatcapTexture), out pair); } private static bool TryGetRimMultiplyTexture(GltfData data, VRMC_materials_mtoon mToon, out (SubAssetKey, TextureDescriptor) pair)