Last active
November 4, 2015 03:34
-
-
Save krisr/8281da6f1f35c177e2d3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
float radicalInverse_VdC(uint bits) { | |
bits = (bits << 16u) | (bits >> 16u); | |
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u); | |
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u); | |
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u); | |
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u); | |
return float(bits) * 2.3283064365386963e-10; // / 0x100000000 | |
} | |
vec2 Hammersley(uint i, uint N) { | |
return vec2(float(i)/float(N), radicalInverse_VdC(i)); | |
} | |
vec3 ImportanceSampleGGX(vec2 Xi, float Roughness, vec3 N) { | |
float a = Roughness * Roughness; | |
float Phi = 2 * PI * Xi.x; | |
float CosTheta = sqrt( (1 - Xi.y) / ( 1 + (a*a - 1) * Xi.y ) ); | |
float SinTheta = sqrt( 1 - CosTheta * CosTheta ); | |
vec3 H; | |
H.x = SinTheta * cos( Phi ); | |
H.y = SinTheta * sin( Phi ); | |
H.z = CosTheta; | |
vec3 UpVector = abs(N.z) < 0.999 ? vec3(0,0,1) : vec3(1,0,0); | |
vec3 TangentX = normalize( cross( UpVector, N ) ); | |
vec3 TangentY = cross( N, TangentX ); | |
// Tangent to world space | |
return TangentX * H.x + TangentY * H.y + N * H.z; | |
} | |
vec3 IBLDiffuse(vec3 DiffuseColor, vec3 N, vec3 V) { | |
const uint NumSamples = 1024u; | |
vec3 DiffuseLighting = vec3(0.0); | |
float NoV = saturate(dot(N, V)); | |
for( uint i = 0u; i < NumSamples; i++ ) { | |
vec2 Xi = Hammersley(i, NumSamples); | |
vec3 H2 = ImportanceSampleGGX(Xi, 1.0, N); | |
vec3 L2 = 2.0 * dot( V, H2 ) * H2 - V; | |
float NoL2 = saturate(dot(N, L2)); | |
if(NoL2 > 0.0) { | |
vec3 SampleColor2 = textureCube(environment_texture, L2).rgb; | |
DiffuseLighting += DiffuseColor * SampleColor2 * NoL2 / PI; | |
} | |
} | |
return DiffuseLighting / NumSamples; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment