Guest User

Untitled

a guest
Sep 4th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  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 sampler2D depthTexture;
  10. uniform mat4 u_invMVP;
  11.  
  12. varying vec2 v_sphereCenterScreenSpace;
  13. varying vec4 v_position;
  14. varying vec3 v_normal;
  15.  
  16. uniform vec3 u_sphereCenter;
  17.  
  18. uniform float u_sphereRadius;
  19.  
  20. const float NUM_SAMPLES = 80;
  21.  
  22. varying vec3 v_worldPos;
  23. varying vec2 v_texCoord0;
  24.  
  25. // unpack float-packed depth
  26. float unpack (vec4 colour)
  27. {
  28. const vec4 bitShifts = vec4(1.0 / (256.0 * 256.0 * 256.0),
  29. 1.0 / (256.0 * 256.0),
  30. 1.0 / 256.0,
  31. 1);
  32.  
  33. return dot(colour , bitShifts);
  34. }
  35.  
  36. // calculate god ray from depth map
  37. float getShadow(vec2 texCoord)
  38. {
  39. float sample = unpack(texture2D(depthTexture, texCoord));
  40.  
  41. return step(0.9999, sample);
  42.  
  43. vec4 sampleWorldPos = u_invMVP * vec4(texCoord * 2 - 1, sample * 2 - 1, 1);
  44.  
  45. float dist = distance(sampleWorldPos.xyz / sampleWorldPos.w, u_sphereCenter);
  46.  
  47. return smoothstep(0, 50, dist);
  48. }
  49.  
  50. void main()
  51. {
  52. // get depth value at screen-space fragment position
  53. vec2 normalizedScreenCoords = (vec2(v_position.x, v_position.y) / v_position.w) / 2.0 + 0.5;
  54.  
  55. vec2 textureSphereCenter = v_sphereCenterScreenSpace / 2.0 + 0.5;
  56.  
  57. vec2 direction = normalizedScreenCoords - textureSphereCenter;
  58.  
  59. float distance = length(direction);
  60.  
  61. vec2 directionNormal = normalize(direction);
  62.  
  63. float normalizedScreenDepth = v_position.z / v_position.w;
  64.  
  65. // normalize interpolated normal
  66. vec3 N = normalize(v_normal);
  67.  
  68. float accumulatedGodRay = 0;
  69.  
  70. for (float i = 0; i < NUM_SAMPLES; i++)
  71. {
  72. accumulatedGodRay += 1.0 - getShadow(normalizedScreenCoords - (i / NUM_SAMPLES) * directionNormal
  73. * distance);
  74. }
  75.  
  76. accumulatedGodRay /= NUM_SAMPLES;
  77.  
  78. gl_FragColor = vec4(pow(accumulatedGodRay,1)) * pow(unpack(texture2D(depthTexture, normalizedScreenCoords)), 4);
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment