Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 120
  2.  
  3. ///////////////////////////////////////////////////////////////////////////////
  4. //                              Unchangable Variables                        //
  5. ///////////////////////////////////////////////////////////////////////////////
  6. const int   shadowMapResolution     = 2048;
  7. const float shadowDistance          = 120.0;
  8. const bool  shadowHardwareFiltering = false;
  9. const int   noiseTextureResolution  = 64;
  10.  
  11.  
  12. ///////////////////////////////////////////////////////////////////////////////
  13. //                              Changable Variables                          //
  14. ///////////////////////////////////////////////////////////////////////////////
  15.  
  16. #define HARD            0
  17. #define SOFT            1
  18. #define REALISTIC       2
  19. #define SHADOW_QUALITY  REALISTIC
  20. #define SHADOW_BIAS     0.0065
  21. #define PCSS_SAMPLES    32              //don't make this number greater than 32. You'll just waste GPU time
  22.  
  23. #define SSAO            true
  24. #define SSAO_SAMPLES    8               //more samples = prettier
  25. #define SSAO_STRENGTH   1.0             //bigger number = more SSAO
  26. #define SSAO_RADIUS     100.0             //search a 2-unit radius hemisphere
  27. #define SSAO_MAX_DEPTH  2.0             //if a sample's depth is within 2 units of the world depth, the sample is
  28.                                         //obscured
  29.  
  30. ///////////////////////////////////////////////////////////////////////////////
  31. //                              I need these                                 //
  32. ///////////////////////////////////////////////////////////////////////////////
  33.  
  34. uniform sampler2D gcolor;
  35. uniform sampler2D gdepthtex;
  36. uniform sampler2D gnormal;
  37. uniform sampler2D gaux2;
  38.  
  39. uniform sampler2D shadow;
  40.  
  41. uniform sampler2D noisetex;
  42.  
  43. uniform vec3 cameraPosition;
  44.  
  45. uniform float viewWidth;
  46. uniform float viewHeight;
  47. uniform float far;
  48. uniform float near;
  49.  
  50. uniform mat4 gbufferModelView;
  51. uniform mat4 gbufferProjection;
  52. uniform mat4 gbufferModelViewInverse;
  53. uniform mat4 gbufferProjectionInverse;
  54.  
  55. uniform mat4 shadowModelView;
  56. uniform mat4 shadowProjection;
  57. uniform mat4 shadowModelViewInverse;
  58. uniform mat4 shadowProjectionInverse;
  59.  
  60. varying vec2 coord;
  61.  
  62. varying vec3 lightVector;
  63. varying vec3 lightColor;
  64.  
  65. struct Pixel {
  66.     vec4 position;
  67.     vec4 screenPosition;
  68.     vec3 color;
  69.     vec3 normal;
  70.     float reflectivity;
  71.     float smoothness;
  72.     bool isWater;
  73.    
  74.     bool skipLighting;
  75.    
  76.     vec3 directLighting;
  77.     vec3 torchLighting;
  78.     vec3 ambientLighting;
  79. };
  80.  
  81. struct World {
  82.     vec3 lightDirection;
  83.     vec3 lightColor;
  84. };
  85.  
  86. ///////////////////////////////////////////////////////////////////////////////
  87. //                              Helper Functions                             //
  88. ///////////////////////////////////////////////////////////////////////////////
  89. //Credit to Sonic Ether for depth, normal, and positions
  90.  
  91. float getDepth(  vec2 coord ) {
  92.     return texture2D( gdepthtex, coord ).r;
  93. }
  94.  
  95. float getDepthLinear( vec2 coord ) {
  96.     return 2.0 * near * far / (far + near - (2.0 * texture2D( gdepthtex, coord ).r - 1.0) * (far - near));
  97. }
  98.  
  99. vec4 getScreenSpacePosition() {
  100.     float depth = getDepth( coord );
  101.     vec4 fragposition = gbufferProjectionInverse * vec4( coord.s * 2.0 - 1.0, coord.t * 2.0 - 1.0, 2.0 * depth - 1.0, 1.0 );
  102.          fragposition /= fragposition.w;
  103.     return fragposition;
  104. }
  105.  
  106. vec4 getWorldSpacePosition() {
  107.     vec4 pos = getScreenSpacePosition();
  108.     pos = gbufferModelViewInverse * pos;
  109.     pos.xyz += cameraPosition.xyz;
  110.     return pos;
  111. }
  112.  
  113. vec3 getColor() {
  114.     return texture2D( gcolor, coord ).rgb;
  115. }
  116.  
  117. bool shouldSkipLighting() {
  118.     return texture2D( gaux2, coord ).r > 0.5;
  119. }
  120.  
  121. bool getWater() {
  122.     return texture2D( gaux2, coord ).b > 0.5;
  123. }
  124.  
  125. float getSmoothness() {
  126.     return texture2D( gaux2, coord ).a;
  127. }
  128.  
  129. vec3 getNormal() {
  130.     vec3 normal = normalize( texture2D( gnormal, coord ).xyz * 2.0 - 1.0 );
  131.     normal.x *= -1;
  132.     return normal;
  133. }
  134.  
  135. float getReflectivity() {
  136.     return texture2D( gnormal, coord ).a;
  137. }
  138.  
  139. ///////////////////////////////////////////////////////////////////////////////
  140. //                              Lighting Functions                           //
  141. ///////////////////////////////////////////////////////////////////////////////
  142.  
  143. //from SEUS v8
  144. vec3 calcShadowCoordinate( in Pixel pixel ) {
  145.     vec4 shadowCoord = pixel.position;
  146.     shadowCoord.xyz -= cameraPosition;
  147.     shadowCoord = shadowModelView * shadowCoord;
  148.     shadowCoord = shadowProjection * shadowCoord;
  149.     shadowCoord /= shadowCoord.w;
  150.    
  151.     shadowCoord.st = shadowCoord.st * 0.5 + 0.5;    //take it from [-1, 1] to [0, 1]
  152.     float dFrag = (1 + shadowCoord.z) * 0.5 + 0.005;
  153.    
  154.     return vec3( shadowCoord.st, dFrag );
  155. }
  156.  
  157. //I'm sorry this is so long, OSX doesn't support GLSL 120 arrays
  158. vec2 poisson( int i ) {
  159.     if ( i == 0 ) {
  160.         return vec2( -0.4994766, -0.4100508 );
  161.     } else if( i == 1 ) {
  162.         return vec2(  0.1725386, -0.50636 );
  163.     } else if( i == 2 ) {
  164.         return vec2( -0.3050305,  0.7459931 );
  165.     } else if( i == 3 ) {
  166.         return vec2(  0.3256707,  0.2347208 );
  167.        
  168.     } else if( 1 == 4 ) {
  169.         return vec2( -0.1094937, -0.752005 );
  170.     } else if( i == 5 ) {
  171.         return vec2(  0.5059697, -0.7294227 );
  172.     } else if( i == 6 ) {
  173.         return vec2( -0.3904303,  0.5678311 );
  174.     } else if( i == 7 ) {
  175.         return vec2(  0.3405131,  0.4458854 );
  176.        
  177.     } else if( i == 8 ) {
  178.         return vec2( -0.163072,  -0.9741971 );
  179.     } else if( i == 9 ) {
  180.         return vec2(  0.4260757, -0.02231212 );
  181.     } else if( i == 10 ) {
  182.         return vec2( -0.8977778,  0.1717084 );
  183.     } else if( i == 11 ) {
  184.         return vec2(  0.02903906, 0.3999698 );
  185.        
  186.     } else if( i == 12 ) {
  187.         return vec2( -0.4680224, -0.4418066 );
  188.     } else if( i == 13 ) {
  189.         return vec2(  0.09780561, -0.1236207 );
  190.     } else if( i == 14 ) {
  191.         return vec2( -0.3564819,  0.2770886 );
  192.     } else if( i == 15 ) {
  193.         return vec2(  0.0663829,  0.9336991 );
  194.        
  195.     } else if( i == 16 ) {
  196.         return vec2( -0.8206947, -0.3301564 );
  197.     } else if( i == 17 ) {
  198.         return vec2(  0.1038207, -0.2167438 );
  199.     } else if( i == 18 ) {
  200.         return vec2( -0.3123821,  0.2344262 );
  201.     } else if( i == 19 ) {
  202.         return vec2(  0.1979104,  0.7830779 );
  203.        
  204.     } else if( i == 20 ) {
  205.         return vec2( -0.6740047, -0.4649915 );
  206.     } else if( i == 21 ) {
  207.         return vec2(  0.08938109, -0.005763604 );
  208.     } else if( i == 22 ) {
  209.         return vec2( -0.6670403,  0.658087 );
  210.     } else if( i == 23 ) {
  211.         return vec2(  0.8211543,  0.365194 );
  212.        
  213.     } else if( i == 24 ) {
  214.         return vec2( -0.8381009, -0.1279669 );
  215.     } else if( i == 25 ) {
  216.         return vec2(  0.6365152, -0.229197 );
  217.     } else if( i == 26 ) {
  218.         return vec2( -0.1748933,  0.1948632 );
  219.     } else if( i == 27 ) {
  220.         return vec2(  0.1710306,  0.5527771 );
  221.        
  222.     } else if( i == 28 ) {
  223.         return vec2( -0.5874177, -0.1295959 );
  224.     } else if( i == 29 ) {
  225.         return vec2(  0.6305282, -0.5586912 );
  226.     } else if( i == 30 ) {
  227.         return vec2( -0.030519,  0.3487186 );
  228.     } else {
  229.         return vec2(  0.4240496, -0.1010172 );
  230.     }
  231. }
  232.  
  233. int rand( vec2 seed ) {
  234.     return int( floor( 32 * abs( sin( dot( vec2( 12.45345, 9.2345 ), seed ) * 597123.23432 ) ) ) );
  235. }
  236.  
  237. //Implements the Percentage-Closer Soft Shadow algorithm, as defined by nVidia
  238. //Implemented by DethRaid - github.com/DethRaid
  239. float calcPenumbraSize( vec3 shadowCoord ) {
  240.     float dFragment = shadowCoord.z;
  241.     float dBlocker = 0;
  242.     float penumbra = 0;
  243.     float wLight = 0.5;
  244.  
  245.     // Sample the shadow map 8 times
  246.     float temp;
  247.     float count = 0;
  248.    
  249.     for( int i = 0; i < 8; i++ ) {    
  250.         temp = texture2D( shadow, shadowCoord.st + (poisson( rand( coord ) ) * 0.001 ) ).r;
  251.         if( temp < dFragment ) {
  252.             dBlocker += temp;
  253.             count += 1.0;
  254.         }
  255.     }
  256.  
  257.     if( count > 0.1 ) {
  258.         dBlocker /= count;
  259.         penumbra = wLight * (dFragment - dBlocker) / dFragment;
  260.     }
  261.    
  262.     return penumbra;
  263. }
  264.  
  265. void calcShadowing( inout Pixel pixel ) {
  266.     vec3 shadowCoord = calcShadowCoordinate( pixel );
  267.    
  268.     if( shadowCoord.x > 1 || shadowCoord.x < 0 ||
  269.         shadowCoord.y > 1 || shadowCoord.y < 0 ) {
  270.         return;
  271.     }
  272.    
  273. #if SHADOW_QUALITY == HARD
  274.     float shadowDepth = texture2D( shadow, shadowCoord.st ).r;    
  275.     if( shadowCoord.z - shadowDepth > SHADOW_BIAS ) {
  276.         pixel.directLighting = vec3( 0 );
  277.     }
  278.    
  279. #elif SHADOW_QUALITY >= SOFT
  280.     float penumbraSize = 3.0;
  281.    
  282. #if SHADOW_QUALITY == REALISTIC
  283.     penumbraSize = calcPenumbraSize( shadowCoord );
  284. #endif
  285.    
  286.     float visibility = 1.0;
  287.     float sub = 1.0 / PCSS_SAMPLES;
  288.     int shadowCount = 0;
  289.     for( int i = 0; i < PCSS_SAMPLES; i++ ) {
  290.         int index = rand( coord );
  291.         float shadowDepth = texture2D( shadow, shadowCoord.st + (penumbraSize * poisson( i ) * 0.005) ).r;
  292.         if( shadowCoord.z - shadowDepth > SHADOW_BIAS ) {
  293.             visibility -= sub;
  294.         }
  295.     }
  296.    
  297.     if( visibility < 1 ) {
  298.         pixel.directLighting *= visibility;
  299.     }
  300. #endif
  301. }
  302.  
  303. void calcDirectLighting( inout Pixel pixel ) {
  304.     vec3 normal = normalize( texture2D( gnormal, coord ).xyz * 2.0 - 1.0 );
  305.     float ndotl = dot( lightVector, normal );
  306.     ndotl = clamp( ndotl, 0, 1 );
  307.     pixel.directLighting = lightColor * ndotl;
  308.     if( ndotl > 0.1 ) {
  309.         calcShadowing( pixel );
  310.     }
  311. }
  312.  
  313. //calcualtes the lighting from the torches
  314. void calcTorchLighting( inout Pixel pixel ) {
  315.     vec3 torchColor = vec3( 1, 0.9, 0.5 );
  316.     pixel.torchLighting = torchColor * texture2D( gaux2, coord ).g;
  317. }
  318.  
  319. void calcAmbientLighting( inout Pixel pixel ) {
  320.     pixel.ambientLighting = vec3( 0.15, 0.17, 0.2 );
  321. }
  322.  
  323. ///////////////////////////////////////////////////////////////////////////////
  324. //                              Main Functions                               //
  325. ///////////////////////////////////////////////////////////////////////////////
  326.  
  327. void fillPixelStruct( inout Pixel pixel ) {
  328.     pixel.position =        getWorldSpacePosition();
  329.     pixel.normal =          getNormal();
  330.     pixel.color =           getColor();
  331.     pixel.reflectivity =    getReflectivity();
  332.     pixel.smoothness =      getSmoothness();
  333.     pixel.skipLighting =    shouldSkipLighting();
  334.     pixel.isWater =         getWater();
  335. }
  336.  
  337. vec2 texelToScreen( vec2 texel ) {
  338.     float newx = texel.x / viewWidth;
  339.     float newy = texel.y / viewHeight;
  340.     return vec2( newx, newy );
  341. }
  342.  
  343.  
  344. void calcSSAO( inout Pixel pixel ) {
  345.     //not that it works...
  346.     float ssaoFac = SSAO_STRENGTH;
  347.     float compareDepth = getDepthLinear( coord );
  348.    
  349.     float radiusx = SSAO_RADIUS / viewWidth;
  350.     float radiusy = SSAO_RADIUS / viewHeight;
  351.     vec2 sampleScale = vec2( radiusx, radiusy );
  352.  
  353.     float occlusionPerSample = ssaoFac / float( SSAO_SAMPLES );
  354.  
  355.     vec2 sampleCoord;
  356.     for( int i = 0; i < SSAO_SAMPLES; i++ ) {
  357.         sampleCoord = poisson( i );
  358.         float len = 1;
  359.         sampleCoord *= len * len * sampleScale;
  360.         if( getDepthLinear( sampleCoord ) < compareDepth ) {
  361.             ssaoFac -= occlusionPerSample;
  362.         }
  363.     }
  364.  
  365.     //pixel.directLighting = *= ssaoFac;
  366.     pixel.torchLighting *= ssaoFac;
  367. }
  368.  
  369. vec3 calcLitColor( in Pixel pixel ) {
  370.     vec3 color = pixel.color * pixel.directLighting +
  371.                  pixel.color * pixel.torchLighting +
  372.                  pixel.color * pixel.ambientLighting;
  373.     return color / 2;
  374. }
  375.  
  376. void main() {
  377.     Pixel pixel;
  378.     vec3 finalColor;
  379.    
  380.     fillPixelStruct( pixel );
  381.    
  382.     if( !pixel.skipLighting ) {
  383.         calcDirectLighting( pixel );
  384.         calcTorchLighting( pixel );
  385.         calcAmbientLighting( pixel );
  386.    
  387.         calcSSAO( pixel );
  388.    
  389.         finalColor = calcLitColor( pixel );
  390.     } else {
  391.         finalColor = pixel.color;
  392.     }
  393.  
  394.     gl_FragData[3] = vec4( finalColor, 1 );
  395.     //gl_FragData[3] = vec4( pixel.directLighting, 1 );
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement