Optmize shader structure

This commit is contained in:
Misaki
2025-01-31 17:25:19 +09:00
parent f41c6c9b34
commit e8d1d5923a
19 changed files with 287 additions and 95 deletions

View File

@@ -107,8 +107,8 @@ UTSSurfaceData ConvertSurfaceDataToUTSSurfaceData(SurfaceData surfaceData)
UTSSurfaceData GetUTSSurfaceData(FragInputs input, float3 V)
{
// Not zero initialized to make sure all fields are set.
UTSSurfaceData output;
//ZERO_INITIALIZE(UTSSurfaceData, output);
output.surfaceFeatures = _SurfaceFeatures;
@@ -116,10 +116,19 @@ UTSSurfaceData GetUTSSurfaceData(FragInputs input, float3 V)
output.baseColor = mainTexture.rgb * _BaseColor.rgb;
output.alpha = mainTexture.a;
float4 firstShadingTexture = SAMPLE_TEXTURE2D(_1st_ShadeMap, sampler_BaseColorMap, TRANSFORM_TEX(input.texCoord0, _BaseColorMap));
float4 secondShadingTexture = SAMPLE_TEXTURE2D(_1st_ShadeMap, sampler_BaseColorMap, TRANSFORM_TEX(input.texCoord0, _BaseColorMap));
output.firstShadingColor = lerp(firstShadingTexture.rgb, mainTexture.rgb, _Use_BaseAs1st) * _1st_ShadeColor.rgb;
output.secondShadingColor = lerp(secondShadingTexture.rgb, output.firstShadingColor, _Use_1stAs2nd) * _2nd_ShadeColor.rgb;
#if _SHADING_MODE_THREECOLORSTEP || _SHADING_MODE_SDF
float4 firstShadingTexture = SAMPLE_TEXTURE2D(_1stShadeColorMap, sampler_BaseColorMap, TRANSFORM_TEX(input.texCoord0, _BaseColorMap));
output.firstShadingColor = lerp(firstShadingTexture.rgb, mainTexture.rgb, _UseBaseAs1st) * _1stShadeColor.rgb;
#if _SHADING_MODE_THREECOLORSTEP
float4 secondShadingTexture = SAMPLE_TEXTURE2D(_2ndShadeColorMap, sampler_BaseColorMap, TRANSFORM_TEX(input.texCoord0, _BaseColorMap));
output.secondShadingColor = lerp(secondShadingTexture.rgb, output.firstShadingColor, _Use1stAs2nd) * _2ndShadeColor.rgb;
#else
output.secondShadingColor = 0.0;
#endif
#elif _SHADING_MODE_RAMP
output.firstShadingColor = 0.0;
output.secondShadingColor = 0.0;
#endif
float4 normalLocal = float4(0, 0, 1.0, 1.0);
#if _NORMAL_MAP

View File

@@ -114,11 +114,11 @@ half3 FitWithCurveApprox(half NdotL, half Curvature)
float3 SampleSDFTexture(float3 L, float2 uv, out float angle)
{
float2 right_uv = float2(1 - uv.x, uv.y);
float3 left_SDFTex = SAMPLE_TEXTURE2D(_SDFShadowTex, sampler_SDFShadowTex, uv).rgb;
float3 right_SDFTex = SAMPLE_TEXTURE2D(_SDFShadowTex, sampler_SDFShadowTex, right_uv).rgb;
float3 left_SDFTex = SAMPLE_TEXTURE2D(_SDFShadowMap, sampler_SDFShadowMap, uv).rgb;
float3 right_SDFTex = SAMPLE_TEXTURE2D(_SDFShadowMap, sampler_SDFShadowMap, right_uv).rgb;
float2 leftVector = normalize(mul(UNITY_MATRIX_M, float4(1, 0, 0, 0)).xz);
float2 forwardVector = normalize(mul(UNITY_MATRIX_M, float4(0, 0, 1, 0)).xz);
float2 leftVector = normalize(mul(UNITY_MATRIX_M, float4(1.0, 0.0, 0.0, 0.0)));
float2 forwardVector = normalize(mul(UNITY_MATRIX_M, float4(0.0, 0.0, 1.0, 0.0)));
float2 lightDirection = normalize(L.xz);
angle = 1.0 - (dot(forwardVector, lightDirection) * 0.5 + 0.5);
@@ -162,7 +162,7 @@ UtsShadeMask GetShadeMak(PositionInputs posInput, float3 normalWS, float3 L, SHA
{
UtsShadeMask shadeMask;
#if _SHADOW_MODE_NORMAL
#if _SHADING_MODE_THREECOLORSTEP
float NdotL = dot(normalWS, L);
float halfLambert = 0.5 * NdotL + 0.5;
@@ -176,18 +176,18 @@ UtsShadeMask GetShadeMak(PositionInputs posInput, float3 normalWS, float3 L, SHA
shadeMask.firstShadeMask = saturate((halfLambert - (_2nd_ShadeColor_Step - secondColorFeatherForMask)) / (_2nd_ShadeColor_Step - (_2nd_ShadeColor_Step - secondColorFeatherForMask)));
additionalSpecular = 0;
#elif _SHADOW_MODE_SDF
#elif _SHADING_MODE_SDF
float angle;
float smoothGamma = _SDFSmoothGamma / 10.0f;
float smoothLevel = _SDFSmoothLevel / 10.0f;
float shadowLevel = _SDFShadowLevel / 10.0f;
float3 sdfTexture = SampleSDFTexture(L, uv, angle); // r: sdf shadow, g: sdf noise highlight, b: fixed shadow
float sdfShadowMask = 1.0 - smoothstep(sdfTexture.r - smoothGamma, sdfTexture.r + smoothGamma, angle - shadowLevel);
float sdfShadowMask = smoothstep(sdfTexture.r - smoothLevel, sdfTexture.r + smoothLevel, angle - shadowLevel);
shadeMask.baseShadeMask = sdfShadowMask * sdfTexture.b;
shadeMask.baseShadeMask = sdfShadowMask;
shadeMask.firstShadeMask = 1.0;
additionalSpecular = sdfTexture.g;
additionalSpecular = smoothstep(sdfTexture.g - _SDFHighlightSmoothLevel, sdfTexture.g + _SDFHighlightSmoothLevel, angle - shadowLevel) * _SDFHighlightStrength;
#endif
shadow = smoothstep(0.4, 0.6, shadow);
@@ -209,11 +209,20 @@ DirectLighting UtsShadeSurface(PositionInputs posInput, UtsBSDFData bsdfData, Pr
if (Max3(lightColor.r, lightColor.g, lightColor.b) > 0.0)
{
#if _SHADING_MODE_RAMP
float NdotL = dot(bsdfData.normalWS, L);
float halfLambert = 0.5 * NdotL + 0.5;
float3 rampColor = SAMPLE_TEXTURE2D_ARRAY_LOD(_ShadingGradeMap, s_linear_clamp_sampler, float2(halfLambert, 0.0), _ShadingIndex, 0.0).rgb;
float3 diffuseTerm = bsdfData.diffuseColor * rampColor;
float3 specularTerm = ComputeSpecularTerm(bsdfData, preLightData, V, L) * saturate(NdotL);
#else
float additionalSpecular;
UtsShadeMask shadeMask = GetShadeMak(posInput, bsdfData.normalWS, L, shadow, uv, additionalSpecular);
float3 diffuseTerm = lerp(lerp(bsdfData.secondShadingDiffuseColor, bsdfData.firstShadingDiffuseColor, shadeMask.firstShadeMask), bsdfData.diffuseColor, shadeMask.baseShadeMask);
float3 specularTerm = (ComputeSpecularTerm(bsdfData, preLightData, V, L) + additionalSpecular) * shadeMask.baseShadeMask;
#endif
lighting.diffuse += diffuseTerm * lightColor * diffuseDimmer;
lighting.specular += specularTerm * lightColor * specularDimmer;
@@ -313,7 +322,7 @@ DirectLighting UtsEvaluateAngelRing(FragInputs input, float3 normalWS, float3 V)
float uvMask=lerp(cutU, 1 - cutU, rightside);//discard half of the sdf we sampled (Only one side of highlight wanted)
float lightAtten = pow(max(0, angle - (_SDFShadowLevel / 10.0f)), 0.8);
return smoothstep(lightAtten-_SDFNoseHighlightSmoothRange,lightAtten+_SDFNoseHighlightSmoothRange , uvMask * tex_value) * tex_value; // Safeguard, return 0 when tex_value = 0
return smoothstep(lightAtten-_SDFHighlightSmoothLevel,lightAtten+_SDFHighlightSmoothLevel , uvMask * tex_value) * tex_value; // Safeguard, return 0 when tex_value = 0
}
#endif

View File

@@ -80,9 +80,6 @@ SAMPLER(sampler_CoatMaskMap);
TEXTURE2D(_SSSLutMap);
//SAMPLER(sampler_SSSLutMap); //Use s_linear_clamp_sampler instead
TEXTURE2D(_SDFShadowTex);
SAMPLER(sampler_SDFShadowTex);
TEXTURE2D(_HairShadowTex);
TEXTURE2D(_HairBlendingTex);
@@ -184,10 +181,6 @@ float _SpecularAAThreshold;
#ifndef LAYERED_LIT_SHADER
// Set of users variables
float4 _BaseColor;
float4 _BaseColorMap_ST;
float4 _BaseColorMap_TexelSize;
float4 _BaseColorMap_MipInfo;
float _Metallic;
float _MetallicRemapMin;
@@ -325,21 +318,6 @@ PROP_DECL(float, _LinkDetailsWithBase);
#endif // LAYERED_LIT_SHADER
// Tessellation specific
#ifdef TESSELLATION_ON
float _TessellationFactor;
float _TessellationFactorMinDistance;
float _TessellationFactorMaxDistance;
float _TessellationFactorTriangleSize;
float _TessellationShapeFactor;
float _TessellationBackFaceCullEpsilon;
float _TessellationObjectScale;
float _TessellationTilingScale;
#endif
// Following two variables are feeded by the C++ Editor for Scene selection
int _ObjectId;
int _PassValue;

View File

@@ -1,14 +1,18 @@
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
TEXTURE2D(_1st_ShadeMap);
TEXTURE2D(_2nd_ShadeMap);
TEXTURE2D(_1stShadeColorMap);
TEXTURE2D(_2ndShadeColorMap);
TEXTURE2D(_SDFShadowMap);
SAMPLER(sampler_SDFShadowMap);
TEXTURE2D_ARRAY(_ShadingGradeMap);
TEXTURE2D(_MatCapMap);
sampler _Set_1st_ShadePosition;
sampler _Set_2nd_ShadePosition;
sampler _ShadingGradeMap;
sampler _HighColor_Tex;
sampler _Set_HighColorMask;
sampler _Set_RimLightMask;

View File

@@ -1,25 +1,29 @@
// cosntant in Unity Toon Shader
// Shading Color
float4 _BaseColor;
float4 _BaseColorMap_ST;
float4 _BaseColorMap_TexelSize;
float4 _BaseColorMap_MipInfo;
float4 _1stShadeColor;
float4 _2ndShadeColor;
fixed _UseBaseAs1st;
fixed _Use1stAs2nd;
float _SDFShadowLevel;
float _SDFSmoothLevel;
float _SDFHighlightStrength;
float _SDFHighlightSmoothLevel;
float _ShadingIndex;
float _utsTechnique;
float4 _Color;
fixed _Use_BaseAs1st;
fixed _Use_1stAs2nd;
fixed _Is_LightColor_Base;
float4 _1st_ShadeColor;
fixed _Is_LightColor_1st_Shade;
float4 _2nd_ShadeColor;
fixed _Is_LightColor_2nd_Shade;
fixed _Is_NormalMapToBase;
fixed _Set_SystemShadowsToBase;
float _SurfaceFeatures;
float _Tweak_SystemShadowsLevel;
float _ShadowBias;
float _SDFShadowLevel;
float _SDFSmoothGamma;
float _SDFNoseHighlightCoef;
float _SDFNoseHighlightSmoothRange;
float _EyeParallaxAmount;
float _HairBlendingFactor;