Advertisement
Guest User

Untitled

a guest
Feb 13th, 2011
27,958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.68 KB | None | 0 0
  1. /*
  2. ------------------------------------------------
  3. Note:
  4.     - DOF doesn't seem work well with the other
  5.       shaders. The blur overrides all other shaders
  6.      
  7.     - Bloom seems to reduce the effect of
  8.       the SSAO shader
  9.  
  10. Credits:
  11.     - azraeil ( DOF Shader )
  12.     - Yourself ( CEL Shading )
  13.     - Dilla/snowcrash ( Bloom Shader )
  14.     - Leadwerks ( SSAO Shader )
  15.     - .jhn ( pseudo-AA Shader )
  16. ------------------------------------------------
  17. */
  18.  
  19. // To disable one of these features put a "//" in front of the "#define"
  20.  
  21. //#define DOF_ENABLED
  22. #define BLOOM_ENABLED
  23. //#define AA_ENABLED
  24. #define SSAO_ENABLED
  25. #define CEL_SHADING
  26.  
  27. #ifdef CEL_SHADING
  28.     const float CEL_EDGE_THRESHOLD = 0.15;
  29.     const float CEL_EDGE_THICKNESS = 0.65;
  30. #endif
  31. #ifdef BLOOM_ENABLED
  32.     const float BLOOM_STRENGTH = 0.25;
  33. #endif
  34. #ifdef DOF_ENABLED
  35.     const float HYPERFOCAL = 4.0;
  36. #endif
  37.  
  38. /*
  39. -------------------------------------------------------
  40.  The standard user should not mess with anything below
  41. -------------------------------------------------------
  42. */
  43.  
  44. uniform sampler2D sampler0;
  45. uniform sampler2D sampler1;
  46. uniform sampler2D sampler2;
  47. uniform sampler2D bgl_RenderedTexture;
  48.  
  49. uniform float displayWidth;
  50. uniform float displayHeight;
  51. uniform float aspectRatio;
  52. uniform float near;
  53. uniform float far;
  54.  
  55. const float INFINITY = 1000.0;
  56.  
  57. float getDepth( vec2 coord ) {
  58.     float depth = texture2D( sampler1, coord ).x;
  59.     float depth2 = texture2D( sampler2, coord ).x;
  60.     if ( depth2 < 1.0 ) {
  61.         depth = depth2;
  62.     }
  63.     if ( depth == 1.0 ) {
  64.         return INFINITY;
  65.     }
  66.     return 2.0 * near * far / ( far + near - ( 2.0 * depth - 1.0 ) * ( far - near ) );
  67. }
  68.  
  69. #ifdef SSAO_ENABLED // Stuff only SSAO uses
  70.     float compareDepths( in float depth1, in float depth2 ) {
  71.         float aoCap = 1.0;
  72.         float aoMultiplier = 5000.0;
  73.         float depthTolerance = 0.0;
  74.         float aorange = 512.0; // units in space the AO effect extends to (this gets divided by the camera far range
  75.         float diff = sqrt( clamp( 1.0 - ( depth1 - depth2 ) / ( aorange / ( far - near ) ), 0.0, 1.0 ) );
  76.         float ao = min( aoCap, max( 0.0, depth1 - depth2 - depthTolerance ) * aoMultiplier ) * diff;
  77.         return ao;
  78.     }
  79.  
  80.     vec4 getSSAO() {
  81.         float depth = getDepth( gl_TexCoord[0].st );
  82.         float d;
  83.        
  84.         float pw = 1.0 / displayWidth;
  85.         float ph = 1.0 / displayHeight;
  86.         float aoCap = 1.0;
  87.         float ao = 0.0;
  88.         float aoMultiplier = 5000.0;
  89.         float depthTolerance = 0.0;
  90.         float aoscale=1.0;
  91.  
  92.         d=getDepth( vec2(gl_TexCoord[0].x+pw,gl_TexCoord[0].y+ph));
  93.         ao+=compareDepths(depth,d)/aoscale;
  94.  
  95.         d=getDepth( vec2(gl_TexCoord[0].x-pw,gl_TexCoord[0].y+ph));
  96.         ao+=compareDepths(depth,d)/aoscale;
  97.  
  98.         d=getDepth( vec2(gl_TexCoord[0].x+pw,gl_TexCoord[0].y-ph));
  99.         ao+=compareDepths(depth,d)/aoscale;
  100.  
  101.         d=getDepth( vec2(gl_TexCoord[0].x-pw,gl_TexCoord[0].y-ph));
  102.         ao+=compareDepths(depth,d)/aoscale;
  103.        
  104.         pw*=2.0;
  105.         ph*=2.0;
  106.         aoMultiplier/=2.0;
  107.         aoscale*=1.2;
  108.        
  109.         d=getDepth( vec2(gl_TexCoord[0].x+pw,gl_TexCoord[0].y+ph));
  110.         ao+=compareDepths(depth,d)/aoscale;
  111.  
  112.         d=getDepth( vec2(gl_TexCoord[0].x-pw,gl_TexCoord[0].y+ph));
  113.         ao+=compareDepths(depth,d)/aoscale;
  114.  
  115.         d=getDepth( vec2(gl_TexCoord[0].x+pw,gl_TexCoord[0].y-ph));
  116.         ao+=compareDepths(depth,d)/aoscale;
  117.  
  118.         d=getDepth( vec2(gl_TexCoord[0].x-pw,gl_TexCoord[0].y-ph));
  119.         ao+=compareDepths(depth,d)/aoscale;
  120.  
  121.         pw*=2.0;
  122.         ph*=2.0;
  123.         aoMultiplier/=2.0;
  124.         aoscale*=1.2;
  125.        
  126.         d=getDepth( vec2(gl_TexCoord[0].x+pw,gl_TexCoord[0].y+ph));
  127.         ao+=compareDepths(depth,d)/aoscale;
  128.  
  129.         d=getDepth( vec2(gl_TexCoord[0].x-pw,gl_TexCoord[0].y+ph));
  130.         ao+=compareDepths(depth,d)/aoscale;
  131.  
  132.         d=getDepth( vec2(gl_TexCoord[0].x+pw,gl_TexCoord[0].y-ph));
  133.         ao+=compareDepths(depth,d)/aoscale;
  134.  
  135.         d=getDepth( vec2(gl_TexCoord[0].x-pw,gl_TexCoord[0].y-ph));
  136.         ao+=compareDepths(depth,d)/aoscale;
  137.        
  138.         pw*=2.0;
  139.         ph*=2.0;
  140.         aoMultiplier/=2.0;
  141.         aoscale*=1.2;
  142.        
  143.         d=getDepth( vec2(gl_TexCoord[0].x+pw,gl_TexCoord[0].y+ph));
  144.         ao+=compareDepths(depth,d)/aoscale;
  145.  
  146.         d=getDepth( vec2(gl_TexCoord[0].x-pw,gl_TexCoord[0].y+ph));
  147.         ao+=compareDepths(depth,d)/aoscale;
  148.  
  149.         d=getDepth( vec2(gl_TexCoord[0].x+pw,gl_TexCoord[0].y-ph));
  150.         ao+=compareDepths(depth,d)/aoscale;
  151.  
  152.         d=getDepth( vec2(gl_TexCoord[0].x-pw,gl_TexCoord[0].y-ph));
  153.         ao+=compareDepths(depth,d)/aoscale;
  154.  
  155.         ao/=16.0;
  156.        
  157.         return vec4(1.1-ao);
  158.     }
  159. #endif
  160.  
  161. #ifdef AA_ENABLED // Stuff only AA uses
  162.     float expDepth2linearDepth( vec2 coord ) {
  163.         float depth = texture2D( sampler1, coord ).x;
  164.         float depth2 = texture2D( sampler2, coord ).x;
  165.         if ( depth2 < 1.0 ) {
  166.             depth = depth2;
  167.         }
  168.         if ( depth == 1.0 ) {
  169.             return INFINITY;
  170.         }
  171.         return ( 2.0 * near ) / ( far + near - depth * ( far - near ) );
  172.     }
  173.  
  174.     vec4 getAA() {
  175.         vec4 newColor = vec4(0.0);
  176.  
  177.         float depth = expDepth2linearDepth(gl_TexCoord[0].xy);
  178.         vec2 aspectCorrection = vec2(1.0, aspectRatio) * 0.005;
  179.         float offsetx = 0.12 * aspectCorrection.x;
  180.         float offsety = 0.12 * aspectCorrection.y;
  181.         float depthThreshold=0.01;
  182.  
  183.         if ( abs( depth - expDepth2linearDepth( gl_TexCoord[0].xy + vec2( offsetx, 0 ) ) ) > depthThreshold || abs( depth - expDepth2linearDepth( gl_TexCoord[0].xy + vec2( -offsetx, 0 ) ) ) > depthThreshold || abs( depth - expDepth2linearDepth( gl_TexCoord[0].xy + vec2( 0, offsety ) ) ) > depthThreshold || abs( depth - expDepth2linearDepth( gl_TexCoord[0].xy + vec2( 0, -offsety ) ) ) > depthThreshold ) {
  184.             newColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-offsetx, offsety));
  185.             newColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0, offsety));
  186.             newColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(offsetx, offsety));
  187.             newColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-offsetx, 0));
  188.             newColor += texture2D(sampler0, gl_TexCoord[0].st);
  189.             newColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(offsetx, 0));
  190.             newColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-offsetx, -offsety));
  191.             newColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0, -offsety));
  192.             newColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(offsetx, -offsety));
  193.             newColor /= 9.0;
  194.         } else
  195.             newColor=texture2D(sampler0, gl_TexCoord[0].st);
  196.  
  197.         return newColor;
  198.     }
  199. #endif
  200.  
  201. #ifdef BLOOM_ENABLED // Stuff only bloom uses
  202.     vec4 getBloom() {
  203.         vec4 sum = vec4(0);
  204.         vec2 texcoord = vec2(gl_TexCoord[0]);
  205.         for( int i = -3; i < 3; i++) { // Lower loop count for performance
  206.             for ( int j = -3; j < 3; j++ ) {
  207.                 sum += texture2D( bgl_RenderedTexture, texcoord + vec2( j, i ) * 0.004 ) * BLOOM_STRENGTH;
  208.             }
  209.         }
  210.         if (texture2D(bgl_RenderedTexture, texcoord).r < 0.3) {
  211.             sum = sum*sum*0.012 + texture2D(bgl_RenderedTexture, texcoord);
  212.         } else {
  213.             if (texture2D(bgl_RenderedTexture, texcoord).r < 0.5) {
  214.                 sum = sum*sum*0.009 + texture2D(bgl_RenderedTexture, texcoord);
  215.             } else {
  216.                 sum = sum*sum*0.0075 + texture2D(bgl_RenderedTexture, texcoord);
  217.             }
  218.         }
  219.         return sum;
  220.     }
  221. #endif
  222.  
  223. #ifdef DOF_ENABLED // Stuff only DOF uses
  224.  
  225.     float samples = float( 0 );
  226.     vec2 space;
  227.  
  228.     float getCursorDepth( vec2 coord ) {
  229.         return 2.0 * near * far / ( far + near - ( 2.0 * texture2D( sampler1, coord ).x - 1.0 ) * ( far - near ) );
  230.     }
  231.  
  232.     vec4 getSampleWithBoundsCheck(vec2 offset) {
  233.         vec2 coord = gl_TexCoord[0].st + offset;
  234.         if (coord.s <= 1.0 && coord.s >= 0.0 && coord.t <= 1.0 && coord.t >= 0.0) {
  235.             samples += 1.0;
  236.             return texture2D(sampler0, coord);
  237.         } else {
  238.             return vec4(0.0);
  239.         }
  240.     }
  241.  
  242.     vec4 getBlurredColor() {
  243.         vec4 blurredColor = vec4( 0.0 );
  244.         float depth = getDepth( gl_TexCoord[0].xy );
  245.         vec2 aspectCorrection = vec2( 1.0, aspectRatio ) * 0.005;
  246.  
  247.         vec2 ac0_4 = 0.4 * aspectCorrection;    // 0.4
  248.         vec2 ac0_29 = 0.29 * aspectCorrection;  // 0.29
  249.         vec2 ac0_15 = 0.15 * aspectCorrection;  // 0.15
  250.         vec2 ac0_37 = 0.37 * aspectCorrection;  // 0.37
  251.        
  252.         vec2 lowSpace = gl_TexCoord[0].st;
  253.         vec2 highSpace = 1.0 - lowSpace;
  254.         space = vec2( min( lowSpace.s, highSpace.s ), min( lowSpace.t, highSpace.t ) );
  255.            
  256.         if (space.s >= ac0_4.s && space.t >= ac0_4.t) {
  257.  
  258.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, ac0_4.t));
  259.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_4.s, 0.0));  
  260.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(0.0, -ac0_4.t));
  261.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_4.s, 0.0));
  262.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29.s, -ac0_29.t));
  263.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_29.s, ac0_29.t));
  264.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29.s, ac0_29.t));
  265.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_29.s, -ac0_29.t));
  266.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15.s, ac0_37.t));
  267.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_37.s, ac0_15.t));
  268.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_37.s, -ac0_15.t));
  269.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_15.s, -ac0_37.t));
  270.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_15.s, ac0_37.t));
  271.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_37.s, ac0_15.t));
  272.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(-ac0_37.s, -ac0_15.t));
  273.             blurredColor += texture2D(sampler0, gl_TexCoord[0].st + vec2(ac0_15.s, -ac0_37.t));
  274.             blurredColor /= 16.0;
  275.            
  276.         } else {
  277.            
  278.             blurredColor += getSampleWithBoundsCheck(vec2(0.0, ac0_4.t));
  279.             blurredColor += getSampleWithBoundsCheck(vec2(ac0_4.s, 0.0));  
  280.             blurredColor += getSampleWithBoundsCheck(vec2(0.0, -ac0_4.t));
  281.             blurredColor += getSampleWithBoundsCheck(vec2(-ac0_4.s, 0.0));
  282.             blurredColor += getSampleWithBoundsCheck(vec2(ac0_29.s, -ac0_29.t));
  283.             blurredColor += getSampleWithBoundsCheck(vec2(ac0_29.s, ac0_29.t));
  284.             blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29.s, ac0_29.t));
  285.             blurredColor += getSampleWithBoundsCheck(vec2(-ac0_29.s, -ac0_29.t));
  286.             blurredColor += getSampleWithBoundsCheck(vec2(ac0_15.s, ac0_37.t));
  287.             blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37.s, ac0_15.t));
  288.             blurredColor += getSampleWithBoundsCheck(vec2(ac0_37.s, -ac0_15.t));
  289.             blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15.s, -ac0_37.t));
  290.             blurredColor += getSampleWithBoundsCheck(vec2(-ac0_15.s, ac0_37.t));
  291.             blurredColor += getSampleWithBoundsCheck(vec2(ac0_37.s, ac0_15.t));
  292.             blurredColor += getSampleWithBoundsCheck(vec2(-ac0_37.s, -ac0_15.t));
  293.             blurredColor += getSampleWithBoundsCheck(vec2(ac0_15.s, -ac0_37.t));
  294.             blurredColor /= samples;
  295.            
  296.         }
  297.  
  298.         return blurredColor;
  299.     }
  300. #endif
  301.  
  302. #ifdef CEL_SHADING
  303.     float getEdgeDepth(vec2 coord) {
  304.         float depth = texture2D( sampler1, coord ).x;
  305.         float depth2 = texture2D( sampler2, coord ).x;
  306.         if ( depth2 < 1.0 ) {
  307.             depth = depth2;
  308.         }
  309.        
  310.         if ( depth == 1.0 ) {
  311.             return INFINITY;
  312.         }
  313.        
  314.         return depth * near / ( far + ( near - far ) * depth );
  315.     }
  316.  
  317.     vec4 edgeDetect( vec2 coord ) {
  318.         vec2 o11 = vec2(1.0, aspectRatio)*CEL_EDGE_THICKNESS/displayWidth;
  319.         vec4 color = vec4(0.0);
  320.      
  321.         float depth = getEdgeDepth(coord);
  322.         float avg = 0.0;
  323.         float laplace = 24.0 * depth;
  324.         float sample;
  325.         int n = 0;
  326.        
  327.         if (depth < INFINITY) {
  328.             avg += depth;
  329.             ++n;
  330.         }
  331.      
  332.         for (int i = -2; i <= 2; ++i) {
  333.             for (int j = -2; j <= 2; ++j) {
  334.                 if (i != 0 || j != 0) {
  335.                     sample = getEdgeDepth(coord + vec2(float( i ) * o11.s, float( j ) * o11.t));
  336.                     laplace -= sample;
  337.                     if (sample < INFINITY) {
  338.                         ++n;
  339.                         avg += sample;
  340.                     }
  341.                 }
  342.             }
  343.         }
  344.        
  345.         avg = clamp( avg/ float( n ), 0.0, 1.0);
  346.      
  347.         if ( laplace > avg * CEL_EDGE_THRESHOLD ) {
  348.             color.rgb = mix( vec3( 0.0 ), gl_Fog.color.rgb, 0.75 * avg * avg);
  349.             color.a = 1.0;
  350.         }
  351.      
  352.         return color;
  353.     }
  354. #endif
  355.  
  356. void main() {
  357.     vec4 baseColor;
  358.    
  359. #ifdef AA_ENABLED
  360.     baseColor = getAA();
  361. #else
  362.     baseColor = texture2D( sampler0, gl_TexCoord[0].st );
  363. #endif
  364.  
  365. #ifdef SSAO_ENABLED
  366.     baseColor *= getSSAO();
  367. #endif
  368.  
  369. #ifdef BLOOM_ENABLED
  370.     baseColor = mix( baseColor, getBloom(), 0.5 );
  371. #endif
  372.    
  373. #ifdef CEL_SHADING
  374.     vec4 outlineColor = edgeDetect( gl_TexCoord[0].st );
  375.     if (outlineColor.a != 0.0) {
  376.         baseColor.rgb = outlineColor.rgb;
  377.     }
  378. #endif
  379.  
  380. #ifdef DOF_ENABLED
  381.     float depth = getDepth( gl_TexCoord[0].st );
  382.     float cursorDepth = getCursorDepth( vec2( 0.5, 0.5 ) );
  383.     if ( depth < cursorDepth )
  384.         baseColor = mix( baseColor, getBlurredColor(), clamp(2.0 * ((clamp(cursorDepth, 0.0, HYPERFOCAL) - depth) / (clamp(cursorDepth, 0.0, HYPERFOCAL))), 0.0, 1.0));
  385.     else
  386.         baseColor = mix( baseColor, getBlurredColor(), 1.0 - clamp( ( ( ( cursorDepth * HYPERFOCAL ) / ( HYPERFOCAL - cursorDepth ) ) - ( depth - cursorDepth ) ) / ((cursorDepth * HYPERFOCAL) / (HYPERFOCAL - cursorDepth)), 0.0, 1.0));
  387. #endif
  388.    
  389.     gl_FragColor = baseColor;
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement