-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#pragma once | ||
|
||
// GGX | ||
float DistributionBRDF(float roughness, float NxH) | ||
{ | ||
|
||
// float m = roughness * roughness; | ||
// float m2 = m * m; | ||
// float d = ( NxH * m2 - NxH ) * NxH + 1; | ||
// return m2 / ( d*d ); | ||
|
||
float m = roughness * roughness; | ||
float m2 = m * m; | ||
float NoH2 = NxH * NxH; | ||
return exp( (NoH2 - 1) / (m2 * NoH2) ) / ( M_PI * m2 * NoH2 * NoH2 ); | ||
|
||
// float m = roughness * roughness; | ||
// float m2 = m * m; | ||
// float n = 2 / m2 - 2; | ||
// return (n+2) / (2*M_PI) * pow( NxH, n ); // 1 mad, 1 exp, 1 mul, 1 log | ||
|
||
} | ||
|
||
// Schlick | ||
float GeometricBRDF(float roughness, float NxV, float NxL) | ||
{ | ||
// Fixed-value: | ||
// return 0.25; | ||
|
||
float a = roughness * roughness; | ||
float a2 = a * a; | ||
|
||
float v = NxV + sqrt( NxV * (NxV - NxV * a2) + a2 ); | ||
float l = NxL + sqrt( NxL * (NxL - NxL * a2) + a2 ); | ||
return saturate(1.0 / ( v * l )); | ||
|
||
// float a = roughness * roughness; | ||
// float Vis_SmithV = NxL * ( NxV * ( 1 - a ) + a ); | ||
// float Vis_SmithL = NxV * ( NxL * ( 1 - a ) + a ); | ||
// return 0.5 / ( Vis_SmithV + Vis_SmithL ); | ||
} | ||
|
||
// Schlick | ||
vec3 FresnelBRDF(vec3 specularColor, float LxH) | ||
{ | ||
return specularColor + (1.0 - specularColor) * pow(1.0 - LxH, 5.0); | ||
} | ||
|
||
|
||
vec3 DiffuseBRDF(vec3 diffuseColor, float roughness, float NxV, float NxL, float VxH) { | ||
float VxL = 2 * VxH - 1; | ||
float m = roughness * roughness; | ||
float m2 = m * m; | ||
float C1 = 1 - 0.5 * m2 / (m2 + 0.33); | ||
float Cosri = VxL - NxV * NxL; | ||
float C2 = 0.45 * m2 / (m2 + 0.09) * Cosri * ( Cosri >= 0 ? min( 1, NxL / NxV ) : NxL ); | ||
return diffuseColor / M_PI * ( NxL * C1 + C2 ); | ||
} | ||
|
||
float safeDot(vec3 a, vec3 b) { | ||
return max(0.0, dot(a, b)); | ||
} | ||
|
||
|
||
float Vis_Smith( float Roughness, float NoV, float NoL ) | ||
{ | ||
float a = Roughness * Roughness; | ||
float a2 = a*a; | ||
|
||
float Vis_SmithV = NoV + sqrt( NoV * (NoV - NoV * a2) + a2 ); | ||
float Vis_SmithL = NoL + sqrt( NoL * (NoL - NoL * a2) + a2 ); | ||
return 1.0 / ( Vis_SmithV * Vis_SmithL ); | ||
} | ||
|
||
|
||
|
||
vec3 computeSpecular(vec3 specularColor, float roughness, float NxL, float LxH, float NxV, float NxH) { | ||
float specularD = DistributionBRDF(roughness, NxH); | ||
float specularG = GeometricBRDF(roughness, NxV, NxL); | ||
vec3 specularF = FresnelBRDF(specularColor, LxH); | ||
return (specularD * specularG) * specularF; | ||
} |