implements lighting & emission.

This commit is contained in:
Masataka SUMI 2021-06-01 19:21:28 +09:00
parent d25eb746df
commit 485c43efe6
6 changed files with 80 additions and 23 deletions

View File

@ -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?

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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

View File

@ -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;
}