Skip to content

Instantly share code, notes, and snippets.

@pyalot
Created March 26, 2015 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyalot/123e0ebe5de3969f943c to your computer and use it in GitHub Desktop.
Save pyalot/123e0ebe5de3969f943c to your computer and use it in GitHub Desktop.
environment prefiltering
float lambertNDF(vec3 normal, vec3 dir, float roughness){
float exponent = getLambertExponent(roughness*PI*0.5);
float lambert = max(0.0, dot(normal, dir));
return pow(lambert, exponent);
}
float ggxNDF(vec3 normal, vec3 dir, float roughness){
float a = roughness*roughness;
float d = max(0.000001, dot(normal, dir));
//float ggx = sqrt((1.0-d)/(1.0+(a*a-1.0)*d));
float ggx = (a*a)/pow(1.0+(a*a-1.0)*d*d, 2.0);
return ggx*d;
}
vec3 envLookup(vec3 normal){
vec2 dir = normalize(normal.xz);
vec2 texcoord = vec2(
atan(-dir.x, -dir.y)/TAU+0.5,
acos(normal.y)/PI
);
return texture2D(envmap, texcoord).rgb;
}
vec4 prefilter(float roughness, vec3 normal){
vec4 accum = vec4(0.0);
for(int i=0; i<numSamples; i++){
vec3 dir = samples[i];
dir *= step(0.0, dot(normal, dir))*2.0-1.0; // reflect sample dir to above the normal
//float weight = lambertNDF(normal, dir, roughness);
float weight = ggxNDF(normal, dir, roughness);
vec3 color = envLookup(dir);
accum += vec4(color*weight, weight);
}
return accum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment