Skip to content

Commit 52473b5

Browse files
committed
Update the implementation of DiffuseGGX()
1 parent 347f0e3 commit 52473b5

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

  • Assets/ScriptableRenderPipeline

Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.hlsl

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,11 @@ void BSDF( float3 V, float3 L, float3 positionWS, PreLightData preLightData, BS
693693
bsdfData.roughnessB = ClampRoughnessForAnalyticalLights(bsdfData.roughnessB);
694694

695695
#ifdef LIT_USE_BSDF_PRE_LAMBDAV
696-
Vis = V_SmithJointGGXAnisoLambdaV( preLightData.TdotV, preLightData.BdotV, preLightData.NdotV, TdotL, BdotL, NdotL,
696+
Vis = V_SmithJointGGXAnisoLambdaV( preLightData.TdotV, preLightData.BdotV, NdotV, TdotL, BdotL, NdotL,
697697
bsdfData.roughnessT, bsdfData.roughnessB, preLightData.anisoGGXLambdaV);
698698
#else
699699
// TODO: Do comparison between this correct version and the one from isotropic and see if there is any visual difference
700-
Vis = V_SmithJointGGXAniso( preLightData.TdotV, preLightData.BdotV, preLightData.NdotV, TdotL, BdotL, NdotL,
700+
Vis = V_SmithJointGGXAniso( preLightData.TdotV, preLightData.BdotV, NdotV, TdotL, BdotL, NdotL,
701701
bsdfData.roughnessT, bsdfData.roughnessB);
702702
#endif
703703

@@ -708,18 +708,22 @@ void BSDF( float3 V, float3 L, float3 positionWS, PreLightData preLightData, BS
708708
bsdfData.roughness = ClampRoughnessForAnalyticalLights(bsdfData.roughness);
709709

710710
#ifdef LIT_USE_BSDF_PRE_LAMBDAV
711-
Vis = V_SmithJointGGX(NdotL, preLightData.NdotV, bsdfData.roughness, preLightData.ggxLambdaV);
711+
Vis = V_SmithJointGGX(NdotL, NdotV, bsdfData.roughness, preLightData.ggxLambdaV);
712712
#else
713-
Vis = V_SmithJointGGX(NdotL, preLightData.NdotV, bsdfData.roughness);
713+
Vis = V_SmithJointGGX(NdotL, NdotV, bsdfData.roughness);
714714
#endif
715715
D = D_GGX(NdotH, bsdfData.roughness);
716716
}
717717
specularLighting = F * (Vis * D);
718-
#ifdef LIT_DIFFUSE_LAMBERT_BRDF
719-
float diffuseTerm = Lambert();
720-
#else
721-
float diffuseTerm = DisneyDiffuse(preLightData.NdotV, NdotL, LdotH, bsdfData.perceptualRoughness);
722-
#endif
718+
719+
#ifdef LIT_DIFFUSE_LAMBERT_BRDF
720+
float diffuseTerm = Lambert();
721+
#elif LIT_DIFFUSE_GGX_BRDF
722+
float3 diffuseTerm = DiffuseGGX(bsdfData.diffuseColor, NdotV, NdotL, NdotH, LdotV, bsdfData.perceptualRoughness);
723+
#else
724+
float diffuseTerm = DisneyDiffuse(NdotV, NdotL, LdotH, bsdfData.perceptualRoughness);
725+
#endif
726+
723727
diffuseLighting = bsdfData.diffuseColor * diffuseTerm;
724728
}
725729

Assets/ScriptableRenderPipeline/ShaderLibrary/BSDF.hlsl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,25 @@ float DisneyDiffuse(float NdotV, float NdotL, float LdotH, float perceptualRough
227227
}
228228

229229
// Ref: Diffuse Lighting for GGX + Smith Microsurfaces, p. 113.
230-
float3 DiffuseGGXNoPI(float3 albedo, float NdotV, float NdotL, float NdotH, float LdotV, float roughness)
230+
float3 DiffuseGGXNoPI(float3 albedo, float NdotV, float NdotL, float NdotH, float LdotV, float perceptualRoughness)
231231
{
232232
float facing = 0.5 + 0.5 * LdotV;
233233
float rough = facing * (0.9 - 0.4 * facing) * ((0.5 + NdotH) / NdotH);
234234
float transmitL = 1 - F_Schlick(0, 1, NdotL);
235235
float transmitV = 1 - F_Schlick(0, 1, NdotV);
236-
float smooth = transmitL * transmitV * 1.05; // Normalize F_t over the hemisphere
237-
float single = lerp(smooth, rough, roughness); // Rescaled by PI
238-
float multi = roughness * (0.1159 * PI); // Rescaled by PI
236+
float smooth = transmitL * transmitV * 1.05; // Normalize F_t over the hemisphere
237+
float single = lerp(smooth, rough, perceptualRoughness); // Rescaled by PI
238+
// This constant is picked s.t. setting perceptualRoughness, albedo and all angles to 1
239+
// allows us to match the Lambertian and the Disney Diffuse models. Original value: 0.1159.
240+
float multiple = perceptualRoughness * (0.079577 * PI); // Rescaled by PI
239241

240-
return single + albedo * multi;
242+
return single + albedo * multiple;
241243
}
242244

243-
float3 DiffuseGGX(float3 albedo, float NdotV, float NdotL, float NdotH, float LdotV, float roughness)
245+
float3 DiffuseGGX(float3 albedo, float NdotV, float NdotL, float NdotH, float LdotV, float perceptualRoughness)
244246
{
245-
return INV_PI * DiffuseGGXNoPI(albedo, NdotV, NdotL, NdotH, LdotV, roughness);
247+
// Note that we could save 2 cycles by inlining the multiplication by INV_PI.
248+
return INV_PI * DiffuseGGXNoPI(albedo, NdotV, NdotL, NdotH, LdotV, perceptualRoughness);
246249
}
247250

248251
#endif // UNITY_BSDF_INCLUDED

0 commit comments

Comments
 (0)