koolkat

Untitled

Dec 5th, 2011
11,542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.69 KB | None | 0 0
  1. //Credits
  2. //God Rays: Blizzard
  3. //Depth offield: Azraeil.
  4. //Bloom: CosmicSpore
  5. /*
  6. IMPORTANT
  7. In order for god rays to work you must replace this line in your gbuffers_basic.fsh
  8.  
  9. gl_FragData[1] = vec4(vec3(gl_FragCoord.z), 1.0);
  10.  
  11. with this
  12.  
  13. gl_FragData[1] = vec4(0);
  14.  
  15. */
  16. #version 120
  17.  
  18. //To disable an effect place 2 slashes before it's line
  19. #define DOF
  20. #define GODRAY
  21. #define BLOOM
  22. #define CROSSPROCESS
  23.  
  24. // If you want a higher quality blur for DOF, remove the forward slashes from the following line:
  25. //#define USE_HIGH_QUALITY_BLUR
  26.  
  27. uniform sampler2D gdepth;
  28. uniform sampler2D composite;
  29. uniform vec3 sunPosition;
  30. uniform float worldTime;
  31. uniform float aspectRatio;
  32. uniform float near;
  33. uniform float far;
  34. varying vec4 texcoord;
  35.  
  36. float getDepth(vec2 coord) {
  37.     return 2.0 * near * far / (far + near - (2.0 * texture2D(gdepth, coord).x - 1.0) * (far - near));
  38. }
  39.  
  40. #ifdef DOF
  41. // HYPERFOCAL = (Focal Distance ^ 2)/(Circle of Confusion * F Stop) + Focal Distance
  42. const float HYPERFOCAL = 3.132;
  43. const float PICONSTANT = 3.14159;
  44. vec4 getBlurredColor();
  45. vec4 getSample(vec2 coord, vec2 aspectCorrection);
  46. vec4 getSampleWithBoundsCheck(vec2 offset);
  47. float samples = 0.0;
  48. vec2 space;
  49. #endif
  50.  
  51. #ifdef GODRAY
  52. const float GR_DECAY    = 0.95;
  53. const float GR_DENSITY  = 0.5;
  54. const float GR_EXPOSURE = 0.2;  //Brightness of God-Rays
  55. const int GR_SAMPLES    = 32;   //Decrease/increase to improve performance/quality
  56. #endif
  57.  
  58. #ifdef BLOOM
  59. const float BLOOM_AMOUNT = 5;
  60. #endif
  61.  
  62. void main() {
  63.     vec4 color = texture2D(composite, texcoord.st);
  64. #ifdef DOF
  65.     float depth = getDepth(texcoord.st);
  66.        
  67.     float cursorDepth = getDepth(vec2(0.5, 0.5));
  68.    
  69.     // foreground blur = 1/2 background blur. Blur should follow exponential pattern until cursor = hyperfocal -- Cursor before hyperfocal
  70.     // Blur should go from 0 to 1/2 hyperfocal then clear to infinity -- Cursor @ hyperfocal.
  71.     // hyperfocal to inifity is clear though dof extends from 1/2 hyper to hyper -- Cursor beyond hyperfocal
  72.    
  73.     float mixAmount = 0.0;
  74.    
  75.     if (depth < cursorDepth) {
  76.         mixAmount = clamp(2.0 * ((clamp(cursorDepth, 0.0, HYPERFOCAL) - depth) / (clamp(cursorDepth, 0.0, HYPERFOCAL))), 0.0, 1.0);
  77.     } else if (cursorDepth == HYPERFOCAL) {
  78.         mixAmount = 0.0;
  79.     } else {
  80.         mixAmount =  1.0 - clamp((((cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth)) - (depth - cursorDepth)) / ((cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth)), 0.0, 1.0);
  81.     }
  82.    
  83.     if (mixAmount != 0.0) {
  84.         color = mix(color, getBlurredColor(), mixAmount);
  85.     }
  86. #endif
  87.  
  88. #ifdef GODRAY
  89.     float threshold = 0.99 * far;
  90.     bool foreground = false;
  91.     float depthGR = getDepth(texcoord.st);
  92.     if ((worldTime < 14000 || worldTime > 22000) && sunPosition.z < 0 && depthGR < threshold)
  93.         {
  94.                 vec2 lightPos = sunPosition.xy / -sunPosition.z;
  95.                 lightPos.y *= aspectRatio;
  96.                 lightPos = (lightPos + 1.0)/2.0;
  97.                 vec2 texCoord = texcoord.st;
  98.                 vec2 delta = (texCoord - lightPos) * GR_DENSITY / float(GR_SAMPLES);
  99.                 float decay = -sunPosition.z / 100.0;
  100.                 vec3 colorGR = vec3(0.0);
  101.                 for (int i = 0; i < GR_SAMPLES; i++) {
  102.                         texCoord -= delta;
  103.                         if (texCoord.x < 0.0 || texCoord.x > 1.0) {
  104.                                 if (texCoord.y < 0.0 || texCoord.y > 1.0) {
  105.                                         break;
  106.                                 }
  107.                         }
  108.                         vec3 sample = vec3(0.0);
  109.                         if (getDepth(texCoord) > threshold) {
  110.                                 sample = texture2D(composite, texCoord).rgb;
  111.                         }
  112.                         sample *= vec3(decay);
  113.                         if (distance(texCoord, lightPos) > 0.05) sample *= 0.2;
  114.                         colorGR += sample;
  115.                         decay *= GR_DECAY;
  116.                 }
  117.                 color = (color + GR_EXPOSURE * vec4(colorGR, 0.0));
  118.         }
  119. #endif
  120.  
  121. #ifdef BLOOM
  122.     int j;
  123.     int i;
  124.     vec4 sum = vec4(0);
  125.         float count = 0;
  126.     for( i= -4 ;i < 4; i++) {
  127.         for (j = -3; j < 3; j++) {
  128.             vec2 coord = texcoord.st + vec2(j,i) * 0.004;
  129.                 if(coord.x > 0 && coord.x < 1 && coord.y > 0 && coord.y < 1){
  130.                     sum += texture2D(composite, coord) * BLOOM_AMOUNT;
  131.                     count += 1;
  132.                 }
  133.             }
  134.     }
  135.     sum = sum / vec4(count);
  136.     if (color.r < 0.3)
  137.     {
  138.         color += sum*sum*0.012;
  139.     }
  140.     else
  141.     {
  142.         if (color.r < 0.5)
  143.         {
  144.             color += sum*sum*0.009;
  145.         }
  146.         else
  147.         {
  148.             color += sum*sum*0.0075;
  149.         }
  150.     }
  151. #endif
  152. #ifdef CROSSPROCESS
  153.     color.r =  color.r*1.3+0.01;
  154.     color.g = color.g*1.2;
  155.     color.b = color.b*0.75+0.10;
  156. #endif
  157.     gl_FragColor = color;
  158. }
  159.  
  160. #ifdef DOF
  161. vec4 getBlurredColor() {
  162.     vec4 blurredColor = vec4(0.0);
  163.     float depth = getDepth(texcoord.xy);
  164.     vec2 aspectCorrection = vec2(1.0, aspectRatio) * 0.005;
  165.  
  166.     vec2 ac0_4 = 0.4 * aspectCorrection;    // 0.4
  167. #ifdef USE_HIGH_QUALITY_BLUR
  168.     vec2 ac0_4x0_4 = 0.4 * ac0_4;           // 0.16
  169.     vec2 ac0_4x0_7 = 0.7 * ac0_4;           // 0.28
  170. #endif
  171.    
  172.     vec2 ac0_29 = 0.29 * aspectCorrection;  // 0.29
  173. #ifdef USE_HIGH_QUALITY_BLUR
  174.     vec2 ac0_29x0_7 = 0.7 * ac0_29;         // 0.203
  175.     vec2 ac0_29x0_4 = 0.4 * ac0_29;         // 0.116
  176. #endif
  177.    
  178.     vec2 ac0_15 = 0.15 * aspectCorrection;  // 0.15
  179.     vec2 ac0_37 = 0.37 * aspectCorrection;  // 0.37
  180. #ifdef USE_HIGH_QUALITY_BLUR
  181.     vec2 ac0_15x0_9 = 0.9 * ac0_15;         // 0.135
  182.     vec2 ac0_37x0_9 = 0.37 * ac0_37;        // 0.1369
  183. #endif
  184.    
  185.     vec2 lowSpace = texcoord.st;
  186.     vec2 highSpace = 1.0 - lowSpace;
  187.     space = vec2(min(lowSpace.s, highSpace.s), min(lowSpace.t, highSpace.t));
  188.        
  189.     if (space.s >= ac0_4.s && space.t >= ac0_4.t) {
  190.  
  191.         blurredColor += texture2D(composite, texcoord.st + vec2(0.0, ac0_4.t));
  192.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_4.s, 0.0));  
  193.         blurredColor += texture2D(composite, texcoord.st + vec2(0.0, -ac0_4.t));
  194.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_4.s, 0.0));
  195.        
  196. #ifdef USE_HIGH_QUALITY_BLUR
  197.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_4x0_7.s, 0.0));      
  198.         blurredColor += texture2D(composite, texcoord.st + vec2(0.0, -ac0_4x0_7.t));    
  199.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_4x0_7.s, 0.0));    
  200.         blurredColor += texture2D(composite, texcoord.st + vec2(0.0, ac0_4x0_7.t));
  201.    
  202.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_4x0_4.s, 0.0));
  203.         blurredColor += texture2D(composite, texcoord.st + vec2(0.0, -ac0_4x0_4.t));
  204.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_4x0_4.s, 0.0));
  205.         blurredColor += texture2D(composite, texcoord.st + vec2(0.0, ac0_4x0_4.t));
  206. #endif
  207.  
  208.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_29.s, -ac0_29.t));
  209.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_29.s, ac0_29.t));
  210.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_29.s, ac0_29.t));
  211.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_29.s, -ac0_29.t));
  212.    
  213. #ifdef USE_HIGH_QUALITY_BLUR
  214.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_29x0_7.s, ac0_29x0_7.t));
  215.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_29x0_7.s, -ac0_29x0_7.t));
  216.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_29x0_7.s, ac0_29x0_7.t));
  217.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_29x0_7.s, -ac0_29x0_7.t));
  218.        
  219.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_29x0_4.s, ac0_29x0_4.t));
  220.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_29x0_4.s, -ac0_29x0_4.t));
  221.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_29x0_4.s, ac0_29x0_4.t));
  222.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_29x0_4.s, -ac0_29x0_4.t));
  223. #endif     
  224.        
  225.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_15.s, ac0_37.t));
  226.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_37.s, ac0_15.t));
  227.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_37.s, -ac0_15.t));
  228.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_15.s, -ac0_37.t));
  229.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_15.s, ac0_37.t));
  230.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_37.s, ac0_15.t));
  231.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_37.s, -ac0_15.t));
  232.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_15.s, -ac0_37.t));
  233.  
  234. #ifdef USE_HIGH_QUALITY_BLUR
  235.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_15x0_9.s, ac0_37x0_9.t));
  236.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_37x0_9.s, ac0_15x0_9.t));
  237.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_37x0_9.s, -ac0_15x0_9.t));
  238.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_15x0_9.s, -ac0_37x0_9.t));
  239.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_15x0_9.s, ac0_37x0_9.t));
  240.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_37x0_9.s, ac0_15x0_9.t));
  241.         blurredColor += texture2D(composite, texcoord.st + vec2(-ac0_37x0_9.s, -ac0_15x0_9.t));
  242.         blurredColor += texture2D(composite, texcoord.st + vec2(ac0_15x0_9.s, -ac0_37x0_9.t));
  243. #endif
  244.  
  245. #ifdef USE_HIGH_QUALITY_BLUR
  246.         blurredColor /= 41.0;
  247. #else
  248.         blurredColor /= 16.0;
  249. #endif
  250.        
  251.     } else {
  252.        
  253.         blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4.t));
  254.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_4.s, 0.0));  
  255.         blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4.t));
  256.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4.s, 0.0));
  257.        
  258. #ifdef USE_HIGH_QUALITY_BLUR
  259.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_4x0_7.s, 0.0));      
  260.         blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4x0_7.t));    
  261.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4x0_7.s, 0.0));    
  262.         blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4x0_7.t));
  263.    
  264.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_4x0_4.s, 0.0));
  265.         blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4x0_4.t));
  266.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4x0_4.s, 0.0));
  267.         blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4x0_4.t));
  268. #endif
  269.  
  270.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_29.s, -ac0_29.t));
  271.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_29.s, ac0_29.t));
  272.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29.s, ac0_29.t));
  273.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29.s, -ac0_29.t));
  274.    
  275. #ifdef USE_HIGH_QUALITY_BLUR
  276.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_29x0_7.s, ac0_29x0_7.t));
  277.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_29x0_7.s, -ac0_29x0_7.t));
  278.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_7.s, ac0_29x0_7.t));
  279.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_7.s, -ac0_29x0_7.t));
  280.        
  281.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_29x0_4.s, ac0_29x0_4.t));
  282.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_29x0_4.s, -ac0_29x0_4.t));
  283.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_4.s, ac0_29x0_4.t));
  284.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29x0_4.s, -ac0_29x0_4.t));
  285. #endif
  286.                
  287.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_15.s, ac0_37.t));
  288.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37.s, ac0_15.t));
  289.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_37.s, -ac0_15.t));
  290.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15.s, -ac0_37.t));
  291.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15.s, ac0_37.t));
  292.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_37.s, ac0_15.t));
  293.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37.s, -ac0_15.t));
  294.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_15.s, -ac0_37.t));
  295.        
  296. #ifdef USE_HIGH_QUALITY_BLUR
  297.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_15x0_9.s, ac0_37x0_9.t));
  298.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37x0_9.s, ac0_15x0_9.t));
  299.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_37x0_9.s, -ac0_15x0_9.t));
  300.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15x0_9.s, -ac0_37x0_9.t));
  301.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15x0_9.s, ac0_37x0_9.t));
  302.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_37x0_9.s, ac0_15x0_9.t));
  303.         blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37x0_9.s, -ac0_15x0_9.t));
  304.         blurredColor += getSampleWithBoundsCheck(vec2(ac0_15x0_9.s, -ac0_37x0_9.t));
  305. #endif
  306.    
  307.         blurredColor /= samples;
  308.        
  309.     }
  310.  
  311.     return blurredColor;
  312. }
  313.  
  314. vec4 getSampleWithBoundsCheck(vec2 offset) {
  315.     vec2 coord = texcoord.st + offset;
  316.     if (coord.s <= 1.0 && coord.s >= 0.0 && coord.t <= 1.0 && coord.t >= 0.0) {
  317.         samples += 1.0;
  318.         return texture2D(composite, coord);
  319.     } else {
  320.         return vec4(0.0);
  321.     }
  322. }
  323. #endif
Advertisement
Add Comment
Please, Sign In to add comment