make function arguments constant.

This commit is contained in:
Masataka SUMI 2021-06-02 20:07:39 +09:00
parent f857935bbd
commit e6d5b4d653
6 changed files with 63 additions and 48 deletions

View File

@ -5,7 +5,7 @@
static const float PI_2 = 6.28318530718;
static const float EPS_COL = 0.00001;
inline half3 mtoon_linearstep(half3 start, half3 end, half t)
inline half3 mtoon_linearstep(const half3 start, const half3 end, const half t)
{
return min(max((t - start) / (end - start), 0.0), 1.0);
}
@ -15,7 +15,7 @@ inline bool MToon_IsPerspective()
return unity_OrthoParams.w != 1.0;
}
inline float3 MToon_GetWorldSpaceNormalizedViewDir(float3 positionWS)
inline float3 MToon_GetWorldSpaceNormalizedViewDir(const float3 positionWS)
{
if (MToon_IsPerspective())
{
@ -27,7 +27,7 @@ inline float3 MToon_GetWorldSpaceNormalizedViewDir(float3 positionWS)
}
}
inline half3x3 MToon_GetTangentToWorld(half3 normalWS, half4 tangentWS)
inline half3x3 MToon_GetTangentToWorld(const half3 normalWS, const half4 tangentWS)
{
const half3 normalizedNormalWS = normalize(normalWS);
const half3 normalizedTangentWS = normalize(tangentWS.xyz);
@ -37,6 +37,7 @@ inline half3x3 MToon_GetTangentToWorld(half3 normalWS, half4 tangentWS)
return half3x3(normalizedTangentWS, normalizedBitangentWS, normalizedNormalWS);
}
// Compile-time constant
inline bool MToon_IsForwardBasePass()
{
#if defined(UNITY_PASS_FORWARDBASE)
@ -44,7 +45,17 @@ inline bool MToon_IsForwardBasePass()
#elif defined(UNITY_PASS_FORWARDADD)
return false;
#else
// ????
// unexpected
return false;
#endif
}
// Compile-time constant
inline bool MToon_IsUvAnimationOn()
{
#if defined(_UVANIMATION)
return true;
#else
return false;
#endif
}

View File

@ -11,7 +11,7 @@
#include "./vrmc_materials_mtoon_uv.hlsl"
#include "./vrmc_materials_mtoon_unity_lighting.hlsl"
half4 MToonFragment(Varyings input) : SV_Target
half4 MToonFragment(const Varyings input) : SV_Target
{
// Get MToon UV (with UVAnimation)
const float2 uv = GetMToonUv(input.uv);
@ -44,13 +44,13 @@ half4 MToonFragment(Varyings input) : SV_Target
const UnityLighting unityLighting = GetUnityLighting(input, normalWS);
// Get MToon Lighting
MToonInput lightingInput;
lightingInput.uv = uv;
lightingInput.normalWS = normalWS;
lightingInput.viewDirWS = input.viewDirWS;
lightingInput.litColor = litColor.rgb;
lightingInput.alpha = alpha;
const half4 col = GetMToonLighting(unityLighting, lightingInput);
MToonInput mtoonInput;
mtoonInput.uv = uv;
mtoonInput.normalWS = normalWS;
mtoonInput.viewDirWS = input.viewDirWS;
mtoonInput.litColor = litColor.rgb;
mtoonInput.alpha = alpha;
const half4 col = GetMToonLighting(unityLighting, mtoonInput);
UNITY_APPLY_FOG(i.fogCoord, col);

View File

@ -8,7 +8,7 @@
#include "./vrmc_materials_mtoon_input.hlsl"
#include "./vrmc_materials_mtoon_attribute.hlsl"
Varyings MToonVertex(Attributes v) // v is UnityCG macro specified name.
Varyings MToonVertex(const Attributes v) // v is UnityCG macro specified name.
{
Varyings output = (Varyings)0;

View File

@ -14,7 +14,7 @@ struct MToonInput
half alpha;
};
inline half GetMToonLighting_Reflectance(UnityLighting lighting, MToonInput input)
inline half GetMToonLighting_Reflectance(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);
@ -23,7 +23,7 @@ inline half GetMToonLighting_Reflectance(UnityLighting lighting, MToonInput inpu
return mtoon_linearstep(-1.0 + shadingToony, +1.0 - shadingToony, shadingInput + shadingShift);
}
inline half3 GetMToonLighting_BasicLighting(UnityLighting unityLight, MToonInput input, half reflectance)
inline half3 GetMToonLighting_BasicLighting(const UnityLighting unityLight, const MToonInput input, const half reflectance)
{
if (MToon_IsForwardBasePass())
{
@ -43,7 +43,7 @@ inline half3 GetMToonLighting_BasicLighting(UnityLighting unityLight, MToonInput
}
}
inline half3 GetMToonLighting_Emissive(MToonInput input)
inline half3 GetMToonLighting_Emissive(const MToonInput input)
{
if (MToon_IsForwardBasePass())
{
@ -55,7 +55,7 @@ inline half3 GetMToonLighting_Emissive(MToonInput input)
}
}
inline half3 GetMToonLighting_Rim(MToonInput input, half3 lighting)
inline half3 GetMToonLighting_Rim(const MToonInput input, const half3 lighting)
{
if (MToon_IsForwardBasePass())
{
@ -73,7 +73,7 @@ inline half3 GetMToonLighting_Rim(MToonInput input, half3 lighting)
}
}
half4 GetMToonLighting(UnityLighting unityLight, MToonInput input)
half4 GetMToonLighting(const UnityLighting unityLight, const MToonInput input)
{
const half reflectance = GetMToonLighting_Reflectance(unityLight, input);

View File

@ -16,30 +16,33 @@ struct UnityLighting
half3 directLightAttenuation;
};
UnityLighting GetUnityLighting(Varyings input, half3 normalWS)
UnityLighting GetUnityLighting(const Varyings input, const half3 normalWS)
{
UNITY_LIGHT_ATTENUATION(atten, input, input.positionWS);
const half3 lightDir = normalize(UnityWorldSpaceLightDir(input.positionWS));
const half3 lightColor = _LightColor0.rgb;
UnityLighting output = (UnityLighting) 0;
#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;
if (MToon_IsForwardBasePass())
{
UnityLighting output;
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;
return output;
}
else
{
UnityLighting output;
output.indirectLight = 0;
output.indirectLightEqualized = 0;
output.directLightColor = lightColor;
output.directLightDirection = lightDir;
output.directLightAttenuation = atten;
return output;
}
}
#endif

View File

@ -5,22 +5,23 @@
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
float2 GetMToonUv(float2 geometryUv)
float2 GetMToonUv(const float2 geometryUv)
{
// get raw uv with _MainTex_ST
const float2 uvRaw = TRANSFORM_TEX(geometryUv, _MainTex);
#if defined(_UVANIMATION)
const float uvAnimationCoef = UNITY_SAMPLE_TEX2D(_UvAnimMaskTex, uvRaw).b * _Time.y;
const float2 uvAnimationAdd = uvAnimationCoef * float2(_UvAnimScrollXSpeed, _UvAnimScrollYSpeed);
const float rotateRad = uvAnimationCoef * _UvAnimRotationSpeed * PI_2;
const float2 rotatePivot = float2(0.5, 0.5);
const float2 uv = mul(float2x2(cos(rotateRad), -sin(rotateRad), sin(rotateRad), cos(rotateRad)), uvRaw + uvAnimationAdd - rotatePivot) + rotatePivot;
#else
const float2 uv = uvRaw;
#endif
return uv;
if (MToon_IsUvAnimationOn())
{
const float uvAnimationCoef = UNITY_SAMPLE_TEX2D(_UvAnimMaskTex, uvRaw).b * _Time.y;
const float2 uvAnimationAdd = uvAnimationCoef * float2(_UvAnimScrollXSpeed, _UvAnimScrollYSpeed);
const float rotateRad = uvAnimationCoef * _UvAnimRotationSpeed * PI_2;
const float2 rotatePivot = float2(0.5, 0.5);
return mul(float2x2(cos(rotateRad), -sin(rotateRad), sin(rotateRad), cos(rotateRad)), uvRaw + uvAnimationAdd - rotatePivot) + rotatePivot;
}
else
{
return uvRaw;
}
}
#endif