Navigation Menu

Skip to content

Commit

Permalink
Use GGX D + Schlick G for BSDF and burley for Diffuse
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Lejeune committed Dec 23, 2014
1 parent 161840a commit f9e321e
Show file tree
Hide file tree
Showing 27 changed files with 172 additions and 82 deletions.
4 changes: 2 additions & 2 deletions data/shaders/IBL.frag
Expand Up @@ -7,7 +7,7 @@ out vec4 FragColor;

vec3 DecodeNormal(vec2 n);
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
vec3 DiffuseIBL(vec3 normal, vec3 color);
vec3 DiffuseIBL(vec3 normal, vec3 V, float roughness, vec3 color);
vec3 SpecularIBL(vec3 normal, vec3 V, float roughness, vec3 F0);

void main(void)
Expand All @@ -23,7 +23,7 @@ void main(void)
vec3 eyedir = -normalize(xpos.xyz);
float specval = texture(ntex, uv).z;

vec3 dielectric = DiffuseIBL(normal, color) + SpecularIBL(normal, eyedir, specval, vec3(0.04));
vec3 dielectric = DiffuseIBL(normal, eyedir, specval, color) + SpecularIBL(normal, eyedir, specval, vec3(0.04));
vec3 metal = SpecularIBL(normal, eyedir, specval, color);
float metalness = texture(ntex, uv).a;

Expand Down
8 changes: 4 additions & 4 deletions data/shaders/detailledobject.frag
Expand Up @@ -12,7 +12,7 @@ in vec3 nor;
in vec2 uv;
in vec2 uv_bis;

layout(location = 0) out vec4 EncodedNormal_Specular_Reflectance;
layout(location = 0) out vec4 EncodedNormal_Roughness_Metalness;
layout(location = 1) out vec4 Colors;
layout(location = 2) out float EmitMap;

Expand All @@ -31,9 +31,9 @@ void main(void)
float glossmap = texture(SpecMap, uv).r;
float reflectance = texture(SpecMap, uv).g;

EncodedNormal_Specular_Reflectance.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Specular_Reflectance.z = glossmap;
EncodedNormal_Specular_Reflectance.w = reflectance;
EncodedNormal_Roughness_Metalness.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Roughness_Metalness.z = 1. - glossmap;
EncodedNormal_Roughness_Metalness.w = reflectance;
Colors = vec4(color.rgb, 1.);
EmitMap = texture(SpecMap, uv).b;
}
7 changes: 6 additions & 1 deletion data/shaders/gi.frag
Expand Up @@ -5,6 +5,7 @@
uniform sampler2D ntex;
uniform sampler2D dtex;
uniform sampler2D ctex;
uniform sampler2D dfg;

uniform sampler3D SHR;
uniform sampler3D SHG;
Expand Down Expand Up @@ -88,5 +89,9 @@ void main()
vec3 color = texture(ctex, uv).rgb;
float reflectance = texture(ntex, uv).a;

FragColor = max(16. * vec4((1. - reflectance) * color * GI, 0.), vec4(0.));
float roughness = texture(ntex, uv).z;
vec3 eyedir = -normalize(pos_screen_space.xyz / pos_screen_space.w);
float NdotV = clamp(dot(eyedir, normal_screen_space), 0., 1.);

FragColor = max(1. * vec4((1. - reflectance) * color * GI * texture(dfg, vec2(NdotV, roughness)).b, 0.), vec4(0.));
}
4 changes: 3 additions & 1 deletion data/shaders/importance_sampling_specular.frag
Expand Up @@ -23,7 +23,9 @@ void main(void)
float Theta = texelFetch(samples, i).r;
float Phi = texelFetch(samples, i).g;

vec3 L = cos(Theta) * RayDir + sin(Theta) * cos(Phi) * Tangent + sin(Theta) * sin(Phi) * Bitangent;
vec3 H = cos(Theta) * RayDir + sin(Theta) * cos(Phi) * Tangent + sin(Theta) * sin(Phi) * Bitangent;
vec3 L = 2 * dot(RayDir, H) * H - RayDir;

float NdotL = clamp(dot(RayDir, L), 0., 1.);
FinalColor += textureLod(tex, L, 0.) * NdotL;
weight += NdotL;
Expand Down
8 changes: 4 additions & 4 deletions data/shaders/instanced_detailledobject.frag
Expand Up @@ -13,7 +13,7 @@ in vec3 nor;
in vec2 uv;
in vec2 uv_bis;

layout(location = 0) out vec4 EncodedNormal_Specular_Reflectance;
layout(location = 0) out vec4 EncodedNormal_Roughness_Metalness;
layout(location = 1) out vec4 Colors;
layout(location = 2) out float EmitMap;

Expand All @@ -39,9 +39,9 @@ void main(void)
#endif
color *= detail;

EncodedNormal_Specular_Reflectance.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Specular_Reflectance.z = glossmap;
EncodedNormal_Specular_Reflectance.w = reflectance;
EncodedNormal_Roughness_Metalness.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Roughness_Metalness.z = 1. - glossmap;
EncodedNormal_Roughness_Metalness.w = reflectance;
Colors = vec4(color.rgb, 1.);
EmitMap = emitval;
}
8 changes: 4 additions & 4 deletions data/shaders/instanced_normalmap.frag
Expand Up @@ -13,7 +13,7 @@ in vec3 tangent;
in vec3 bitangent;
in vec2 uv;

layout(location = 0) out vec4 EncodedNormal_Specular_Reflectance;
layout(location = 0) out vec4 EncodedNormal_Roughness_Metalness;
layout(location = 1) out vec4 Colors;
layout(location = 2) out float EmitMap;

Expand Down Expand Up @@ -44,9 +44,9 @@ void main()
vec3 Frag_bitangent = cross(Frag_normal, Frag_tangent);

vec3 FragmentNormal = TS_normal.x * Frag_tangent + TS_normal.y * Frag_bitangent - TS_normal.z * Frag_normal;
EncodedNormal_Specular_Reflectance.xy = 0.5 * EncodeNormal(normalize(FragmentNormal)) + 0.5;
EncodedNormal_Specular_Reflectance.z = gloss;
EncodedNormal_Specular_Reflectance.w = reflectance;
EncodedNormal_Roughness_Metalness.xy = 0.5 * EncodeNormal(normalize(FragmentNormal)) + 0.5;
EncodedNormal_Roughness_Metalness.z = 1. - gloss;
EncodedNormal_Roughness_Metalness.w = reflectance;
Colors = vec4(col.rgb, 1.);
EmitMap = emitval;
}
8 changes: 4 additions & 4 deletions data/shaders/instanced_object.frag
Expand Up @@ -11,7 +11,7 @@ in vec4 color;
in vec3 nor;
in vec2 uv;

layout(location = 0) out vec4 EncodedNormal_Specular_Reflectance;
layout(location = 0) out vec4 EncodedNormal_Roughness_Metalness;
layout(location = 1) out vec4 Colors;
layout(location = 2) out float EmitMap;

Expand All @@ -34,8 +34,8 @@ void main(void)
float reflectance = texture(glosstex, uv).g;
float emitval = texture(glosstex, uv).b;
#endif
EncodedNormal_Specular_Reflectance.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Specular_Reflectance.z = glossmap;
EncodedNormal_Specular_Reflectance.w = reflectance;
EncodedNormal_Roughness_Metalness.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Roughness_Metalness.z = 1. - glossmap;
EncodedNormal_Roughness_Metalness.w = reflectance;
EmitMap = emitval;
}
8 changes: 4 additions & 4 deletions data/shaders/instanced_objectref.frag
Expand Up @@ -11,7 +11,7 @@ in vec4 color;
in vec3 nor;
in vec2 uv;

layout(location = 0) out vec4 EncodedNormal_Specular_Reflectance;
layout(location = 0) out vec4 EncodedNormal_Roughness_Metalness;
layout(location = 1) out vec4 Colors;
layout(location = 2) out float EmitMap;

Expand All @@ -35,9 +35,9 @@ void main() {
if (col.a < 0.5)
discard;
Colors = vec4(col.rgb * pow(color.rgb, vec3(2.2)), 1.);
EncodedNormal_Specular_Reflectance.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Specular_Reflectance.z = glossmap;
EncodedNormal_Specular_Reflectance.w = reflectance;
EncodedNormal_Roughness_Metalness.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Roughness_Metalness.z = 1. - glossmap;
EncodedNormal_Roughness_Metalness.w = reflectance;
EmitMap = emitval;
}

8 changes: 4 additions & 4 deletions data/shaders/normalmap.frag
Expand Up @@ -12,7 +12,7 @@ in vec3 tangent;
in vec3 bitangent;
in vec2 uv;

layout(location = 0) out vec4 EncodedNormal_Specular_Reflectance;
layout(location = 0) out vec4 EncodedNormal_Roughness_Metalness;
layout(location = 1) out vec4 Colors;
layout(location = 2) out float EmitMap;

Expand All @@ -30,9 +30,9 @@ void main()
vec3 Frag_bitangent = cross(Frag_normal, Frag_tangent);

vec3 FragmentNormal = TS_normal.x * Frag_tangent + TS_normal.y * Frag_bitangent - TS_normal.z * Frag_normal;
EncodedNormal_Specular_Reflectance.xy = 0.5 * EncodeNormal(normalize(FragmentNormal)) + 0.5;
EncodedNormal_Specular_Reflectance.z = gloss;
EncodedNormal_Specular_Reflectance.w = reflectance;
EncodedNormal_Roughness_Metalness.xy = 0.5 * EncodeNormal(normalize(FragmentNormal)) + 0.5;
EncodedNormal_Roughness_Metalness.z = 1. - gloss;
EncodedNormal_Roughness_Metalness.w = reflectance;

vec4 col = texture(tex, uv);
#ifdef Use_Bindless_Texture
Expand Down
8 changes: 4 additions & 4 deletions data/shaders/object.frag
Expand Up @@ -10,7 +10,7 @@ in vec3 nor;
in vec2 uv;
in vec4 color;

layout(location = 0) out vec4 EncodedNormal_Specular_Reflectance;
layout(location = 0) out vec4 EncodedNormal_Roughness_Metalness;
layout(location = 1) out vec4 Colors;
layout(location = 2) out float EmitMap;

Expand All @@ -21,8 +21,8 @@ void main(void)
Colors = vec4(texture(tex, uv).rgb * pow(color.rgb, vec3(2.2)), 1.);
float glossmap = texture(glosstex, uv).r;
float reflectance = texture(glosstex, uv).g;
EncodedNormal_Specular_Reflectance.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Specular_Reflectance.z = glossmap;
EncodedNormal_Specular_Reflectance.w = reflectance;
EncodedNormal_Roughness_Metalness.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Roughness_Metalness.z = 1. - glossmap;
EncodedNormal_Roughness_Metalness.w = reflectance;
EmitMap = texture(glosstex, uv).b;
}
8 changes: 4 additions & 4 deletions data/shaders/objectref.frag
Expand Up @@ -10,7 +10,7 @@ in vec4 color;
in vec3 nor;
in vec2 uv;

layout(location = 0) out vec4 EncodedNormal_Specular_Reflectance;
layout(location = 0) out vec4 EncodedNormal_Roughness_Metalness;
layout(location = 1) out vec4 Colors;
layout(location = 2) out float EmitMap;

Expand All @@ -23,9 +23,9 @@ void main() {
Colors = vec4(col.rgb * pow(color.rgb, vec3(2.2)), 1.);
float glossmap = texture(glosstex, uv).r;
float reflectance = texture(glosstex, uv).g;
EncodedNormal_Specular_Reflectance.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Specular_Reflectance.z = glossmap;
EncodedNormal_Specular_Reflectance.w = reflectance;
EncodedNormal_Roughness_Metalness.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Roughness_Metalness.z = 1. - glossmap;
EncodedNormal_Roughness_Metalness.w = reflectance;
EmitMap = texture(glosstex, uv).b;
}

7 changes: 5 additions & 2 deletions data/shaders/pointlight.frag
Expand Up @@ -38,7 +38,10 @@ void main()

float NdotL = clamp(dot(norm, L), 0., 1.);
vec3 color = texture(ctex, texc).rgb;
float reflectance = texture(ntex, texc).a;
float metalness = texture(ntex, texc).a;

FragColor = vec4(NdotL * light_col * att * mix(DiffuseBRDF(norm, eyedir, L, color, roughness), SpecularBRDF(norm, eyedir, L, color, roughness), reflectance), 1.);
vec3 Dielectric = DiffuseBRDF(norm, eyedir, L, color, roughness) + SpecularBRDF(norm, eyedir, L, vec3(.04), roughness);
vec3 Metal = SpecularBRDF(norm, eyedir, L, color, roughness);

FragColor = vec4(NdotL * light_col * att *mix(Dielectric, Metal, metalness), 1.);
}
8 changes: 4 additions & 4 deletions data/shaders/splatting.frag
Expand Up @@ -18,7 +18,7 @@ in vec3 nor;
in vec2 uv;
in vec2 uv_bis;

layout(location = 0) out vec4 EncodedNormal_Specular_Reflectance;
layout(location = 0) out vec4 EncodedNormal_Roughness_Metalness;
layout(location = 1) out vec4 Colors;
layout(location = 2) out float EmitMap;

Expand Down Expand Up @@ -50,8 +50,8 @@ void main() {

float glossmap = texture(glosstex, uv).r;
float reflectance = texture(glosstex, uv).g;
EncodedNormal_Specular_Reflectance.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Specular_Reflectance.z = glossmap;
EncodedNormal_Specular_Reflectance.w = reflectance;
EncodedNormal_Roughness_Metalness.xy = 0.5 * EncodeNormal(normalize(nor)) + 0.5;
EncodedNormal_Roughness_Metalness.z = 1. - glossmap;
EncodedNormal_Roughness_Metalness.w = reflectance;
EmitMap = texture(glosstex, uv).b;
}
7 changes: 4 additions & 3 deletions data/shaders/sunlight.frag
Expand Up @@ -23,7 +23,7 @@ void main() {
vec3 Lightdir = SunMRP(norm, eyedir);
float NdotL = clamp(dot(norm, Lightdir), 0., 1.);

float reflectance = texture(ntex, uv).a;
float metalness = texture(ntex, uv).a;

/* if (hasclouds == 1)
{
Expand All @@ -34,6 +34,7 @@ void main() {
outcol *= cloud;
}*/


FragColor = vec4(NdotL * sun_col * mix(DiffuseBRDF(norm, eyedir, Lightdir, color, roughness), SpecularBRDF(norm, eyedir, Lightdir, color, roughness), reflectance), 0.);
vec3 Dielectric = DiffuseBRDF(norm, eyedir, Lightdir, color, roughness) + SpecularBRDF(norm, eyedir, Lightdir, vec3(.04), roughness);
vec3 Metal = SpecularBRDF(norm, eyedir, Lightdir, color, roughness);
FragColor = vec4(NdotL * sun_col * mix(Dielectric, Metal, metalness), 0.);
}
7 changes: 5 additions & 2 deletions data/shaders/sunlightshadow.frag
Expand Up @@ -41,7 +41,7 @@ void main() {
float NdotL = clamp(dot(norm, Lightdir), 0., 1.);
vec3 color = texture(ctex, uv).rgb;

float reflectance = texture(ntex, uv).a;
float metalness = texture(ntex, uv).a;

// Shadows
float factor;
Expand All @@ -55,5 +55,8 @@ void main() {
factor = getShadowFactor(xpos.xyz, 3);
else
factor = 1.;
FragColor = vec4(factor * NdotL * sun_col * mix(DiffuseBRDF(norm, eyedir, Lightdir, color, roughness), SpecularBRDF(norm, eyedir, Lightdir, color, roughness), reflectance), .0);

vec3 Dielectric = DiffuseBRDF(norm, eyedir, Lightdir, color, roughness) + SpecularBRDF(norm, eyedir, Lightdir, vec3(.04), roughness);
vec3 Metal = SpecularBRDF(norm, eyedir, Lightdir, color, roughness);
FragColor = vec4(factor * NdotL * sun_col * mix(Dielectric, Metal, metalness), 0.);
}
2 changes: 1 addition & 1 deletion data/shaders/tonemap.frag
Expand Up @@ -12,7 +12,7 @@ void main()
vec4 col = texture(tex, uv);

vec3 Yxy = getCIEYxy(col.rgb);
col.rgb = getRGBFromCIEXxy(vec3(3.14 * Yxy.x, Yxy.y, Yxy.z));
col.rgb = getRGBFromCIEXxy(vec3(2. * Yxy.x, Yxy.y, Yxy.z));

// Uncharted2 tonemap with Auria's custom coefficients
vec4 perChannel = (col * (6.9 * col + .5)) / (col * (5.2 * col + 1.7) + 0.06);
Expand Down
14 changes: 12 additions & 2 deletions data/shaders/utils/DiffuseBRDF.frag
@@ -1,5 +1,15 @@
// Lambert model
// Burley model from Frostbite going pbr paper
vec3 DiffuseBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness)
{
return color/ 3.14;
float biaised_roughness = 0.05 + 0.95 * roughness;
// Half Light View direction
vec3 H = normalize(eyedir + lightdir);
float LdotH = clamp(dot(lightdir, H), 0., 1.);
float NdotL = clamp(dot(lightdir, normal), 0., 1.);
float NdotV = clamp(dot(lightdir, eyedir), 0., 1.);

float Fd90 = 0.5 + 2 * LdotH * LdotH * biaised_roughness * biaised_roughness;
float SchlickFresnelL = (1. + (Fd90 - 1.) * (1. - pow(NdotL, 5.)));
float SchlickFresnelV = (1. + (Fd90 - 1.) * (1. - pow(NdotV, 5.)));
return color * SchlickFresnelL * SchlickFresnelV / 3.14;
}
8 changes: 6 additions & 2 deletions data/shaders/utils/DiffuseIBL.frag
Expand Up @@ -2,6 +2,8 @@
// See http://graphics.stanford.edu/papers/envmap/
// Coefficients are calculated in IBL.cpp

uniform sampler2D dfg;

mat4 getMatrix(float L00, float L1m1, float L10, float L11, float L2m2, float L2m1, float L20, float L21, float L22)
{
float c1 = 0.429043, c2 = 0.511664, c3 = 0.743125, c4 = 0.886227, c5 = 0.247708;
Expand All @@ -14,7 +16,7 @@ mat4 getMatrix(float L00, float L1m1, float L10, float L11, float L2m2, float L2
);
}

vec3 DiffuseIBL(vec3 normal, vec3 color)
vec3 DiffuseIBL(vec3 normal, vec3 V, float roughness, vec3 color)
{
// Convert normal in wobLd space (where SH coordinates were computed)
vec4 extendednormal = transpose(ViewMatrix) * vec4(normal, 0.);
Expand All @@ -34,5 +36,7 @@ vec3 DiffuseIBL(vec3 normal, vec3 color)
float g = dot(extendednormal, gmat * extendednormal);
float b = dot(extendednormal, bmat * extendednormal);

return max(vec3(r, g, b), vec3(0.)) * color / 3.14;
float NdotV = clamp(dot(V, normal), 0., 1.);

return max(vec3(r, g, b), vec3(0.)) * texture(dfg, vec2(NdotV, roughness)).b * color / 3.14;
}
29 changes: 20 additions & 9 deletions data/shaders/utils/SpecularBRDF.frag
Expand Up @@ -4,27 +4,38 @@ vec3 Fresnel(vec3 viewdir, vec3 halfdir, vec3 basecolor)
return clamp(basecolor + (1. - basecolor) * pow(1. - clamp(dot(viewdir, halfdir), 0., 1.), 5.), vec3(0.), vec3(1.));
}

// Reduced because we include the term below the fraction
// We use implicit
vec3 ReducedGeometric()

// Schlick geometry term
float G1(vec3 V, vec3 normal, float k)
{
float NdotV = clamp(dot(V, normal), 0., 1.);
return 1. / (NdotV * (1. - k) + k);
}

// Smith model
// We factor the (n.v) (n.l) factor in the denominator
float ReducedGeometric(vec3 lightdir, vec3 viewdir, vec3 normal, float roughness)
{
return vec3(1.);
float k = (roughness + 1.) * (roughness + 1.) / 8.;
return G1(lightdir, normal, k) * G1(viewdir, normal, k);
}

// Blinn Phong
// GGX
float Distribution(float roughness, vec3 normal, vec3 halfdir)
{
float r = exp2(10. * roughness + 1.);
float alpha = roughness * roughness * roughness * roughness;
float NdotH = clamp(dot(normal, halfdir), 0., 1.);
float normalisationFactor = (r + 2.) / (2. * 3.14);
return normalisationFactor * pow(NdotH, r);
float normalisationFactor = 1. / 3.14;
float denominator = NdotH * NdotH * (alpha - 1.) + 1.;
return normalisationFactor * alpha / (denominator * denominator);
}

vec3 SpecularBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness)
{
// Half Light View direction
vec3 H = normalize(eyedir + lightdir);
float biaised_roughness = 0.05 + 0.95 * roughness;

// Microfacet model
return Fresnel(eyedir, H, color) * ReducedGeometric() * Distribution(roughness, normal, H) / 4.;
return Fresnel(eyedir, H, color) * ReducedGeometric(lightdir, eyedir, normal, biaised_roughness) * Distribution(biaised_roughness, normal, H) / 4.;
}

0 comments on commit f9e321e

Please sign in to comment.