More SSR.

This commit is contained in:
Lubos Lenco 2016-03-29 16:50:51 +02:00
parent ffdd0f1f3c
commit 63be820f91
3 changed files with 31 additions and 19 deletions

View file

@ -8,8 +8,10 @@ precision mediump float;
#define TwoPI (2.0 * PI)
uniform sampler2D envmap;
// uniform sampler2D tex;
in vec3 normal;
// in vec2 texCoord;
vec2 envMapEquirect(vec3 normal) {
float phi = acos(normal.z);
@ -18,7 +20,10 @@ vec2 envMapEquirect(vec3 normal) {
}
void main() {
// if (texture(tex, texCoord).a == 0.0) {
// discard;
// }
vec3 n = normalize(normal);
gl_FragColor = texture(envmap, envMapEquirect(n));
// gl_FragData[0] = texture(envmap, envMapEquirect(n));
}

View file

@ -10,6 +10,7 @@ uniform mat4 P;
in vec2 pos;
out vec3 normal;
// out vec2 texCoord;
mat3 transpose_(mat3 m) {
return mat3(m[0][0], m[1][0], m[2][0],
@ -58,9 +59,11 @@ mat4 inverse_(mat4 m) {
a20 * b03 - a21 * b01 + a22 * b00) / det;
}
// const vec2 madd = vec2(0.5, 0.5);
void main() {
mat4 invP = inverse_(P);
mat4 invP = inverse_(P);
// mat4 invP = inverse(P);
mat3 invMV = transpose_(mat3(V));
@ -68,6 +71,9 @@ void main() {
vec3 unprojected = (invP * p).xyz;
normal = invMV * unprojected;
// Scale vertex attribute to [0-1] range
// texCoord = pos.xy * madd + madd;
gl_Position = vec4(pos.xy, 0.0, 1.0);
}

View file

@ -11,7 +11,6 @@ uniform sampler2D gbuffer2;
uniform mat4 P;
uniform mat4 V;
// const vec2 gTexSizeInv = vec2(1.0 / 1136.0, 1.0 / 640.0);
const float rayStep = 0.25;
const float minRayStep = 0.1;
const float maxSteps = 20;
@ -27,7 +26,7 @@ in vec2 texCoord;
vec3 hitCoord;
float dDepth;
vec3 BinarySearch(vec3 dir) {
vec3 binarySearch(vec3 dir) {
float depth;
vec4 projectedCoord;
@ -94,7 +93,7 @@ vec3 BinarySearch(vec3 dir) {
return vec3(projectedCoord.xy, depth);
}
vec4 RayCast(vec3 dir) {
vec4 rayCast(vec3 dir) {
dir *= rayStep;
float depth;
@ -106,7 +105,7 @@ vec4 RayCast(vec3 dir) {
depth = texture(gbuffer0, projectedCoord.xy).a;
dDepth = hitCoord.z - depth;
if (dDepth < 0.0) {
return vec4(BinarySearch(dir), 1.0);
return vec4(binarySearch(dir), 1.0);
}
hitCoord += dir;
@ -116,7 +115,7 @@ vec4 RayCast(vec3 dir) {
depth = texture(gbuffer0, projectedCoord.xy).a;
dDepth = hitCoord.z - depth;
if (dDepth < 0.0) {
return vec4(BinarySearch(dir), 1.0);
return vec4(binarySearch(dir), 1.0);
}
hitCoord += dir;
@ -126,7 +125,7 @@ vec4 RayCast(vec3 dir) {
depth = texture(gbuffer0, projectedCoord.xy).a;
dDepth = hitCoord.z - depth;
if (dDepth < 0.0) {
return vec4(BinarySearch(dir), 1.0);
return vec4(binarySearch(dir), 1.0);
}
hitCoord += dir;
@ -136,7 +135,7 @@ vec4 RayCast(vec3 dir) {
depth = texture(gbuffer0, projectedCoord.xy).a;
dDepth = hitCoord.z - depth;
if (dDepth < 0.0) {
return vec4(BinarySearch(dir), 1.0);
return vec4(binarySearch(dir), 1.0);
}
hitCoord += dir;
@ -146,7 +145,7 @@ vec4 RayCast(vec3 dir) {
depth = texture(gbuffer0, projectedCoord.xy).a;
dDepth = hitCoord.z - depth;
if (dDepth < 0.0) {
return vec4(BinarySearch(dir), 1.0);
return vec4(binarySearch(dir), 1.0);
}
hitCoord += dir;
@ -156,7 +155,7 @@ vec4 RayCast(vec3 dir) {
depth = texture(gbuffer0, projectedCoord.xy).a;
dDepth = hitCoord.z - depth;
if (dDepth < 0.0) {
return vec4(BinarySearch(dir), 1.0);
return vec4(binarySearch(dir), 1.0);
}
hitCoord += dir;
@ -166,7 +165,7 @@ vec4 RayCast(vec3 dir) {
depth = texture(gbuffer0, projectedCoord.xy).a;
dDepth = hitCoord.z - depth;
if (dDepth < 0.0) {
return vec4(BinarySearch(dir), 1.0);
return vec4(binarySearch(dir), 1.0);
}
hitCoord += dir;
@ -176,7 +175,7 @@ vec4 RayCast(vec3 dir) {
depth = texture(gbuffer0, projectedCoord.xy).a;
dDepth = hitCoord.z - depth;
if (dDepth < 0.0) {
return vec4(BinarySearch(dir), 1.0);
return vec4(binarySearch(dir), 1.0);
}
hitCoord += dir;
@ -186,7 +185,7 @@ vec4 RayCast(vec3 dir) {
depth = texture(gbuffer0, projectedCoord.xy).a;
dDepth = hitCoord.z - depth;
if (dDepth < 0.0) {
return vec4(BinarySearch(dir), 1.0);
return vec4(binarySearch(dir), 1.0);
}
hitCoord += dir;
@ -196,7 +195,7 @@ vec4 RayCast(vec3 dir) {
depth = texture(gbuffer0, projectedCoord.xy).a;
dDepth = hitCoord.z - depth;
if (dDepth < 0.0) {
return vec4(BinarySearch(dir), 1.0);
return vec4(binarySearch(dir), 1.0);
}
// }
@ -209,7 +208,9 @@ void main() {
float roughness = texture(gbuffer1, texCoord).a;
float specular = 1.0 - roughness;
if (specular == 0.0) {
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
vec4 texColor = texture(tex, texCoord);
gl_FragColor = texColor;
// gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
return;
}
@ -228,13 +229,13 @@ void main() {
hitCoord = viewPos.xyz;
dDepth = 0.0;
vec4 coords = RayCast(reflected * max(minRayStep, -viewPos.z));
vec4 coords = rayCast(reflected * max(minRayStep, -viewPos.z));
vec2 dCoords = abs(vec2(0.5, 0.5) - coords.xy);
float screenEdgefactor = clamp(1.0 - (dCoords.x + dCoords.y), 0.0, 1.0);
float screenEdgeFactor = clamp(1.0 - (dCoords.x + dCoords.y), 0.0, 1.0);
float intensity = pow(specular, reflectionSpecularFalloffExponent) *
screenEdgefactor * clamp(-reflected.z, 0.0, 1.0) *
screenEdgeFactor * clamp(-reflected.z, 0.0, 1.0) *
clamp((searchDist - length(viewPos.xyz - hitCoord)) * searchDistInv, 0.0, 1.0) * coords.w;
vec4 reflCol = vec4(texture(tex, coords.xy).rgb, 1.0);