|
@@ -1,4 +1,5 @@
|
|
uniform vec2 g_Resolution;
|
|
uniform vec2 g_Resolution;
|
|
|
|
+uniform vec2 g_ResolutionInverse;
|
|
uniform vec2 m_FrustumNearFar;
|
|
uniform vec2 m_FrustumNearFar;
|
|
uniform sampler2D m_Texture;
|
|
uniform sampler2D m_Texture;
|
|
uniform sampler2D m_Normals;
|
|
uniform sampler2D m_Normals;
|
|
@@ -15,12 +16,9 @@ uniform vec2[4] m_Samples;
|
|
|
|
|
|
varying vec2 texCoord;
|
|
varying vec2 texCoord;
|
|
|
|
|
|
-float depthv;
|
|
|
|
-
|
|
|
|
-vec3 getPosition(in vec2 uv){
|
|
|
|
|
|
+vec3 getPosition(float depthv, in vec2 uv){
|
|
//Reconstruction from depth
|
|
//Reconstruction from depth
|
|
- depthv =texture2D(m_DepthTexture,uv).r;
|
|
|
|
- float depth= (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - depthv* (m_FrustumNearFar.y-m_FrustumNearFar.x));
|
|
|
|
|
|
+ float depth = (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - depthv * (m_FrustumNearFar.y-m_FrustumNearFar.x));
|
|
|
|
|
|
//one frustum corner method
|
|
//one frustum corner method
|
|
float x = mix(-m_FrustumCorner.x, m_FrustumCorner.x, uv.x);
|
|
float x = mix(-m_FrustumCorner.x, m_FrustumCorner.x, uv.x);
|
|
@@ -29,6 +27,19 @@ vec3 getPosition(in vec2 uv){
|
|
return depth* vec3(x, y, m_FrustumCorner.z);
|
|
return depth* vec3(x, y, m_FrustumCorner.z);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+vec3 approximateNormal(in vec3 pos,in vec2 texCoord){
|
|
|
|
+ float step = g_ResolutionInverse.x ;
|
|
|
|
+ float stepy = g_ResolutionInverse.y ;
|
|
|
|
+ float depth2 = texture2D(m_DepthTexture,texCoord + vec2(step,-stepy)).r;
|
|
|
|
+ float depth3 = texture2D(m_DepthTexture,texCoord + vec2(-step,-stepy)).r;
|
|
|
|
+ vec3 pos2 = vec3(getPosition(depth2,texCoord + vec2(step,-stepy)));
|
|
|
|
+ vec3 pos3 = vec3(getPosition(depth3,texCoord + vec2(-step,-stepy)));
|
|
|
|
+
|
|
|
|
+ vec3 v1 = (pos - pos2).xyz;
|
|
|
|
+ vec3 v2 = (pos3 - pos2).xyz;
|
|
|
|
+ return normalize(cross(-v1, v2));
|
|
|
|
+}
|
|
|
|
+
|
|
vec3 getNormal(in vec2 uv){
|
|
vec3 getNormal(in vec2 uv){
|
|
return normalize(texture2D(m_Normals, uv).xyz * 2.0 - 1.0);
|
|
return normalize(texture2D(m_Normals, uv).xyz * 2.0 - 1.0);
|
|
}
|
|
}
|
|
@@ -39,7 +50,8 @@ vec2 getRandom(in vec2 uv){
|
|
}
|
|
}
|
|
|
|
|
|
float doAmbientOcclusion(in vec2 tc, in vec3 pos, in vec3 norm){
|
|
float doAmbientOcclusion(in vec2 tc, in vec3 pos, in vec3 norm){
|
|
- vec3 diff = getPosition(tc)- pos;
|
|
|
|
|
|
+ float depthv = texture2D(m_DepthTexture, tc).r;
|
|
|
|
+ vec3 diff = getPosition(depthv, tc)- pos;
|
|
vec3 v = normalize(diff);
|
|
vec3 v = normalize(diff);
|
|
float d = length(diff) * m_Scale;
|
|
float d = length(diff) * m_Scale;
|
|
|
|
|
|
@@ -58,14 +70,21 @@ void main(){
|
|
|
|
|
|
float result;
|
|
float result;
|
|
|
|
|
|
- //vec2 vec[4] = { vec2(1.0, 0.0), vec2(-1.0, 0.0), vec2(0.0, 1.0), vec2(0.0, -1.0) };
|
|
|
|
- vec3 position = getPosition(texCoord);
|
|
|
|
- //optimization, do not calculate AO if depth is 1
|
|
|
|
- if(depthv==1.0){
|
|
|
|
- gl_FragColor=vec4(1.0);
|
|
|
|
- return;
|
|
|
|
|
|
+
|
|
|
|
+ float depthv = texture2D(m_DepthTexture, texCoord).r;
|
|
|
|
+ //optimization, do not calculate AO if depth is 1
|
|
|
|
+ if(depthv == 1.0){
|
|
|
|
+ gl_FragColor = vec4(1.0);
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
- vec3 normal = getNormal(texCoord);
|
|
|
|
|
|
+ vec3 position = getPosition(depthv, texCoord);
|
|
|
|
+
|
|
|
|
+ #ifdef APPROXIMATE_NORMALS
|
|
|
|
+ vec3 normal = approximateNormal(position, texCoord);
|
|
|
|
+ #else
|
|
|
|
+ vec3 normal = getNormal(texCoord);
|
|
|
|
+ #endif
|
|
|
|
+
|
|
vec2 rand = getRandom(texCoord);
|
|
vec2 rand = getRandom(texCoord);
|
|
|
|
|
|
float ao = 0.0;
|
|
float ao = 0.0;
|
|
@@ -85,5 +104,5 @@ void main(){
|
|
ao /= float(iterations) * 4.0;
|
|
ao /= float(iterations) * 4.0;
|
|
result = 1.0 - ao;
|
|
result = 1.0 - ao;
|
|
|
|
|
|
- gl_FragColor=vec4(result);
|
|
|
|
|
|
+ gl_FragColor = vec4(result);
|
|
}
|
|
}
|