Suppress TextureFetch if not needed.

This commit is contained in:
Masataka SUMI 2021-06-07 20:11:00 +09:00
parent 977482df90
commit ccbc53d0d8
5 changed files with 89 additions and 16 deletions

View File

@ -89,8 +89,11 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon"
#pragma multi_compile_instancing
#pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON
#pragma multi_compile_local __ _NORMALMAP
#pragma multi_compile_local __ _UVANIMATION
#pragma multi_compile_local __ _MTOON_NORMALMAP
#pragma multi_compile_local __ _MTOON_SHADINGMAP
#pragma multi_compile_local __ _MTOON_EMISSIVE
#pragma multi_compile_local __ _MTOON_UVANIMATION
#pragma multi_compile_local __ _MTOON_RIM
#pragma vertex MToonVertex
#pragma fragment MToonFragment
@ -122,11 +125,14 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon"
#pragma multi_compile_instancing
#pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON
#pragma multi_compile_local __ _NORMALMAP
#pragma multi_compile_local __ _UVANIMATION
#pragma multi_compile_local __ _MTOON_OUTLINEMAP
#pragma multi_compile_local __ _MTOON_NORMALMAP
#pragma multi_compile_local __ _MTOON_SHADINGMAP
#pragma multi_compile_local __ _MTOON_EMISSIVE
#pragma multi_compile_local __ _MTOON_UVANIMATION
#pragma multi_compile_local __ _MTOON_RIM
#define MTOON_PASS_OUTLINE
#define MTOON_OUTLINE_WIDTH_WORLD
#pragma vertex MToonVertex
#pragma fragment MToonFragment
@ -158,8 +164,11 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon"
#pragma multi_compile_instancing
#pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON
#pragma multi_compile_local __ _NORMALMAP
#pragma multi_compile_local __ _UVANIMATION
#pragma multi_compile_local __ _MTOON_NORMALMAP
#pragma multi_compile_local __ _MTOON_SHADINGMAP
#pragma multi_compile_local __ _MTOON_EMISSIVE
#pragma multi_compile_local __ _MTOON_UVANIMATION
#pragma multi_compile_local __ _MTOON_RIM
#pragma vertex MToonVertex
#pragma fragment MToonFragment

View File

@ -31,7 +31,27 @@ inline bool MToon_IsOutlinePass()
// Compile-time constant
inline bool MToon_IsUvAnimationOn()
{
#if defined(_UVANIMATION)
#if defined(_MTOON_UVANIMATION)
return true;
#else
return false;
#endif
}
// Compile-time constant
inline bool MToon_IsRimOn()
{
#if defined(_MTOON_RIM)
return true;
#else
return false;
#endif
}
// Compile-time constant
inline bool MToon_IsEmissiveOn()
{
#if defined(_MTOON_EMISSIVE)
return true;
#else
return false;
@ -41,7 +61,27 @@ inline bool MToon_IsUvAnimationOn()
// Compile-time constant
inline bool MToon_IsNormalMapOn()
{
#if defined(_NORMALMAP)
#if defined(_MTOON_NORMALMAP)
return true;
#else
return false;
#endif
}
// Compile-time constant
inline bool MToon_IsShadingMapOn()
{
#if defined(_MTOON_SHADINGMAP)
return true;
#else
return false;
#endif
}
// Compile-time constant
inline bool MToon_IsOutlineMapOn()
{
#if defined(_MTOON_OUTLINEMAP)
return true;
#else
return false;

View File

@ -6,7 +6,7 @@
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
float2 GetMToonGeometry_Uv(const float2 geometryUv)
inline float2 GetMToonGeometry_Uv(const float2 geometryUv)
{
// get raw uv with _MainTex_ST
const float2 uvRaw = TRANSFORM_TEX(geometryUv, _MainTex);
@ -14,10 +14,10 @@ float2 GetMToonGeometry_Uv(const float2 geometryUv)
if (MToon_IsUvAnimationOn())
{
const float uvAnimationCoef = UNITY_SAMPLE_TEX2D(_UvAnimMaskTex, uvRaw).b * _Time.y;
const float2 uvAnimationAdd = uvAnimationCoef * float2(_UvAnimScrollXSpeed, _UvAnimScrollYSpeed);
const float2 translate = 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;
return mul(float2x2(cos(rotateRad), -sin(rotateRad), sin(rotateRad), cos(rotateRad)), uvRaw + translate - rotatePivot) + rotatePivot;
}
else
{

View File

@ -12,12 +12,24 @@ struct VertexPositionInfo
float4 positionCS;
};
inline half MToon_GetOutlineVertex_OutlineWidth(const float2 uv)
{
if (MToon_IsOutlineMapOn())
{
return _OutlineWidth * UNITY_SAMPLE_TEX2D_LOD(_OutlineWidthTex, uv, 0);
}
else
{
return _OutlineWidth;
}
}
inline VertexPositionInfo MToon_GetOutlineVertex(const float3 positionOS, const half3 normalOS, const float2 uv)
{
if (MToon_IsOutlineModeWorldCoordinates())
{
const float3 positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1)).xyz;
const half outlineWidth = _OutlineWidth * UNITY_SAMPLE_TEX2D_LOD(_OutlineWidthTex, uv, 0);
const half outlineWidth = MToon_GetOutlineVertex_OutlineWidth(uv);
const half3 normalWS = UnityObjectToWorldNormal(normalOS);
VertexPositionInfo output;

View File

@ -17,11 +17,23 @@ struct MToonInput
half outlineFactor;
};
inline half GetMToonLighting_Reflectance_ShadingShift(const MToonInput input)
{
if (MToon_IsShadingMapOn())
{
return UNITY_SAMPLE_TEX2D(_ShadingShiftTex, input.uv).r * _ShadingShiftTexScale + _ShadingShiftFactor;
}
else
{
return _ShadingShiftFactor;
}
}
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);
const half shadingShift = UNITY_SAMPLE_TEX2D(_ShadingShiftTex, input.uv).r * _ShadingShiftTexScale + _ShadingShiftFactor;
const half shadingShift = GetMToonLighting_Reflectance_ShadingShift(input);
const half shadingToony = _ShadingToonyFactor;
return mtoon_linearstep(-1.0 + shadingToony, +1.0 - shadingToony, shadingInput + shadingShift);
}
@ -55,7 +67,7 @@ inline half3 GetMToonLighting_GlobalIllumination(const UnityLighting unityLight,
inline half3 GetMToonLighting_Emissive(const MToonInput input)
{
if (MToon_IsForwardBasePass())
if (MToon_IsForwardBasePass() && MToon_IsEmissiveOn())
{
return UNITY_SAMPLE_TEX2D(_EmissionMap, input.uv).rgb * _EmissionColor.rgb;
}
@ -67,7 +79,7 @@ inline half3 GetMToonLighting_Emissive(const MToonInput input)
inline half3 GetMToonLighting_Rim(const MToonInput input, const half3 lighting)
{
if (MToon_IsForwardBasePass())
if (MToon_IsForwardBasePass() && MToon_IsRimOn())
{
const half3 worldUpWS = half3(0, 1, 0);
// TODO: use view space axis if abs(dot(viewDir, worldUp)) == 1.0