Standard shader: spherical harmonics irradiance;

This commit is contained in:
bjorn 2019-06-20 21:17:32 -07:00
parent 286cb711e4
commit b6d65922af
1 changed files with 28 additions and 8 deletions

View File

@ -134,7 +134,8 @@ const char* lovrStandardFragmentShader = ""
"in vec3 vViewPosition; \n"
"in vec3 vWorldPosition; \n"
"in mat3 vTangentMatrix; \n"
"uniform vec3 uWorldLightDirection = vec3(-1., -1., -1.); \n"
"uniform vec3 lovrIrradiance[9]; \n"
"uniform vec3 lovrLightDirection = vec3(-1., -1., -1.); \n"
""
"float D_GGX(float NoH, float roughness) { \n"
" float alpha = roughness * roughness; \n"
@ -151,8 +152,23 @@ const char* lovrStandardFragmentShader = ""
" return .5 / max(GGXV + GGXL, 1e-5); \n"
"} \n"
""
"vec3 F_Schlick(vec3 F0, float LoH) { \n"
" return F0 + (vec3(1.) - F0) * pow(1. - LoH, 5.); \n"
"vec3 F_Schlick(vec3 F0, float VoH) { \n"
" return F0 + (vec3(1.) - F0) * pow(1. - VoH, 5.); \n"
"} \n"
""
"vec3 E_SphericalHarmonics(vec3 sh[9], vec3 n) { \n"
" n = -n; // WHY (maybe cmgen is returning upside down coefficients?) \n"
" return max("
"sh[0] + "
"sh[1] * n.y + "
"sh[2] * n.z + "
"sh[3] * n.x + "
"sh[4] * n.y * n.x + "
"sh[5] * n.y * n.z + "
"sh[6] * (3. * n.z * n.z - 1.) + "
"sh[7] * n.z * n.x + "
"sh[8] * (n.x * n.x - n.y * n.y)"
", 0.); \n"
"} \n"
""
"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n"
@ -166,22 +182,26 @@ const char* lovrStandardFragmentShader = ""
""
" vec3 N = normalize(normal); \n"
" vec3 V = normalize(-vViewPosition); \n"
" vec3 L = normalize(-uWorldLightDirection); \n"
" vec3 L = normalize(-lovrLightDirection); \n"
" vec3 H = normalize(V + L); \n"
""
" float NoV = abs(dot(N, V)) + 1e-5; \n"
" float NoL = clamp(dot(N, L), 0., 1.); \n"
" float NoH = clamp(dot(N, H), 0., 1.); \n"
" float LoH = clamp(dot(L, H), 0., 1.); \n"
" float VoH = clamp(dot(L, H), 0., 1.); \n"
""
" float D = D_GGX(NoH, roughness); \n"
" float G = G_SmithGGXCorrelated(NoV, NoL, roughness); \n"
" vec3 F = F_Schlick(F0, LoH); \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 color = (diffuse + specular) * NoL * occlusion + emissive; \n"
" return vec4(color, 1.); \n"
""
" vec3 direct = (diffuse / PI + specular) * NoL * occlusion; \n"
" vec3 indirect = diffuse * E; \n"
""
" return vec4(direct + indirect + emissive, 1.); \n"
"}";
const char* lovrCubeVertexShader = ""