Skip to content

Commit d094c5a

Browse files
slunitysebastienlagarde
authored andcommitted
[10.x.x] Axf fix shininess exp to roughness, ssr transparent and alpha test #587
1 parent 937e9c1 commit d094c5a

File tree

6 files changed

+53
-32
lines changed

6 files changed

+53
-32
lines changed
Lines changed: 2 additions & 2 deletions
Loading

com.unity.render-pipelines.high-definition/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
5959
- Tentative fix for missing include in depth of field shaders.
6060
- Fixed Wizard check on default volume profile to also check it is not the default one in package.
6161
- Fixed light layers not correctly disabled when the lightlayers is set to Nothing and Lightlayers isn't enabled in HDRP Asset
62+
- Fixed AxF handling of roughness for Blinn-Phong type materials
63+
- Fixed AxF UI errors when surface type is switched to transparent
6264

6365
### Changed
6466
- Shadowmask and realtime reflection probe property are hide in Quality settings

com.unity.render-pipelines.high-definition/Editor/Material/AxF/AxFGUI.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,13 @@ static public void SetupMaterialKeywordsAndPass(Material material)
132132
// Keywords for opt-out of decals and SSR:
133133
bool decalsEnabled = material.HasProperty(kEnableDecals) && material.GetFloat(kEnableDecals) > 0.0f;
134134
CoreUtils.SetKeyword(material, "_DISABLE_DECALS", decalsEnabled == false);
135-
bool ssrEnabled = material.HasProperty(kEnableSSR) && material.GetFloat(kEnableSSR) > 0.0f;
136-
CoreUtils.SetKeyword(material, "_DISABLE_SSR", ssrEnabled == false);
135+
bool ssrEnabled = false;
136+
if (material.GetSurfaceType() == SurfaceType.Transparent)
137+
ssrEnabled = material.HasProperty(kReceivesSSRTransparent) ? material.GetFloat(kReceivesSSRTransparent) != 0 : false;
138+
else
139+
ssrEnabled = material.HasProperty(kReceivesSSR) ? material.GetFloat(kReceivesSSR) != 0 : false;
140+
CoreUtils.SetKeyword(material, "_DISABLE_SSR", material.HasProperty(kReceivesSSR) && material.GetFloat(kReceivesSSR) == 0.0f);
141+
CoreUtils.SetKeyword(material, "_DISABLE_SSR_TRANSPARENT", material.HasProperty(kReceivesSSRTransparent) && material.GetFloat(kReceivesSSRTransparent) == 0.0);
137142
CoreUtils.SetKeyword(material, "_ENABLE_GEOMETRIC_SPECULAR_AA", material.HasProperty(kEnableGeometricSpecularAA) && material.GetFloat(kEnableGeometricSpecularAA) > 0.0f);
138143
CoreUtils.SetKeyword(material, "_SPECULAR_OCCLUSION_NONE", material.HasProperty(kSpecularOcclusionMode) && material.GetFloat(kSpecularOcclusionMode) == 0.0f);
139144

com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.hlsl

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,8 @@ PreLightData GetPreLightData(float3 viewWS_Clearcoat, PositionInputs posInput
11751175
preLightData.iblPerceptualRoughness = PerceptualRoughnessBeckmannToGGX(preLightData.iblPerceptualRoughness);
11761176
break;
11771177

1178-
// case 1: // @TODO: Support Blinn-Phong FGD?
1178+
case 1: //Phong
1179+
case 4: //Blinn-Phong : just approximate with Cook-Torrance which uses a Beckmann distribution
11791180
case 2:
11801181
GetPreIntegratedFGDCookTorranceAndLambert(NdotV_UnderCoat, preLightData.iblPerceptualRoughness, tempF0, preLightData.specularFGD, preLightData.diffuseFGD, specularReflectivity);
11811182
preLightData.specularFGD *= GetPreIntegratedFGDCookTorranceSampleMutiplier();
@@ -1186,8 +1187,6 @@ PreLightData GetPreLightData(float3 viewWS_Clearcoat, PositionInputs posInput
11861187
GetPreIntegratedFGDGGXAndLambert(NdotV_UnderCoat, preLightData.iblPerceptualRoughness, tempF0, preLightData.specularFGD, preLightData.diffuseFGD, specularReflectivity);
11871188
break;
11881189

1189-
// case 4: // @TODO: Support Blinn-Phong FGD?
1190-
11911190
default: // Use GGX by default
11921191
GetPreIntegratedFGDGGXAndLambert(NdotV_UnderCoat, preLightData.iblPerceptualRoughness, tempF0, preLightData.specularFGD, preLightData.diffuseFGD, specularReflectivity);
11931192
break;
@@ -1356,14 +1355,6 @@ PreLightData GetPreLightData(float3 viewWS_Clearcoat, PositionInputs posInput
13561355
case 1: // BLINN-PHONG
13571356
case 4: // PHONG;
13581357
{
1359-
// According to https://computergraphics.stackexchange.com/questions/1515/what-is-the-accepted-method-of-converting-shininess-to-roughness-and-vice-versa
1360-
// float exponent = 2/roughness^4 - 2;
1361-
//
1362-
float exponent = PerceptualRoughnessToRoughness(preLightData.iblPerceptualRoughness);
1363-
float roughness = pow(max(0.0, 2.0 / (exponent + 2)), 1.0 / 4.0);
1364-
// todo_modes todo_pseudorefract: cant use undercoat like that
1365-
//float2 UV = LTCGetSamplingUV(NdotV_UnderCoat, RoughnessToPerceptualRoughness(roughness));
1366-
float2 UV = LTCGetSamplingUV(NdotV_Clearcoat, RoughnessToPerceptualRoughness(roughness));
13671358
preLightData.ltcTransformSpecular = LTCSampleMatrix(UV, LTC_MATRIX_INDEX_COOK_TORRANCE);
13681359
break;
13691360
}
@@ -1576,7 +1567,8 @@ float3 ComputeWard(float3 H, float LdotH, float NdotL, float NdotV, PreLightData
15761567

15771568
float3 ComputeBlinnPhong(float3 H, float LdotH, float NdotL, float NdotV, PreLightData preLightData, BSDFData bsdfData)
15781569
{
1579-
float2 exponents = exp2(bsdfData.roughness.xy);
1570+
// See AxFGetRoughnessFromSpecularLobeTexture in AxFData
1571+
float2 exponents = 2 * rcp(max(0.0001,(bsdfData.roughness.xy*bsdfData.roughness.xy))) - 2;
15801572

15811573
// Evaluate normal distribution function
15821574
float3 tsH = float3(dot(H, bsdfData.tangentWS), dot(H, bsdfData.biTangentWS), dot(H, bsdfData.normalWS));

com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.shader

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Shader "HDRP/AxF"
141141

142142
[ToggleUI] _SupportDecals("Support Decals", Float) = 1.0
143143
[ToggleUI] _ReceivesSSR("Receives SSR", Float) = 1.0
144-
144+
[ToggleUI] _ReceivesSSRTransparent("Receives SSR Transparent", Float) = 0.0
145145
[ToggleUI] _AddPrecomputedVelocity("AddPrecomputedVelocity", Float) = 0.0
146146

147147
}
@@ -167,6 +167,7 @@ Shader "HDRP/AxF"
167167

168168
#pragma shader_feature_local _DISABLE_DECALS
169169
#pragma shader_feature_local _DISABLE_SSR
170+
#pragma shader_feature_local _DISABLE_SSR_TRANSPARENT
170171
#pragma shader_feature_local _ENABLE_GEOMETRIC_SPECULAR_AA
171172

172173
#pragma shader_feature_local _ADD_PRECOMPUTED_VELOCITY

com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxFData.hlsl

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,29 @@ void ApplyDecalToSurfaceData(DecalSurfaceData decalSurfaceData, inout SurfaceDat
464464
#endif
465465
}
466466

467+
bool HasPhongTypeBRDF()
468+
{
469+
uint type = ((_SVBRDF_BRDFType >> 1) & 7);
470+
return type == 1 || type == 4;
471+
}
472+
473+
float2 AxFGetRoughnessFromSpecularLobeTexture(float2 specularLobe)
474+
{
475+
// For Blinn-Phong, AxF encodes specularLobe.xy as log2(shiniExp_xy) so
476+
// shiniExp = exp2(abs(specularLobe.xy))
477+
// A good fit for a corresponding Beckmann roughness is
478+
// roughnessBeckmann^2 = 2 /(shiniExp + 2)
479+
// See eg
480+
// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html
481+
// http://simonstechblog.blogspot.com/2011/12/microfacet-brdf.html
482+
483+
// We thus have
484+
// roughnessBeckmann = sqrt(2) * rsqrt(exp2(abs(specularLobe.xy)) + 2);
485+
// shiniExp = 2 * rcp(max(0.0001,(roughnessBeckmann*roughnessBeckmann))) - 2;
486+
487+
return (HasPhongTypeBRDF() ? (sqrt(2) * rsqrt(exp2(abs(specularLobe)) + 2)) : specularLobe);
488+
}
489+
467490
void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
468491
{
469492
#ifdef _DOUBLESIDED_ON
@@ -479,8 +502,18 @@ void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs p
479502
InitTextureUVMapping(input, uvMapping);
480503
ZERO_INITIALIZE(SurfaceData, surfaceData);
481504

505+
float alpha = AXF_SAMPLE_SMP_TEXTURE2D(_SVBRDF_AlphaMap, sampler_SVBRDF_AlphaMap, uvMapping).x;
506+
507+
#ifdef _ALPHATEST_ON
508+
// TODOTODO: Move alpha test earlier and test.
509+
float alphaCutoff = _AlphaCutoff;
482510

483-
float alpha = 1.0;
511+
#if SHADERPASS == SHADERPASS_SHADOWS
512+
GENERIC_ALPHA_TEST(alpha, _UseShadowThreshold ? _AlphaCutoffShadow : alphaCutoff);
513+
#else
514+
GENERIC_ALPHA_TEST(alpha, alphaCutoff);
515+
#endif
516+
#endif
484517

485518
surfaceData.ambientOcclusion = 1.0;
486519
surfaceData.specularOcclusion = 1.0;
@@ -494,7 +527,8 @@ void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs p
494527

495528
surfaceData.diffuseColor = AXF_SAMPLE_SMP_TEXTURE2D(_SVBRDF_DiffuseColorMap, sampler_SVBRDF_DiffuseColorMap, uvMapping).xyz;
496529
surfaceData.specularColor = AXF_SAMPLE_SMP_TEXTURE2D(_SVBRDF_SpecularColorMap, sampler_SVBRDF_SpecularColorMap, uvMapping).xyz;
497-
surfaceData.specularLobe.xy = _SVBRDF_SpecularLobeMapScale * AXF_SAMPLE_SMP_TEXTURE2D(_SVBRDF_SpecularLobeMap, sampler_SVBRDF_SpecularLobeMap, uvMapping).xy;
530+
surfaceData.specularLobe.xy = _SVBRDF_SpecularLobeMapScale * AxFGetRoughnessFromSpecularLobeTexture(
531+
AXF_SAMPLE_SMP_TEXTURE2D(_SVBRDF_SpecularLobeMap, sampler_SVBRDF_SpecularLobeMap, uvMapping).xy);
498532

499533
// The AxF models include both a general coloring term that they call "specular color" while the f0 is actually another term,
500534
// seemingly always scalar:
@@ -524,8 +558,6 @@ void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs p
524558
// Useless for SVBRDF, will be optimized out
525559
//SetFlakesSurfaceData(uvMapping, surfaceData);
526560

527-
alpha = AXF_SAMPLE_SMP_TEXTURE2D(_SVBRDF_AlphaMap, sampler_SVBRDF_AlphaMap, uvMapping).x;
528-
529561
//-----------------------------------------------------------------------------
530562
// _AXF_BRDF_TYPE_CAR_PAINT
531563
//-----------------------------------------------------------------------------
@@ -604,17 +636,6 @@ void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs p
604636
// the handedness of the world space (tangentToWorld can be passed right handed while
605637
// Unity's WS is left handed, so this makes a difference here).
606638

607-
#ifdef _ALPHATEST_ON
608-
// TODOTODO: Move alpha test earlier and test.
609-
float alphaCutoff = _AlphaCutoff;
610-
611-
#if SHADERPASS == SHADERPASS_SHADOWS
612-
GENERIC_ALPHA_TEST(alpha, _UseShadowThreshold ? _AlphaCutoffShadow : alphaCutoff);
613-
#else
614-
GENERIC_ALPHA_TEST(alpha, alphaCutoff);
615-
#endif
616-
#endif
617-
618639
#if defined(_ENABLE_GEOMETRIC_SPECULAR_AA)
619640
// Specular AA for geometric curvature
620641

0 commit comments

Comments
 (0)