diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index a638c3629..70e08e020 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -1,57 +1,26 @@ -uniform float blueLmn[9]; -uniform float greenLmn[9]; -uniform float redLmn[9]; uniform sampler2D ntex; uniform sampler2D dtex; -uniform samplerCube tex; -uniform mat4 TransposeViewMatrix; out vec4 Diff; out vec4 Spec; vec3 DecodeNormal(vec2 n); vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix); - -mat4 getMatrix(float L[9]) -{ - float c1 = 0.429043, c2 = 0.511664, c3 = 0.743125, c4 = 0.886227, c5 = 0.247708; - - return mat4( - c1 * L[8] /*L22*/, c1 * L[4] /*L2-2*/, c1 * L[7] /*L21*/, c2 * L[3] /*L11*/, - c1 * L[4], - c1 * L[8], c1 * L[5] /*L2-1*/, c2 * L[1] /*L1-1*/, - c1 * L[7], c1 * L[5], c3 * L[6] /*L20*/, c2 * L[2] /*L10*/, - c2 * L[3], c2 * L[1], c2 * L[2], c4 * L[0] /*L00*/ - c5 * L[6] - ); -} +vec3 DiffuseIBL(vec3 normal); +vec3 SpecularIBL(vec3 normal, vec3 V, float roughness); void main(void) { vec2 uv = gl_FragCoord.xy / screen; vec3 normal = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.)); - // Convert normal in world space (where SH coordinates were computed) - vec4 extendednormal = TransposeViewMatrix * vec4(normal, 0.); - extendednormal.w = 1.; - mat4 rmat = getMatrix(redLmn); - mat4 gmat = getMatrix(greenLmn); - mat4 bmat = getMatrix(blueLmn); - - float r = dot(extendednormal, rmat * extendednormal); - float g = dot(extendednormal, gmat * extendednormal); - float b = dot(extendednormal, bmat * extendednormal); - - Diff = max(0.25 * vec4(r, g, b, .1), vec4(0.)); + Diff = vec4(0.25 * DiffuseIBL(normal), 1.); float z = texture(dtex, uv).x; vec4 xpos = getPosFromUVDepth(vec3(uv, z), InverseProjectionMatrix); vec3 eyedir = -normalize(xpos.xyz); - vec3 sampleDirection = reflect(-eyedir, normal); - sampleDirection = (InverseViewMatrix * vec4(sampleDirection, 0.)).xyz; - float specval = texture(ntex, uv).z; - // Assume 8 level of lod (ie 256x256 texture) - float lodval = 8. * (1. - specval); - vec4 specular = textureLod(tex, sampleDirection, lodval); - Spec = max(specular, vec4(0.)); + + Spec = vec4(SpecularIBL(normal, eyedir, specval), 1.); } diff --git a/data/shaders/utils/DiffuseIBL.frag b/data/shaders/utils/DiffuseIBL.frag new file mode 100644 index 000000000..0d4d14194 --- /dev/null +++ b/data/shaders/utils/DiffuseIBL.frag @@ -0,0 +1,32 @@ +uniform float blueLmn[9]; +uniform float greenLmn[9]; +uniform float redLmn[9]; +uniform mat4 TransposeViewMatrix; + +mat4 getMatrix(float L[9]) +{ + float c1 = 0.429043, c2 = 0.511664, c3 = 0.743125, c4 = 0.886227, c5 = 0.247708; + + return mat4( + c1 * L[8] /*L22*/, c1 * L[4] /*L2-2*/, c1 * L[7] /*L21*/, c2 * L[3] /*L11*/, + c1 * L[4], - c1 * L[8], c1 * L[5] /*L2-1*/, c2 * L[1] /*L1-1*/, + c1 * L[7], c1 * L[5], c3 * L[6] /*L20*/, c2 * L[2] /*L10*/, + c2 * L[3], c2 * L[1], c2 * L[2], c4 * L[0] /*L00*/ - c5 * L[6] + ); +} + +vec3 DiffuseIBL(vec3 normal) +{ + // Convert normal in world space (where SH coordinates were computed) + vec4 extendednormal = TransposeViewMatrix * vec4(normal, 0.); + extendednormal.w = 1.; + mat4 rmat = getMatrix(redLmn); + mat4 gmat = getMatrix(greenLmn); + mat4 bmat = getMatrix(blueLmn); + + float r = dot(extendednormal, rmat * extendednormal); + float g = dot(extendednormal, gmat * extendednormal); + float b = dot(extendednormal, bmat * extendednormal); + + return max(vec3(r, g, b), vec3(0.)); +} \ No newline at end of file diff --git a/data/shaders/utils/SpecularIBL.frag b/data/shaders/utils/SpecularIBL.frag new file mode 100644 index 000000000..04832dfa7 --- /dev/null +++ b/data/shaders/utils/SpecularIBL.frag @@ -0,0 +1,11 @@ +uniform samplerCube probe; + +vec3 SpecularIBL(vec3 normal, vec3 V, float roughness) +{ + vec3 sampleDirection = reflect(-V, normal); + sampleDirection = (InverseViewMatrix * vec4(sampleDirection, 0.)).xyz; + + // Assume 8 level of lod (ie 256x256 texture) + float lodval = 8. * (1. - roughness); + return textureLod(probe, sampleDirection, lodval).rgb; +} \ No newline at end of file diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 4f64a4fb8..c9a3c3569 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -1649,9 +1649,11 @@ namespace FullScreenShader GL_VERTEX_SHADER, file_manager->getAsset("shaders/screenquad.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/decodeNormal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getPosFromUVDepth.frag").c_str(), + GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/DiffuseIBL.frag").c_str(), + GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/SpecularIBL.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/IBL.frag").c_str()); AssignUniforms("TransposeViewMatrix", "blueLmn[0]", "greenLmn[0]", "redLmn[0]"); - AssignSamplerNames(Program, 0, "ntex", 1, "dtex", 2, "tex"); + AssignSamplerNames(Program, 0, "ntex", 1, "dtex", 2, "probe"); } ShadowedSunLightShader::ShadowedSunLightShader()