Reconsctruct pos from depth.

This commit is contained in:
Lubos Lenco 2016-04-17 16:07:54 +02:00
parent f290684ca4
commit a6f6489dd0
15 changed files with 207 additions and 29 deletions

View file

@ -30,6 +30,7 @@ uniform float metalness;
uniform float mask;
in vec3 position;
in float depth;
in vec4 mvpposition;
#ifdef _AMTex
in vec2 texCoord;
@ -85,7 +86,7 @@ void main() {
float occlusion = 1.0;
#endif
float depth = mvpposition.z / mvpposition.w;
// float depth = mvpposition.z / mvpposition.w;
// occlusion - pack with mask

View file

@ -29,6 +29,10 @@
"id": "V",
"link": "_viewMatrix"
},
{
"id": "MV",
"link": "_modelViewMatrix"
},
{
"id": "P",
"link": "_projectionMatrix"

View file

@ -31,6 +31,7 @@ uniform mat4 M;
uniform mat4 NM;
// uniform mat4 VNM;
uniform mat4 V;
uniform mat4 MV;
uniform mat4 P;
uniform mat4 LMVP;
uniform vec4 albedo_color;
@ -41,6 +42,7 @@ uniform float skinBones[50 * 12];
#endif
out vec3 position;
out float depth;
out vec4 mvpposition;
#ifdef _AMTex
out vec2 texCoord;
@ -102,19 +104,19 @@ void main() {
#endif
lPos = LMVP * sPos;
mat4 VM = V * M;
// mat4 MV = V * M;
#ifdef _Billboard
// Spherical
VM[0][0] = 1.0; VM[0][1] = 0.0; VM[0][2] = 0.0;
VM[1][0] = 0.0; VM[1][1] = 1.0; VM[1][2] = 0.0;
VM[2][0] = 0.0; VM[2][1] = 0.0; VM[2][2] = 1.0;
MV[0][0] = 1.0; MV[0][1] = 0.0; MV[0][2] = 0.0;
MV[1][0] = 0.0; MV[1][1] = 1.0; MV[1][2] = 0.0;
MV[2][0] = 0.0; MV[2][1] = 0.0; MV[2][2] = 1.0;
// Cylindrical
//VM[0][0] = 1.0; VM[0][1] = 0.0; VM[0][2] = 0.0;
//VM[2][0] = 0.0; VM[2][1] = 0.0; VM[2][2] = 1.0;
//MV[0][0] = 1.0; MV[0][1] = 0.0; MV[0][2] = 0.0;
//MV[2][0] = 0.0; MV[2][1] = 0.0; MV[2][2] = 1.0;
#endif
gl_Position = P * VM * sPos;
gl_Position = P * MV * sPos;
#ifdef _AMTex
texCoord = tex;
@ -135,6 +137,13 @@ void main() {
vec3 mPos = vec4(M * sPos).xyz;
position = mPos;
vec4 vp = V * vec4(mPos, 1.0);
vec3 vposition = vp.xyz / vp.w;
float zNear = 0.1;
float zFar = 1000.0;
depth = (-vposition.z-zNear)/(zFar-zNear);
mvpposition = gl_Position;
lightDir = light - mPos;
eyeDir = eye - mPos;

View file

@ -18,11 +18,15 @@ uniform sampler2D senvmapRadiance;
uniform sampler2D senvmapIrradiance;
uniform sampler2D senvmapBrdf;
uniform mat4 invP;
uniform mat4 invV;
uniform mat4 invVP;
uniform mat4 LMVP;
uniform vec3 light;
uniform vec3 eye;
in vec2 texCoord;
in vec3 vViewRay;
vec2 envMapEquirect(vec3 normal) {
float phi = acos(normal.z);
@ -134,6 +138,24 @@ vec2 octahedronWrap(vec2 v) {
return (1.0 - abs(v.yx)) * (vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0));
}
vec3 worldPosFromDepth(float zOverW) {
// Get the depth buffer value at this pixel
// float zOverW = depth; // * 2.0 - 1.0
// H is the viewport position at this pixel in the range -1 to 1
// vec4 H = vec4(texCoord.x * 2.0 - 1.0, (texCoord.y) * 2.0 - 1.0, zOverW, 1.0);
// Transform by the view-projection inverse
// vec4 D = invVP * H;
// Divide by w to get the world position
// vec4 worldPos = D / D.w;
// return vec3(D.xyz / D.w);
vec3 ndc = vec3(texCoord*2.0-1.0, zOverW*2.0-1.0);
vec4 v0 = invP*vec4(ndc, 1.0);
vec3 reconViewPos = v0.xyz/v0.w;
vec4 reconWorldPos = invV * v0;
return reconWorldPos.xyz / reconWorldPos.w;
}
void main() {
vec4 g0 = texture(gbuffer0, texCoord); // Normals, depth
@ -151,7 +173,14 @@ void main() {
n = normalize(n);
// vec3 n = g0.rgb;
vec3 p = g1.rgb;
// vec3 p = worldPosFromDepth(depth);
const float zNear = 0.1;
const float zFar = 1000.0;
vec3 reconViewPos = vViewRay * (-(depth*(zFar-zNear)+zNear));
vec3 p = vec4(invV * vec4(reconViewPos, 1.0)).xyz;
// vec3 p = g1.rgb;
//n = normalize(n);
vec3 baseColor = g2.rgb;

View file

@ -25,6 +25,18 @@
"id": "eye",
"link": "_cameraPosition"
},
{
"id": "invVP",
"link": "_inverseViewProjectionMatrix"
},
{
"id": "invV",
"link": "_inverseViewMatrix"
},
{
"id": "invP",
"link": "_inverseProjectionMatrix"
},
{
"id": "LMVP",
"link": "_lightModelViewProjectionMatrix"

View file

@ -4,9 +4,12 @@
precision highp float;
#endif
uniform mat4 invP;
in vec2 pos;
out vec2 texCoord;
out vec3 vViewRay;
const vec2 madd = vec2(0.5, 0.5);
@ -15,4 +18,10 @@ void main() {
texCoord = pos.xy * madd + madd;
gl_Position = vec4(pos.xy, 0.0, 1.0);
vec4 v = vec4(pos.x, pos.y, 1.0, 1.0); //ndc (at the back of cube)
v = invP * v;
v /= v.w; //view coordinate
v /= v.z; //normalize by z for scaling
vViewRay = v.xyz;
}

View file

@ -9,25 +9,35 @@ precision mediump float;
uniform sampler2D gbuffer0;
uniform sampler2D gbuffer1;
uniform sampler2D gbuffer2;
// uniform sampler2D gbuffer3;
uniform sampler2D tex;
uniform mat4 prevVP;
uniform mat4 invVP;
uniform mat4 invV;
in vec3 vViewRay;
in vec2 texCoord;
const int samples = 8;
vec2 getVelocity(vec2 texCoord, float depth) {
const float zNear = 0.1;
const float zFar = 1000.0;
float delinDepth = (-(depth*(zFar-zNear)+zNear));
// Get the depth buffer value at this pixel
float zOverW = depth; // * 2.0 - 1.0
// float zOverW = depth; // * 2.0 - 1.0
// H is the viewport position at this pixel in the range -1 to 1
vec4 H = vec4(texCoord.x * 2.0 - 1.0, (texCoord.y) * 2.0 - 1.0, zOverW, 1.0);
// vec4 H = vec4(texCoord.x * 2.0 - 1.0, (texCoord.y) * 2.0 - 1.0, zOverW, 1.0);
vec4 H = vec4(texCoord.x * 2.0 - 1.0, (texCoord.y) * 2.0 - 1.0, delinDepth, 1.0);
// Transform by the view-projection inverse
vec4 D = invVP * H;
// vec4 D = invVP * H;
// Divide by w to get the world position
vec4 worldPos = D / D.w;
// vec4 worldPos = D / D.w;
vec3 reconViewPos = vViewRay * delinDepth;
vec3 p = vec4(invV * vec4(reconViewPos, 1.0)).xyz;
vec4 worldPos = vec4(p, 1.0);
// Current viewport position
vec4 currentPos = H;

View file

@ -24,6 +24,14 @@
{
"id": "invVP",
"link": "_inverseViewProjectionMatrix"
},
{
"id": "invV",
"link": "_inverseViewMatrix"
},
{
"id": "invP",
"link": "_inverseProjectionMatrix"
}
],
"texture_params": [],

View file

@ -4,9 +4,12 @@
precision highp float;
#endif
uniform mat4 invP;
in vec2 pos;
out vec2 texCoord;
out vec3 vViewRay;
const vec2 madd = vec2(0.5, 0.5);
@ -15,4 +18,10 @@ void main() {
texCoord = pos.xy * madd + madd;
gl_Position = vec4(pos.xy, 0.0, 1.0);
vec4 v = vec4(pos.x, pos.y, 1.0, 1.0); //ndc (at the back of cube)
v = invP * v;
v /= v.w; //view coordinate
v /= v.z; //normalize by z for scaling
vViewRay = v.xyz;
}

View file

@ -22,14 +22,18 @@ uniform sampler2D gbuffer0;
uniform sampler2D gbuffer1;
uniform sampler2D gbuffer2;
uniform sampler2D snoise;
uniform mat4 invV;
uniform mat4 invP;
uniform vec3 eye;
const float PI = 3.1415926535;
// const vec2 screenSize = vec2(800.0, 600.0);
const vec2 screenSize = vec2(1920.0, 1080.0);
const float aoSize = 0.6;//0.43;
const vec2 screenSize = vec2(800.0, 600.0);
// const vec2 screenSize = vec2(1920.0, 1080.0);
const float aoSize = 0.2;//0.43;
const int kernelSize = 8;
const float strength = 0.8;//0.55;
const float strength = 0.1;//0.55;
in vec3 vViewRay;
in vec2 texCoord;
float linearize(float depth, float znear, float zfar) {
@ -44,6 +48,24 @@ vec2 octahedronWrap(vec2 v) {
return (1.0 - abs(v.yx)) * (vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0));
}
vec3 getWorldPos(vec2 coord) {
const float zNear = 0.1;
const float zFar = 1000.0;
float depth = texture(gbuffer0, coord).a;
vec3 reconViewPos = vViewRay * (-(depth*(zFar-zNear)+zNear));
vec4 wp = invV * vec4(reconViewPos, 1.0);
return wp.xyz;
}
// vec3 getViewPos(vec2 coord) {
// float x = coord.s * 2.0 - 1.0;
// float y = coord.t * 2.0 - 1.0;
// float z = texture(gbuffer0, coord).b;
// z = z * 2.0 - 1.0;
// vec4 posProj = vec4(x, y, z, 1.0);
// vec4 posView = invP * posProj;
// }
void main() {
vec2 kernel[kernelSize];
kernel[0] = vec2(1.0, 0.0);
@ -74,15 +96,15 @@ void main() {
vec3 N;
N.z = 1.0 - abs(enc.x) - abs(enc.y);
N.xy = N.z >= 0.0 ? enc.xy : octahedronWrap(enc.xy);
N = normalize(N);
N = normalize(N);
// vec3 N = g0.rgb;
vec3 P = g1.rgb;
vec3 P = getWorldPos(texCoord);
// vec3 P = g1.rgb;
// Get the current pixel's positiom
vec3 currentPos = P;
// float currentDistance = length(currentPos);
float currentDistance = linearize(g0.a, 0.1, 1000.0);
float currentDistance = distance(currentPos, eye);
// float currentDistance = linearize(g0.b, 0.1, 1000.0);
vec3 currentNormal = N;
vec2 aspectRatio = vec2(min(1.0, screenSize.y / screenSize.x), min(1.0, screenSize.x / screenSize.y));

View file

@ -20,6 +20,26 @@
{
"id": "snoise",
"link": "_noise8"
},
{
"id": "invP",
"link": "_inverseProjectionMatrix"
},
{
"id": "invV",
"link": "_inverseViewMatrix"
},
{
"id": "eye",
"link": "_cameraPosition"
},
{
"id": "tiV",
"link": "_transposeInverseViewMatrix"
},
{
"id": "P",
"link": "_projectionMatrix"
}
],
"texture_params": [],

View file

@ -4,9 +4,12 @@
precision highp float;
#endif
uniform mat4 invP;
in vec2 pos;
out vec2 texCoord;
out vec3 vViewRay;
const vec2 madd = vec2(0.5, 0.5);
@ -15,4 +18,10 @@ void main() {
texCoord = pos.xy * madd + madd;
gl_Position = vec4(pos.xy, 0.0, 1.0);
vec4 v = vec4(pos.x, pos.y, 1.0, 1.0); //ndc (at the back of cube)
v = invP * v;
v /= v.w; //view coordinate
v /= v.z; //normalize by z for scaling
vViewRay = v.xyz;
}

View file

@ -24,6 +24,7 @@ const float falloffExp = 3.0;
// uniform float searchDist;
// uniform float falloffExp;
in vec3 vViewRay;
in vec2 texCoord;
vec3 hitCoord;
@ -41,10 +42,22 @@ vec4 getProjectedCoord(vec3 hitCoord) {
}
float getDeltaDepth(vec3 hitCoord) {
vec4 viewPos = vec4(texture(gbuffer1, getProjectedCoord(hitCoord).xy).rgb, 1.0);
viewPos = V * viewPos;
float depth = viewPos.z;
return depth - hitCoord.z;
float d = texture(gbuffer0, getProjectedCoord(hitCoord).xy).a;
const float zNear = 0.1;
const float zFar = 1000.0;
float delinDepth = (-(d*(zFar-zNear)+zNear));
// vec3 reconViewPos = vViewRay * delinDepth;
// vec3 p = vec4(invV * vec4(reconViewPos, 1.0)).xyz;
// vec4 worldPos = vec4(p, 1.0);
// vec4 viewPos = vec4(texture(gbuffer1, getProjectedCoord(hitCoord).xy).rgb, 1.0);
// viewPos = V * viewPos;
// float depth = viewPos.z;
// return depth - hitCoord.z;
return delinDepth - hitCoord.z;
}
vec3 binarySearch(vec3 dir) {
@ -183,8 +196,18 @@ void main() {
if (viewNormal.z <= 0.9) discard; // Only up facing surfaces for now
viewNormal = tiV * normalize(viewNormal);
vec4 viewPos = vec4(texture(gbuffer1, texCoord).rgb, 1.0);
viewPos = V * viewPos;
// vec4 viewPos = vec4(texture(gbuffer1, texCoord).rgb, 1.0);
// viewPos = V * viewPos;
float d = texture(gbuffer0, texCoord).a;
const float zNear = 0.1;
const float zFar = 1000.0;
float delinDepth = (-(d*(zFar-zNear)+zNear));
vec3 viewPos = vViewRay * delinDepth;
vec3 reflected = normalize(reflect((viewPos.xyz), normalize(viewNormal.xyz)));
hitCoord = viewPos.xyz;

View file

@ -21,6 +21,10 @@
"id": "P",
"link": "_projectionMatrix"
},
{
"id": "invP",
"link": "_inverseProjectionMatrix"
},
{
"id": "V",
"link": "_viewMatrix"

View file

@ -4,9 +4,12 @@
precision highp float;
#endif
uniform mat4 invP;
in vec2 pos;
out vec2 texCoord;
out vec3 vViewRay;
const vec2 madd = vec2(0.5, 0.5);
@ -15,4 +18,10 @@ void main() {
texCoord = pos.xy * madd + madd;
gl_Position = vec4(pos.xy, 0.0, 1.0);
vec4 v = vec4(pos.x, pos.y, 1.0, 1.0); //ndc (at the back of cube)
v = invP * v;
v /= v.w; //view coordinate
v /= v.z; //normalize by z for scaling
vViewRay = v.xyz;
}