X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=pbrfrag.glsl;h=99523ec604f99b0fd7673e56848d14a837328c13;hp=16a9fa7a1ba6128257bff2175ee0b24a8868d407;hb=d80972d96e5fcd444657f937ab2700039efa83d2;hpb=9e43c799021b7bcca324b988aae44e98b05d10b4 diff --git a/pbrfrag.glsl b/pbrfrag.glsl index 16a9fa7..99523ec 100644 --- a/pbrfrag.glsl +++ b/pbrfrag.glsl @@ -13,6 +13,8 @@ uniform sampler2D metallicMap; uniform sampler2D roughnessMap; uniform sampler2D aoMap; uniform samplerCube irradianceMap; +uniform samplerCube prefilterMap; +uniform sampler2D brdfMap; out vec4 fragColor; @@ -76,6 +78,8 @@ void main() { vec3 F0 = mix(vec3(0.04), albedo, metallic); + vec3 R = reflect(-V, N); + // reflectance vec3 Lo = vec3(0.f); for (int i = 0; i < lightPositions.length(); i++) { @@ -102,9 +106,20 @@ void main() { Lo += (kD * albedo / PI + specular) * radiance * mdot(N, L); } - vec3 kD = 1.f - fresnelSchlickRoughness(mdot(N, V), F0, roughness); - vec3 diffuse = texture(irradianceMap, N).rgb * albedo; - vec3 ambient = (kD * diffuse) * ao; + vec3 F = fresnelSchlickRoughness(mdot(N, V), F0, roughness); + + vec3 kS = F; + vec3 kD = (1.f - kS) * (1.f - metallic); + + vec3 irradiance = texture(irradianceMap, N).rgb; + vec3 diffuse = irradiance * albedo; + + const float maxReflectionLoD = 4.f; + vec3 prefilteredColor = textureLod(prefilterMap, R, roughness * maxReflectionLoD).rgb; + vec2 envBRDF = texture(brdfMap, vec2(mdot(N, V), roughness)).rg; + vec3 specular = prefilteredColor * (F * envBRDF.x + envBRDF.y); + + vec3 ambient = (kD * diffuse + specular) * ao; vec3 color = ambient + Lo; color = color / (color + vec3(1.f)); // map to HDR