Standard shader: Add image-based lighting;
Very WIP, still need to add the BRDF lookup table.
@@ -138,6 +138,8 @@ const char* lovrStandardFragmentShader = ""
"in vec3 vViewPosition; \n"
"in vec3 vWorldPosition; \n"
"in mat3 vTangentMatrix; \n"
+"uniform sampler2D lovrBRDFLookup; \n"
+"uniform samplerCube lovrSpecularIrradianceTexture; \n"
"uniform vec3 lovrIrradiance[9]; \n"
"uniform vec3 lovrLightDirection = vec3(-1., -1., -1.); \n"
""
@@ -161,7 +163,7 @@ const char* lovrStandardFragmentShader = ""
"} \n"
""
"vec3 E_SphericalHarmonics(vec3 sh[9], vec3 n) { \n"
-" n = -n; // WHY (maybe cmgen is returning upside down coefficients?) \n"
+" n = -n; // WHY \n"
" return max("
"sh[0] + "
"sh[1] * n.y + "
@@ -188,6 +190,7 @@ const char* lovrStandardFragmentShader = ""
" vec3 V = normalize(-vViewPosition); \n"
" vec3 L = normalize(-lovrLightDirection); \n"
" vec3 H = normalize(V + L); \n"
+" vec3 R = normalize(reflect(-V, N)); \n"
""
" float NoV = abs(dot(N, V)) + 1e-5; \n"
" float NoL = clamp(dot(N, L), 0., 1.); \n"
@@ -197,13 +200,15 @@ const char* lovrStandardFragmentShader = ""
" float D = D_GGX(NoH, roughness); \n"
" float G = G_SmithGGXCorrelated(NoV, NoL, roughness); \n"
" vec3 F = F_Schlick(F0, VoH); \n"
-" vec3 E = E_SphericalHarmonics(lovrIrradiance, N); \n"
""
-" vec3 specular = vec3(D * G * F); \n"
-" vec3 diffuse = (vec3(1.) - F) * (1. - metalness) * baseColor; \n"
+" vec3 specularDirect = vec3(D * G * F); \n"
+" vec3 diffuseDirect = (vec3(1.) - F) * (1. - metalness) * baseColor; \n"
+" vec3 direct = (diffuseDirect / PI + specularDirect) * NoL * occlusion; \n"
""
-" vec3 direct = (diffuse / PI + specular) * NoL * occlusion; \n"
-" vec3 indirect = diffuse * E; \n"
+" vec2 lookup = texture(lovrBRDFLookup, vec2(NoV, roughness)).rg; \n"
+" vec3 specularIndirect = (F0 * lookup.r + lookup.g) * textureLod(lovrSpecularIrradianceTexture, R, roughness * 1.).rgb; \n"
+" vec3 diffuseIndirect = diffuseDirect * E_SphericalHarmonics(lovrIrradiance, N); \n"
+" vec3 indirect = diffuseIndirect + specularIndirect; \n"
""
" return vec4(direct + indirect + emissive, 1.); \n"
"}";
