Improve AmbientScope and OutlineScope

This commit is contained in:
2025-02-02 17:04:05 +09:00
parent ecd0cfdb9f
commit 281dfbc4f0
11 changed files with 200 additions and 143 deletions

View File

@@ -102,15 +102,18 @@ IndirectLighting UtsEvaluateBSDF_ScreenSpaceReflection(PositionInputs posInput,
IndirectLighting lighting;
ZERO_INITIALIZE(IndirectLighting, lighting);
// TODO: this texture is sparse (mostly black). Can we avoid reading every texel? How about using Hi-S?
float4 ssrLighting = LOAD_TEXTURE2D_X(_SsrLightingTexture, posInput.positionSS);
InversePreExposeSsrLighting(ssrLighting);
if (_SSRWeight > 0.0)
{
// TODO: this texture is sparse (mostly black). Can we avoid reading every texel? How about using Hi-S?
float4 ssrLighting = LOAD_TEXTURE2D_X(_SsrLightingTexture, posInput.positionSS);
InversePreExposeSsrLighting(ssrLighting);
// Apply the weight on the ssr contribution (if required)
ApplyScreenSpaceReflectionWeight(ssrLighting);
// Apply the weight on the ssr contribution (if required)
ApplyScreenSpaceReflectionWeight(ssrLighting);
reflectionHierarchyWeight = ssrLighting.a;
lighting.specularReflected = ssrLighting.rgb * preLightData.specularFGD;
reflectionHierarchyWeight = ssrLighting.a;
lighting.specularReflected = lerp(lighting.specularReflected, ssrLighting.rgb * preLightData.specularFGD, _SSRWeight);
}
return lighting;
}
@@ -125,7 +128,8 @@ void UtsEvaluateBSDF_BakeDiffuse(PositionInputs posInput, PreLightData preLightD
#if !defined(_SURFACE_TYPE_TRANSPARENT) && !defined(SCREEN_SPACE_INDIRECT_DIFFUSE_DISABLED)
if (_IndirectDiffuseMode != INDIRECTDIFFUSEMODE_OFF)
{
builtinData.bakeDiffuseLighting = LOAD_TEXTURE2D_X(_IndirectDiffuseTexture, posInput.positionSS).xyz * GetInverseCurrentExposureMultiplier();
float3 ssgiLighting = LOAD_TEXTURE2D_X(_IndirectDiffuseTexture, posInput.positionSS).xyz * GetInverseCurrentExposureMultiplier();
builtinData.bakeDiffuseLighting = lerp(builtinData.bakeDiffuseLighting, ssgiLighting.rgb, _SSGIWeight);
}
else
#endif
@@ -168,7 +172,8 @@ void UtsEvaluateBSDF_MatCapDiffuse(float3 positionWS, float3 normalWS, inout Bui
uv.x *= -1;
uv = uv * 0.5 + 0.5;
builtinData.bakeDiffuseLighting = SAMPLE_TEXTURE2D_LOD(_MatCapMap, s_linear_clamp_sampler, uv, UNITY_SPECCUBE_LOD_STEPS).rgb * GetInverseCurrentExposureMultiplier();
float lod = clamp(UNITY_SPECCUBE_LOD_STEPS + _IndirectDiffuseMatCapLod, 0.0, 10.0);
builtinData.bakeDiffuseLighting = SAMPLE_TEXTURE2D_LOD(_IndirectDiffuseMatCapMap, s_trilinear_clamp_sampler, uv, lod).rgb * GetInverseCurrentExposureMultiplier();
}
IndirectLighting UtsEvaluateBSDF_MatCapSpecular(float3 positionWS, UtsBSDFData bsdfData, PreLightData preLightData)
@@ -184,7 +189,8 @@ IndirectLighting UtsEvaluateBSDF_MatCapSpecular(float3 positionWS, UtsBSDFData b
uv.x *= -1;
uv = uv * 0.5 + 0.5;
lighting.specularReflected = SAMPLE_TEXTURE2D_LOD(_MatCapMap, s_linear_clamp_sampler, uv, PerceptualRoughnessToMipmapLevel(bsdfData.perceptualRoughness)).rgb;
float lod = clamp(PerceptualRoughnessToMipmapLevel(bsdfData.perceptualRoughness) + _IndirectSpecularMatCapLod, 0.0, 10.0);
lighting.specularReflected = SAMPLE_TEXTURE2D_LOD(_IndirectSpecularMatCapMap, s_trilinear_clamp_sampler, uv, lod).rgb;
lighting.specularReflected *= preLightData.specularFGD * GetInverseCurrentExposureMultiplier();
return lighting;
@@ -240,27 +246,37 @@ IndirectLighting UtsEvaluateBSDF_Env(LightLoopContext lightLoopContext, Position
return lighting;
}
void ApplyAmbientOcclusion(AmbientOcclusionFactor aoFactor, inout BuiltinData builtinData, inout AggregateLighting lighting)
{
builtinData.bakeDiffuseLighting = APPLY_WEIGHT(builtinData.bakeDiffuseLighting, aoFactor.indirectAmbientOcclusion, _SSAOWeight);
lighting.indirect.specularReflected = APPLY_WEIGHT(lighting.indirect.specularReflected, aoFactor.indirectSpecularOcclusion, _SSAOWeight);
lighting.direct.diffuse = APPLY_WEIGHT(lighting.direct.diffuse, aoFactor.directAmbientOcclusion, _SSAOWeight);
lighting.direct.specular = APPLY_WEIGHT(lighting.direct.specular, aoFactor.directSpecularOcclusion, _SSAOWeight);
}
void AdjustIndirectLighting(PreLightData preLightData, UtsBSDFData bsdfData, inout BuiltinData builtinData, inout AggregateLighting lighting)
{
builtinData.bakeDiffuseLighting = ApplyCurrentExposureMultiplier(builtinData.bakeDiffuseLighting * bsdfData.diffuseColor * preLightData.diffuseFGD * _IndirectDiffuseIntensity);
lighting.indirect.specularReflected = ApplyCurrentExposureMultiplier(lighting.indirect.specularReflected * bsdfData.fresnel0 * _IndirectSpecularIntensity);
}
void UtsPostEvaluateBSDF(PositionInputs posInput, PreLightData preLightData, UtsBSDFData bsdfData, BuiltinData builtinData, AggregateLighting lighting, out LightLoopOutput lightLoopOutput)
{
#if !defined(_INDIRECT_DIFFUSE_MODE_OFF) && !defined(_INDIRECT_SPECULAR_MODE_OFF)
AmbientOcclusionFactor aoFactor;
GetScreenSpaceAmbientOcclusionMultibounce(posInput.positionSS, preLightData.NdotV, bsdfData.perceptualRoughness, bsdfData.ambientOcclusion, bsdfData.specularOcclusion, bsdfData.diffuseColor, bsdfData.fresnel0, aoFactor);
builtinData.bakeDiffuseLighting = APPLY_WEIGHT(builtinData.bakeDiffuseLighting, aoFactor.indirectAmbientOcclusion, _AO_Factor);
lighting.indirect.specularReflected = APPLY_WEIGHT(lighting.indirect.specularReflected, aoFactor.indirectSpecularOcclusion, _AO_Factor);
lighting.direct.diffuse = APPLY_WEIGHT(lighting.direct.diffuse, aoFactor.directAmbientOcclusion, _AO_Factor);
lighting.direct.specular = APPLY_WEIGHT(lighting.direct.specular, aoFactor.directSpecularOcclusion, _AO_Factor);
builtinData.bakeDiffuseLighting = ApplyCurrentExposureMultiplier(builtinData.bakeDiffuseLighting * bsdfData.diffuseColor * preLightData.diffuseFGD * _ID_Intensity);
lighting.indirect.specularReflected = ApplyCurrentExposureMultiplier(lighting.indirect.specularReflected * bsdfData.fresnel0 * _IR_Intensity);
ApplyAmbientOcclusion(aoFactor, builtinData, lighting);
#endif
AdjustIndirectLighting(preLightData, bsdfData, builtinData, lighting);
lightLoopOutput.diffuseLighting = lighting.direct.diffuse + builtinData.bakeDiffuseLighting;
// In regular pbr, we need to multiple diffuse color here with direct diffuse lighting. However, in UTS, we have already multiplied it when evaluating the direct diffuse since we need to apply the shading color.
lightLoopOutput.diffuseLighting = lighting.direct.diffuse + builtinData.bakeDiffuseLighting + builtinData.emissiveColor;
lightLoopOutput.specularLighting = lighting.direct.specular + lighting.indirect.specularReflected;
// Rescale the GGX to account for the multiple scattering.
lightLoopOutput.specularLighting *= 1.0 + bsdfData.fresnel0 * preLightData.energyCompensation;
ApplyExposureAdjustment(lightLoopOutput.diffuseLighting);
ApplyExposureAdjustment(lightLoopOutput.specularLighting);
lightLoopOutput.diffuseLighting += builtinData.emissiveColor;
}
#endif

View File

@@ -247,9 +247,9 @@ void UtsLightLoop(FragInputs fragInputs, PositionInputs posInput, UtsBSDFData bs
#endif
float3 lightInReflDir = 0.0;
#ifdef _INDIRECT_DIFFUSE_OFF
#ifdef _INDIRECT_DIFFUSE_MODE_OFF
#elif _INDIRECT_DIFFUSE_IBL
#elif _INDIRECT_DIFFUSE_MODE_IBL
bool replaceBakeDiffuseLighting = false;
#if !defined(_SURFACE_TYPE_TRANSPARENT) // No SSGI/RTGI/Mixed effect on transparent
if (_IndirectDiffuseMode != INDIRECTDIFFUSEMODE_OFF)
@@ -273,18 +273,18 @@ void UtsLightLoop(FragInputs fragInputs, PositionInputs posInput, UtsBSDFData bs
{
UtsEvaluateBSDF_BakeDiffuse(posInput, preLightData, bsdfData, V, builtinData, lightInReflDir);
}
#elif _INDIRECT_DIFFUSE_MATCAP
#elif _INDIRECT_DIFFUSE_MODE_MATCAP
//builtinData.bakeDiffuseLighting = UtsEvaluateColor_MatCap(posInput.positionWS, bsdfData.normalWS, 0.0);
UtsEvaluateBSDF_MatCapDiffuse(posInput.positionWS, bsdfData.normalWS, builtinData);
#elif _INDIRECT_DIFFUSE_RAMP
#elif _INDIRECT_DIFFUSE_MODE_RAMP
UtsEvaluateBSDF_Ramp(posInput, bsdfData, builtinData);
#endif
if (featureFlags & LIGHTFEATUREFLAGS_ENV)
{
#if _INDIRECT_SPECULAR_OFF
#if _INDIRECT_SPECULAR_MODE_OFF
#elif _INDIRECT_SPECULAR_IBL
#elif _INDIRECT_SPECULAR_MODE_IBL
context.sampleReflection = SINGLE_PASS_CONTEXT_SAMPLE_REFLECTION_PROBES;
#if SCALARIZE_LIGHT_LOOP
@@ -346,13 +346,13 @@ void UtsLightLoop(FragInputs fragInputs, PositionInputs posInput, UtsBSDFData bs
}
}
#elif _INDIRECT_SPECULAR_MATCAP
#elif _INDIRECT_SPECULAR_MODE_MATCAP
IndirectLighting lighting = UtsEvaluateBSDF_MatCapSpecular(posInput.positionWS, bsdfData, preLightData);
AccumulateIndirectLighting(lighting, aggregateLighting);
#endif
}
#if _INDIRECT_SPECULAR_IBL
#if _INDIRECT_SPECULAR_MODE_IBL
// Only apply the sky IBL if the sky texture is available
if ((featureFlags & LIGHTFEATUREFLAGS_SKY) && _EnvLightSkyEnabled)
{

View File

@@ -161,9 +161,6 @@ float4 _EmissiveColorMap_ST;
float _TexWorldScaleEmissive;
float4 _UVMappingMaskEmissive;
float _ID_Intensity;
float _IR_Intensity;
float4 _InvPrimScale; // Only XY are used
// Wind
@@ -202,12 +199,6 @@ float4 _SpecularColor;
float _ToonSpecularStep;
float _ToonSpecularFeather;
float _ReceivesSSR;
float _ReceivesSSAO;
float _AO_Factor;
float _ReceivesSSGI;
float _GIMultiplier;
float _NormalScale;
float4 _DetailMap_ST;

View File

@@ -9,7 +9,8 @@ SAMPLER(sampler_SDFShadingMap);
TEXTURE2D_ARRAY(_ShadingRampMap);
TEXTURE2D(_MatCapMap);
TEXTURE2D(_IndirectDiffuseMatCapMap);
TEXTURE2D(_IndirectSpecularMatCapMap);
sampler _Set_1st_ShadePosition;
sampler _Set_2nd_ShadePosition;
@@ -27,7 +28,4 @@ TEXTURE2D(_OutlineWidthMap);
SAMPLER(sampler_OutlineWidthMap);
TEXTURE2D(_OutlineColorMap);
SAMPLER(sampler_OutlineColorMap);
TEXTURE2D(_BakedNormalMap);
SAMPLER(sampler_BakedNormalMap);
SAMPLER(sampler_OutlineColorMap);

View File

@@ -25,6 +25,30 @@ float _SDFHighlightStrength;
float _ShadowDistanceBias;
float _ShadowNormalBias;
// Ambient
float _IndirectDiffuseMatCapLod;
float _IndirectDiffuseIntensity;
float _SSAOWeight;
float _SSGIWeight;
float _IndirectSpecularMatCapLod;
float _IndirectSpecularIntensity;
float _SSRWeight;
// Outline
float _OutlineWidth;
float4 _OutlineColor;
fixed _AlbedoAffectOutline;
fixed _SkyColorAffectOutline;
float _SkyColorIntensity;
float _OutlineFadeIn;
float _OutlineFadeOut;
fixed _UseSmoothedNormal;
float _utsTechnique;
float4 _Color;
@@ -170,13 +194,6 @@ float _AngelRingIntensity;
float _AngelRingOffsetU;
float _AngelRingOffsetV;
float _OutlineWidth;
float4 _OutlineColor;
fixed _AlbedoAffectOutline;
float _OutlineFadeIn;
float _OutlineFadeOut;
fixed _UseSmoothedNormal;
#if defined(_UTS_TOON_EV_PER_MODEL)
// not in materials
int _ToonLightHiCutFilter;

View File

@@ -132,26 +132,23 @@ void Frag(PackedVaryingsToPS packedInput,
float4 objPos = mul(unity_ObjectToWorld, float4(0, 0, 0, 1));
float4 uv0 = input.texCoord0;
// The following temporary definition of unity_AmbientEquator is for HDRP only.
//float4 unity_AmbientEquator = float4(0.05, 0.05, 0.05, 1.0); //Todo.
//v.2.0.9
//float3 envLightSource_GradientEquator = unity_AmbientEquator.rgb > 0.05 ? unity_AmbientEquator.rgb : half3(0.05, 0.05, 0.05);
float3 envLightSource_GradientEquator = ShadeSH9(float4(0, 1, 0, 0));
float3 envLightSource_SkyboxIntensity = max(
SampleBakedGI_UTS_OutLine(objPos.xyz, float3(0.0, 0.0, 0.0), input.texCoord1.xy, input.texCoord2.xy),
SampleBakedGI_UTS_OutLine(objPos.xyz, float3(0.0, -1.0, 0.0), input.texCoord1.xy, input.texCoord2.xy)
SampleBakedGI_UTS_OutLine(objPos.xyz, float3(0.0, 1.0, 0.0), input.texCoord1.xy, input.texCoord2.xy),
SampleBakedGI_UTS_OutLine(objPos.xyz, float3(0.0, -1.0, -0.0), input.texCoord1.xy, input.texCoord2.xy)
).rgb;
float3 ambientSkyColor = envLightSource_SkyboxIntensity.rgb > 0.0 ? envLightSource_SkyboxIntensity : envLightSource_GradientEquator;
ambientSkyColor *= GetCurrentExposureMultiplier() * 5.0f;
ambientSkyColor *= GetCurrentExposureMultiplier() * _SkyColorIntensity;
float3 baseColor = SAMPLE_TEXTURE2D(_BaseColorMap, sampler_BaseColorMap, TRANSFORM_TEX(uv0, _BaseColorMap)).rgb;
baseColor *= _BaseColor.rgb;
float4 outlineColor = _OutlineColor;
outlineColor.rgb = lerp(outlineColor.rgb, outlineColor.rgb + ambientSkyColor, _SkyColorAffectOutline);
#if _OUTLINE_COLOR_MAP
outlineColor *= SAMPLE_TEXTURE2D(_OutlineColorMap, sampler_OutlineColorMap, TRANSFORM_TEX(uv0, _BaseColorMap)).rgb;
#endif
outlineColor.rgb = lerp(outlineColor.rgb * ambientSkyColor, outlineColor.rgb * ambientSkyColor * baseColor, _AlbedoAffectOutline);
outlineColor.rgb = lerp(outlineColor.rgb, outlineColor.rgb * baseColor, _AlbedoAffectOutline);
float3 volColor, volOpacity;