Deferred shadows.

This commit is contained in:
Lubos Lenco 2016-03-22 23:35:54 +01:00
parent 01c8806abd
commit d486d38614
7 changed files with 198 additions and 11 deletions

View file

@ -61,6 +61,39 @@
],
"vertex_shader": "deferred.vert.glsl",
"fragment_shader": "deferred.frag.glsl"
},
{
"id": "shadowmap",
"params": [
{
"id": "depth_write",
"value": "true"
},
{
"id": "compare_mode",
"value": "less"
},
{
"id": "cull_mode",
"value": "counter_clockwise"
},
{
"id": "blend_source",
"value": "blend_one"
},
{
"id": "blend_destination",
"value": "blend_zero"
}
],
"links": [
{
"id": "LMVP",
"link": "_lightModelViewProjectionMatrix"
}
],
"vertex_shader": "shadowmap.vert.glsl",
"fragment_shader": "shadowmap.frag.glsl"
}
]
}

View file

@ -0,0 +1,25 @@
#version 450
#ifdef GL_ES
precision mediump float;
#endif
#ifdef _NMTex
#define _AMTex
#endif
in vec4 position;
void main() {
float depth = position.z / position.w;
// depth += 0.005;
// gl_FragDepth = depth;
gl_FragColor = vec4(depth, 0.0, 0.0, 1.0);
// VSM
// float dx = dFdx(depth);
// float dy = dFdy(depth);
// gl_FragColor = vec4(depth, pow(depth, 2.0) + 0.25 * (dx * dx + dy * dy), 0.0, 1.0);
}

View file

@ -0,0 +1,42 @@
#version 450
#ifdef GL_ES
precision highp float;
#endif
#ifdef _NMTex
#define _AMTex
#endif
in vec3 pos;
in vec3 nor;
#ifdef _AMTex
in vec2 tex;
#endif
#ifdef _VCols
in vec4 col;
#endif
#ifdef _NMTex
in vec3 tan;
in vec3 bitan;
#endif
#ifdef _Skinning
in vec4 bone;
in vec4 weight;
#endif
#ifdef _Instancing
in vec3 off;
#endif
uniform mat4 LMVP;
out vec4 position;
void main() {
#ifdef _Instancing
gl_Position = LMVP * vec4(pos + off, 1.0);
#else
gl_Position = LMVP * vec4(pos, 1.0);
#endif
position = gl_Position;
}

View file

@ -18,6 +18,7 @@ uniform sampler2D senvmapRadiance;
uniform sampler2D senvmapIrradiance;
uniform sampler2D senvmapBrdf;
uniform mat4 LMVP;
uniform vec3 light;
uniform vec3 eye;
@ -71,6 +72,64 @@ vec3 diffuseBRDF(vec3 albedo, float roughness, float nv, float nl, float vh, flo
return lambert(albedo, nl);
}
float texture2DCompare(vec2 uv, float compare){
float depth = texture(shadowMap, uv).r * 2.0 - 1.0;
return step(compare, depth);
}
float texture2DShadowLerp(vec2 size, vec2 uv, float compare){
vec2 texelSize = vec2(1.0) / size;
vec2 f = fract(uv * size + 0.5);
vec2 centroidUV = floor(uv * size + 0.5) / size;
float lb = texture2DCompare(centroidUV + texelSize * vec2(0.0, 0.0), compare);
float lt = texture2DCompare(centroidUV + texelSize * vec2(0.0, 1.0), compare);
float rb = texture2DCompare(centroidUV + texelSize * vec2(1.0, 0.0), compare);
float rt = texture2DCompare(centroidUV + texelSize * vec2(1.0, 1.0), compare);
float a = mix(lb, lt, f.y);
float b = mix(rb, rt, f.y);
float c = mix(a, b, f.x);
return c;
}
float PCF(vec2 size, vec2 uv, float compare){
float result = 0.0;
// for (int x = -1; x <= 1; x++){
// for(int y = -1; y <= 1; y++){
// vec2 off = vec2(x, y) / size;
// result += texture2DShadowLerp(size, uv + off, compare);
vec2 off = vec2(-1, -1) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(-1, 0) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(-1, 1) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(0, -1) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(0, 0) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(0, 1) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(1, -1) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(1, 0) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(1, 1) / size;
result += texture2DShadowLerp(size, uv + off, compare);
// }
// }
return result / 9.0;
}
float shadowTest(vec4 lPos) {
vec4 lPosH = lPos / lPos.w;
lPosH.x = (lPosH.x + 1.0) / 2.0;
lPosH.y = 1.0 - ((-lPosH.y + 1.0) / (2.0));
return PCF(vec2(2048, 2048), lPosH.st, lPosH.z - 0.005);
}
void main() {
vec4 g0 = texture(gbuffer0, texCoord); // Normals, depth
@ -106,7 +165,12 @@ void main() {
vec3 albedo = surfaceAlbedo(baseColor, metalness);
vec3 f0 = surfaceF0(baseColor, metalness);
vec4 lPos = LMVP * vec4(vec3(p), 1.0);
float visibility = 1.0;
if (lPos.w > 0.0) {
visibility = shadowTest(lPos);
// visibility = 1.0;
}
// Direct
vec3 direct = diffuseBRDF(albedo, roughness, dotNV, dotNL, dotVH, dotLV) + specularBRDF(f0, roughness, dotNL, dotNH, dotNV, dotVH, dotLH);

View file

@ -32,6 +32,10 @@
{
"id": "eye",
"link": "_cameraPosition"
},
{
"id": "LMVP",
"link": "_lightModelViewProjectionMatrix"
}
],
"vertex_shader": "deferred_light.vert.glsl",

View file

@ -92,16 +92,35 @@ float texture2DShadowLerp(vec2 size, vec2 uv, float compare){
float PCF(vec2 size, vec2 uv, float compare){
float result = 0.0;
for (int x = -1; x <= 1; x++){
for(int y = -1; y <= 1; y++){
vec2 off = vec2(x, y) / size;
result += texture2DShadowLerp(size, uv + off, compare);
}
}
// for (int x = -1; x <= 1; x++){
// for(int y = -1; y <= 1; y++){
// vec2 off = vec2(x, y) / size;
// result += texture2DShadowLerp(size, uv + off, compare);
vec2 off = vec2(-1, -1) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(-1, 0) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(-1, 1) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(0, -1) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(0, 0) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(0, 1) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(1, -1) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(1, 0) / size;
result += texture2DShadowLerp(size, uv + off, compare);
off = vec2(1, 1) / size;
result += texture2DShadowLerp(size, uv + off, compare);
// }
// }
return result / 9.0;
}
float shadowTest(vec4 lPos, float dotNL) {
float shadowTest(vec4 lPos) {
vec4 lPosH = lPos / lPos.w;
lPosH.x = (lPosH.x + 1.0) / 2.0;
lPosH.y = 1.0 - ((-lPosH.y + 1.0) / (2.0));
@ -266,8 +285,8 @@ void main() {
float visibility = 1.0;
if (receiveShadow) {
if (lPos.w > 0.0) {
visibility = shadowTest(lPos, dotNL);
visibility = 1.0;
visibility = shadowTest(lPos);
// visibility = 1.0;
}
}

View file

@ -15,8 +15,8 @@ void main() {
float depth = position.z / position.w;
// depth += 0.005;
gl_FragDepth = depth;
// gl_FragColor = vec4(depth, 0.0, 0.0, 1.0);
// gl_FragDepth = depth;
gl_FragColor = vec4(depth, 0.0, 0.0, 1.0);
// VSM
// float dx = dFdx(depth);