From 7df789afdeff5a3b53bf1237e4cf373855e0f22e Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Thu, 28 Oct 2021 21:28:09 +0900 Subject: [PATCH] Divide & Refactoring classes in migration MToon. --- Assets/VRM10/Runtime/Migration/Materials.meta | 3 + .../MigrationMToonMaterial.cs} | 256 +----------------- .../MigrationMToonMaterial.cs.meta} | 0 .../Materials/Vrm0XMToonTextureIndexMap.cs | 18 ++ .../Vrm0XMToonTextureIndexMap.cs.meta | 11 + .../Migration/Materials/Vrm0XMToonValue.cs | 246 +++++++++++++++++ .../Materials/Vrm0XMToonValue.cs.meta | 3 + .../VRM10/Runtime/Migration/MigrationVrm.cs | 4 +- 8 files changed, 284 insertions(+), 257 deletions(-) create mode 100644 Assets/VRM10/Runtime/Migration/Materials.meta rename Assets/VRM10/Runtime/Migration/{MigrationMToon.cs => Materials/MigrationMToonMaterial.cs} (58%) rename Assets/VRM10/Runtime/Migration/{MigrationMToon.cs.meta => Materials/MigrationMToonMaterial.cs.meta} (100%) create mode 100644 Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonTextureIndexMap.cs create mode 100644 Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonTextureIndexMap.cs.meta create mode 100644 Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonValue.cs create mode 100644 Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonValue.cs.meta diff --git a/Assets/VRM10/Runtime/Migration/Materials.meta b/Assets/VRM10/Runtime/Migration/Materials.meta new file mode 100644 index 000000000..6eee5178a --- /dev/null +++ b/Assets/VRM10/Runtime/Migration/Materials.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f68d2e3e086d4768ae14cee7b28493cc +timeCreated: 1635418532 \ No newline at end of file diff --git a/Assets/VRM10/Runtime/Migration/MigrationMToon.cs b/Assets/VRM10/Runtime/Migration/Materials/MigrationMToonMaterial.cs similarity index 58% rename from Assets/VRM10/Runtime/Migration/MigrationMToon.cs rename to Assets/VRM10/Runtime/Migration/Materials/MigrationMToonMaterial.cs index c770feab4..44782c9d0 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationMToon.cs +++ b/Assets/VRM10/Runtime/Migration/Materials/MigrationMToonMaterial.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using MToon; using UniGLTF; using UniGLTF.Extensions.VRMC_materials_mtoon; @@ -13,261 +12,8 @@ using RenderMode = MToon.RenderMode; namespace UniVRM10 { - public static class MigrationMToon + public static class MigrationMToonMaterial { - static Color ToColor(JsonNode node, ColorSpace srcColorSpace, ColorSpace dstColorSpace) - { - return node.ArrayItems().Select(x => x.GetSingle()).ToArray().ToColor4(srcColorSpace, dstColorSpace); - } - - static float[] ToFloat4(JsonNode node) - { - return node.ArrayItems().Select(x => x.GetSingle()).ToArray(); - } - - struct TextureIndexMap - { - // glTF - public int? MainTex; - public int? BumpMap; - public int? EmissionMap; - // VRMC_materials_mtoon - public int? ShadeTexture; - public int? ReceiveShadowTexture; - public int? ShadingGradeTexture; - public int? RimTexture; - public int? SphereAdd; - public int? OutlineWidthTexture; - public int? UvAnimMaskTexture; - } - - /// - /// vrm-0 の json から vrm-0 の MToon.Definition を生成する。 - /// - /// Texture2D は作成せずに、直接 index を操作する。 - /// - /// - private sealed class Vrm0XMToonValue - { - public MToon.MToonDefinition Definition { get; } - public Dictionary TextureOffsetScales { get; } - public TextureIndexMap TextureIndexMap { get; } - - public Vrm0XMToonValue(JsonNode vrmMaterial) - { - var definition = new MToon.MToonDefinition - { - Color = new MToon.ColorDefinition { }, - Lighting = new MToon.LightingDefinition - { - LightingInfluence = new MToon.LightingInfluenceDefinition { }, - LitAndShadeMixing = new MToon.LitAndShadeMixingDefinition { }, - Normal = new MToon.NormalDefinition { } - }, - Emission = new MToon.EmissionDefinition { }, - MatCap = new MToon.MatCapDefinition { }, - Meta = new MToon.MetaDefinition { }, - Outline = new MToon.OutlineDefinition { }, - Rendering = new MToon.RenderingDefinition { }, - Rim = new MToon.RimDefinition { }, - TextureOption = new MToon.TextureUvCoordsDefinition { } - }; - - var offsetScale = new Dictionary(); - foreach (var kv in vrmMaterial["vectorProperties"].ObjectItems()) - { - var key = kv.Key.GetString(); - switch (key) - { - // Lighting - case "_Color": - definition.Color.LitColor = ToColor(kv.Value, ColorSpace.sRGB, ColorSpace.sRGB); - break; - case "_ShadeColor": - definition.Color.ShadeColor = ToColor(kv.Value, ColorSpace.sRGB, ColorSpace.sRGB); - break; - - // Emission - case "_EmissionColor": - definition.Emission.EmissionColor = ToColor(kv.Value, ColorSpace.Linear, ColorSpace.Linear); - break; - - // Rim Lighting - case "_RimColor": - definition.Rim.RimColor = ToColor(kv.Value, ColorSpace.sRGB, ColorSpace.sRGB); - break; - - // Outline - case "_OutlineColor": - definition.Outline.OutlineColor = ToColor(kv.Value, ColorSpace.sRGB, ColorSpace.sRGB); - break; - - // Texture ST - case "_MainTex": - case "_ShadeTexture": - case "_BumpMap": - case "_EmissionMap": - case "_OutlineWidthTexture": - case "_ReceiveShadowTexture": - case "_RimTexture": - case "_ShadingGradeTexture": - case "_SphereAdd": - case "_UvAnimMaskTexture": - // scale, offset - offsetScale.Add(key, ToFloat4(kv.Value)); - break; - - default: -#if VRM_DEVELOP - Debug.LogWarning($"vectorProperties: {kv.Key}: {kv.Value}"); -#endif - break; - } - } - - foreach (var kv in vrmMaterial["floatProperties"].ObjectItems()) - { - var value = kv.Value.GetSingle(); - switch (kv.Key.GetString()) - { - // Rendering - case "_BlendMode": - definition.Rendering.RenderMode = (MToon.RenderMode)(int)value; - break; - case "_CullMode": - definition.Rendering.CullMode = (MToon.CullMode)(int)value; - break; - case "_Cutoff": - definition.Color.CutoutThresholdValue = value; - break; - - // Lighting - case "_BumpScale": - definition.Lighting.Normal.NormalScaleValue = value; - break; - case "_LightColorAttenuation": - definition.Lighting.LightingInfluence.LightColorAttenuationValue = value; - break; - case "_ShadeShift": - definition.Lighting.LitAndShadeMixing.ShadingShiftValue = value; - break; - case "_ShadeToony": - definition.Lighting.LitAndShadeMixing.ShadingToonyValue = value; - break; - case "_ShadingGradeRate": - // Not supported - break; - case "_ReceiveShadowRate": - // Not supported - break; - - // GI - case "_IndirectLightIntensity": - definition.Lighting.LightingInfluence.GiIntensityValue = value; - break; - - // Rim Lighting - case "_RimFresnelPower": - definition.Rim.RimFresnelPowerValue = value; - break; - case "_RimLift": - definition.Rim.RimLiftValue = value; - break; - case "_RimLightingMix": - definition.Rim.RimLightingMixValue = value; - break; - - // Outline - case "_OutlineColorMode": - definition.Outline.OutlineColorMode = (MToon.OutlineColorMode)value; - break; - case "_OutlineLightingMix": - definition.Outline.OutlineLightingMixValue = value; - break; - case "_OutlineScaledMaxDistance": - definition.Outline.OutlineScaledMaxDistanceValue = value; - break; - case "_OutlineWidth": - definition.Outline.OutlineWidthValue = value; - break; - case "_OutlineWidthMode": - if (value > 2) - { - value = 0; - } - definition.Outline.OutlineWidthMode = (MToon.OutlineWidthMode)value; - break; - - // UV Animation - case "_UvAnimRotation": - definition.TextureOption.UvAnimationRotationSpeedValue = value; - break; - - case "_UvAnimScrollX": - definition.TextureOption.UvAnimationScrollXSpeedValue = value; - break; - - case "_UvAnimScrollY": - definition.TextureOption.UvAnimationScrollYSpeedValue = value; - break; - - case "_OutlineCullMode": - case "_ZWrite": - case "_DstBlend": - case "_SrcBlend": - case "_MToonVersion": - case "_DebugMode": - // Auto generated - break; - - default: -#if VRM_DEVELOP - Debug.LogWarning($"floatProperties: {kv.Key} is unknown"); -#endif - break; - } - } - - var map = new TextureIndexMap(); - - foreach (var kv in vrmMaterial["textureProperties"].ObjectItems()) - { - var index = kv.Value.GetInt32(); - switch (kv.Key.GetString()) - { - // Lighting - case "_MainTex": map.MainTex = index; break; - case "_ShadeTexture": map.ShadeTexture = index; break; - case "_BumpMap": map.BumpMap = index; break; - case "_ReceiveShadowTexture": map.ReceiveShadowTexture = index; break; - case "_ShadingGradeTexture": map.ShadingGradeTexture = index; break; - // Emission - case "_EmissionMap": map.EmissionMap = index; break; - // Rim Lighting - case "_RimTexture": map.RimTexture = index; break; - case "_SphereAdd": map.SphereAdd = index; break; - // Outline - case "_OutlineWidthTexture": map.OutlineWidthTexture = index; break; - // UV Animation - case "_UvAnimMaskTexture": map.UvAnimMaskTexture = index; break; - default: -#if VRM_DEVELOP - Debug.LogWarning($"textureProperties: {kv.Key} is unknown"); -#endif - break; - } - } - - definition.Rendering.RenderQueueOffsetNumber = - vrmMaterial["renderQueue"].GetInt32() - - MToon.Utils.GetRenderQueueRequirement(definition.Rendering.RenderMode).DefaultValue; - - Definition = definition; - TextureOffsetScales = offsetScale; - TextureIndexMap = map; - } - } - public static void Migrate(glTF gltf, JsonNode vrm0) { // Create MToonDefinition(0.x) from JSON(0.x) diff --git a/Assets/VRM10/Runtime/Migration/MigrationMToon.cs.meta b/Assets/VRM10/Runtime/Migration/Materials/MigrationMToonMaterial.cs.meta similarity index 100% rename from Assets/VRM10/Runtime/Migration/MigrationMToon.cs.meta rename to Assets/VRM10/Runtime/Migration/Materials/MigrationMToonMaterial.cs.meta diff --git a/Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonTextureIndexMap.cs b/Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonTextureIndexMap.cs new file mode 100644 index 000000000..36c6d4740 --- /dev/null +++ b/Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonTextureIndexMap.cs @@ -0,0 +1,18 @@ +namespace UniVRM10 +{ + internal struct Vrm0XMToonTextureIndexMap + { + // glTF + public int? MainTex; + public int? BumpMap; + public int? EmissionMap; + // VRMC_materials_mtoon + public int? ShadeTexture; + public int? ReceiveShadowTexture; + public int? ShadingGradeTexture; + public int? RimTexture; + public int? SphereAdd; + public int? OutlineWidthTexture; + public int? UvAnimMaskTexture; + } +} \ No newline at end of file diff --git a/Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonTextureIndexMap.cs.meta b/Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonTextureIndexMap.cs.meta new file mode 100644 index 000000000..7972c38b2 --- /dev/null +++ b/Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonTextureIndexMap.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 50d80cf68f2f7c1488b02f62a96782ae +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonValue.cs b/Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonValue.cs new file mode 100644 index 000000000..0968453ee --- /dev/null +++ b/Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonValue.cs @@ -0,0 +1,246 @@ +using System.Collections.Generic; +using System.Linq; +using UniGLTF; +using UniJSON; +using UnityEngine; +using ColorSpace = VRMShaders.ColorSpace; + +namespace UniVRM10 +{ + /// + /// vrm-0 の json から vrm-0 の MToon.Definition を生成する。 + /// + /// Texture2D は作成せずに、直接 index を操作する。 + /// + /// + internal sealed class Vrm0XMToonValue + { + public MToon.MToonDefinition Definition { get; } + public Dictionary TextureOffsetScales { get; } + public Vrm0XMToonTextureIndexMap TextureIndexMap { get; } + + public Vrm0XMToonValue(JsonNode vrmMaterial) + { + var definition = new MToon.MToonDefinition + { + Color = new MToon.ColorDefinition { }, + Lighting = new MToon.LightingDefinition + { + LightingInfluence = new MToon.LightingInfluenceDefinition { }, + LitAndShadeMixing = new MToon.LitAndShadeMixingDefinition { }, + Normal = new MToon.NormalDefinition { } + }, + Emission = new MToon.EmissionDefinition { }, + MatCap = new MToon.MatCapDefinition { }, + Meta = new MToon.MetaDefinition { }, + Outline = new MToon.OutlineDefinition { }, + Rendering = new MToon.RenderingDefinition { }, + Rim = new MToon.RimDefinition { }, + TextureOption = new MToon.TextureUvCoordsDefinition { } + }; + + var offsetScale = new Dictionary(); + foreach (var kv in vrmMaterial["vectorProperties"].ObjectItems()) + { + var key = kv.Key.GetString(); + switch (key) + { + // Lighting + case "_Color": + definition.Color.LitColor = ToColor(kv.Value, ColorSpace.sRGB, ColorSpace.sRGB); + break; + case "_ShadeColor": + definition.Color.ShadeColor = ToColor(kv.Value, ColorSpace.sRGB, ColorSpace.sRGB); + break; + + // Emission + case "_EmissionColor": + definition.Emission.EmissionColor = ToColor(kv.Value, ColorSpace.Linear, ColorSpace.Linear); + break; + + // Rim Lighting + case "_RimColor": + definition.Rim.RimColor = ToColor(kv.Value, ColorSpace.sRGB, ColorSpace.sRGB); + break; + + // Outline + case "_OutlineColor": + definition.Outline.OutlineColor = ToColor(kv.Value, ColorSpace.sRGB, ColorSpace.sRGB); + break; + + // Texture ST + case "_MainTex": + case "_ShadeTexture": + case "_BumpMap": + case "_EmissionMap": + case "_OutlineWidthTexture": + case "_ReceiveShadowTexture": + case "_RimTexture": + case "_ShadingGradeTexture": + case "_SphereAdd": + case "_UvAnimMaskTexture": + // scale, offset + offsetScale.Add(key, ToFloat4(kv.Value)); + break; + + default: +#if VRM_DEVELOP + Debug.LogWarning($"vectorProperties: {kv.Key}: {kv.Value}"); +#endif + break; + } + } + + foreach (var kv in vrmMaterial["floatProperties"].ObjectItems()) + { + var value = kv.Value.GetSingle(); + switch (kv.Key.GetString()) + { + // Rendering + case "_BlendMode": + definition.Rendering.RenderMode = (MToon.RenderMode)(int)value; + break; + case "_CullMode": + definition.Rendering.CullMode = (MToon.CullMode)(int)value; + break; + case "_Cutoff": + definition.Color.CutoutThresholdValue = value; + break; + + // Lighting + case "_BumpScale": + definition.Lighting.Normal.NormalScaleValue = value; + break; + case "_LightColorAttenuation": + definition.Lighting.LightingInfluence.LightColorAttenuationValue = value; + break; + case "_ShadeShift": + definition.Lighting.LitAndShadeMixing.ShadingShiftValue = value; + break; + case "_ShadeToony": + definition.Lighting.LitAndShadeMixing.ShadingToonyValue = value; + break; + case "_ShadingGradeRate": + // Not supported + break; + case "_ReceiveShadowRate": + // Not supported + break; + + // GI + case "_IndirectLightIntensity": + definition.Lighting.LightingInfluence.GiIntensityValue = value; + break; + + // Rim Lighting + case "_RimFresnelPower": + definition.Rim.RimFresnelPowerValue = value; + break; + case "_RimLift": + definition.Rim.RimLiftValue = value; + break; + case "_RimLightingMix": + definition.Rim.RimLightingMixValue = value; + break; + + // Outline + case "_OutlineColorMode": + definition.Outline.OutlineColorMode = (MToon.OutlineColorMode)value; + break; + case "_OutlineLightingMix": + definition.Outline.OutlineLightingMixValue = value; + break; + case "_OutlineScaledMaxDistance": + definition.Outline.OutlineScaledMaxDistanceValue = value; + break; + case "_OutlineWidth": + definition.Outline.OutlineWidthValue = value; + break; + case "_OutlineWidthMode": + if (value > 2) + { + value = 0; + } + definition.Outline.OutlineWidthMode = (MToon.OutlineWidthMode)value; + break; + + // UV Animation + case "_UvAnimRotation": + definition.TextureOption.UvAnimationRotationSpeedValue = value; + break; + + case "_UvAnimScrollX": + definition.TextureOption.UvAnimationScrollXSpeedValue = value; + break; + + case "_UvAnimScrollY": + definition.TextureOption.UvAnimationScrollYSpeedValue = value; + break; + + case "_OutlineCullMode": + case "_ZWrite": + case "_DstBlend": + case "_SrcBlend": + case "_MToonVersion": + case "_DebugMode": + // Auto generated + break; + + default: +#if VRM_DEVELOP + Debug.LogWarning($"floatProperties: {kv.Key} is unknown"); +#endif + break; + } + } + + var map = new Vrm0XMToonTextureIndexMap(); + + foreach (var kv in vrmMaterial["textureProperties"].ObjectItems()) + { + var index = kv.Value.GetInt32(); + switch (kv.Key.GetString()) + { + // Lighting + case "_MainTex": map.MainTex = index; break; + case "_ShadeTexture": map.ShadeTexture = index; break; + case "_BumpMap": map.BumpMap = index; break; + case "_ReceiveShadowTexture": map.ReceiveShadowTexture = index; break; + case "_ShadingGradeTexture": map.ShadingGradeTexture = index; break; + // Emission + case "_EmissionMap": map.EmissionMap = index; break; + // Rim Lighting + case "_RimTexture": map.RimTexture = index; break; + case "_SphereAdd": map.SphereAdd = index; break; + // Outline + case "_OutlineWidthTexture": map.OutlineWidthTexture = index; break; + // UV Animation + case "_UvAnimMaskTexture": map.UvAnimMaskTexture = index; break; + default: +#if VRM_DEVELOP + Debug.LogWarning($"textureProperties: {kv.Key} is unknown"); +#endif + break; + } + } + + definition.Rendering.RenderQueueOffsetNumber = + vrmMaterial["renderQueue"].GetInt32() - + MToon.Utils.GetRenderQueueRequirement(definition.Rendering.RenderMode).DefaultValue; + + Definition = definition; + TextureOffsetScales = offsetScale; + TextureIndexMap = map; + } + + private static Color ToColor(JsonNode node, ColorSpace srcColorSpace, ColorSpace dstColorSpace) + { + return node.ArrayItems().Select(x => ListTreeNodeExtensions.GetSingle(x)).ToArray().ToColor4(srcColorSpace, dstColorSpace); + } + + private static float[] ToFloat4(JsonNode node) + { + return node.ArrayItems().Select(x => x.GetSingle()).ToArray(); + } + } +} \ No newline at end of file diff --git a/Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonValue.cs.meta b/Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonValue.cs.meta new file mode 100644 index 000000000..cfcc79e55 --- /dev/null +++ b/Assets/VRM10/Runtime/Migration/Materials/Vrm0XMToonValue.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 83b04861c6ae4b64b724b122b261f4da +timeCreated: 1635423935 \ No newline at end of file diff --git a/Assets/VRM10/Runtime/Migration/MigrationVrm.cs b/Assets/VRM10/Runtime/Migration/MigrationVrm.cs index 079cd30c4..64a46379e 100644 --- a/Assets/VRM10/Runtime/Migration/MigrationVrm.cs +++ b/Assets/VRM10/Runtime/Migration/MigrationVrm.cs @@ -105,9 +105,9 @@ namespace UniVRM10 UniGLTF.Extensions.VRMC_springBone.GltfSerializer.SerializeTo(ref gltf.extensions, springBone); } - // MToon + // MToon { - MigrationMToon.Migrate(gltf, vrm0); + MigrationMToonMaterial.Migrate(gltf, vrm0); } // Serialize whole glTF