Advertisement
Uhyve

Minecraft Ray Scattering - 3rd Try

Apr 13th, 2011
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. // Based on Fabien Sanglard's Light Scattering shader
  2. // http://www.fabiensanglard.net/lightScattering/index.php
  3.  
  4. uniform sampler2D sampler0;
  5. uniform sampler2D sampler1;
  6. uniform sampler2D sampler2;
  7.  
  8. uniform float displayWidth;
  9. uniform float displayHeight;
  10.  
  11. uniform vec3 sunPosition;
  12. uniform float far;
  13.  
  14. const float exposure = 0.008;
  15. const float decay = 1.0;
  16. const float density = 0.95;
  17. const float weight = 4.65;
  18. const int NUM_SAMPLES = 100;
  19.  
  20. void main()
  21. {  
  22.     vec4 sun = gl_ProjectionMatrix * vec4(sunPosition, 1.0); // should this be 1.0 or 0.0?
  23.     sun.xyz /= sun.w;
  24.     vec2 lightPositionOnScreen = sun.xy * vec2(displayWidth/2.0, displayHeight/2.0);
  25.  
  26.     vec2 deltaTextCoord = vec2( gl_TexCoord[0].st - lightPositionOnScreen.xy );
  27.     vec2 textCoo = gl_TexCoord[0].st;
  28.     deltaTextCoord *= 1.0 / float(NUM_SAMPLES) * density;
  29.     float illuminationDecay = 1.0;
  30.     vec4 crepRen = vec4(0.0, 0.0, 0.0, 1.0);
  31.    
  32.     for(int i=0; i < NUM_SAMPLES ; i++)
  33.     {
  34.         textCoo -= deltaTextCoord;
  35.             vec4 sample = texture2D(sampler1, textCoo );
  36.         sample *= illuminationDecay * weight;
  37.         crepRen += sample;
  38.         illuminationDecay *= decay;
  39.     }
  40.  
  41.     crepRen-=(NUM_SAMPLES * 4.5); // hacky - lowers brightness, without getting rid of rays.
  42.     crepRen*= exposure;
  43.     crepRen.a = 1.0;
  44.     crepRen = clamp(crepRen, 0.0, 1.0);
  45.  
  46.     vec4 sampler = texture2D(sampler0, gl_TexCoord[0].st);
  47.     sampler.a = 1.0;
  48.     gl_FragColor = clamp(sampler + crepRen, 0.0, 1.0);
  49.     //gl_FragColor = crepRen; // uncomment line to see ray output
  50.     //gl_FragColor.rgb = sunPosition.xyz / far; // uncomment line to see sun position output
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement