Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- precision highp float;
- uniform sampler2D depthtex;
- uniform sampler2D normaltex;
- uniform sampler2D indextex;
- uniform sampler2D tangenttex;
- uniform float xdim; //1/sw
- uniform float ydim; //1/sh
- uniform vec3 samples[8];
- varying vec2 scrpos;
- uniform vec4 MVP1;
- uniform vec4 MVP2;
- uniform vec4 MVP3;
- uniform vec4 MVP4;
- highp float getDepth(highp vec2 pos)
- {
- highp vec4 packedZValue = texture2D(depthtex, pos);
- const highp vec4 bitShifts = vec4(1.0 / (256.0 * 256.0 * 256.0),
- 1.0 / (256.0 * 256.0),
- 1.0 / 256.0,
- 1.0);
- highp float shadow = dot(packedZValue , bitShifts);
- return shadow;
- }
- vec3 dirfromtex(sampler2D ttx, vec2 coord)
- {
- vec3 cl = texture2D(ttx, coord).xyz;
- cl = cl - 0.5;
- cl = cl * 2.0;
- // 0..1 => -0.5..0.5 => -1.0..1.0
- return cl;
- }
- float dp43(vec4 matrow, vec3 p)
- {
- return ( (matrow.x*p.x) + (matrow.y*p.y) + (matrow.z*p.z) + matrow.w );
- }
- float dp33(vec3 matrow, vec3 p)
- {
- return matrow.x*p.x + matrow.y*p.y + matrow.z*p.z;
- }
- vec3 vectorAB( vec3 A, vec3 B)
- {
- return B-A;
- }
- const float imopi = 0.01745329251;
- uniform vec3 dirX;
- uniform vec3 dirY;
- uniform int sw;
- uniform int sh;
- uniform float fov;
- uniform float z_near;
- uniform float z_far;
- uniform vec3 campos;
- uniform vec3 front_vec;
- vec3 GetWorldPosFromScreen(vec2 scrcoord, float depth)
- {
- float aspect = float (sw) / float (sh);
- float a = fov / 2.0;
- float cotangent = 1.0 / tan( a * imopi );
- float ax = z_near / cotangent;
- float screen_w = 2.0*ax;
- float screen_h = screen_w;
- screen_w = screen_w * aspect;
- float scr_coord_x = scrcoord.x;
- float scr_coord_y = 1.0-scrcoord.y;
- vec3 dir = front_vec;
- //move to lower left corner
- vec3 start_pos = (campos + dir * z_near) + (-dirX * (screen_w / 2.0)) + (-dirY * (screen_h/2.0));
- vec3 start = start_pos + (dirX * (screen_w * scr_coord_x)) + (dirY * (screen_h * scr_coord_y));
- vec3 ray = normalize( vectorAB(campos, start) );
- return campos+ray*(z_far*depth);
- }
- const float ssao_range = 10.0;
- bool almostthesame(vec3 v1, vec3 v2, float diff)
- {
- float x= abs(v1.x-v2.x);
- float y= abs(v1.y-v2.y);
- float z= abs(v1.z-v2.z);
- return ( (x <= diff) && (y <= diff) && (z <= diff) );
- }
- void main()
- {
- float sample_depth = getDepth(scrpos);
- if (sample_depth > 0.7) //zfar is like 10000 units
- {
- gl_FragColor = vec4(1.0, 1.0, 1.0, 0.0); //leave it untouched
- return;
- }
- vec3 frag_normal = dirfromtex(normaltex, scrpos); //front
- vec3 frag_color = texture2D(indextex, scrpos).xyz;
- //get the world position from depthmap
- //vec3 world_pos = GetWorldPosFromScreen(scrpos, sample_depth);
- /*
- //get normal and tangent
- vec3 sample_front = dirfromtex(normaltex, scrpos); //front
- vec3 sample_right = dirfromtex(tangenttex, scrpos); //let it be right
- vec3 sample_up = cross(sample_front, sample_right); //up
- //compute rotation matrix
- vec3 tbn1 = vec3(sample_right.x, sample_up.x, sample_front.x);
- vec3 tbn2 = vec3(sample_right.y, sample_up.y, sample_front.y);
- vec3 tbn3 = vec3(sample_right.z, sample_up.z, sample_front.z);
- //now we can rotate kernel samples by it
- float bias = 0.001 / 10000.0;//0.0005;
- //float radius = 0.5;
- //float xdim = 1.0 / sw;
- //float ydim = 1.0 / sh;
- */
- int num_samples = 8;
- float occlusion = float ( num_samples );
- float bias = 0.01 / 10000.0;
- gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
- for (int i=0; i < num_samples; i++)
- {
- /*
- //rotate kernel sample
- vec3 rot_sample_dir;
- rot_sample_dir.x = dp33(tbn1, samples[i]);
- rot_sample_dir.y = dp33(tbn2, samples[i]);
- rot_sample_dir.z = dp33(tbn3, samples[i]);
- //now we have to define ssao range, this means in what distance ssao occurs
- //since x units can define y mm or cm or meters we calculate the distance on cpu
- vec3 sample_pos = world_pos + rot_sample_dir*ssao_range;
- //given new world position compute scr coord from it
- vec4 sclip;
- sclip.x = dp43(MVP1, sample_pos);
- sclip.y = dp43(MVP2, sample_pos);
- sclip.z = dp43(MVP3, sample_pos);
- sclip.w = dp43(MVP4, sample_pos);
- sclip.xyz = sclip.xyz / sclip.w;
- sclip.xyz = sclip.xyz * 0.5 + 0.5;
- //we should add an exception if outer sample depth is infinity
- //sample_pos = world_pos + sample[i]*vec3(xdim
- */
- vec3 sclip =
- vec3(scrpos,0.0) + samples[i]*(vec3(xdim,ydim,0.0) * 1.5);
- //vec3(xdim/2.0,ydim/2.0,0.0);
- //float outer_sample_depth = getDepth(sclip.xy);
- vec3 sample_color = texture2D(indextex, sclip.xy).xyz;
- vec3 sample_normal = dirfromtex(normaltex, sclip.xy); //front
- bool same_object = almostthesame(frag_color, sample_color, 1.0/255.0); //dont shade the same surface, this has to be at leastt 8bit color component representation to work why its 255 not 256 because max unsigned char is 255 and we accomodate 0 in this equation too
- bool same_normal = almostthesame(frag_normal, sample_normal, 1.0/120.0); //-127..127 representation but we want to loose some accy
- bool processed = false;
- if ((same_object) && (!same_normal))
- {
- gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
- return;
- occlusion = occlusion - 1.0;
- processed = true;
- }
- if ((!same_object) && (same_normal))
- {
- gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
- return;
- occlusion = occlusion - 1.0;
- processed = true;
- }
- if ((!same_object) && (!same_normal))
- {
- gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
- return;
- occlusion = occlusion - 1.0;
- processed = true;
- }
- //occlusion += (outer_sample_depth >= sample_depth ? 1.0 : 0.0);
- //else occlusion = occlusion + 1.0;
- }
- occlusion = occlusion / float ( num_samples );
- if (occlusion < 0.998) occlusion = 0.0;
- gl_FragColor = vec4(occlusion, occlusion, occlusion, 1.0-occlusion);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement