From 485c43efe6617fae666dfc754afdc4f89aa1ee09 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Tue, 1 Jun 2021 19:21:28 +0900 Subject: [PATCH] implements lighting & emission. --- .../VRM10/vrmc_materials_mtoon.shader | 4 +- .../VRM10/vrmc_materials_mtoon_define.hlsl | 5 ++ ...vrmc_materials_mtoon_forward_fragment.hlsl | 7 ++- .../VRM10/vrmc_materials_mtoon_input.hlsl | 4 +- .../VRM10/vrmc_materials_mtoon_lighting.hlsl | 49 +++++++++++++++++-- .../vrmc_materials_mtoon_unity_lighting.hlsl | 34 +++++++------ 6 files changed, 80 insertions(+), 23 deletions(-) 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 80fb7ee05..b4c78f4ec 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader @@ -16,10 +16,10 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" _ShadeTex ("mtoon.shadeMultiplyTexture", 2D) = "white" {} [Normal] _BumpMap ("normalTexture", 2D) = "bump" {} // Unity specified name _BumpScale ("normalTexture.scale", Float) = 1.0 // Unity specified name - _ShadingShiftColor ("mtoon.shadingShiftFactor", Range(0, 1)) = 0 + _ShadingShiftFactor ("mtoon.shadingShiftFactor", Range(-1, 1)) = 0 _ShadingShiftTex ("mtoon.shadingShiftTexture", 2D) = "black" {} // channel R _ShadingShiftTexScale ("mtoon.shadingShiftTexture.scale", Float) = 1 - _ShadingToonyColor ("mtoon.shadingToonyFactor", Range(0, 1)) = 0.9 + _ShadingToonyFactor ("mtoon.shadingToonyFactor", Range(0, 1)) = 0.9 // _ShadingToonyTex ("mtoon.shadingToonyTexture", 2D) = "black" {} // parameter texture // need? // _ShadingToonyTexScale ("mtoon.shadingToonyTexture.scale", Float) = 1 // need? diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl index 46d0b2bf3..5067bfea9 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl @@ -5,4 +5,9 @@ static const float PI_2 = 6.28318530718; static const float EPS_COL = 0.00001; +inline half3 mtoon_linearstep(half3 start, half3 end, half t) +{ + return min(max((t - start) / (end - start), 0.0), 1.0); +} + #endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl index af43b1340..48fd5fd07 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl @@ -47,7 +47,12 @@ half4 MToonFragment(Varyings input) : SV_Target const UnityLighting unityLighting = GetUnityLighting(input, normalWS); // Get MToon Lighting - const half4 col = GetMToonLighting(unityLighting.directLight, unityLighting.indirectLight, uv, litColor, alpha); + MToonLightingInput lightingInput; + lightingInput.normalWS = normalWS; + lightingInput.uv = uv; + lightingInput.litColor = litColor.rgb; + lightingInput.alpha = alpha; + const half4 col = GetMToonLighting(unityLighting, lightingInput); UNITY_APPLY_FOG(i.fogCoord, col); 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 8519868c0..5870fd6f6 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 @@ -20,15 +20,15 @@ float4 _MainTex_ST; // Colors half4 _Color; half4 _ShadeColor; -half4 _ShadingShiftColor; -half4 _ShadingToonyColor; half4 _EmissionColor; half4 _RimColor; half4 _OutlineColor; // Floats half _Cutoff; half _BumpScale; +half _ShadingShiftFactor; half _ShadingShiftTexScale; +half _ShadingToonyFactor; half _GiEqualization; half _RimFresnelPower; half _RimLift; diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl index 6e54b3008..982d7018f 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl @@ -1,13 +1,56 @@ #ifndef VRMC_MATERIALS_MTOON_LIGHTING_INCLUDED #define VRMC_MATERIALS_MTOON_LIGHTING_INCLUDED +#include "./vrmc_materials_mtoon_define.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" +#include "./vrmc_materials_mtoon_unity_lighting.hlsl" -half4 GetMToonLighting(half3 directLight, half3 indirectLight, float2 uv, half4 litColor, half alpha) +struct MToonLightingInput { - const half4 albedo = litColor; + float2 uv; + half3 normalWS; + half3 litColor; + half alpha; +}; - return half4(albedo.rgb * (directLight + indirectLight), alpha); +half4 GetMToonLighting(UnityLighting lighting, MToonLightingInput input) +{ + const half dotNL = dot(input.normalWS, lighting.directLightDirection); + const half shadingInput = lerp(-1, 1, mtoon_linearstep(-1, 1, dotNL) * lighting.directLightAttenuation); + const half shadingShift = UNITY_SAMPLE_TEX2D(_ShadingShiftTex, input.uv).r * _ShadingShiftTexScale + _ShadingShiftFactor; + const half shadingToony = _ShadingToonyFactor; + const half shading = mtoon_linearstep(-1.0 + shadingToony, +1.0 - shadingToony, shadingInput + shadingShift); + + // lighting +#if defined(UNITY_PASS_FORWARDBASE) + const half3 shadeColor = UNITY_SAMPLE_TEX2D(_ShadeTex, input.uv).rgb * _ShadeColor.rgb; + + const half3 direct = lerp(shadeColor, input.litColor, shading) * lighting.directLightColor; + const half3 indirect = input.litColor * lerp(lighting.indirectLight, lighting.indirectLightEqualized, _GiEqualization); + +#elif defined(UNITY_PASS_FORWARDADD) + const half3 direct = input.litColor * shading * lighting.directLightColor * lerp(1, 0.5, shadingToony); + const half3 indirect = 0; + +#else + const half3 direct = 0; // unexpected + const half3 indirect = 0; + +#endif + + // emission +#if defined(UNITY_PASS_FORWARDBASE) + const half3 emission = UNITY_SAMPLE_TEX2D(_EmissionMap, input.uv).rgb * _EmissionColor.rgb; +#elif defined(UNITY_PASS_FORWARDADD) + const half3 emission = 0; +#else + const half3 emission = 0; +#endif + + + const half3 col = direct + indirect + emission; + + return half4(col, input.alpha); } #endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl index 3cf6e9afa..363dbef2b 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl @@ -9,8 +9,11 @@ struct UnityLighting { - half3 directLight; half3 indirectLight; + half3 indirectLightEqualized; + half3 directLightColor; + half3 directLightDirection; + half3 directLightAttenuation; }; UnityLighting GetUnityLighting(Varyings input, half3 normalWS) @@ -19,22 +22,23 @@ UnityLighting GetUnityLighting(Varyings input, half3 normalWS) const half3 lightDir = normalize(UnityWorldSpaceLightDir(input.positionWS)); const half3 lightColor = _LightColor0.rgb; - const half dotNL = dot(lightDir, normalWS); - -#if defined(UNITY_PASS_FORWARDBASE) - const half3 indirect = ShadeSH9(half4(normalWS, 1)); - const half3 direct = lightColor * max(0, dotNL) * atten; -#elif defined(UNITY_PASS_FORWARDADD) - const half3 indirect = 0; - const half3 direct = lightColor * max(0, dotNL) * atten; -#else - const half3 indirect = half3(1, 1, 0); // error - const half3 direct = 0; -#endif UnityLighting output = (UnityLighting) 0; - output.directLight = direct; - output.indirectLight = indirect; + +#if defined(UNITY_PASS_FORWARDBASE) + output.indirectLight = ShadeSH9(half4(normalWS, 1)); + output.indirectLightEqualized = (ShadeSH9(half4(0, 1, 0, 1)) + ShadeSH9(half4(0, -1, 0, 1))) * 0.5; + output.directLightColor = lightColor; + output.directLightDirection = lightDir; + output.directLightAttenuation = atten; +#elif defined(UNITY_PASS_FORWARDADD) + output.indirectLight = 0; + output.indirectLightEqualized = 0; + output.directLightColor = lightColor; + output.directLightDirection = lightDir; + output.directLightAttenuation = atten; +#endif + return output; }