Skip to content

Instantly share code, notes, and snippets.

@fkaa
Created January 24, 2014 18:26
Show Gist options
  • Select an option

  • Save fkaa/8603135 to your computer and use it in GitHub Desktop.

Select an option

Save fkaa/8603135 to your computer and use it in GitHub Desktop.
#pragma mark -
#pragma mark Default shader
/***
* ____ _ _
* / ___|| |__ __ _ __| | ___ _ __
* \___ \| '_ \ / _` |/ _` |/ _ \ '__|
* ___) | | | | (_| | (_| | __/ |
* |____/|_| |_|\__,_|\__,_|\___|_|
*
*/
const char* Game::vs_default = glsl(120,
attribute vec4 a_coord;
varying vec2 v_uv;
void main() {
v_uv = a_coord.zw;
gl_Position = vec4(a_coord.x, a_coord.y, 0., 1.);
}
);
const char* Game::fs_default = glsl(120,
uniform sampler2D u_texture;
varying vec2 v_uv;
void main() {
gl_FragColor = texture2D(u_texture, v_uv);
}
);
#pragma mark SSAO Shader
const char* Game::vs_ssao = glsl(120,
attribute vec4 a_coord;
varying vec2 v_uv;
void main() {
v_uv = a_coord.zw;
gl_Position = vec4(a_coord.x, a_coord.y, 0., 1.);
}
);
const char* Game::fs_ssao = glsl(120,
uniform sampler2D u_diffuse;
uniform sampler2D u_normal;
uniform sampler2D u_depth;
uniform mat4 inv_mat;
uniform float total_strength = .5;
uniform float falloff = 0.005;
uniform float radius = 0.2;
uniform float base = 0.;
uniform float area = 0.0000075;
varying vec2 v_uv;
const vec3 rvalues[16] = vec3[](
vec3(0.53812504, 0.18565957, -0.43192),
vec3(0.13790712, 0.24864247, 0.44301823),
vec3(0.33715037, 0.56794053, -0.005789503),
vec3(-0.6999805, -0.04511441, -0.0019965635),
vec3(0.06896307, -0.15983082, -0.85477847),
vec3(0.056099437, 0.006954967, -0.1843352),
vec3(-0.014653638, 0.14027752, 0.0762037),
vec3(0.010019933, -0.1924225, -0.034443386),
vec3(-0.35775623, -0.5301969, -0.43581226),
vec3(-0.3169221, 0.106360726, 0.015860917),
vec3(0.010350345, -0.58698344, 0.0046293875),
vec3(-0.08972908, -0.49408212, 0.3287904),
vec3(0.7119986, -0.0154690035, -0.09183723),
vec3(-0.053382345, 0.059675813, -0.5411899),
vec3(0.035267662, -0.063188605, 0.54602677),
vec3(-0.47761092, 0.2847911, -0.0271716)
);
const int SAMPLES = 10;
float linear(sampler2D sampler, vec2 uv) {
float n = 1.0;
float f = 1000.0;
float z = texture2D(sampler, uv).x;
return (2.0 * n) / (f + n - z * (f - n));
}
float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
void main() {
vec4 diffuse = texture2D(u_diffuse, v_uv);
vec4 normal = texture2D(u_normal, v_uv);
float depth = linear(u_depth, v_uv);
vec3 rand = vec3(rand(v_uv), rand(1. - v_uv), rand(.5 * v_uv)) * .6;
vec3 pos = vec3(v_uv.xy, depth);
vec3 norm = normal.xyz;
float occlusion = 0.;
float radius_depth = radius / depth;
for (int i = 0; i < SAMPLES; i++) {
vec3 ray = radius_depth * reflect(rvalues[i], rand);
vec3 hemi_ray = pos + sign(dot(ray, norm)) * ray;
float occluder_depth = linear(u_depth, clamp(hemi_ray.xy, 0., 1.));
float difference = depth - occluder_depth;
occlusion += step(falloff, difference) * (1. - smoothstep(falloff, area, difference));
}
float ao = 1. - total_strength * occlusion * (1. / SAMPLES);
gl_FragColor = vec4(vec3(clamp(ao, 0., 1.)), 1.) * diffuse;
}
);
#pragma mark -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment