Update custom pass to global custom pass

This commit is contained in:
Misaki
2024-10-23 20:15:07 +09:00
parent e441bb7911
commit d0554a73bb
64 changed files with 949 additions and 46128 deletions

View File

@@ -304,8 +304,9 @@ void Frag(PackedVaryingsToPS packedInput,
ZERO_INITIALIZE(UTSAggregateLighting, utsAggregateLighting);
UTSLightData customMainLight;
UTSLightData mainPunctualLight;
mainPunctualLight.lightColor = float3(0, 0, 0);
customMainLight.shadowValue = 1.0f;
//UTSLightData mainPunctualLight;
//mainPunctualLight.lightColor = float3(0, 0, 0);
#define UNITY_PROJ_COORD(a) a
#define UNITY_SAMPLE_SCREEN_SHADOW(tex, uv) tex2Dproj( tex, UNITY_PROJ_COORD(uv) ).r
@@ -350,13 +351,13 @@ void Frag(PackedVaryingsToPS packedInput,
float3 lightColor = ApplyCurrentExposureMultiplier(lightData.color);
float3 lightDirection = -lightData.forward;
#ifndef LIGHT_EVALUATION_NO_COOKIE
#ifndef LIGHT_EVALUATION_NO_COOKIE
if (lightData.cookieMode != COOKIEMODE_NONE)
{
float3 lightToSample = input.positionRWS - lightData.positionRWS;
lightColor *= EvaluateCookie_Directional(context, lightData, lightToSample);
}
#endif
#endif
UTSLightData utsLightData;
utsLightData.lightDirection = lightDirection;
@@ -366,18 +367,20 @@ void Frag(PackedVaryingsToPS packedInput,
utsLightData.shadowTint = lightData.shadowTint;
utsLightData.penumbraTint = lightData.penumbraTint;
customMainLight = utsLightData;
// Evaluate sun shadows.
if (_DirectionalShadowIndex >= 0)
{
DirectionalLightData light = _DirectionalLightDatas[_DirectionalShadowIndex];
#if defined(SCREEN_SPACE_SHADOWS_ON) && !defined(_SURFACE_TYPE_TRANSPARENT) && !defined(UTS_USE_RAYTRACING_SHADOW)
#if defined(SCREEN_SPACE_SHADOWS_ON) && !defined(_SURFACE_TYPE_TRANSPARENT) && !defined(UTS_USE_RAYTRACING_SHADOW)
if (UtsUseScreenSpaceShadow(light, bsdfData.normalWS))
{
// HDRP Contact Shadow
context.shadowValue = GetScreenSpaceColorShadow(posInput, light.screenSpaceShadowIndex).SHADOW_TYPE_SWIZZLE;
}
else
#endif
#endif
{
// TODO: this will cause us to load from the normal buffer first. Does this cause a performance problem?
float3 L = -light.forward;
@@ -388,7 +391,7 @@ void Frag(PackedVaryingsToPS packedInput,
!ShouldEvaluateThickObjectTransmission(V, L, preLightData, bsdfData, light.shadowIndex))
{
#if defined(UTS_USE_RAYTRACING_SHADOW)
#if defined(UTS_USE_RAYTRACING_SHADOW)
{
/*
struct PositionInputs
@@ -406,37 +409,37 @@ void Frag(PackedVaryingsToPS packedInput,
float r = UNITY_SAMPLE_SCREEN_SHADOW(_RaytracedHardShadow, float4(posInput.positionNDC.xy, lightDirection * _ShadowBias, 1));
context.shadowValue = r;
}
#else
#else
{
context.shadowValue = GetDirectionalShadowAttenuation(context.shadowContext,
posInput.positionSS, posInput.positionWS + lightDirection * _ShadowBias, GetNormalForShadowBias(bsdfData),
light.shadowIndex, L);
}
#endif // UTS_USE_RAYTRACING_SHADOW
#endif // UTS_USE_RAYTRACING_SHADOW
}
#if defined (UTS_USE_RAYTRACING_SHADOW)
#if defined (UTS_USE_RAYTRACING_SHADOW)
else
{
float r = UNITY_SAMPLE_SCREEN_SHADOW(_RaytracedHardShadow, float4(posInput.positionNDC.xy, lightDirection * _ShadowBias, 1));
context.shadowValue = r;
}
#endif // UTS_USE_RAYTRACING_SHADOW
#endif // UTS_USE_RAYTRACING_SHADOW
}
context.shadowValue = lerp(1, context.shadowValue, lightData.shadowDimmer);
customMainLight.shadowValue = context.shadowValue;
}
#if defined(UTS_DEBUG_SELFSHADOW)
#if defined(UTS_DEBUG_SELFSHADOW)
if (_DirectionalShadowIndex >= 0)
finalColor = UTS_SelfShdowMainLight(context, input, _DirectionalShadowIndex);
#else
#else
UTS_MainLight(context, input, utsLightData, surfaceData, bsdfData, inverseClipping, channelAlpha, utsData, utsAggregateLighting);
#endif
#endif
int i = 0; // Declare once to avoid the D3D11 compiler warning.
@@ -455,11 +458,11 @@ void Frag(PackedVaryingsToPS packedInput,
utsLightData.shadowTint = _DirectionalLightDatas[i].shadowTint;
utsLightData.penumbraTint = _DirectionalLightDatas[i].penumbraTint;
#if defined(UTS_DEBUG_SELFSHADOW)
#if defined(UTS_DEBUG_SELFSHADOW)
#else
#else
UTS_OtherLights(context, input, utsLightData, surfaceData, bsdfData, 0, i_normalDir, notDirectional, channelAlpha, utsAggregateLighting);
#endif
#endif
}
}
}
@@ -474,14 +477,14 @@ void Frag(PackedVaryingsToPS packedInput,
{
uint lightCount, lightStart;
#ifndef LIGHTLOOP_DISABLE_TILE_AND_CLUSTER
#ifndef LIGHTLOOP_DISABLE_TILE_AND_CLUSTER
GetCountAndStart(posInput, LIGHTCATEGORY_PUNCTUAL, lightStart, lightCount);
#else // LIGHTLOOP_DISABLE_TILE_AND_CLUSTER
#else // LIGHTLOOP_DISABLE_TILE_AND_CLUSTER
lightCount = _PunctualLightCount;
lightStart = 0;
#endif
#endif
bool fastPath = false;
#if SCALARIZE_LIGHT_LOOP
#if SCALARIZE_LIGHT_LOOP
uint lightStartLane0;
fastPath = IsFastPath(lightStart, lightStartLane0);
@@ -489,7 +492,7 @@ void Frag(PackedVaryingsToPS packedInput,
{
lightStart = lightStartLane0;
}
#endif
#endif
@@ -506,11 +509,11 @@ void Frag(PackedVaryingsToPS packedInput,
while (v_lightListOffset < lightCount)
{
v_lightIdx = FetchIndex(lightStart, v_lightListOffset);
#if SCALARIZE_LIGHT_LOOP
#if SCALARIZE_LIGHT_LOOP
uint s_lightIdx = ScalarizeElementIndex(v_lightIdx, fastPath);
#else
#else
uint s_lightIdx = v_lightIdx;
#endif
#endif
if (s_lightIdx == -1)
break;
@@ -531,13 +534,6 @@ void Frag(PackedVaryingsToPS packedInput,
float3 additionalLightColor = ApplyCurrentExposureMultiplier(lightColor.rgb) * lightColor.a;
const float notDirectional = 1.0f;
float3 lightDirectionToObject;
float4 distancesToObject; // {d, d^2, 1/d, d_proj}
float3 objectCenter = (TransformObjectToWorld(float3(0, 0, 0)));
GetPunctualLightVectors(objectCenter, s_lightData, lightDirectionToObject, distancesToObject);
float4 lightColorToObject = EvaluateLight_Punctual(context, posInput, s_lightData, lightDirectionToObject, distancesToObject);
float3 additionalLightColorToObject = ApplyCurrentExposureMultiplier(lightColorToObject.rgb) * lightColorToObject.a;
UTSLightData utsLightData;
utsLightData.lightColor = additionalLightColor;
utsLightData.lightDirection = lightDirection;
@@ -546,23 +542,31 @@ void Frag(PackedVaryingsToPS packedInput,
utsLightData.shadowTint = s_lightData.shadowTint;
utsLightData.penumbraTint = s_lightData.penumbraTint;
if (length(utsLightData.lightColor) >= length(mainPunctualLight.lightColor))
{
//mainPunctualLight = utsLightData;
mainPunctualLight.lightColor = additionalLightColorToObject;
mainPunctualLight.lightDirection = lightDirectionToObject;
}
#if defined(UTS_DEBUG_SELFSHADOW)
#if defined(UTS_DEBUG_SELFSHADOW)
#else
#else
posInput.positionWS = posInput.positionWS + lightDirection * _ShadowBias;
float shadow = EvaluateShadow_Punctual(context, posInput, s_lightData, builtinData, GetNormalForShadowBias(bsdfData), lightDirection, distances);
context.shadowValue = shadow;
posInput.positionWS = posInput.positionWS - lightDirection * _ShadowBias;
if (length(utsLightData.lightColor) >= length(customMainLight.lightColor))
{
float3 lightDirectionToObject;
float4 distancesToObject; // {d, d^2, 1/d, d_proj}
float3 objectCenter = (TransformObjectToWorld(float3(0, 0, 0)));
GetPunctualLightVectors(objectCenter, s_lightData, lightDirectionToObject, distancesToObject);
float4 lightColorToObject = EvaluateLight_Punctual(context, posInput, s_lightData, lightDirectionToObject, distancesToObject);
float3 additionalLightColorToObject = ApplyCurrentExposureMultiplier(lightColorToObject.rgb) * lightColorToObject.a;
customMainLight = utsLightData;
customMainLight.lightColor = additionalLightColorToObject;
customMainLight.lightDirection = lightDirectionToObject;
customMainLight.shadowValue = context.shadowValue;
}
UTS_OtherLights(context, input, utsLightData, surfaceData, bsdfData, s_lightData.lightType, i_normalDir, notDirectional, channelAlpha, utsAggregateLighting);
#endif
#endif
}
}
@@ -575,12 +579,12 @@ void Frag(PackedVaryingsToPS packedInput,
{
uint lightCount, lightStart;
#ifndef LIGHTLOOP_DISABLE_TILE_AND_CLUSTER
#ifndef LIGHTLOOP_DISABLE_TILE_AND_CLUSTER
GetCountAndStart(posInput, LIGHTCATEGORY_AREA, lightStart, lightCount);
#else
#else
lightCount = _AreaLightCount;
lightStart = _PunctualLightCount;
#endif
#endif
// COMPILER BEHAVIOR WARNING!
// If rectangle lights are before line lights, the compiler will duplicate light matrices in VGPR because they are used differently between the two types of lights.
@@ -645,13 +649,13 @@ void Frag(PackedVaryingsToPS packedInput,
//Evaluate the shadow part
float shadow;
posInput.positionWS = posInput.positionWS + utsLightData.lightDirection * _ShadowBias;
#if defined(SCREEN_SPACE_SHADOWS_ON) && !defined(_SURFACE_TYPE_TRANSPARENT)
#if defined(SCREEN_SPACE_SHADOWS_ON) && !defined(_SURFACE_TYPE_TRANSPARENT)
if ((s_lightData.screenSpaceShadowIndex & SCREEN_SPACE_SHADOW_INDEX_MASK) != INVALID_SCREEN_SPACE_SHADOW)
{
shadow = GetScreenSpaceShadow(posInput, s_lightData.screenSpaceShadowIndex);
}
else
#endif
#endif
{
shadow = EvaluateShadow_RectArea(context, posInput, s_lightData, builtinData, GetNormalForShadowBias(bsdfData), normalize(s_lightData.positionRWS), length(s_lightData.positionRWS));
}
@@ -659,13 +663,13 @@ void Frag(PackedVaryingsToPS packedInput,
posInput.positionWS = posInput.positionWS - lightDirection * _ShadowBias;
}
#if defined(UTS_DEBUG_SELFSHADOW)
#if defined(UTS_DEBUG_SELFSHADOW)
#else
#else
UTS_OtherLights(context, input, utsLightData, surfaceData, bsdfData, s_lightData.lightType, i_normalDir, notDirectional, channelAlpha, utsAggregateLighting);
//utsAggregateLighting.directDiffuse += ltcValue.rgb * ltcValue.a * intensity * s_lightData.diffuseDimmer;
//utsAggregateLighting.directDiffuse += intensity;
#endif
#endif
/*
if(s_lightData.lightType == GPULIGHTTYPE_RECTANGLE)
@@ -829,7 +833,7 @@ void Frag(PackedVaryingsToPS packedInput,
}
}
}
#endif
#endif // SHADEROPTIONS_AREA_LIGHTS
#ifdef _EMISSIVE_SIMPLE
float4 _Emissive_Tex_var = tex2D(_Emissive_Tex, TRANSFORM_TEX(Set_UV0, _Emissive_Tex));
@@ -872,7 +876,8 @@ void Frag(PackedVaryingsToPS packedInput,
//v.2.0.6: GI_Intensity with Intensity Multiplier Filter
#endif
customMainLight = GetCustomMainLightData(builtinData, mainPunctualLight);
// We directly calculate custome main light during the light loop in upper code to avoid extra calculation
//customMainLight = GetCustomMainLightData(builtinData, mainPunctualLight);
#if _SDFShadow || _RECEIVE_HAIR_SHADOW
float3 defaultLightDirection = normalize(UNITY_MATRIX_V[2].xyz + UNITY_MATRIX_V[1].xyz);
@@ -888,6 +893,8 @@ void Frag(PackedVaryingsToPS packedInput,
float4 _1st_ShadeMap_var = lerp(SAMPLE_TEXTURE2D(_1st_ShadeMap, sampler_BaseColorMap,TRANSFORM_TEX(Set_UV0, _1st_ShadeMap)), _MainTex_var, _Use_BaseAs1st);
float3 _1st_Shade_var = lerp((_1st_ShadeMap_var.rgb * _1st_ShadeColor.rgb), ((_1st_ShadeMap_var.rgb * _1st_ShadeColor.rgb) * lightColor), _Is_LightColor_1st_Shade);
float systemShadowValue = lerp(1.0f, saturate(customMainLight.shadowValue * 2.0f), _Set_SystemShadowsToBase);
#endif
#ifdef _SDFShadow
@@ -897,12 +904,11 @@ void Frag(PackedVaryingsToPS packedInput,
bool rightside;
float2 SDF_UV = TRANSFORM_TEX(Set_UV0, _BaseColorMap);
float4 sdfRes = SDFResult(rightside, angle, customMainLight.lightDirection, SDF_UV);
float sdfMask = SDFMask(angle, sdfRes.r);
utsAggregateLighting.directDiffuse = lerp(bsdfData.diffuseColor * _BaseColor * lightColor, _1st_Shade_var, sdfMask);
utsAggregateLighting.directSpecular = lerp(utsAggregateLighting.directSpecular, 0, sdfMask);
utsAggregateLighting.directDiffuse += _SDFNoseHighlightCoef * SDFNoseHighlight(angle, sdfRes.g, rightside, SDF_UV) * lightColor;
//utsAggregateLighting.directDiffuse = lightColor;
float sdfShadowValue = 1.0f - SDFMask(angle, sdfRes.r);
utsAggregateLighting.directDiffuse = lerp(_1st_Shade_var, bsdfData.diffuseColor * _BaseColor.rgb * lightColor, sdfShadowValue * systemShadowValue);
utsAggregateLighting.directSpecular = lerp(0, utsAggregateLighting.directSpecular, sdfShadowValue * systemShadowValue);
utsAggregateLighting.directSpecular += _SDFNoseHighlightCoef * SDFNoseHighlight(angle, sdfRes.g, rightside, SDF_UV) * lightColor;
#endif
#if defined(_RECEIVE_HAIR_SHADOW) && defined(_HAIR_SHADOWS)
@@ -911,19 +917,18 @@ void Frag(PackedVaryingsToPS packedInput,
if(hairShadowOpacity > 0)
{
float2 scrPos = input.positionSS.xy;
float3 viewLightDir = TransformWorldToViewDir(customMainLight.lightDirection) / posInput.linearDepth; // / posInput.deviceDepth; when linearDepth grows large, the movement amount should be lower since we are getting further from the face.
float shadowLength = _HairShadowDistance * 5.0 * max(0.5, posInput.linearDepth * _HairShadowDistanceScaleFactor);
float2 samplingPoint = (scrPos + shadowLength * viewLightDir.xy ) * _ScreenSize.zw;
float3 viewLightDir = TransformWorldToViewDir(customMainLight.lightDirection); // / posInput.deviceDepth; when linearDepth grows large, the movement amount should be lower since we are getting further from the face.
float shadowLength = _HairShadowDistance * 5.0 * max(0.5, posInput.linearDepth * _HairShadowDistanceScaleFactor) / posInput.linearDepth;
float2 samplingPoint = (input.positionSS.xy + shadowLength * viewLightDir.xy * (_ScreenSize.xy / float2 (1920.0f, 1080.0f))) * _ScreenSize.zw; // Use 1080p as the reference resolution to achieve consistent shadow lengths across various screen resolutions.
// Then sample the hair buffer, to see if the fragment lands in shadow.
float3 hairBuffer = SAMPLE_TEXTURE2D(_HairShadowTex, s_trilinear_clamp_sampler, samplingPoint);
float4 hairBuffer = SAMPLE_TEXTURE2D(_HairShadowTex, s_trilinear_clamp_sampler, samplingPoint);
float hairDepth = hairBuffer.r;
float depthCorrect = posInput.deviceDepth <= hairDepth + _HairShadowDepthBias ? 1 : 0; // Hair < Face means Hair are closer to camera
// Note that we have LinearEyeDepth in the buffer. A comparison of depth is needed so that we don't project the shadow of hair behind the face.
float hairShadow = lerp(0,hairShadowOpacity,depthCorrect);
utsAggregateLighting.directDiffuse = lerp(utsAggregateLighting.directDiffuse, _1st_Shade_var, hairShadow);
utsAggregateLighting.directSpecular = lerp(utsAggregateLighting.directSpecular, 0, hairShadow);
utsAggregateLighting.directDiffuse = lerp(utsAggregateLighting.directDiffuse, _1st_Shade_var, hairShadow * systemShadowValue);
utsAggregateLighting.directSpecular = lerp(utsAggregateLighting.directSpecular, 0, hairShadow * systemShadowValue);
}
#endif
@@ -950,25 +955,7 @@ void Frag(PackedVaryingsToPS packedInput,
outColor = float4(outColor.rgb, Set_Opacity * ApplyChannelAlpha(channelAlpha));
#endif
#if defined(UTS_DEBUG_SHADOWMAP) || defined(UTS_DEBUG_SELFSHADOW)
outColor.rgb = 1;
#ifdef UTS_DEBUG_SELFSHADOW
outColor.rgb = min(finalColor, outColor.rgb);
#endif
#ifdef UTS_DEBUG_SHADOWMAP
#ifdef UTS_DEBUG_SHADOWMAP_BINALIZATION
outColor.rgb = min(context.shadowValue < 0.9f ? clamp(context.shadowValue - 0.2, 0.0, 0.9) : 1.0f, outColor.rgb);
#else
outColor.rgb = min(context.shadowValue, outColor.rgb);
#endif
#endif // ifdef UTS_DEBUG_SHADOWMAP
#endif // defined(UTS_DEBUG_SHADOWMAP) || defined(UTS_DEBUG_SELFSHADOW)
#ifdef _EYEBROW_SEETHROUGH
// By Suomi, 20230915
// The eyebrow should use transparent pass and utilize the hair depth texture we have from HairShadowPass
float2 samplingPoint = posInput.positionNDC;
@@ -989,9 +976,22 @@ void Frag(PackedVaryingsToPS packedInput,
//outColor.rgb = float3(hDepth.rg * 5,0);
#endif
#if defined(UTS_DEBUG_SHADOWMAP) || defined(UTS_DEBUG_SELFSHADOW)
outColor.rgb = 1;
#ifdef UTS_DEBUG_SELFSHADOW
outColor.rgb = min(finalColor, outColor.rgb);
#endif
#ifdef UTS_DEBUG_SHADOWMAP
#ifdef UTS_DEBUG_SHADOWMAP_BINALIZATION
outColor.rgb = min(context.shadowValue < 0.9f ? clamp(context.shadowValue - 0.2, 0.0, 0.9) : 1.0f, outColor.rgb);
#else
outColor.rgb = min(context.shadowValue, outColor.rgb);
#endif
#endif // ifdef UTS_DEBUG_SHADOWMAP
#endif // defined(UTS_DEBUG_SHADOWMAP) || defined(UTS_DEBUG_SELFSHADOW)
#ifdef _DEPTHOFFSET_ON
outputDepth = posInput.deviceDepth;