SHARE
TWEET

Untitled

a guest Sep 7th, 2014 205 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifdef GL_ES
  2.         precision highp float;
  3. #endif
  4.  
  5. uniform float exposure;
  6. uniform float decay;
  7. uniform float density;
  8. uniform float weight;
  9. uniform vec2 lightPositionOnScreen;
  10.  
  11. uniform sampler2D depthTexture;
  12. uniform sampler2D u_prevFrame;
  13.  
  14. uniform mat4 u_MVP;
  15. uniform mat4 u_invViewProj;
  16. uniform mat4 u_prevFrameMVP;
  17. uniform mat4 u_curInvMvp;
  18.  
  19. uniform vec2 jitter;
  20.  
  21. uniform float width;
  22. uniform float height;
  23.  
  24. uniform float frameCounter;
  25.  
  26. const float ITERATION_SIZE = 60;
  27.  
  28. varying vec2 v_texCoord0;
  29.  
  30. // unpack float-packed depth
  31. float unpack (vec4 colour)
  32. {
  33.     const vec4 bitShifts = vec4(1.0 / (256.0 * 256.0 * 256.0),
  34.                                 1.0 / (256.0 * 256.0),
  35.                                 1.0 / 256.0,
  36.                                 1);
  37.  
  38.     return dot(colour , bitShifts);
  39. }
  40.  
  41. float getDepth(vec2 texCoord)
  42. {
  43.     return unpack(texture2D(depthTexture, texCoord));
  44. }
  45.  
  46. // reproject coordinates to previous frame
  47. vec3 getLastFrameScreenCoords(vec2 texCoord, float currentDepth)
  48. {
  49.     // TODO: precalculate at CPU
  50.     vec4 currentWorldCoords = u_prevFrameMVP * (u_curInvMvp * vec4(texCoord, currentDepth, 1));
  51.  
  52.     return currentWorldCoords.xyz / currentWorldCoords.w;
  53. }
  54.  
  55. // calculate god ray from depth map
  56. float calcGodRay(vec2 texCoord)
  57. {
  58.     float sample = unpack(texture2D(depthTexture, texCoord));
  59.     sample = step(0.99999, sample);
  60.  
  61.     return sample;
  62. }
  63.  
  64. // fetched from the internets
  65. float rand(vec2 co){
  66.     return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
  67. }
  68.  
  69. void main()
  70. {
  71.     // get (albeit low-precision) depth
  72.     float currentDepth = getDepth(v_texCoord0);
  73.  
  74.     // get last frame coordinates of our current uv's
  75.     vec3 lastFrameScreenCoords = getLastFrameScreenCoords(v_texCoord0 * 2.0 - 1.0, currentDepth * 2.0 - 1.0);
  76.  
  77.     // set up some variables...
  78.     float accumulatedGodray = 0.0;
  79.     vec2 screenSpaceDistance = vec2(v_texCoord0.st - lightPositionOnScreen.xy);
  80.     float sampleLength = length(screenSpaceDistance);
  81.     vec2 textureToLightPosNormal = normalize(screenSpaceDistance);
  82.     vec2 k = textureToLightPosNormal * sampleLength;
  83.  
  84.     // previous frame
  85.     vec4 prevFrameData = texture2D(u_prevFrame, lastFrameScreenCoords.xy / 2.0 + 0.5);
  86.  
  87.     // offset raymarching start point
  88.     vec2 startPos = v_texCoord0 - (fract(jitter.x + rand(v_texCoord0 + jitter.y))) * screenSpaceDistance;
  89.     // calculate remaining distance
  90.     vec2 remainingDistance = startPos - lightPositionOnScreen.xy;
  91.  
  92.     // raymarch
  93.     // TODO: kalle_h: binary occluder depth map generation
  94.     // TODO: store map @ rgb565 red channel
  95.     for (float i = 0; i < ITERATION_SIZE; i += 1)
  96.     {
  97.         vec2 sampleDistance = startPos - (i / ITERATION_SIZE) * remainingDistance;
  98.         float godRay = calcGodRay(sampleDistance);
  99.         accumulatedGodray += godRay;
  100.     }
  101.  
  102.     // last frame data
  103.     float prevFrameAccumulatedGodray = prevFrameData.x;
  104.     // difference in z
  105.     float zDist = distance(lastFrameScreenCoords.z / 2.0 + 0.5, prevFrameData.y);
  106.  
  107.     float lambda = 0;
  108.  
  109.     // handle out of bounds uv
  110.     // TODO: uv's larger than target
  111.     if (sign(lastFrameScreenCoords.x) / 2.0 + 0.5 < 0 || sign(lastFrameScreenCoords.y) / 2.0 + 0.5 < 0)
  112.     {
  113.        lambda = 0;
  114.     }
  115.     else
  116.     {
  117.         lambda = 0.4;
  118.     }
  119.  
  120.     // cache hit / cache miss ?
  121.     float cacheResult = step(1.0 / 256.0, zDist);
  122.     const float probability = ITERATION_SIZE;
  123.  
  124.     if (cacheResult == 0.0)
  125.     {
  126.         // combine last frame with current frame
  127.         accumulatedGodray = mix(prevFrameAccumulatedGodray,accumulatedGodray / probability, lambda);
  128.     }
  129.     else
  130.     {
  131.         // cache miss, use only current frame
  132.         accumulatedGodray = (accumulatedGodray / probability);
  133.     }
  134.  
  135.     // clamp samples
  136.     accumulatedGodray *= step(7.0 / 256.0, accumulatedGodray);
  137.  
  138.     gl_FragColor = vec4(accumulatedGodray, currentDepth, 0, 0);
  139. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top