1
0
Fork 0

Screen space ambient occlusion effect

This commit is contained in:
Frederic Bouvier 2012-05-09 23:51:26 +02:00
parent c5732c38ba
commit 140263d611
5 changed files with 175 additions and 0 deletions

10
Effects/ssao-blur-1.eff Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<PropertyList>
<name>Effects/ssao-blur-1</name>
<inherits-from>Effects/blur</inherits-from>
<parameters>
<blurOffset_x>4.0</blurOffset_x>
<blurOffset_y>0.0</blurOffset_y>
<buffer-name>ao-1</buffer-name>
</parameters>
</PropertyList>

10
Effects/ssao-blur-2.eff Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<PropertyList>
<name>Effects/ssao-blur-2</name>
<inherits-from>Effects/blur</inherits-from>
<parameters>
<blurOffset_x>0.0</blurOffset_x>
<blurOffset_y>4.0</blurOffset_y>
<buffer-name>ao-2</buffer-name>
</parameters>
</PropertyList>

84
Effects/ssao.eff Normal file
View file

@ -0,0 +1,84 @@
<?xml version="1.0" encoding="utf-8"?>
<PropertyList>
<name>Effects/ssao</name>
<parameters>
<g_scale type="float">1.0</g_scale>
<g_bias type="float">0.0</g_bias>
<g_intensity type="float">7.0</g_intensity>
<g_sample_rad type="float">0.03</g_sample_rad>
<random_size type="float">800.0</random_size>
</parameters>
<technique n="11">
<pass>
<texture-unit>
<unit>0</unit>
<type>buffer</type>
<name>depth</name>
</texture-unit>
<texture-unit>
<unit>1</unit>
<type>buffer</type>
<name>normal</name>
</texture-unit>
<texture-unit>
<unit>2</unit>
<type>buffer</type>
<name>spec-emis</name>
</texture-unit>
<texture-unit>
<unit>3</unit>
<type>noise</type>
</texture-unit>
<program>
<vertex-shader>Shaders/ssao.vert</vertex-shader>
<fragment-shader>Shaders/ssao.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
</program>
<uniform>
<name>depth_tex</name>
<type>sampler-2d</type>
<value type="int">0</value>
</uniform>
<uniform>
<name>normal_tex</name>
<type>sampler-2d</type>
<value type="int">1</value>
</uniform>
<uniform>
<name>spec_emis_tex</name>
<type>sampler-2d</type>
<value type="int">2</value>
</uniform>
<uniform>
<name>noise_tex</name>
<type>sampler-2d</type>
<value type="int">3</value>
</uniform>
<uniform>
<name>g_scale</name>
<type>float</type>
<value><use>g_scale</use></value>
</uniform>
<uniform>
<name>g_bias</name>
<type>float</type>
<value><use>g_bias</use></value>
</uniform>
<uniform>
<name>g_intensity</name>
<type>float</type>
<value><use>g_intensity</use></value>
</uniform>
<uniform>
<name>g_sample_rad</name>
<type>float</type>
<value><use>g_sample_rad</use></value>
</uniform>
<uniform>
<name>random_size</name>
<type>float</type>
<value><use>random_size</use></value>
</uniform>
</pass>
</technique>
</PropertyList>

64
Shaders/ssao.frag Normal file
View file

@ -0,0 +1,64 @@
#version 120
#extension GL_EXT_gpu_shader4 : enable
uniform sampler2D normal_tex;
uniform sampler2D depth_tex;
uniform sampler2D spec_emis_tex;
uniform sampler3D noise_tex;
uniform vec2 fg_BufferSize;
uniform vec3 fg_Planes;
uniform vec4 fg_du;
uniform vec4 fg_dv;
uniform float g_scale;
uniform float g_bias;
uniform float g_intensity;
uniform float g_sample_rad;
uniform float random_size;
uniform unsigned int osg_FrameNumber;
varying vec4 ray;
const vec2 v[4] = vec2[](vec2(1.0,0.0),vec2(-1.0,0.0),vec2(0.0,1.0),vec2(0.0,-1.0));
vec3 position( vec3 viewDir, vec2 coords, sampler2D depth_tex );
vec3 normal_decode(vec2 enc);
vec2 getRandom( in vec2 uv ) {
unsigned int level = osg_FrameNumber - ((osg_FrameNumber / 64U) * 64U);
return normalize( texture3D( noise_tex, vec3(uv*50.0, float(level) / 64.0) ).xy * 0.14 - 0.07 );
}
vec3 getPosition(in vec2 uv, in vec2 uv0, in vec4 ray0) {
vec2 duv = uv - uv0;
vec4 ray = ray0 + fg_du * duv.x + fg_dv * duv.y;
vec3 viewDir = normalize( ray.xyz );
return position(viewDir, uv, depth_tex);
}
float doAmbientOcclusion(in vec2 tcoord, in vec2 uv, in vec3 p, in vec3 cnorm, in vec4 ray) {
vec3 diff = getPosition(tcoord+uv,tcoord,ray)-p;
float d = length(diff);
vec3 v = diff / d;
d *= g_scale;
return max(0.0, dot( cnorm,v ) - g_bias) * (1.0/(1.0+d)) * g_intensity;
}
void main() {
vec2 coords = gl_TexCoord[0].xy;
float initialized = texture2D( spec_emis_tex, coords ).a;
if ( initialized < 0.1 )
discard;
vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg);
vec3 viewDir = normalize(ray.xyz);
vec3 pos = position(viewDir, coords, depth_tex);
vec2 rand = getRandom(coords);
float ao = 0.0;
float rad = g_sample_rad;
int iterations = 4;
for (int j = 0; j < 1; ++j ) {
vec2 coord1 = reflect( v[j], rand ) * rad;
vec2 coord2 = vec2( coord1.x*0.707 - coord1.y*0.707, coord1.x*0.707 + coord1.y*0.707 );
ao += doAmbientOcclusion(coords,coord1*0.25,pos,normal,ray);
ao += doAmbientOcclusion(coords,coord2*0.5,pos,normal,ray);
ao += doAmbientOcclusion(coords,coord1*0.75,pos,normal,ray);
ao += doAmbientOcclusion(coords,coord2,pos,normal,ray);
}
ao /= 16.0;
gl_FragColor = vec4( vec3(1.0 - ao), 1.0 );
}

7
Shaders/ssao.vert Normal file
View file

@ -0,0 +1,7 @@
uniform mat4 fg_ProjectionMatrixInverse;
varying vec4 ray;
void main() {
gl_Position = gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
ray = fg_ProjectionMatrixInverse * gl_Vertex;
}