follow 0.x lighting behaviour in mtoon 1.0

This commit is contained in:
Masataka SUMI 2021-06-11 21:45:13 +09:00
parent 48842419d0
commit ecf684722a

View File

@ -28,29 +28,51 @@ inline half GetMToonLighting_Reflectance_ShadingShift(const MToonInput input)
}
}
inline half GetMToonLighting_Reflectance(const UnityLighting lighting, const MToonInput input)
inline half GetMToonLighting_DotNL(const UnityLighting lighting, const MToonInput 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 = GetMToonLighting_Reflectance_ShadingShift(input);
const half shadingToony = _ShadingToonyFactor;
return mtoon_linearstep(-1.0 + shadingToony, +1.0 - shadingToony, shadingInput + shadingShift);
return dot(input.normalWS, lighting.directLightDirection);
}
inline half3 GetMToonLighting_DirectLighting(const UnityLighting unityLight, const MToonInput input, const half reflectance)
inline half GetMToonLighting_Shade(const UnityLighting lighting, const MToonInput input, const half dotNL)
{
const half shadeShift = GetMToonLighting_Reflectance_ShadingShift(input);
const half shadeToony = _ShadingToonyFactor;
if (MToon_IsForwardBasePass())
{
const half3 shadeColor = UNITY_SAMPLE_TEX2D(_ShadeTex, input.uv).rgb * _ShadeColor.rgb;
return lerp(shadeColor, input.litColor, reflectance) * unityLight.directLightColor;
const half shadeInput = lerp(-1, 1, mtoon_linearstep(-1, 1, dotNL) * lighting.directLightAttenuation);
return mtoon_linearstep(-1.0 + shadeToony, +1.0 - shadeToony, shadeInput + shadeShift);
}
else
{
return input.litColor * reflectance * unityLight.directLightColor * 0.5;
const half shadeInput = dotNL;
return mtoon_linearstep(-1.0 + shadeToony, +1.0 - shadeToony, shadeInput + shadeShift);
}
}
inline half GetMToonLighting_Shadow(const UnityLighting lighting, const half dotNL)
{
if (MToon_IsForwardBasePass())
{
return 1;
}
else
{
// heuristic term for weak lights.
// 0.5: heuristic.
// min(0, dotNL) + 1: darken if (dotNL < 0) by using half lambert.
return lighting.directLightAttenuation * 0.5 * (min(0, dotNL) + 1);
}
}
inline half3 GetMToonLighting_DirectLighting(const UnityLighting unityLight, const MToonInput input, const half shade, const half shadow)
{
const half3 shadeColor = UNITY_SAMPLE_TEX2D(_ShadeTex, input.uv).rgb * _ShadeColor.rgb;
const half3 albedo = lerp(shadeColor, input.litColor, shade);
return albedo * unityLight.directLightColor * shadow;
}
inline half3 GetMToonLighting_GlobalIllumination(const UnityLighting unityLight, const MToonInput input)
{
if (MToon_IsForwardBasePass())
@ -92,38 +114,45 @@ inline half3 GetMToonLighting_Rim_Matcap(const MToonInput input)
}
}
inline half3 GetMToonLighting_Rim(const MToonInput input, const half3 reflectance)
inline half3 GetMToonLighting_Rim(const UnityLighting unityLight, const MToonInput input, const half shadow)
{
const half3 parametricRimFactor = pow(saturate(1.0 - dot(input.normalWS, input.viewDirWS) + _RimLift), _RimFresnelPower) * _RimColor.rgb;
const half3 matcapFactor = GetMToonLighting_Rim_Matcap(input);
const half3 directLightingFactor = unityLight.directLightColor * shadow;
half3 rimLightingFactor;
if (MToon_IsForwardBasePass())
{
const half3 parametricRimFactor = pow(saturate(1.0 - dot(input.normalWS, input.viewDirWS) + _RimLift), _RimFresnelPower) * _RimColor.rgb;
const half3 rimLightingFactor = lerp(half3(1, 1, 1), reflectance, _RimLightingMix);
const half3 matcapFactor = GetMToonLighting_Rim_Matcap(input);
const half3 indirectLightingFactor = unityLight.indirectLight;
if (MToon_IsRimMapOn())
{
return (matcapFactor + parametricRimFactor) * rimLightingFactor * UNITY_SAMPLE_TEX2D(_RimTex, input.uv).rgb;
}
else
{
return (matcapFactor + parametricRimFactor) * rimLightingFactor;
}
rimLightingFactor = lerp(half3(1, 1, 1), directLightingFactor + indirectLightingFactor, _RimLightingMix);
}
else
{
return 0;
rimLightingFactor = lerp(half3(0, 0, 0), directLightingFactor, _RimLightingMix);
}
if (MToon_IsRimMapOn())
{
return (matcapFactor + parametricRimFactor) * rimLightingFactor * UNITY_SAMPLE_TEX2D(_RimTex, input.uv).rgb;
}
else
{
return (matcapFactor + parametricRimFactor) * rimLightingFactor;
}
}
half4 GetMToonLighting(const UnityLighting unityLight, const MToonInput input)
{
const half reflectance = GetMToonLighting_Reflectance(unityLight, input);
const half dotNL = GetMToonLighting_DotNL(unityLight, input);
const half shade = GetMToonLighting_Shade(unityLight, input, dotNL);
const half shadow = GetMToonLighting_Shadow(unityLight, dotNL);
const half3 direct = GetMToonLighting_DirectLighting(unityLight, input, reflectance);
const half3 direct = GetMToonLighting_DirectLighting(unityLight, input, shade, shadow);
const half3 indirect = GetMToonLighting_GlobalIllumination(unityLight, input);
const half3 lighting = direct + indirect;
const half3 emissive = GetMToonLighting_Emissive(input);
const half3 rim = GetMToonLighting_Rim(input, reflectance);
const half3 rim = GetMToonLighting_Rim(unityLight, input, shadow);
const half3 baseCol = lighting + emissive + rim;