Clean up sunlight shader + factorize diffusebrdf
This commit is contained in:
parent
17fe9812b1
commit
1732b1cfef
@ -6,11 +6,12 @@ flat in float energy;
|
||||
flat in vec3 col;
|
||||
flat in float radius;
|
||||
|
||||
out vec4 Diffuse;
|
||||
out vec4 Specular;
|
||||
out vec4 Diff;
|
||||
out vec4 Spec;
|
||||
|
||||
vec3 DecodeNormal(vec2 n);
|
||||
vec3 SpecularBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
|
||||
vec3 DiffuseBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
|
||||
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
|
||||
|
||||
void main()
|
||||
@ -36,7 +37,9 @@ void main()
|
||||
vec3 L = -normalize(xpos.xyz - light_pos);
|
||||
|
||||
float NdotL = clamp(dot(norm, L), 0., 1.);
|
||||
vec3 Specular = SpecularBRDF(norm, eyedir, L, vec3(1.), roughness);
|
||||
vec3 Diffuse = DiffuseBRDF(norm, eyedir, L, vec3(1.), roughness);
|
||||
|
||||
Diffuse = vec4(NdotL * light_col * att, 1.);
|
||||
Specular = vec4(SpecularBRDF(norm, eyedir, L, light_col, roughness) * NdotL * att, 1.);
|
||||
Diff = vec4(Diffuse * NdotL * light_col * att, 1.);
|
||||
Spec = vec4(Specular * NdotL * light_col * att, 1.);
|
||||
}
|
||||
|
@ -1,19 +1,16 @@
|
||||
uniform sampler2D ntex;
|
||||
uniform sampler2D dtex;
|
||||
//uniform sampler2D cloudtex;
|
||||
|
||||
uniform vec3 direction;
|
||||
uniform vec3 col;
|
||||
uniform float sunangle = .54;
|
||||
|
||||
//uniform int hasclouds;
|
||||
//uniform vec2 wind;
|
||||
|
||||
out vec4 Diff;
|
||||
out vec4 Spec;
|
||||
|
||||
vec3 DecodeNormal(vec2 n);
|
||||
vec3 SpecularBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
|
||||
vec3 DiffuseBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
|
||||
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
|
||||
|
||||
vec3 getMostRepresentativePoint(vec3 direction, vec3 R, float angularRadius)
|
||||
@ -28,33 +25,26 @@ vec3 getMostRepresentativePoint(vec3 direction, vec3 R, float angularRadius)
|
||||
|
||||
void main() {
|
||||
vec2 uv = gl_FragCoord.xy / screen;
|
||||
float z = texture(dtex, uv).x;
|
||||
vec4 xpos = getPosFromUVDepth(vec3(uv, z), InverseProjectionMatrix);
|
||||
float z = texture(dtex, uv).x;
|
||||
vec4 xpos = getPosFromUVDepth(vec3(uv, z), InverseProjectionMatrix);
|
||||
|
||||
if (z < 0.03)
|
||||
{
|
||||
// Skyboxes are fully lit
|
||||
Diff = vec4(1.0);
|
||||
Spec = vec4(1.0);
|
||||
return;
|
||||
}
|
||||
|
||||
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
||||
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
||||
float roughness = texture(ntex, uv).z;
|
||||
vec3 eyedir = -normalize(xpos.xyz);
|
||||
|
||||
// Normalized on the cpu
|
||||
// Normalized on the cpu
|
||||
vec3 L = direction;
|
||||
|
||||
float NdotL = clamp(dot(norm, L), 0., 1.);
|
||||
|
||||
float angle = 3.14 * sunangle / 180.;
|
||||
vec3 R = reflect(-eyedir, norm);
|
||||
vec3 Lightdir = getMostRepresentativePoint(direction, R, angle);
|
||||
|
||||
vec3 Specular = SpecularBRDF(norm, eyedir, Lightdir, col, roughness) * NdotL;
|
||||
vec3 Specular = SpecularBRDF(norm, eyedir, Lightdir, vec3(1.), roughness);
|
||||
vec3 Diffuse = DiffuseBRDF(norm, eyedir, Lightdir, vec3(1.), roughness);
|
||||
|
||||
vec3 outcol = NdotL * col;
|
||||
Diff = vec4(NdotL * Diffuse * col, 1.);
|
||||
Spec = vec4(NdotL * Specular * col, 1.);
|
||||
|
||||
/* if (hasclouds == 1)
|
||||
{
|
||||
@ -64,7 +54,4 @@ void main() {
|
||||
|
||||
outcol *= cloud;
|
||||
}*/
|
||||
|
||||
Diff = vec4(NdotL * col, 1.);
|
||||
Spec = vec4(Specular, 1.);
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ out vec4 Spec;
|
||||
|
||||
vec3 DecodeNormal(vec2 n);
|
||||
vec3 SpecularBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
|
||||
vec3 DiffuseBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
|
||||
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
|
||||
|
||||
vec3 getMostRepresentativePoint(vec3 direction, vec3 R, float angularRadius)
|
||||
@ -29,83 +30,50 @@ vec3 getMostRepresentativePoint(vec3 direction, vec3 R, float angularRadius)
|
||||
return (DdotR < d) ? normalize(d * D + normalize (S) * r) : R;
|
||||
}
|
||||
|
||||
float getShadowFactor(vec3 pos, float bias, int index)
|
||||
float getShadowFactor(vec3 pos, int index)
|
||||
{
|
||||
vec4 shadowcoord = (ShadowViewProjMatrixes[index] * InverseViewMatrix * vec4(pos, 1.0));
|
||||
shadowcoord.xy /= shadowcoord.w;
|
||||
vec2 shadowtexcoord = shadowcoord.xy * 0.5 + 0.5;
|
||||
|
||||
vec2 shadowoffset[4] = vec2[](
|
||||
vec2(-1., -1.),
|
||||
vec2(-1., 1.),
|
||||
vec2(1., -1.),
|
||||
vec2(1., 1.)
|
||||
);
|
||||
|
||||
vec4 shadowcoord = (ShadowViewProjMatrixes[index] * InverseViewMatrix * vec4(pos, 1.0));
|
||||
shadowcoord.xy /= shadowcoord.w;
|
||||
vec2 shadowtexcoord = shadowcoord.xy * 0.5 + 0.5;
|
||||
|
||||
float z = texture(shadowtex, vec3(shadowtexcoord, float(index))).x;
|
||||
float d = shadowcoord.z;
|
||||
return min(pow(exp(-32. * d) * z, 8.), 1.);
|
||||
float z = texture(shadowtex, vec3(shadowtexcoord, float(index))).x;
|
||||
float d = shadowcoord.z;
|
||||
return min(pow(exp(-32. * d) * z, 8.), 1.);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec2 uv = gl_FragCoord.xy / screen;
|
||||
float z = texture(dtex, uv).x;
|
||||
vec4 xpos = getPosFromUVDepth(vec3(uv, z), InverseProjectionMatrix);
|
||||
float z = texture(dtex, uv).x;
|
||||
vec4 xpos = getPosFromUVDepth(vec3(uv, z), InverseProjectionMatrix);
|
||||
|
||||
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
||||
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
||||
float roughness =texture(ntex, uv).z;
|
||||
vec3 eyedir = -normalize(xpos.xyz);
|
||||
|
||||
// Normalized on the cpu
|
||||
// Normalized on the cpu
|
||||
vec3 L = direction;
|
||||
|
||||
float NdotL = clamp(dot(norm, L), 0., 1.);
|
||||
|
||||
float angle = 3.14 * sunangle / 180.;
|
||||
vec3 R = reflect(-eyedir, norm);
|
||||
vec3 Lightdir = getMostRepresentativePoint(direction, R, angle);
|
||||
|
||||
vec3 Specular = SpecularBRDF(norm, eyedir, Lightdir, col, roughness) * NdotL;
|
||||
vec3 Specular = SpecularBRDF(norm, eyedir, Lightdir, vec3(1.), roughness);
|
||||
vec3 Diffuse = DiffuseBRDF(norm, eyedir, Lightdir, vec3(1.), roughness);
|
||||
|
||||
// Shadows
|
||||
float factor;
|
||||
if (xpos.z < split0)
|
||||
factor = getShadowFactor(xpos.xyz, 0);
|
||||
else if (xpos.z < split1)
|
||||
factor = getShadowFactor(xpos.xyz, 1);
|
||||
else if (xpos.z < split2)
|
||||
factor = getShadowFactor(xpos.xyz, 2);
|
||||
else if (xpos.z < splitmax)
|
||||
factor = getShadowFactor(xpos.xyz, 3);
|
||||
else
|
||||
factor = 1.;
|
||||
|
||||
vec3 outcol = NdotL * col;
|
||||
|
||||
|
||||
// Shadows
|
||||
float bias = 0.005 * tan(acos(NdotL)); // According to the slope
|
||||
bias = clamp(bias, 0., 0.01);
|
||||
float factor;
|
||||
if (xpos.z < split0)
|
||||
factor = getShadowFactor(xpos.xyz, bias, 0);
|
||||
/* else if (xpos.z < 6.)
|
||||
{
|
||||
float a = getShadowFactor(xpos.xyz, bias, 0), b = getShadowFactor(xpos.xyz, bias, 1);
|
||||
factor = mix(a, b, (xpos.z - 5.));
|
||||
}*/
|
||||
else if (xpos.z < split1)
|
||||
factor = getShadowFactor(xpos.xyz, bias, 1);
|
||||
/* else if (xpos.z < 21.)
|
||||
{
|
||||
float a = getShadowFactor(xpos.xyz, bias, 1), b = getShadowFactor(xpos.xyz, bias, 2);
|
||||
factor = mix(a, b, (xpos.z - 20.));
|
||||
}*/
|
||||
else if (xpos.z < split2)
|
||||
factor = getShadowFactor(xpos.xyz, bias, 2);
|
||||
/* else if (xpos.z < 55.)
|
||||
{
|
||||
float a = getShadowFactor(xpos.xyz, bias, 2), b = getShadowFactor(xpos.xyz, bias, 3);
|
||||
factor = mix(a, b, (xpos.z - 50.) / 5.);
|
||||
}*/
|
||||
else if (xpos.z < splitmax)
|
||||
factor = getShadowFactor(xpos.xyz, bias, 3);
|
||||
/* else if (xpos.z < 150.)
|
||||
{
|
||||
factor = mix(getShadowFactor(xpos.xyz, bias, 3), 1., (xpos.z - 145.) / 5.);
|
||||
}*/
|
||||
else
|
||||
factor = 1.;
|
||||
Diff = vec4(factor * NdotL * col, 1.);
|
||||
Spec = vec4(factor * Specular, 1.);
|
||||
return;
|
||||
Diff = vec4(factor * NdotL * Diffuse * col, 1.);
|
||||
Spec = vec4(factor * NdotL * Specular * col, 1.);
|
||||
}
|
||||
|
5
data/shaders/utils/DiffuseBRDF.frag
Normal file
5
data/shaders/utils/DiffuseBRDF.frag
Normal file
@ -0,0 +1,5 @@
|
||||
// Lambert model
|
||||
vec3 DiffuseBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness)
|
||||
{
|
||||
return color;
|
||||
}
|
@ -1413,6 +1413,7 @@ namespace LightShader
|
||||
GL_VERTEX_SHADER, file_manager->getAsset("shaders/pointlight.vert").c_str(),
|
||||
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/decodeNormal.frag").c_str(),
|
||||
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/SpecularBRDF.frag").c_str(),
|
||||
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/DiffuseBRDF.frag").c_str(),
|
||||
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getPosFromUVDepth.frag").c_str(),
|
||||
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/pointlight.frag").c_str());
|
||||
|
||||
@ -1612,6 +1613,7 @@ 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/SpecularBRDF.frag").c_str(),
|
||||
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/DiffuseBRDF.frag").c_str(),
|
||||
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getPosFromUVDepth.frag").c_str(),
|
||||
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/sunlight.frag").c_str());
|
||||
|
||||
@ -1638,6 +1640,7 @@ 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/SpecularBRDF.frag").c_str(),
|
||||
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/DiffuseBRDF.frag").c_str(),
|
||||
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getPosFromUVDepth.frag").c_str(),
|
||||
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/sunlightshadow.frag").c_str());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user