Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ssao3.frag
- // sokadik ssao kiserlet
- //
- // Caiwan / IR
- // 2012
- uniform sampler2D tex0;
- uniform sampler2D normalTex;
- uniform sampler2D depthTex;
- uniform sampler2D noiseTex;
- uniform vec2 resolution;
- uniform float near;
- uniform float far;
- uniform float fov;
- uniform float noiseSize;
- uniform mat4 projectionMatrix;
- float aspectRatio;
- vec2 noiseScale = vec2(3.,3.);
- uniform vec3 kernel[32];
- const int kernelSize = 8;
- const float radius = .1;
- //http://www.john-chapman.net/content.php?id=8
- vec2 getTexcoord(vec2 uv){
- vec2 uv_ = uv;
- uv_.s = clamp(uv.s * (resolution.x/2048.), 0., 1.);
- uv_.t = clamp(uv.t * (resolution.y/2048.), 0., 1.); //anyad
- return uv_;
- }
- vec3 getNormal(vec2 uv){
- return (2.*texture2D(normalTex, getTexcoord(uv)).xyz)-1.; // anyad geci
- }
- vec3 getNormal2(){
- return (2.*texture2D(normalTex, gl_TexCoord[0].st).xyz)-1.;
- }
- float getLinearDepth(float zz, float _near, float _far){
- float z = 2.*_far*_near/((_far-_near)*(zz-(_far+_near)/(_far-_near)));
- return -z;
- }
- float getDepth(vec2 uv){
- //float zz = float(texture2D(depthTex, getTexcoord(uv)));
- //float z = 2.*far*near/((far-near)*(zz-(far+near)/(far-near)));
- //return z;
- //return getLinearDepth(zz, near, far);
- return getLinearDepth(float(texture2D(depthTex, getTexcoord(uv))), near, far);
- }
- vec3 getViewRay(vec2 tc) {
- float hfar = 2.0 * tan(fov/2.0) * far;
- float wfar = hfar * aspectRatio;
- vec3 ray = vec3(wfar * (tc.x - 0.5), hfar * (tc.y - 0.5), -far);
- return ray;
- }
- void main (){
- vec2 screenPos = gl_FragCoord.xy/resolution;
- //screenPos.y = 1.0 - screenPos.y;
- noiseScale = noiseSize / resolution;
- aspectRatio = resolution.x / resolution.y;
- float linearDepth = getDepth(screenPos);
- vec3 origin = getViewRay(screenPos) * linearDepth;
- //vec3 normal = normalize(vNormal);
- vec3 normal = getNormal2();
- //gl_FragColor.rgb = texture2D(normalTex, gl_TexCoord[0].st).xyz;
- //gl_FragColor.a = 1.;
- //return;
- vec3 rvec = texture2D(noiseTex, screenPos.xy * noiseScale).xyz * 2.0 - 1.0;
- //vec3 rvec = texture2D(noiseTex, screenPos.xy).xyz * 2.0 - 1.0;
- vec3 tangent = normalize(rvec - normal * dot(rvec, normal));
- vec3 bitangent = cross(normal, tangent);
- mat3 tbn = mat3(tangent, bitangent, normal);
- gl_FragColor.rgb = .5*rvec+.5;
- //gl_FragColor.rgb = vec3(linearDepth/far);
- //float z = float(texture2D(depthTex, getTexcoord(screenPos)));
- //float z = texture2D(depthTex, getTexcoord(screenPos)).r;
- //float lz = getLinearDepth(z, 1., 100.)/100.;
- //gl_FragColor.rgb = vec3(lz);
- gl_FragColor.a = 1.;
- return;
- float occlusion = 0.0;
- if (linearDepth-far < 0.)
- for(int i = 0; i < kernelSize; ++i) {
- vec3 sample = origin + (tbn * kernel[i]) * radius;
- vec4 offset = projectionMatrix * vec4(sample, 1.0);
- offset.xy /= offset.w;
- offset.xy = offset.xy * 0.5 + 0.5;
- float sampleDepth = -sample.z/far;
- float depthBufferValue = getDepth(offset.xy);
- float range_check = abs(linearDepth - depthBufferValue);
- if (range_check < radius && depthBufferValue <= sampleDepth) {
- occlusion += 1.0;
- }
- }
- occlusion = 1.0 - occlusion / float(kernelSize);
- gl_FragColor.rgb = vec3(occlusion);
- gl_FragColor.a = 1.0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement