Advertisement
Guest User

RoadCrewWorker

a guest
Feb 20th, 2011
11,367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.43 KB | None | 0 0
  1. //Minecraft Combination final shader//
  2. /* final.fsh
  3.     Shader combination by rcw
  4.     Daxnitro: glsl mod
  5.     azraeil: Dof
  6.     Blackops: Combination shader
  7.     cferrill1: kickass nh/s textures
  8.     Yourself:
  9. */
  10.  
  11. /*Features and Settings*/
  12. /* Alter here */
  13. #define ENABLE_GAMMA 1.2
  14.  
  15. #define ENABLE_PSEUDO_CELL
  16. #define CELL_FACTOR 0.4
  17. #define CELL_DELTA 0.0025
  18.  
  19. #define ENABLE_AA
  20. #define AA_depthThreshold 0.01
  21.  
  22. #define ENABLE_SSAO
  23. #define SSAO_LOOP 4
  24. #define SSAO_Cap 1.0
  25. #define SSAO_depthTolerance 0.0001
  26.  
  27. #define ENABLE_BLOOM
  28. #define BLOOM_LOOP 1
  29. #define BLOOM_OFFSET 0.002
  30. #define BLOOM_STRENGTH 0.2
  31.  
  32. #define ENABLE_DOF
  33. #define DOF_HYPERFOCAL 4.0
  34. #define DOF_STRENGTH 0.003
  35. #define DOF_PI 3.14159265
  36. //3589793238462
  37.  
  38. //#define ENABLE_SHADOWTRACE
  39.  
  40. uniform sampler2D sampler0;
  41. uniform sampler2D sampler1;
  42. uniform sampler2D sampler2;
  43.  
  44. uniform float near;
  45. uniform float far;
  46.  
  47. uniform float aspectRatio;
  48. uniform float displayHeight;
  49. uniform float displayWidth;
  50.  
  51. /*######################################Helpers###################################*/
  52. float getMinDepth(vec2 coord) {
  53.     float d = texture2D(sampler2, coord).x;
  54.     if (d < 1.0) return d;
  55.     return texture2D(sampler1, coord).x;
  56. }
  57. float getDepth(vec2 coord) {
  58.     float depth= getMinDepth(coord);
  59.     return 2.0 * near * far / ( far + near - ( 2.0 * depth - 1.0 ) * ( far - near ) );
  60. }
  61. vec3 BlendOverlay(vec3 base,vec3 blend){
  62.     return vec3(
  63.         (base.r < 0.5 ? (2.0 * base.r * blend.r) : (1.0 - 2.0 * (1.0 - base.r) * (1.0 - blend.r))),
  64.         (base.g < 0.5 ? (2.0 * base.g * blend.g) : (1.0 - 2.0 * (1.0 - base.g) * (1.0 - blend.g))),
  65.         (base.b < 0.5 ? (2.0 * base.b * blend.b) : (1.0 - 2.0 * (1.0 - base.b) * (1.0 - blend.b)))
  66.     );
  67. }
  68.  
  69. /*######################################AA###################################*/
  70. // 10 depth lookups, 3x3 texture kernel
  71. #ifdef ENABLE_AA
  72. float AA_depth( vec2 coord ) {
  73.     float depth= getMinDepth(coord);
  74.     return ( 2.0 * near ) / ( far + near - depth * ( far - near ) );
  75. }
  76. vec4 getAA(vec2 coord) {
  77.     vec4 sum = vec4(0.0);
  78.     float depth = AA_depth(coord);
  79.     vec2 offset = vec2(1.0, aspectRatio) * 0.005 * 0.12;
  80.     if (
  81.             abs( depth - AA_depth( coord + vec2( offset.x, 0 ) ) ) > AA_depthThreshold ||
  82.             abs( depth - AA_depth( coord + vec2( -offset.x, 0 ) ) ) > AA_depthThreshold ||
  83.             abs( depth - AA_depth( coord + vec2( 0, offset.y ) ) ) > AA_depthThreshold ||
  84.             abs( depth - AA_depth( coord + vec2( 0, -offset.y ) ) ) > AA_depthThreshold
  85.         ) {
  86.         //3x3 Kernel
  87.         sum += texture2D(sampler0, coord + vec2(-offset.x, offset.y));
  88.         sum += texture2D(sampler0, coord + vec2(0, offset.y));
  89.         sum += texture2D(sampler0, coord + offset);
  90.        
  91.         sum += texture2D(sampler0, coord + vec2(-offset.x, 0));
  92.         sum += texture2D(sampler0, coord);
  93.         sum += texture2D(sampler0, coord + vec2(offset.x, 0));
  94.        
  95.         sum += texture2D(sampler0, coord -offset);
  96.         sum += texture2D(sampler0, coord + vec2(0, -offset.y));
  97.         sum += texture2D(sampler0, coord + vec2(offset.x, -offset.y));
  98.        
  99.         sum /= 9.0;
  100.     } else
  101.     sum=texture2D(sampler0, coord);
  102.  
  103.     return sum;
  104. }
  105. #endif
  106.  
  107. /*######################################SSAO###################################*/
  108. // SSAO_LOOP * 4 + 1 depth lookups
  109. #ifdef ENABLE_SSAO
  110. float getSSAOFactor(vec2 coord) {
  111.     float depth = getDepth(coord);
  112.     float d= 0.0;
  113.     float ao = 0.0;
  114.     float aoMultiplier= 300.0;
  115.     float pw = 1.0 / displayWidth;
  116.     float ph = 1.0 / displayHeight;
  117.     for( int i = 0; i < SSAO_LOOP; i++) {
  118.         d=getDepth( vec2(coord.x+pw,coord.y+ph));   ao+=clamp((depth-d-SSAO_depthTolerance) * aoMultiplier,0.0,SSAO_Cap);
  119.         d=getDepth( vec2(coord.x-pw,coord.y+ph));   ao+=clamp((depth-d-SSAO_depthTolerance) * aoMultiplier,0.0,SSAO_Cap);
  120.         d=getDepth( vec2(coord.x+pw,coord.y-ph));   ao+=clamp((depth-d-SSAO_depthTolerance) * aoMultiplier,0.0,SSAO_Cap);
  121.         d=getDepth( vec2(coord.x-pw,coord.y-ph));   ao+=clamp((depth-d-SSAO_depthTolerance) * aoMultiplier,0.0,SSAO_Cap);
  122.         pw*=2.0;   ph*=2.0;   aoMultiplier/=2.0;
  123.     }
  124.     return 1.0 - ao/16.0;
  125. }
  126. #endif
  127.  
  128. /*######################################BLOOM###################################*/
  129. // (2*BLOOM_LOOP+1)^2 + 1 texture lookups
  130. #ifdef ENABLE_BLOOM
  131. vec4 getBloom(vec2 coord) {
  132.     vec4 sum = vec4(0);
  133.     for( int i = -BLOOM_LOOP; i <= BLOOM_LOOP; i++) {
  134.         for ( int j = -BLOOM_LOOP; j <= BLOOM_LOOP; j++ ) {
  135.             sum += texture2D( sampler0, coord + vec2( j, i ) * BLOOM_OFFSET ) * BLOOM_STRENGTH;
  136.         }
  137.     }
  138.     sum = sum*sum;
  139.  
  140.     vec4 rt=texture2D(sampler0, coord);
  141.     if (rt.r < 0.3) sum *= 0.012;
  142.     else if (rt.r < 0.5) sum *= 0.009;
  143.     else sum *= 0.0075;
  144.     return (sum-rt*0.1)*2.0;
  145. }
  146. #endif
  147.  
  148. /*######################################PSEUDO_CELL###################################*/
  149. // 6 depth lookups
  150. #ifdef ENABLE_PSEUDO_CELL
  151. float getCellShaderFactor(vec2 coord) {
  152.     float d=getDepth(coord);
  153.     vec3 n=normalize(vec3(getDepth(coord+vec2(CELL_DELTA,0.0))-d,getDepth(coord+vec2(0.0,CELL_DELTA))-d ,CELL_FACTOR));
  154.     return n.z;//clamp(n.z*3.0,0.0,1.0);
  155. }
  156. #endif
  157.  
  158. /*######################################DOF###################################*/
  159. // 1 depth lookup, 16 texture lookups
  160. #ifdef ENABLE_DOF
  161. vec4 sampleClamped(vec2 coord) {
  162.     return (coord.s <= 1.0 && coord.s >= 0.0 && coord.t <= 1.0 && coord.t >= 0.0) ? texture2D(sampler0, coord) : vec4(0.0);
  163. }
  164. vec4 getBlurredColor(vec2 coord,float strength) {
  165.     vec4 sum = vec4( 0.0 );
  166.     float depth = getDepth( coord );
  167.     vec2 aspectCorrection = vec2( 1.0, aspectRatio ) * DOF_STRENGTH * strength;
  168.  
  169.     vec2 ac0_4 = 0.4 * aspectCorrection;   // 0.4
  170.     vec2 ac0_29 = 0.29 * aspectCorrection;   // 0.29
  171.     vec2 ac0_15 = 0.15 * aspectCorrection;   // 0.15
  172.     vec2 ac0_37 = 0.37 * aspectCorrection;   // 0.37
  173.  
  174.     vec2 lowSpace = coord;
  175.     vec2 highSpace = 1.0 - lowSpace;
  176.     vec2 space = vec2( min( lowSpace.s, highSpace.s ), min( lowSpace.t, highSpace.t ) );
  177.    
  178.     if (space.s >= ac0_4.s && space.t >= ac0_4.t) {
  179.  
  180.         sum += texture2D(sampler0, coord + vec2(0.0, ac0_4.t));
  181.         sum += texture2D(sampler0, coord + vec2(ac0_4.s, 0.0));  
  182.         sum += texture2D(sampler0, coord + vec2(0.0, -ac0_4.t));
  183.         sum += texture2D(sampler0, coord + vec2(-ac0_4.s, 0.0));
  184.        
  185.         sum += texture2D(sampler0, coord + vec2(ac0_29.s, -ac0_29.t));
  186.         sum += texture2D(sampler0, coord + vec2(ac0_29.s, ac0_29.t));
  187.         sum += texture2D(sampler0, coord + vec2(-ac0_29.s, ac0_29.t));
  188.         sum += texture2D(sampler0, coord + vec2(-ac0_29.s, -ac0_29.t));
  189.        
  190.         sum += texture2D(sampler0, coord + vec2(ac0_15.s, ac0_37.t));
  191.         sum += texture2D(sampler0, coord + vec2(-ac0_37.s, ac0_15.t));
  192.         sum += texture2D(sampler0, coord + vec2(ac0_37.s, -ac0_15.t));
  193.         sum += texture2D(sampler0, coord + vec2(-ac0_15.s, -ac0_37.t));
  194.        
  195.         sum += texture2D(sampler0, coord + vec2(-ac0_15.s, ac0_37.t));
  196.         sum += texture2D(sampler0, coord + vec2(ac0_37.s, ac0_15.t));
  197.         sum += texture2D(sampler0, coord + vec2(-ac0_37.s, -ac0_15.t));
  198.         sum += texture2D(sampler0, coord + vec2(ac0_15.s, -ac0_37.t));
  199.         sum /= 16.0;
  200.        
  201.     } else {
  202.        
  203.         sum += sampleClamped(coord + vec2(0.0, ac0_4.t));
  204.         sum += sampleClamped(coord + vec2(ac0_4.s, 0.0));  
  205.         sum += sampleClamped(coord + vec2(0.0, -ac0_4.t));
  206.         sum += sampleClamped(coord + vec2(-ac0_4.s, 0.0));
  207.        
  208.         sum += sampleClamped(coord + vec2(ac0_29.s, -ac0_29.t));
  209.         sum += sampleClamped(coord + vec2(ac0_29.s, ac0_29.t));
  210.         sum += sampleClamped(coord + vec2(-ac0_29.s, ac0_29.t));
  211.         sum += sampleClamped(coord + vec2(-ac0_29.s, -ac0_29.t));
  212.        
  213.         sum += sampleClamped(coord + vec2(ac0_15.s, ac0_37.t));
  214.         sum += sampleClamped(coord + vec2(-ac0_37.s, ac0_15.t));
  215.         sum += sampleClamped(coord + vec2(ac0_37.s, -ac0_15.t));
  216.         sum += sampleClamped(coord + vec2(-ac0_15.s, -ac0_37.t));
  217.        
  218.         sum += sampleClamped(coord + vec2(-ac0_15.s, ac0_37.t));
  219.         sum += sampleClamped(coord + vec2(ac0_37.s, ac0_15.t));
  220.         sum += sampleClamped(coord + vec2(-ac0_37.s, -ac0_15.t));
  221.         sum += sampleClamped(coord + vec2(ac0_15.s, -ac0_37.t));
  222.        
  223.         sum /= 16.0;
  224.        
  225.     }
  226.  
  227.     return sum;
  228. }
  229.  
  230. float getDOFStrength(float depth,float dcursor){  
  231.     if ( dcursor > DOF_HYPERFOCAL )
  232.     {
  233.         if ( depth < DOF_HYPERFOCAL ) return ( depth < 0.5 * DOF_HYPERFOCAL ) ? 1.0 :   sin( 2.0 * ( depth - 0.25 * DOF_HYPERFOCAL ) / DOF_HYPERFOCAL * DOF_PI) / 2.0 + 0.5;
  234.         else return 0.0;
  235.     }
  236.     else
  237.     {
  238.         return
  239.             ( depth - dcursor > ( dcursor * DOF_HYPERFOCAL) / ( DOF_HYPERFOCAL - dcursor ) )    ?   1.0 :
  240.             ( depth > dcursor ) ? sin( DOF_PI * ( ( depth - dcursor ) - 0.5 * ( ( dcursor * DOF_HYPERFOCAL ) / ( DOF_HYPERFOCAL - dcursor ) ) ) / ( ( dcursor * DOF_HYPERFOCAL ) / ( DOF_HYPERFOCAL - dcursor ) ) ) / 2.0 + 0.5 :
  241.             ( depth <= 0.5 * dcursor ) ?    1.0 :
  242.             sin(2.0 * (depth - 0.25 * dcursor) / dcursor * DOF_PI) / 2.0 + 0.5 ;
  243.     }
  244.     return 0.0;
  245. }
  246. #endif
  247.  
  248. /*######################################MAIN###################################*/
  249. void main() {
  250.    
  251.     #ifdef ENABLE_AA
  252.     gl_FragColor = getAA(gl_TexCoord[0].st);
  253.     #else
  254.     gl_FragColor = texture2D(sampler0, gl_TexCoord[0].st);
  255.     #endif
  256.    
  257.     #ifdef ENABLE_DOF
  258.     float dof=getDOFStrength( getDepth( gl_TexCoord[0].st ), getDepth( vec2( 0.5, 0.5 ) ));
  259.     gl_FragColor=getBlurredColor(gl_TexCoord[0].st,dof);
  260.     #endif
  261.    
  262.     #ifdef ENABLE_SSAO
  263.     gl_FragColor.rgb *= (getSSAOFactor(gl_TexCoord[0].st));
  264.     #endif
  265.    
  266.    
  267.     #ifdef ENABLE_PSEUDO_CELL
  268.     gl_FragColor.rgb *= (getCellShaderFactor(gl_TexCoord[0].st));
  269.     #endif
  270.    
  271.     #ifdef ENABLE_BLOOM
  272.     gl_FragColor.rgb = BlendOverlay(gl_FragColor.rgb, getBloom(gl_TexCoord[0].st).rgb*1.0+0.5);
  273.     #endif 
  274.    
  275.     #ifdef ENABLE_GAMMA
  276.     if (gl_FragColor.a == 0.0) {   gl_FragColor = gl_Fog.color;    }
  277.     else {        gl_FragColor.rgb = pow(gl_FragColor.rgb, vec3(1.0/ENABLE_GAMMA));    }
  278.     #endif
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement