Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.62 KB | None | 0 0
  1. precision highp float;
  2.  
  3. uniform sampler2D depthtex;
  4. uniform sampler2D normaltex;
  5. uniform sampler2D indextex;
  6. uniform sampler2D tangenttex;
  7.  
  8. uniform float xdim; //1/sw
  9. uniform float ydim; //1/sh
  10.  
  11. uniform vec3 samples[8];
  12. varying vec2 scrpos;
  13.  
  14. uniform vec4 MVP1;
  15. uniform vec4 MVP2;
  16. uniform vec4 MVP3;
  17. uniform vec4 MVP4;
  18. highp float getDepth(highp vec2 pos)
  19. {
  20.  
  21.     highp vec4 packedZValue = texture2D(depthtex, pos);
  22.  
  23.     const highp vec4 bitShifts = vec4(1.0 / (256.0 * 256.0 * 256.0),
  24.                         1.0 / (256.0 * 256.0),
  25.                         1.0 / 256.0,
  26.                         1.0);
  27.     highp float shadow = dot(packedZValue , bitShifts);
  28.  
  29.     return shadow;
  30. }
  31.  
  32. vec3 dirfromtex(sampler2D ttx, vec2 coord)
  33. {
  34.     vec3 cl = texture2D(ttx, coord).xyz;
  35.     cl = cl - 0.5;
  36.     cl = cl * 2.0;
  37. //  0..1 =>  -0.5..0.5 => -1.0..1.0
  38. return cl;
  39. }
  40.  
  41.  
  42. float dp43(vec4 matrow, vec3 p)
  43. {
  44. return ( (matrow.x*p.x) + (matrow.y*p.y) + (matrow.z*p.z) + matrow.w );
  45. }
  46.  
  47.  
  48. float dp33(vec3 matrow, vec3 p)
  49. {
  50. return matrow.x*p.x + matrow.y*p.y + matrow.z*p.z;
  51. }
  52.  
  53. vec3 vectorAB( vec3 A,  vec3 B)
  54. {
  55. return B-A;
  56. }
  57.  
  58. const float imopi          = 0.01745329251;
  59. uniform vec3 dirX;
  60. uniform vec3 dirY;
  61. uniform int sw;
  62. uniform int sh;
  63. uniform float fov;
  64. uniform float z_near;
  65. uniform float z_far;
  66. uniform vec3 campos;
  67. uniform vec3 front_vec;
  68.  
  69.  
  70. vec3 GetWorldPosFromScreen(vec2 scrcoord, float depth)
  71. {
  72.  
  73.  
  74.     float aspect = float (sw) / float (sh);
  75.     float a = fov / 2.0;
  76. float cotangent = 1.0 / tan( a * imopi );
  77.  
  78. float ax = z_near / cotangent;
  79.  
  80. float screen_w = 2.0*ax;
  81.  
  82. float screen_h = screen_w;
  83.  
  84. screen_w = screen_w * aspect;
  85.  
  86. float scr_coord_x = scrcoord.x;
  87. float scr_coord_y = 1.0-scrcoord.y;
  88.  
  89.  
  90. vec3 dir = front_vec;
  91.  
  92. //move to lower left corner
  93. vec3 start_pos = (campos + dir * z_near) + (-dirX * (screen_w / 2.0)) + (-dirY * (screen_h/2.0));
  94.  
  95. vec3 start = start_pos + (dirX * (screen_w * scr_coord_x)) + (dirY * (screen_h * scr_coord_y));
  96.  
  97. vec3 ray = normalize( vectorAB(campos, start) );
  98.  
  99.  
  100. return campos+ray*(z_far*depth);
  101. }
  102.  
  103.  
  104. const float ssao_range = 10.0;
  105.  
  106. bool almostthesame(vec3 v1, vec3 v2, float diff)
  107. {
  108.     float x= abs(v1.x-v2.x);
  109.     float y= abs(v1.y-v2.y);
  110.     float z= abs(v1.z-v2.z);
  111.     return ( (x <= diff) && (y <= diff) && (z <= diff) );
  112. }
  113.  
  114. void main()
  115. {
  116.  
  117. float sample_depth = getDepth(scrpos);
  118.  
  119. if (sample_depth > 0.7) //zfar is like 10000 units
  120. {
  121.     gl_FragColor = vec4(1.0, 1.0, 1.0, 0.0); //leave it untouched
  122.     return;
  123. }
  124. vec3 frag_normal = dirfromtex(normaltex, scrpos); //front
  125. vec3 frag_color = texture2D(indextex, scrpos).xyz;
  126. //get the world position from depthmap
  127. //vec3 world_pos = GetWorldPosFromScreen(scrpos, sample_depth);
  128. /*
  129. //get normal and tangent
  130. vec3 sample_front = dirfromtex(normaltex, scrpos); //front
  131. vec3 sample_right = dirfromtex(tangenttex, scrpos); //let it be right
  132. vec3 sample_up = cross(sample_front, sample_right); //up
  133.  
  134. //compute rotation matrix
  135. vec3 tbn1 = vec3(sample_right.x, sample_up.x, sample_front.x);
  136. vec3 tbn2 = vec3(sample_right.y, sample_up.y, sample_front.y);
  137. vec3 tbn3 = vec3(sample_right.z, sample_up.z, sample_front.z);
  138.  
  139. //now we can rotate kernel samples by it
  140.  
  141. float bias = 0.001 / 10000.0;//0.0005;
  142.  
  143.  
  144. //float radius = 0.5;
  145.  
  146. //float xdim = 1.0 / sw;
  147. //float ydim = 1.0 / sh;
  148. */
  149. int num_samples = 8;
  150. float occlusion =  float ( num_samples );
  151. float bias = 0.01 / 10000.0;
  152. gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
  153. for (int i=0; i < num_samples; i++)
  154. {
  155.     /*
  156.     //rotate kernel sample
  157.     vec3 rot_sample_dir;
  158. rot_sample_dir.x = dp33(tbn1, samples[i]);
  159. rot_sample_dir.y = dp33(tbn2, samples[i]);
  160. rot_sample_dir.z = dp33(tbn3, samples[i]);
  161.  
  162.  
  163. //now we have to define ssao range, this means in what distance ssao occurs
  164. //since x units can define y mm or cm or meters we calculate the distance on cpu
  165. vec3 sample_pos = world_pos + rot_sample_dir*ssao_range;
  166.  
  167. //given new world position compute scr coord from it
  168. vec4 sclip;
  169. sclip.x = dp43(MVP1, sample_pos);
  170. sclip.y = dp43(MVP2, sample_pos);
  171. sclip.z = dp43(MVP3, sample_pos);
  172. sclip.w = dp43(MVP4, sample_pos);
  173.  
  174. sclip.xyz = sclip.xyz / sclip.w;
  175. sclip.xyz = sclip.xyz * 0.5 + 0.5;
  176. //we should add an exception if outer sample depth is infinity
  177. //sample_pos = world_pos + sample[i]*vec3(xdim
  178. */
  179. vec3 sclip =
  180. vec3(scrpos,0.0) + samples[i]*(vec3(xdim,ydim,0.0) * 1.5);
  181. //vec3(xdim/2.0,ydim/2.0,0.0);
  182. //float outer_sample_depth = getDepth(sclip.xy);
  183. vec3 sample_color = texture2D(indextex, sclip.xy).xyz;
  184. vec3 sample_normal = dirfromtex(normaltex, sclip.xy); //front
  185. 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
  186. bool same_normal = almostthesame(frag_normal, sample_normal, 1.0/120.0); //-127..127 representation but we want to loose some accy
  187. bool processed = false;
  188.  
  189. if ((same_object) && (!same_normal))
  190. {
  191.     gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
  192.     return;
  193.     occlusion = occlusion - 1.0;
  194.     processed = true;
  195. }
  196. if ((!same_object) && (same_normal))
  197. {
  198.         gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
  199.     return;
  200. occlusion = occlusion - 1.0;
  201.     processed = true;
  202. }
  203.  
  204. if ((!same_object) && (!same_normal))
  205. {
  206.         gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
  207.     return;
  208. occlusion = occlusion - 1.0;
  209.     processed = true;
  210. }
  211.  
  212.  
  213. //occlusion += (outer_sample_depth >= sample_depth ? 1.0 : 0.0);
  214. //else occlusion = occlusion + 1.0;
  215. }  
  216.  
  217. occlusion = occlusion / float ( num_samples );
  218. if (occlusion < 0.998) occlusion = 0.0;
  219. gl_FragColor = vec4(occlusion, occlusion, occlusion, 1.0-occlusion);
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement