From 6f441acbfd59764454bd07ad275e820333f42af8 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 21 Sep 2022 18:12:44 +0900 Subject: [PATCH] Add implementation of matcapFactor --- .../IO/Material/Vrm10MToonMaterialExporter.cs | 1 + .../IO/Material/Vrm10MToonMaterialImporter.cs | 7 +++ .../VRM10/MToon10/Editor/MToonInspector.cs | 49 +++++++++++-------- .../VRM10/vrmc_materials_mtoon.shader | 1 + .../VRM10/vrmc_materials_mtoon_input.hlsl | 1 + .../vrmc_materials_mtoon_lighting_mtoon.hlsl | 4 +- .../VRM10/MToon10/Runtime/MToon10Context.cs | 4 ++ .../UnityShaderLab/Properties/MToon10Prop.cs | 1 + .../Properties/MToon10Properties.cs | 1 + 9 files changed, 46 insertions(+), 23 deletions(-) diff --git a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs index 1f6a1c45b..f684739ae 100644 --- a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialExporter.cs @@ -101,6 +101,7 @@ namespace UniVRM10 } // Rim Lighting + mtoon.MatcapFactor = context.MatcapColorFactorSrgb.ToFloat3(ColorSpace.sRGB, ColorSpace.Linear); var matcapTextureIndex = textureExporter.RegisterExportingAsSRgb(context.MatcapTexture, needsAlpha: false); if (matcapTextureIndex != -1) { diff --git a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialImporter.cs b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialImporter.cs index 10b50a686..c0a5420a1 100644 --- a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialImporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialImporter.cs @@ -79,6 +79,13 @@ namespace UniVRM10 yield return (MToon10Prop.EmissiveFactor.ToUnityShaderLabName(), emissionColor.Value); } + // Matcap + var matcapColor = mToon?.MatcapFactor?.ToColor3(gltfColorSpace, ColorSpace.sRGB); + if (matcapColor.HasValue) + { + yield return (MToon10Prop.MatcapColorFactor.ToUnityShaderLabName(), matcapColor.Value); + } + // Rim Lighting var rimColor = mToon?.ParametricRimColorFactor?.ToColor3(gltfColorSpace, ColorSpace.sRGB); if (rimColor.HasValue) diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs index af3348f40..da625d88a 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs @@ -147,37 +147,44 @@ namespace VRMShaders.VRM10.MToon10.Editor using (new LabelScope("Rim Lighting")) { - materialEditor.TexturePropertySingleLine( - new GUIContent("Rim Color", "Rim Color (RGB)"), - props[MToon10Prop.RimMultiplyTexture] - ); if (isAdvancedEditMode) { + materialEditor.TexturePropertySingleLine( + new GUIContent("Mask", "Rim Lighting Mask (RGB)"), + props[MToon10Prop.RimMultiplyTexture] + ); materialEditor.ShaderProperty( props[MToon10Prop.RimLightingMixFactor], - new GUIContent("Rim LightingMix") + new GUIContent("LightingMix") ); EditorGUILayout.Space(); + } + using (new LabelScope("Matcap")) + { materialEditor.TexturePropertySingleLine( - new GUIContent("Matcap Rim", "Matcap Rim (RGB)"), - props[MToon10Prop.MatcapTexture] + new GUIContent("Matcap", "Matcap (RGB)"), + props[MToon10Prop.MatcapTexture], + props[MToon10Prop.MatcapColorFactor] + ); + EditorGUILayout.Space(); + } + + using (new LabelScope("Parametric Rim")) + { + materialEditor.ShaderProperty( + props[MToon10Prop.ParametricRimColorFactor], + new GUIContent("Color") + ); + materialEditor.ShaderProperty( + props[MToon10Prop.ParametricRimFresnelPowerFactor], + new GUIContent("Fresnel Power") + ); + materialEditor.ShaderProperty( + props[MToon10Prop.ParametricRimLiftFactor], + new GUIContent("Lift") ); } - EditorGUILayout.Space(); - - materialEditor.ShaderProperty( - props[MToon10Prop.ParametricRimColorFactor], - new GUIContent("Parametric Rim Color") - ); - materialEditor.ShaderProperty( - props[MToon10Prop.ParametricRimFresnelPowerFactor], - new GUIContent("Parametric Rim Fresnel Power") - ); - materialEditor.ShaderProperty( - props[MToon10Prop.ParametricRimLiftFactor], - new GUIContent("Parametric Rim Lift") - ); } using (new LabelScope("Outline")) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader index 81d8d77f5..0c3ade27c 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader @@ -29,6 +29,7 @@ Shader "VRM10/MToon10" _EmissionMap ("emissiveTexture", 2D) = "white" {} // Unity specified name // Rim Lighting + _MatcapColor ("mtoon.matcapFactor", Color) = (1, 1, 1, 1) _MatcapTex ("mtoon.matcapTexture", 2D) = "black" {} _RimColor ("mtoon.parametricRimColorFactor", Color) = (0, 0, 0, 1) _RimFresnelPower ("mtoon.parametricRimFresnelPowerFactor", Range(0, 100)) = 5.0 diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl index 6f5d361e9..274f51d51 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl @@ -22,6 +22,7 @@ float4 _MainTex_ST; half4 _Color; half4 _ShadeColor; half4 _EmissionColor; +half4 _MatcapColor; half4 _RimColor; half4 _OutlineColor; // Floats diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl index 494ceb559..1077311d8 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl @@ -124,11 +124,11 @@ inline half3 GetMToonLighting_Rim_Matcap(const MToonInput input) const half3 matcapRightAxisWS = normalize(cross(input.viewDirWS, worldUpWS)); const half3 matcapUpAxisWS = normalize(cross(matcapRightAxisWS, input.viewDirWS)); const half2 matcapUv = float2(dot(matcapRightAxisWS, input.normalWS), dot(matcapUpAxisWS, input.normalWS)) * 0.5 + 0.5; - return UNITY_SAMPLE_TEX2D(_MatcapTex, matcapUv).rgb; + return _MatcapColor.rgb * UNITY_SAMPLE_TEX2D(_MatcapTex, matcapUv).rgb; } else { - return 0; + return _MatcapColor.rgb; } } diff --git a/Assets/VRMShaders/VRM10/MToon10/Runtime/MToon10Context.cs b/Assets/VRMShaders/VRM10/MToon10/Runtime/MToon10Context.cs index 67b59ad44..e06f82746 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Runtime/MToon10Context.cs +++ b/Assets/VRMShaders/VRM10/MToon10/Runtime/MToon10Context.cs @@ -111,6 +111,10 @@ namespace VRMShaders.VRM10.MToon10.Runtime get => _material.GetTexture(MToon10Prop.EmissiveTexture); } // Rim Lighting + public Color MatcapColorFactorSrgb + { + get => _material.GetColor(MToon10Prop.MatcapColorFactor); + } public Texture MatcapTexture { get => _material.GetTexture(MToon10Prop.MatcapTexture); diff --git a/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/Properties/MToon10Prop.cs b/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/Properties/MToon10Prop.cs index 6a0b6e386..f21a4c8c3 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/Properties/MToon10Prop.cs +++ b/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/Properties/MToon10Prop.cs @@ -29,6 +29,7 @@ EmissiveTexture, // Rim Lighting + MatcapColorFactor, MatcapTexture, ParametricRimColorFactor, ParametricRimFresnelPowerFactor, diff --git a/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/Properties/MToon10Properties.cs b/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/Properties/MToon10Properties.cs index fb209cfcc..d7d684284 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/Properties/MToon10Properties.cs +++ b/Assets/VRMShaders/VRM10/MToon10/Runtime/UnityShaderLab/Properties/MToon10Properties.cs @@ -28,6 +28,7 @@ namespace VRMShaders.VRM10.MToon10.Runtime [MToon10Prop.EmissiveFactor] = "_EmissionColor", [MToon10Prop.EmissiveTexture] = "_EmissionMap", + [MToon10Prop.MatcapColorFactor] = "_MatcapColor", [MToon10Prop.MatcapTexture] = "_MatcapTex", [MToon10Prop.ParametricRimColorFactor] = "_RimColor", [MToon10Prop.ParametricRimFresnelPowerFactor] = "_RimFresnelPower",