SHARE
TWEET

Untitled

a guest Sep 7th, 2014 184 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 = 8;
  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.     vec4 currentWorldCoords = u_prevFrameMVP * u_curInvMvp * vec4(texCoord, currentDepth, 1) ;
  50.  
  51.     return currentWorldCoords.xyz / currentWorldCoords.w;
  52. }
  53.  
  54. // calculate god ray from depth map
  55. float calcGodRay(vec2 texCoord)
  56. {
  57.     float sample = unpack(texture2D(depthTexture, texCoord));
  58.     sample = step(0.99999, sample);
  59.  
  60.     return sample;
  61. }
  62.  
  63. // fetched from the internets
  64. float rand(vec2 co){
  65.     return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
  66. }
  67.  
  68. void main()
  69. {
  70.     // get (albeit low-precision) depth
  71.     float currentDepth = getDepth(v_texCoord0);
  72.  
  73.     // get last frame coordinates of our current uv's
  74.     vec3 lastFrameScreenCoords = getLastFrameScreenCoords(v_texCoord0 * 2.0 - 1.0, currentDepth * 2.0 - 1.0);
  75.  
  76.     // set up some variables...
  77.     float accumulatedGodray = 0.0;
  78.     vec2 screenSpaceDistance = vec2(v_texCoord0.st - lightPositionOnScreen.xy);
  79.     float sampleLength = length(screenSpaceDistance);
  80.     vec2 textureToLightPosNormal = normalize(screenSpaceDistance);
  81.     vec2 k = textureToLightPosNormal * sampleLength;
  82.  
  83.     // previous frame
  84.     vec4 prevFrameData = texture2D(u_prevFrame, v_texCoord0.xy / 2.0 + 0.5);
  85.  
  86.     // offset raymarching start point
  87.     vec2 startPos = v_texCoord0 - rand(jitter*screenSpaceDistance*1000)*screenSpaceDistance;
  88.     // calculate remaining distance
  89.     vec2 remainingDistance = v_texCoord0 - screenSpaceDistance - startPos;
  90.  
  91.     // raymarch
  92.     for (float i = 0; i < ITERATION_SIZE; i += 1)
  93.     {
  94.         vec2 sampleDistance = startPos + (i / ITERATION_SIZE) * remainingDistance;
  95.         float godRay = calcGodRay(sampleDistance);
  96.         accumulatedGodray += godRay;
  97.     }
  98.  
  99.     // last frame data
  100.     float prevFrameAccumulatedGodray = prevFrameData.x;
  101.     // difference in z
  102.     float zDist = distance(lastFrameScreenCoords.z / 2.0 + 0.5, prevFrameData.y);
  103.  
  104.     float lambda = 0;
  105.  
  106.     // TODO: optimize branching
  107.     if (sign(lastFrameScreenCoords.x) / 2.0 + 0.5 < 0 || sign(lastFrameScreenCoords.y) / 2.0 + 0.5 < 0)
  108.     {
  109.        lambda = 0;
  110.     }
  111.     else
  112.     {
  113.         lambda = 0.75;
  114.     }
  115.  
  116.     // cache hit / cache miss ?
  117.     float cacheResult = step(1.0 / 256.0, zDist);
  118.     const float probability = ITERATION_SIZE;
  119.  
  120.     if (cacheResult == 0.0)
  121.     {
  122.         // combine last frame with current frame
  123.         accumulatedGodray = lambda * prevFrameAccumulatedGodray + (1.0 - lambda) * (accumulatedGodray / probability);
  124.     }
  125.     else
  126.     {
  127.         // cache miss, use only current frame
  128.         accumulatedGodray = (accumulatedGodray / probability);
  129.     }
  130.  
  131.     // clamp samples
  132.     accumulatedGodray *= step(7.0 / 256.0, accumulatedGodray);
  133.  
  134.     gl_FragColor = vec4(accumulatedGodray, currentDepth, 0, 0);
  135. }
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