Guest User

Untitled

a guest
Aug 30th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
4CS 38.57 KB | None | 0 0
  1. #version 120
  2.  
  3. /*
  4.  _______ _________ _______  _______  _
  5. (  ____ \\__   __/(  ___  )(  ____ )( )
  6. | (    \/   ) (   | (   ) || (    )|| |
  7. | (_____    | |   | |   | || (____)|| |
  8. (_____  )   | |   | |   | ||  _____)| |
  9.       ) |   | |   | |   | || (      (_)
  10. /\____) |   | |   | (___) || )       _
  11. \_______)   )_(   (_______)|/       (_)
  12.  
  13. Do not modify this code until you have read the LICENSE.txt contained in the root directory of this shaderpack!
  14.  
  15. */
  16.  
  17. #define BANDING_FIX_FACTOR 1.0f
  18. #define SMOOTH_SKY
  19.  
  20. /* DRAWBUFFERS:2 */
  21.  
  22. const bool gcolorMipmapEnabled = true;
  23. const bool compositeMipmapEnabled = true;
  24.  
  25. uniform sampler2D gcolor;
  26. uniform sampler2D gdepth;
  27. uniform sampler2D gdepthtex;
  28. uniform sampler2D gnormal;
  29. uniform sampler2D composite;
  30. uniform sampler2D noisetex;
  31. //uniform sampler2D gaux1;
  32.  
  33. uniform float near;
  34. uniform float far;
  35. uniform float viewWidth;
  36. uniform float viewHeight;
  37. uniform float rainStrength;
  38. uniform float wetness;
  39. uniform float aspectRatio;
  40. uniform float frameTimeCounter;
  41. uniform int worldTime;
  42.  
  43. uniform mat4 gbufferProjection;
  44. uniform mat4 gbufferProjectionInverse;
  45. uniform mat4 gbufferPreviousProjection;
  46. uniform mat4 gbufferModelViewInverse;
  47. uniform mat4 gbufferPreviousModelView;
  48. uniform mat4 shadowModelViewInverse;
  49.  
  50. uniform vec3 cameraPosition;
  51. uniform vec3 previousCameraPosition;
  52. uniform vec3 fogColor;
  53.  
  54. varying vec4 texcoord;
  55.  
  56. varying vec3 lightVector;
  57. varying vec3 upVector;
  58.  
  59. varying float timeSunrise;
  60. varying float timeNoon;
  61. varying float timeSunset;
  62. varying float timeMidnight;
  63. varying float timeSkyDark;
  64.  
  65. varying vec3 colorSunlight;
  66. varying vec3 colorSkylight;
  67. varying vec3 colorBouncedSunlight;
  68.  
  69.  
  70.  
  71.  
  72. #define ANIMATION_SPEED 1.0f
  73.  
  74. //#define ANIMATE_USING_WORLDTIME
  75.  
  76.  
  77.  
  78. #ifdef ANIMATE_USING_WORLDTIME
  79. #define FRAME_TIME worldTime * ANIMATION_SPEED / 20.0f
  80. #else
  81. #define FRAME_TIME frameTimeCounter * ANIMATION_SPEED
  82. #endif
  83.  
  84.  
  85. /////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  86. /////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87.  
  88. vec3 GetNormals(in vec2 coord) {
  89.     vec3 normal = vec3(0.0f);
  90.          normal = texture2DLod(gnormal, coord.st, 0).rgb;
  91.     normal = normal * 2.0f - 1.0f;
  92.  
  93.     normal = normalize(normal);
  94.  
  95.     return normal;
  96. }
  97.  
  98. float GetDepth(in vec2 coord) {
  99.     return texture2D(gdepthtex, coord).x;
  100. }
  101.  
  102. float   ExpToLinearDepth(in float depth)
  103. {
  104.     return 2.0f * near * far / (far + near - (2.0f * depth - 1.0f) * (far - near));
  105. }
  106.  
  107. float GetDepthLinear(vec2 coord) {
  108.     return 2.0 * near * far / (far + near - (2.0 * texture2D(gdepthtex, coord).x - 1.0) * (far - near));
  109. }
  110.  
  111. vec4    GetViewSpacePosition(in vec2 coord) {   //Function that calculates the screen-space position of the objects in the scene using the depth texture and the texture coordinates of the full-screen quad
  112.     float depth = GetDepth(coord);
  113.           //depth += float(GetMaterialMask(coord, 5)) * 0.38f;
  114.     vec4 fragposition = gbufferProjectionInverse * vec4(coord.s * 2.0f - 1.0f, coord.t * 2.0f - 1.0f, 2.0f * depth - 1.0f, 1.0f);
  115.          fragposition /= fragposition.w;
  116.    
  117.     return fragposition;
  118. }
  119.  
  120. float   GetMaterialIDs(in vec2 coord) {         //Function that retrieves the texture that has all material IDs stored in it
  121.     return texture2D(gdepth, coord).r;
  122. }
  123.  
  124. float GetSunlightVisibility(in vec2 coord)
  125. {
  126.     return texture2D(gdepth, coord).g;
  127. }
  128.  
  129. float cubicPulse(float c, float w, float x)
  130. {
  131.     x = abs(x - c);
  132.     if (x > w) return 0.0f;
  133.     x /= w;
  134.     return 1.0f - x * x * (3.0f - 2.0f * x);
  135. }
  136.  
  137. bool    GetMaterialMask(in vec2 coord, in int ID, in float matID) {
  138.           matID = floor(matID * 255.0f);
  139.  
  140.     if (matID == ID) {
  141.         return true;
  142.     } else {
  143.         return false;
  144.     }
  145. }
  146.  
  147. bool    GetSkyMask(in vec2 coord, in float matID)
  148. {
  149.     matID = floor(matID * 255.0f);
  150.  
  151.     if (matID < 1.0f || matID > 254.0f)
  152.     {
  153.         return true;
  154.     } else {
  155.         return false;
  156.     }
  157. }
  158.  
  159. bool    GetSkyMask(in vec2 coord)
  160. {
  161.     float matID = GetMaterialIDs(coord);
  162.     matID = floor(matID * 255.0f);
  163.  
  164.     if (matID < 1.0f || matID > 254.0f)
  165.     {
  166.         return true;
  167.     } else {
  168.         return false;
  169.     }
  170. }
  171.  
  172. float   GetSpecularity(in vec2 coord)
  173. {
  174.     return texture2D(composite, coord).r;
  175. }
  176.  
  177. float   GetRoughness(in vec2 coord)
  178. {
  179.     return texture2D(composite, coord).b;
  180. }
  181.  
  182. //Water
  183. float   GetWaterTex(in vec2 coord) {                //Function that returns the texture used for water. 0 means "this pixel is not water". 0.5 and greater means "this pixel is water".
  184.     return texture2D(gnormal, coord).b;     //values from 0.5 to 1.0 represent the amount of sky light hitting the surface of the water. It is used to simulate fake sky reflections in composite1.fsh
  185. }
  186.  
  187. bool    GetWaterMask(in float matID) {                  //Function that returns "true" if a pixel is water, and "false" if a pixel is not water.
  188.     matID = floor(matID * 255.0f);
  189.  
  190.     if (matID >= 35.0f && matID <= 51) {
  191.         return true;
  192.     } else {
  193.         return false;
  194.     }
  195. }
  196.  
  197. bool    GetWaterMask(in vec2 coord) {                   //Function that returns "true" if a pixel is water, and "false" if a pixel is not water.
  198.     float matID = floor(GetMaterialIDs(coord) * 255.0f);
  199.  
  200.     if (matID >= 35.0f && matID <= 51) {
  201.         return true;
  202.     } else {
  203.         return false;
  204.     }
  205. }
  206.  
  207. float   GetLightmapSky(in vec2 coord) {
  208.     return texture2D(gdepth, texcoord.st).b;
  209. }
  210.  
  211. vec3 convertScreenSpaceToWorldSpace(vec2 co) {
  212.     vec4 fragposition = gbufferProjectionInverse * vec4(vec3(co, texture2DLod(gdepthtex, co, 0).x) * 2.0 - 1.0, 1.0);
  213.     fragposition /= fragposition.w;
  214.     return fragposition.xyz;
  215. }
  216.  
  217. vec3 convertCameraSpaceToScreenSpace(vec3 cameraSpace) {
  218.     vec4 clipSpace = gbufferProjection * vec4(cameraSpace, 1.0);
  219.     vec3 NDCSpace = clipSpace.xyz / clipSpace.w;
  220.     vec3 screenSpace = 0.5 * NDCSpace + 0.5;
  221.          screenSpace.z = 0.1f;
  222.     return screenSpace;
  223. }
  224.  
  225. float   CalculateDitherPattern1() {
  226.     int[16] ditherPattern = int[16] (0 , 9 , 3 , 11,
  227.                                      13, 5 , 15, 7 ,
  228.                                      4 , 12, 2,  10,
  229.                                      16, 8 , 14, 6 );
  230.  
  231.     vec2 count = vec2(0.0f);
  232.          count.x = floor(mod(texcoord.s * viewWidth, 4.0f));
  233.          count.y = floor(mod(texcoord.t * viewHeight, 4.0f));
  234.  
  235.     int dither = ditherPattern[int(count.x) + int(count.y) * 4];
  236.  
  237.     return float(dither) / 17.0f;
  238. }
  239.  
  240. float   CalculateDitherPattern2() {
  241.     int[16] ditherPattern = int[16] (4 , 12, 2,  10,
  242.                                      16, 8 , 14, 6 ,
  243.                                      0 , 9 , 3 , 11,
  244.                                      13, 5 , 15, 7 );
  245.  
  246.     vec2 count = vec2(0.0f);
  247.          count.x = floor(mod(texcoord.s * viewWidth, 4.0f));
  248.          count.y = floor(mod(texcoord.t * viewHeight, 4.0f));
  249.  
  250.     int dither = ditherPattern[int(count.x) + int(count.y) * 4];
  251.  
  252.     return float(dither) / 17.0f;
  253. }
  254.  
  255. vec3    CalculateNoisePattern1(vec2 offset, float size) {
  256.     vec2 coord = texcoord.st;
  257.  
  258.     coord *= vec2(viewWidth, viewHeight);
  259.     coord = mod(coord + offset, vec2(size));
  260.     coord /= 64.0f;
  261.  
  262.     return texture2D(noisetex, coord).xyz;
  263. }
  264.  
  265. float noise (in float offset)
  266. {
  267.     vec2 coord = texcoord.st + vec2(offset);
  268.     float noise = clamp(fract(sin(dot(coord ,vec2(12.9898f,78.233f))) * 43758.5453f),0.0f,1.0f)*2.0f-1.0f;
  269.     return noise;
  270. }
  271.  
  272. float noise (in vec2 coord, in float offset)
  273. {
  274.     coord += vec2(offset);
  275.     float noise = clamp(fract(sin(dot(coord ,vec2(12.9898f,78.233f))) * 43758.5453f),0.0f,1.0f)*2.0f-1.0f;
  276.     return noise;
  277. }
  278.  
  279. void    DoNightEye(inout vec3 color) {          //Desaturates any color input at night, simulating the rods in the human eye
  280.    
  281.     float amount = 0.8f;                        //How much will the new desaturated and tinted image be mixed with the original image
  282.     vec3 rodColor = vec3(0.2f, 0.5f, 1.0f);     //Cyan color that humans percieve when viewing extremely low light levels via rod cells in the eye
  283.     float colorDesat = dot(color, vec3(1.0f));  //Desaturated color
  284.    
  285.     color = mix(color, vec3(colorDesat) * rodColor, timeSkyDark * amount);
  286.     //color.rgb = color.rgb;   
  287. }
  288.  
  289.  
  290. float Get3DNoise(in vec3 pos)
  291. {
  292.     pos.z += 0.0f;
  293.  
  294.     pos.xyz += 0.5f;
  295.  
  296.     vec3 p = floor(pos);
  297.     vec3 f = fract(pos);
  298.  
  299.     f.x = f.x * f.x * (3.0f - 2.0f * f.x);
  300.     f.y = f.y * f.y * (3.0f - 2.0f * f.y);
  301.     f.z = f.z * f.z * (3.0f - 2.0f * f.z);
  302.  
  303.     vec2 uv =  (p.xy + p.z * vec2(17.0f)) + f.xy;
  304.     vec2 uv2 = (p.xy + (p.z + 1.0f) * vec2(17.0f)) + f.xy;
  305.  
  306.     // uv -= 0.5f;
  307.     // uv2 -= 0.5f;
  308.  
  309.     vec2 coord =  (uv  + 0.5f) / 64.0f;
  310.     vec2 coord2 = (uv2 + 0.5f) / 64.0f;
  311.     float xy1 = texture2D(noisetex, coord).x;
  312.     float xy2 = texture2D(noisetex, coord2).x;
  313.     return mix(xy1, xy2, f.z);
  314. }
  315.  
  316. /////////////////////////STRUCTS///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  317. /////////////////////////STRUCTS///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  318.  
  319. struct MaskStruct {
  320.  
  321.     float matIDs;
  322.  
  323.     bool sky;
  324.     bool land;
  325.     bool tallGrass;
  326.     bool leaves;
  327.     bool ice;
  328.     bool hand;
  329.     bool translucent;
  330.     bool glow;
  331.     bool goldBlock;
  332.     bool ironBlock;
  333.     bool diamondBlock;
  334.     bool emeraldBlock;
  335.     bool sand;
  336.     bool sandstone;
  337.     bool stone;
  338.     bool cobblestone;
  339.     bool wool;
  340.  
  341.     bool torch;
  342.     bool lava;
  343.     bool glowstone;
  344.     bool fire;
  345.  
  346.     bool water;
  347.  
  348. };
  349.  
  350. struct Ray {
  351.     vec3 dir;
  352.     vec3 origin;
  353. };
  354.  
  355. struct Plane {
  356.     vec3 normal;
  357.     vec3 origin;
  358. };
  359.  
  360. struct SurfaceStruct {
  361.     MaskStruct      mask;           //Material ID Masks
  362.    
  363.     //Properties that are required for lighting calculation
  364.         vec3    color;                  //Diffuse texture aka "color texture"
  365.         vec3    normal;                 //Screen-space surface normals
  366.         float   depth;                  //Scene depth
  367.         float   linearDepth;            //Scene depth
  368.  
  369.         float   rDepth;
  370.         float   specularity;
  371.         vec3    specularColor;
  372.         float   roughness;
  373.         float   fresnelPower;
  374.         float   baseSpecularity;
  375.         Ray     viewRay;
  376.  
  377.  
  378.         vec4    viewSpacePosition;
  379.         vec4    worldSpacePosition;
  380.         vec3    worldLightVector;
  381.         vec3    upVector;
  382.         vec3    lightVector;
  383.  
  384.         float   sunlightVisibility;
  385.  
  386.         vec4    reflection;
  387.  
  388.         float   cloudAlpha;
  389. } surface;
  390.  
  391. struct Intersection {
  392.     vec3 pos;
  393.     float distance;
  394.     float angle;
  395. };
  396.  
  397.  
  398.  
  399. /////////////////////////STRUCT FUNCTIONS//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  400. /////////////////////////STRUCT FUNCTIONS//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  401.  
  402. void    CalculateMasks(inout MaskStruct mask) {
  403.     mask.sky            = GetSkyMask(texcoord.st, mask.matIDs);
  404.     mask.land           = !mask.sky;
  405.     mask.tallGrass      = GetMaterialMask(texcoord.st, 2, mask.matIDs);
  406.     mask.leaves         = GetMaterialMask(texcoord.st, 3, mask.matIDs);
  407.     mask.ice            = GetMaterialMask(texcoord.st, 4, mask.matIDs);
  408.     mask.hand           = GetMaterialMask(texcoord.st, 5, mask.matIDs);
  409.     mask.translucent    = GetMaterialMask(texcoord.st, 6, mask.matIDs);
  410.  
  411.     mask.glow           = GetMaterialMask(texcoord.st, 10, mask.matIDs);
  412.  
  413.     mask.goldBlock      = GetMaterialMask(texcoord.st, 20, mask.matIDs);
  414.     mask.ironBlock      = GetMaterialMask(texcoord.st, 21, mask.matIDs);
  415.     mask.diamondBlock   = GetMaterialMask(texcoord.st, 22, mask.matIDs);
  416.     mask.emeraldBlock   = GetMaterialMask(texcoord.st, 23, mask.matIDs);
  417.     mask.sand           = GetMaterialMask(texcoord.st, 24, mask.matIDs);
  418.     mask.sandstone      = GetMaterialMask(texcoord.st, 25, mask.matIDs);
  419.     mask.stone          = GetMaterialMask(texcoord.st, 26, mask.matIDs);
  420.     mask.cobblestone    = GetMaterialMask(texcoord.st, 27, mask.matIDs);
  421.     mask.wool           = GetMaterialMask(texcoord.st, 28, mask.matIDs);
  422.  
  423.     mask.torch          = GetMaterialMask(texcoord.st, 30, mask.matIDs);
  424.     mask.lava           = GetMaterialMask(texcoord.st, 31, mask.matIDs);
  425.     mask.glowstone      = GetMaterialMask(texcoord.st, 32, mask.matIDs);
  426.     mask.fire           = GetMaterialMask(texcoord.st, 33, mask.matIDs);
  427.  
  428.     mask.water          = GetWaterMask(mask.matIDs);
  429. }
  430.  
  431. vec4    ComputeRaytraceReflection(inout SurfaceStruct surface)
  432. {
  433.     float reflectionRange = 2.0f;
  434.     float initialStepAmount = 1.0 - clamp(0.1f / 100.0, 0.0, 0.99);
  435.           initialStepAmount *= 1.0f;
  436.  
  437.  
  438.      // vec2 dither = CalculateNoisePattern1(vec2(0.0f), 4.0f).xy * 2.0f - 1.0f;
  439.      // vec3 ditherNormal = vec3(0.0f);
  440.      //      ditherNormal.x = dither.x;
  441.      //      ditherNormal.y = dither.y;
  442.      //      ditherNormal.z = sqrt(1.0f - dither.x * dither.x - dither.y * dither.y);
  443.      //      ditherNormal.z = -1.0f;
  444.  
  445.      //      ditherNormal = normalize(ditherNormal);
  446.      //      ditherNormal -= normalize(surface.viewSpacePosition.xyz) * 1.0f;
  447.  
  448.  
  449.    
  450.     vec2 screenSpacePosition2D = texcoord.st;
  451.     vec3 cameraSpacePosition = convertScreenSpaceToWorldSpace(screenSpacePosition2D);
  452.    
  453.     vec3 cameraSpaceNormal = surface.normal;
  454.          //cameraSpaceNormal += ditherNormal * 0.65f * surface.roughness;
  455.          
  456.     vec3 cameraSpaceViewDir = normalize(cameraSpacePosition);
  457.     vec3 cameraSpaceVector = initialStepAmount * normalize(reflect(cameraSpaceViewDir,cameraSpaceNormal));
  458.     vec3 cameraSpaceVectorFar = far * normalize(reflect(cameraSpaceViewDir,cameraSpaceNormal));
  459.     vec3 oldPosition = cameraSpacePosition;
  460.     vec3 cameraSpaceVectorPosition = oldPosition + cameraSpaceVector;
  461.     vec3 currentPosition = convertCameraSpaceToScreenSpace(cameraSpaceVectorPosition);
  462.     vec4 color = vec4(pow(texture2D(gcolor, screenSpacePosition2D).rgb, vec3(3.0f + 1.2f)), 0.0);
  463.     const int maxRefinements = 3;
  464.     int numRefinements = 0;
  465.     int count = 0;
  466.     vec2 finalSamplePos = vec2(0.0f);
  467.  
  468.     int numSteps = 0;
  469.  
  470.     //while(count < far/initialStepAmount*reflectionRange)
  471.     for (int i = 0; i < 40; i++)
  472.     {
  473.         if(currentPosition.x < 0 || currentPosition.x > 1 ||
  474.            currentPosition.y < 0 || currentPosition.y > 1 ||
  475.            currentPosition.z < 0 || currentPosition.z > 1 ||
  476.            -cameraSpaceVectorPosition.z > far * 1.4f ||
  477.            -cameraSpaceVectorPosition.z < 0.0f)
  478.         {
  479.            break;
  480.         }
  481.  
  482.         vec2 samplePos = currentPosition.xy;
  483.         float sampleDepth = convertScreenSpaceToWorldSpace(samplePos).z;
  484.  
  485.         float currentDepth = cameraSpaceVectorPosition.z;
  486.         float diff = sampleDepth - currentDepth;
  487.         float error = length(cameraSpaceVector / pow(2.0f, numRefinements));
  488.  
  489.         //If a collision was detected, refine raymarch
  490.         if(diff >= 0 && diff <= error * 2.00f && numRefinements <= maxRefinements)
  491.         {
  492.             //Step back
  493.             cameraSpaceVectorPosition -= cameraSpaceVector / pow(2.0f, numRefinements);
  494.             ++numRefinements;
  495.         //If refinements run out
  496.         }
  497.         else if (diff >= 0 && diff <= error * 4.0f && numRefinements > maxRefinements)
  498.         {
  499.             finalSamplePos = samplePos;
  500.             break;
  501.         }
  502.        
  503.        
  504.        
  505.         cameraSpaceVectorPosition += cameraSpaceVector / pow(2.0f, numRefinements);
  506.  
  507.         if (numSteps > 1)
  508.         cameraSpaceVector *= 1.375f;    //Each step gets bigger
  509.  
  510.         currentPosition = convertCameraSpaceToScreenSpace(cameraSpaceVectorPosition);
  511.         count++;
  512.         numSteps++;
  513.     }
  514.    
  515.     color = pow(texture2DLod(gcolor, finalSamplePos, 0), vec4(2.2f));
  516.    
  517.     if (finalSamplePos.x == 0.0f || finalSamplePos.y == 0.0f) {
  518.         color.a = 0.0f;
  519.     }
  520.  
  521.     if (GetSkyMask(finalSamplePos))
  522.         color.a = 0.0f;
  523.  
  524.     // if (GetWaterMask(finalSamplePos))
  525.     //  color.a = 0.0f;
  526.    
  527.     color.a *= clamp(1 - pow(distance(vec2(0.5), finalSamplePos)*2.0, 2.0), 0.0, 1.0);
  528.     // color.a *= 1.0f - float(GetMaterialMask(finalSamplePos, 0, surface.mask.matIDs));
  529.  
  530.     //surface.color = vec3(numSteps / 10000000.0f);
  531.  
  532.     return color;
  533. }
  534.  
  535. float   CalculateLuminance(in vec3 color) {
  536.     return (color.r * 0.2126f + color.g * 0.7152f + color.b * 0.0722f);
  537. }
  538.  
  539. float   CalculateSunglow(in SurfaceStruct surface) {
  540.  
  541.     float curve = 4.0f;
  542.  
  543.     vec3 npos = normalize(surface.viewSpacePosition.xyz);
  544.     vec3 halfVector2 = normalize(-surface.lightVector + npos);
  545.     float factor = 1.0f - dot(halfVector2, npos);
  546.  
  547.     return factor * factor * factor * factor;
  548. }
  549.  
  550. float   CalculateReflectedSunglow(in SurfaceStruct surface) {
  551.  
  552.     float curve = 4.0f;
  553.  
  554.     vec3 npos = normalize(surface.viewSpacePosition.xyz);
  555.     surface.lightVector = reflect(surface.lightVector, surface.normal);
  556.     vec3 halfVector2 = normalize(-surface.lightVector + npos);
  557.     float factor = 1.0f - dot(halfVector2, npos);
  558.  
  559.     return factor * factor * factor * factor;
  560. }
  561.  
  562. float   CalculateAntiSunglow(in SurfaceStruct surface) {
  563.  
  564.     float curve = 4.0f;
  565.  
  566.     vec3 npos = normalize(surface.viewSpacePosition.xyz);
  567.     vec3 halfVector2 = normalize(surface.lightVector + npos);
  568.     float factor = 1.0f - dot(halfVector2, npos);
  569.  
  570.     return factor * factor * factor * factor;
  571. }
  572.  
  573. float   CalculateSunspot(in SurfaceStruct surface) {
  574.  
  575.     float curve = 1.0f;
  576.  
  577.     vec3 npos = normalize(surface.viewSpacePosition.xyz);
  578.     vec3 halfVector2 = normalize(-surface.lightVector + npos);
  579.  
  580.     float sunProximity = abs(1.0f - dot(halfVector2, npos));
  581.  
  582.     //surface.roughness = 0.5f;
  583.  
  584.     float sizeFactor = 0.959f - surface.roughness * 0.7f;
  585.  
  586.     float sunSpot = (clamp(sunProximity, sizeFactor, 0.96f) - sizeFactor) / (0.96f - sizeFactor);
  587.           sunSpot = pow(cubicPulse(1.0f, 1.0f, sunSpot), 2.0f);
  588.  
  589.     // if (sunProximity > 0.96f) {
  590.     //  return 1.0f;
  591.     // } else {
  592.     //  return 0.0f;
  593.     // }
  594.  
  595.     float result = sunSpot / (surface.roughness * 20.0f + 0.1f);
  596.  
  597.           result *= surface.sunlightVisibility;
  598.  
  599.     return result;
  600.     //return 0.0f;
  601. }
  602.  
  603. vec3    ComputeReflectedSkyGradient(in SurfaceStruct surface) {
  604.     float curve = 5.0f;
  605.     surface.viewSpacePosition.xyz = reflect(surface.viewSpacePosition.xyz, surface.normal);
  606.     vec3 npos = normalize(surface.viewSpacePosition.xyz);
  607.  
  608.     //surface.upVector = reflect(upVector, surface.normal);
  609.     //surface.lightVector = reflect(lightVector, surface.normal);
  610.  
  611.     vec3 halfVector2 = normalize(-surface.upVector + npos);
  612.     float skyGradientFactor = dot(halfVector2, npos);
  613.     float skyGradientRaw = skyGradientFactor;
  614.     float skyDirectionGradient = skyGradientFactor;
  615.  
  616.     if (dot(halfVector2, npos) > 0.75)
  617.         skyGradientFactor = 1.5f - skyGradientFactor;
  618.  
  619.     skyGradientFactor = pow(skyGradientFactor, curve);
  620.  
  621.     vec3 skyColor = CalculateLuminance(pow(gl_Fog.color.rgb, vec3(2.2f))) * colorSkylight;
  622.  
  623.     skyColor *= mix(skyGradientFactor, 1.0f, clamp((0.12f - (timeNoon * 0.1f)) + rainStrength, 0.0f, 1.0f));
  624.     skyColor *= pow(skyGradientFactor, 2.5f) + 0.2f;
  625.     skyColor *= (pow(skyGradientFactor, 1.1f) + 0.425f) * 0.5f;
  626.     skyColor.g *= skyGradientFactor * 3.0f + 1.0f;
  627.  
  628.  
  629.     vec3 linFogColor = pow(gl_Fog.color.rgb, vec3(2.2f));
  630.  
  631.     float fogLum = max(max(linFogColor.r, linFogColor.g), linFogColor.b);
  632.  
  633.  
  634.     float fadeSize = 0.0f;
  635.  
  636.     float fade1 = clamp(skyGradientFactor - 0.05f - fadeSize, 0.0f, 0.2f + fadeSize) / (0.2f + fadeSize);
  637.           fade1 = fade1 * fade1 * (3.0f - 2.0f * fade1);
  638.     vec3 color1 = vec3(5.0f, 2.0, 0.7f) * 0.25f;
  639.          color1 = mix(color1, vec3(1.0f, 0.55f, 0.2f), vec3(timeSunrise + timeSunset));
  640.  
  641.     skyColor *= mix(vec3(1.0f), color1, vec3(fade1));
  642.  
  643.     float fade2 = clamp(skyGradientFactor - 0.11f - fadeSize, 0.0f, 0.2f + fadeSize) / (0.2f + fadeSize);
  644.     vec3 color2 = vec3(1.7f, 1.0f, 0.8f) / 2.0f;
  645.          color2 = mix(color2, vec3(1.0f, 0.15f, 0.5f), vec3(timeSunrise + timeSunset));
  646.  
  647.  
  648.     skyColor *= mix(vec3(1.0f), color2, vec3(fade2 * 0.5f));
  649.  
  650.  
  651.  
  652.  
  653.     float horizonGradient = 1.0f - distance(skyDirectionGradient, 0.72f + fadeSize) / (0.72f + fadeSize);
  654.           horizonGradient = pow(horizonGradient, 10.0f);
  655.           horizonGradient = max(0.0f, horizonGradient);
  656.  
  657.     float sunglow = CalculateSunglow(surface);
  658.           horizonGradient *= sunglow * 2.0f+ (0.65f - timeSunrise * 0.55f - timeSunset * 0.55f);
  659.  
  660.     vec3 horizonColor1 = vec3(1.5f, 1.5f, 1.5f);
  661.          horizonColor1 = mix(horizonColor1, vec3(1.5f, 1.95f, 0.5f) * 2.0f, vec3(timeSunrise + timeSunset));
  662.     vec3 horizonColor2 = vec3(1.5f, 1.2f, 0.8f) * 1.0f;
  663.          horizonColor2 = mix(horizonColor2, vec3(1.9f, 0.6f, 0.4f) * 2.0f, vec3(timeSunrise + timeSunset));
  664.  
  665.     skyColor *= mix(vec3(1.0f), horizonColor1, vec3(horizonGradient) * (1.0f - timeMidnight));
  666.     skyColor *= mix(vec3(1.0f), horizonColor2, vec3(pow(horizonGradient, 2.0f)) * (1.0f - timeMidnight));
  667.  
  668.     float grayscale = fogLum / 20.0f;
  669.           grayscale /= 3.0f;
  670.  
  671.     float rainSkyBrightness = 1.2f;
  672.           rainSkyBrightness *= mix(0.05f, 10.0f, timeMidnight);
  673.  
  674.     skyColor = mix(skyColor, vec3(grayscale * colorSkylight.r) * 0.06f * vec3(0.85f, 0.85f, 1.0f), vec3(rainStrength));
  675.  
  676.  
  677.     skyColor /= fogLum;
  678.  
  679.  
  680.     float antiSunglow = CalculateAntiSunglow(surface);
  681.  
  682.     skyColor *= 1.0f + pow(sunglow, 1.1f) * (7.0f + timeNoon * 1.0f) * (1.0f - rainStrength);
  683.     skyColor *= mix(vec3(1.0f), colorSunlight * 11.0f, clamp(vec3(sunglow) * (1.0f - timeMidnight) * (1.0f - rainStrength), vec3(0.0f), vec3(1.0f)));
  684.     skyColor *= 1.0f + antiSunglow * 2.0f * (1.0f - rainStrength);
  685.  
  686.  
  687.     if (surface.mask.water)
  688.     {
  689.         vec3 sunspot = vec3(CalculateSunspot(surface)) * colorSunlight;
  690.              sunspot *= 50.0f;
  691.              sunspot *= 1.0f - timeMidnight;
  692.              sunspot *= 1.0f - rainStrength;
  693.  
  694.  
  695.         skyColor += sunspot;
  696.     }
  697.  
  698.     skyColor *= pow(1.0f - clamp(skyGradientRaw - 0.75f, 0.0f, 0.25f) / 0.25f, 3.0f);
  699.  
  700.     skyColor *= mix(1.0f, 4.5f, timeNoon);
  701.  
  702.  
  703.     return skyColor;
  704. }
  705.  
  706. Intersection    RayPlaneIntersectionWorld(in Ray ray, in Plane plane)
  707. {
  708.     float rayPlaneAngle = dot(ray.dir, plane.normal);
  709.  
  710.     float planeRayDist = 100000000.0f;
  711.     vec3 intersectionPos = ray.dir * planeRayDist;
  712.  
  713.     if (rayPlaneAngle > 0.0001f || rayPlaneAngle < -0.0001f)
  714.     {
  715.         planeRayDist = dot((plane.origin), plane.normal) / rayPlaneAngle;
  716.         intersectionPos = ray.dir * planeRayDist;
  717.         intersectionPos = -intersectionPos;
  718.  
  719.         intersectionPos += cameraPosition.xyz;
  720.     }
  721.  
  722.     Intersection i;
  723.  
  724.     i.pos = intersectionPos;
  725.     i.distance = planeRayDist;
  726.     i.angle = rayPlaneAngle;
  727.  
  728.     return i;
  729. }
  730.  
  731.  
  732. float GetCoverage(in float coverage, in float density, in float clouds)
  733. {
  734.     clouds = clamp(clouds - (1.0f - coverage), 0.0f, 1.0f - density) / (1.0f - density);
  735.     clouds = max(0.0f, clouds * 1.1f - 0.1f);
  736.     // clouds = clouds = clouds * clouds * (3.0f - 2.0f * clouds);
  737.     // clouds = pow(clouds, 1.0f);
  738.     return clouds;
  739. }
  740.  
  741. vec4 CloudColor2(in vec4 worldPosition, in float sunglow, in vec3 worldLightVector, in float altitude, in float thickness, const bool isShadowPass)
  742. {
  743.  
  744.     float cloudHeight = altitude;
  745.     float cloudDepth  = thickness;
  746.     float cloudUpperHeight = cloudHeight + (cloudDepth / 2.0f);
  747.     float cloudLowerHeight = cloudHeight - (cloudDepth / 2.0f);
  748.  
  749.     worldPosition.xz /= 1.0f + max(0.0f, length(worldPosition.xz - cameraPosition.xz) / 9001.0f);
  750.  
  751.     vec3 p = worldPosition.xyz / 100.0f;
  752.  
  753.        
  754.  
  755.     float t = FRAME_TIME * 1.0f;
  756.           t *= 0.4;
  757.  
  758.  
  759.      p += (Get3DNoise(p * 2.0f + vec3(0.0f, t * 0.01f, 0.0f)) * 2.0f - 1.0f) * 0.10f;
  760.  
  761.       p.x -= (Get3DNoise(p * 0.125f + vec3(0.0f, t * 0.01f, 0.0f)) * 2.0f - 1.0f) * 1.2f;
  762.     // p.xz -= (Get3DNoise(p * 0.0525f + vec3(0.0f, t * 0.01f, 0.0f)) * 2.0f - 1.0f) * 1.7f;
  763.  
  764.  
  765.     p.x *= 0.25f;
  766.     p.x -= t * 0.01f;
  767.  
  768.     vec3 p1 = p * vec3(1.0f, 0.5f, 1.0f)  + vec3(0.0f, t * 0.01f, 0.0f);
  769.     float noise  =  Get3DNoise(p * vec3(1.0f, 0.5f, 1.0f) + vec3(0.0f, t * 0.01f, 0.0f));   p *= 2.0f;  p.x -= t * 0.017f;  p.z += noise * 1.35f;   p.x += noise * 0.5f;                                    vec3 p2 = p;
  770.           noise += (2.0f - abs(Get3DNoise(p) * 2.0f - 0.0f)) * (0.25f);                     p *= 3.0f;  p.xz -= t * 0.005f; p.z += noise * 1.35f;   p.x += noise * 0.5f;    p.x *= 3.0f;    p.z *= 0.55f;   vec3 p3 = p;
  771.              p.z -= (Get3DNoise(p * 0.25f + vec3(0.0f, t * 0.01f, 0.0f)) * 2.0f - 1.0f) * 0.4f;
  772.           noise += (3.0f - abs(Get3DNoise(p) * 3.0f - 0.0f)) * (0.035f);                    p *= 3.0f;  p.xz -= t * 0.005f;                                                                                 vec3 p4 = p;
  773.           noise += (3.0f - abs(Get3DNoise(p) * 3.0f - 0.0f)) * (0.025f);                    p *= 3.0f;  p.xz -= t * 0.005f;
  774.           if (!isShadowPass)
  775.           {
  776.                 noise += ((Get3DNoise(p))) * (0.022f);                                              p *= 3.0f;
  777.                  noise += ((Get3DNoise(p))) * (0.024f);
  778.           }
  779.           noise /= 1.575f;
  780.  
  781.     //cloud edge
  782.     float rainy = mix(wetness, 1.0f, rainStrength);
  783.           //rainy = 0.0f;
  784.     float coverage = 0.55f + rainy * 0.35f;
  785.           //coverage = mix(coverage, 0.97f, rainStrength);
  786.  
  787.           float dist = length(worldPosition.xz - cameraPosition.xz);
  788.           coverage *= max(0.0f, 1.0f - dist / mix(10000.0f, 3000.0f, rainStrength));
  789.     float density = 0.0f;
  790.  
  791.     if (isShadowPass)
  792.     {
  793.         return vec4(GetCoverage(coverage + 0.2f, density + 0.2f, noise));
  794.     }
  795.     else
  796.     {
  797.  
  798.         noise = GetCoverage(coverage, density, noise);
  799.         noise = noise * noise * (3.0f - 2.0f * noise);
  800.  
  801.         const float lightOffset = 0.2f;
  802.  
  803.  
  804.  
  805.         float sundiff = Get3DNoise(p1 + worldLightVector.xyz * lightOffset);
  806.               sundiff += (2.0f - abs(Get3DNoise(p2 + worldLightVector.xyz * lightOffset / 2.0f) * 2.0f - 0.0f)) * (0.55f);     
  807.                             float largeSundiff = sundiff;
  808.                                   largeSundiff = -GetCoverage(coverage, 0.0f, largeSundiff * 1.3f);
  809.               sundiff += (3.0f - abs(Get3DNoise(p3 + worldLightVector.xyz * lightOffset / 5.0f) * 3.0f - 0.0f)) * (0.065f);
  810.               sundiff += (3.0f - abs(Get3DNoise(p4 + worldLightVector.xyz * lightOffset / 8.0f) * 3.0f - 0.0f)) * (0.025f);
  811.               sundiff /= 1.5f;
  812.               sundiff = -GetCoverage(coverage * 1.0f, 0.0f, sundiff);
  813.         float secondOrder   = pow(clamp(sundiff * 1.00f + 1.35f, 0.0f, 1.0f), 7.0f);
  814.         float firstOrder    = pow(clamp(largeSundiff * 1.1f + 1.56f, 0.0f, 1.0f), 3.0f);
  815.  
  816.  
  817.  
  818.         float directLightFalloff = secondOrder;
  819.         float anisoBackFactor = mix(clamp(pow(noise, 1.6f) * 2.5f, 0.0f, 1.0f), 1.0f, pow(sunglow, 1.0f));
  820.  
  821.               directLightFalloff *= anisoBackFactor;
  822.               directLightFalloff *= mix(11.5f, 1.0f, pow(sunglow, 0.5f));
  823.        
  824.  
  825.  
  826.         vec3 colorDirect = colorSunlight * 1.915f;
  827.              colorDirect = mix(colorDirect, colorDirect * vec3(0.2f, 0.5f, 1.0f), timeMidnight);
  828.              colorDirect *= 1.0f + pow(sunglow, 2.0f) * 100.0f * pow(directLightFalloff, 1.1f) * (1.0f - rainStrength);
  829.              // colorDirect *= 1.0f + pow(1.0f - sunglow, 2.0f) * 30.0f * pow(directLightFalloff, 1.1f) * (1.0f - rainStrength);
  830.  
  831.  
  832.         vec3 colorAmbient = mix(colorSkylight, colorSunlight * 2.0f, vec3(0.15f)) * 0.04f;
  833.              colorAmbient *= mix(1.0f, 0.3f, timeMidnight);
  834.              colorAmbient *= mix(1.0f, ((1.0f - noise) + 0.5f) * 1.4f, rainStrength);
  835.              // colorAmbient = mix(colorAmbient, colorAmbient * 3.0f + colorSunlight * 0.05f, vec3(clamp(pow(1.0f - noise, 12.0f) * 1.0f, 0.0f, 1.0f)));
  836.  
  837.  
  838.         directLightFalloff *= 1.0f - rainStrength * 0.45f;
  839.  
  840.  
  841.         //directLightFalloff += (pow(Get3DNoise(p3), 2.0f) * 0.5f + pow(Get3DNoise(p3 * 1.5f), 2.0f) * 0.25f) * 0.02f;
  842.         //directLightFalloff *= Get3DNoise(p2);
  843.  
  844.         vec3 color = mix(colorAmbient, colorDirect, vec3(min(1.0f, directLightFalloff)));
  845.  
  846.         color *= 1.0f;
  847.  
  848.         // noise *= mix(1.0f, 5.0f, sunglow);
  849.  
  850.         vec4 result = vec4(color, noise);
  851.  
  852.         return result;
  853.     }
  854.    
  855. }
  856. void ReflectedCloudPlane(inout vec3 color, inout SurfaceStruct surface)
  857. {
  858.     //Initialize view ray
  859.     vec3 viewVector = normalize(surface.viewSpacePosition.xyz);
  860.          viewVector = reflect(viewVector, surface.normal.xyz);
  861.     vec4 worldVector = gbufferModelViewInverse * (vec4(-viewVector.xyz, 0.0f));
  862.  
  863.     surface.viewRay.dir = normalize(worldVector.xyz);
  864.     surface.viewRay.origin = vec3(0.0f);
  865.  
  866.     float sunglow = CalculateReflectedSunglow(surface);
  867.  
  868.  
  869.     float cloudsAltitude = 540.0f;
  870.     float cloudsThickness = 150.0f;
  871.  
  872.     float cloudsUpperLimit = cloudsAltitude + cloudsThickness * 0.5f;
  873.     float cloudsLowerLimit = cloudsAltitude - cloudsThickness * 0.5f;
  874.  
  875.     float density = 1.0f;
  876.  
  877.     float planeHeight = cloudsUpperLimit;
  878.     float stepSize = 25.5f;
  879.     planeHeight -= cloudsThickness * 0.85f;
  880.  
  881.  
  882.     Plane pl;
  883.     pl.origin = vec3(0.0f, cameraPosition.y - planeHeight, 0.0f);
  884.     pl.normal = vec3(0.0f, 1.0f, 0.0f);
  885.  
  886.     Intersection i = RayPlaneIntersectionWorld(surface.viewRay, pl);
  887.  
  888.     if (i.angle < 0.0f)
  889.     {
  890.         vec4 cloudSample = CloudColor2(vec4(i.pos.xyz * 0.5f + vec3(30.0f), 1.0f), sunglow, surface.worldLightVector, cloudsAltitude, cloudsThickness, false);
  891.              cloudSample.a = min(1.0f, cloudSample.a * density);
  892.  
  893.         color.rgb = mix(color.rgb, cloudSample.rgb * 0.18f, cloudSample.a);
  894.  
  895.         cloudSample = CloudColor2(vec4(i.pos.xyz * 0.65f + vec3(10.0f) + vec3(i.pos.z * 0.5f, 0.0f, 0.0f), 1.0f), sunglow, surface.worldLightVector, cloudsAltitude, cloudsThickness, false);
  896.         cloudSample.a = min(1.0f, cloudSample.a * density);
  897.  
  898.         color.rgb = mix(color.rgb, cloudSample.rgb * 0.18f, cloudSample.a);
  899.     }
  900. }
  901.  
  902. void CloudPlane(inout SurfaceStruct surface)
  903. {
  904.     //Initialize view ray
  905.     vec4 worldVector = gbufferModelViewInverse * (-GetViewSpacePosition(texcoord.st));
  906.  
  907.     surface.viewRay.dir = normalize(worldVector.xyz);
  908.     surface.viewRay.origin = vec3(0.0f);
  909.  
  910.     float sunglow = CalculateSunglow(surface);
  911.  
  912.  
  913.  
  914.     float cloudsAltitude = 540.0f;
  915.     float cloudsThickness = 150.0f;
  916.  
  917.     float cloudsUpperLimit = cloudsAltitude + cloudsThickness * 0.5f;
  918.     float cloudsLowerLimit = cloudsAltitude - cloudsThickness * 0.5f;
  919.  
  920.     float density = 1.0f;
  921.  
  922.     float planeHeight = cloudsUpperLimit;
  923.     float stepSize = 25.5f;
  924.     planeHeight -= cloudsThickness * 0.85f;
  925.  
  926.  
  927.     Plane pl;
  928.     pl.origin = vec3(0.0f, cameraPosition.y - planeHeight, 0.0f);
  929.     pl.normal = vec3(0.0f, 1.0f, 0.0f);
  930.  
  931.     Intersection i = RayPlaneIntersectionWorld(surface.viewRay, pl);
  932.  
  933.     if (i.angle < 0.0f)
  934.     {
  935.         if (i.distance < surface.linearDepth || surface.mask.sky)
  936.         {
  937.             vec4 cloudSample = CloudColor2(vec4(i.pos.xyz * 0.5f + vec3(30.0f), 1.0f), sunglow, surface.worldLightVector, cloudsAltitude, cloudsThickness, false);
  938.                  cloudSample.a = min(1.0f, cloudSample.a * density);
  939.  
  940.             surface.color.rgb = mix(surface.color.rgb, cloudSample.rgb * 0.001f, cloudSample.a);
  941.  
  942.             cloudSample = CloudColor2(vec4(i.pos.xyz * 0.65f + vec3(10.0f) + vec3(i.pos.z * 0.5f, 0.0f, 0.0f), 1.0f), sunglow, surface.worldLightVector, cloudsAltitude, cloudsThickness, false);
  943.             cloudSample.a = min(1.0f, cloudSample.a * density);
  944.  
  945.             surface.color.rgb = mix(surface.color.rgb, cloudSample.rgb * 0.001f, cloudSample.a);
  946.  
  947.         }
  948.     }
  949. }
  950.  
  951. vec4    ComputeFakeSkyReflection(in SurfaceStruct surface) {
  952.  
  953.     vec3 cameraSpacePosition = convertScreenSpaceToWorldSpace(texcoord.st);
  954.     vec3 cameraSpaceNormal = surface.normal;
  955.     vec3 cameraSpaceViewDir = normalize(cameraSpacePosition);
  956.     vec4 color = vec4(0.0f);
  957.  
  958.     color.rgb = ComputeReflectedSkyGradient(surface);
  959.     ReflectedCloudPlane(color.rgb, surface);
  960.     color.rgb *= 0.006f;
  961.     color.rgb *= mix(1.0f, 20000.0f, timeSkyDark);
  962.  
  963.     float viewVector = dot(cameraSpaceViewDir, cameraSpaceNormal);
  964.  
  965.     color.a = pow(clamp(1.0f + viewVector, 0.0f, 1.0f), surface.fresnelPower) * (1.0f - surface.baseSpecularity) + surface.baseSpecularity;
  966.  
  967.     if (viewVector > 0.0f) {
  968.         color.a = 1.0f - pow(clamp(viewVector, 0.0f, 1.0f), 1.0f / surface.fresnelPower) * (1.0f - surface.baseSpecularity) + surface.baseSpecularity;
  969.         color.rgb = vec3(0.0f);
  970.     }
  971.  
  972.  
  973.     DoNightEye(color.rgb);
  974.  
  975.     color.rgb *= mix(1.0f, 0.125f, timeMidnight);
  976.  
  977.     return color;
  978. }
  979.  
  980. void    CalculateSpecularReflections(inout SurfaceStruct surface) {
  981.  
  982.     float specularity = surface.specularity * surface.specularity * surface.specularity;
  983.           specularity = max(0.0f, specularity * 1.15f - 0.15f);
  984.     surface.specularColor = vec3(1.0f);
  985.     //surface.specularity = 1.0f;
  986.     //surface.roughness *= surface.roughness;
  987.  
  988.     bool defaultItself = false;
  989.  
  990.     surface.rDepth = 0.0f;
  991.  
  992.     if (surface.mask.sky)
  993.         specularity = 0.0f;
  994.  
  995.     if (surface.mask.water)
  996.     {
  997.         specularity = 0.7f;
  998.         surface.roughness = 0.0f;
  999.         surface.fresnelPower = 6.0f;
  1000.         surface.baseSpecularity = 0.02f;
  1001.     }
  1002.  
  1003.     if (surface.mask.ironBlock)
  1004.     {
  1005.         surface.baseSpecularity = 1.0f;
  1006.         //specularity = 1.0f;
  1007.         //surface.roughness = 0.0f;
  1008.     }
  1009.  
  1010.     if (surface.mask.goldBlock)
  1011.     {
  1012.         //surface.specularity = 1.0f;
  1013.         //surface.roughness = 0.4f;
  1014.         surface.baseSpecularity = 1.0f;
  1015.         surface.specularColor = vec3(1.0f, 0.32f, 0.002f);
  1016.         surface.specularColor = mix(surface.specularColor, vec3(1.0f), vec3(0.015f));
  1017.     }
  1018.  
  1019.     //surface.roughness = 0.0f;
  1020.  
  1021.     specularity *= 1.0f - surface.cloudAlpha;
  1022.  
  1023.     if (specularity > 0.00f) {
  1024.  
  1025.         vec3 noise3 = vec3(noise(0.0f), noise(1.0f), noise(2.0f));
  1026.  
  1027.         surface.normal += noise3 * 0.00f;
  1028.  
  1029.         vec4 reflection = ComputeRaytraceReflection(surface);
  1030.         //vec4 reflection = vec4(0.0f);
  1031.  
  1032.         float surfaceLightmap = GetLightmapSky(texcoord.st);
  1033.  
  1034.         vec4 fakeSkyReflection = ComputeFakeSkyReflection(surface);
  1035.  
  1036.         vec3 noSkyToReflect = vec3(0.0f);
  1037.  
  1038.         if (defaultItself)
  1039.         {
  1040.             noSkyToReflect = surface.color.rgb;
  1041.         }
  1042.  
  1043.         fakeSkyReflection.rgb = mix(noSkyToReflect, fakeSkyReflection.rgb, clamp(surfaceLightmap * 16 - 5, 0.0f, 1.0f));
  1044.         reflection.rgb = mix(reflection.rgb, fakeSkyReflection.rgb, pow(vec3(1.0f - reflection.a), vec3(10.1f)));
  1045.         reflection.a = fakeSkyReflection.a * specularity;
  1046.  
  1047.  
  1048.         reflection.rgb *= surface.specularColor;
  1049.  
  1050.         surface.color.rgb = mix(surface.color.rgb, reflection.rgb, vec3(reflection.a));
  1051.         surface.reflection = reflection;
  1052.     }
  1053. }
  1054.  
  1055. void CalculateSpecularHighlight(inout SurfaceStruct surface)
  1056. {
  1057.     if (!surface.mask.sky && !surface.mask.water)
  1058.     {
  1059.         //surface.specularity = 0.51f;
  1060.         //surface.roughness = 0.2f;
  1061.  
  1062.         vec3 halfVector = normalize(lightVector - normalize(surface.viewSpacePosition.xyz));
  1063.  
  1064.         float HdotN = max(0.0f, dot(halfVector, surface.normal.xyz));
  1065.  
  1066.         //surface.roughness = sin(FRAME_TIME * 3.1415f) * 0.5f + 0.5f;
  1067.  
  1068.         float gloss = pow(1.0f - surface.roughness + 0.01f, 4.5f);
  1069.  
  1070.         HdotN = clamp(HdotN * (1.0f + gloss * 0.01f), 0.0f, 1.0f);
  1071.  
  1072.         float spec = pow(HdotN, gloss * 8000.0f + 10.0f);
  1073.  
  1074.         //spec *= float(!surface.mask.sky);
  1075.  
  1076.         float fresnel = pow(clamp(1.0f + dot(normalize(surface.viewSpacePosition.xyz), surface.normal.xyz), 0.0f, 1.0f), surface.fresnelPower) * (1.0f - surface.baseSpecularity) + surface.baseSpecularity;
  1077.  
  1078.  
  1079.         spec *= fresnel;
  1080.         spec *= surface.sunlightVisibility;
  1081.  
  1082.         //spec *= pow(1.0f - surface.roughness, 1.5f) * 80000.0f;
  1083.         spec *= gloss * 9000.0f + 10.0f;
  1084.         spec *= surface.specularity * surface.specularity * surface.specularity;
  1085.         spec *= 1.0f - rainStrength;
  1086.  
  1087.         vec3 specularHighlight = spec * mix(colorSunlight, vec3(0.2f, 0.5f, 1.0f) * 0.0005f, vec3(timeMidnight)) * surface.specularColor;
  1088.  
  1089.  
  1090.         surface.color += specularHighlight / 500.0f;
  1091.     }
  1092. }
  1093.  
  1094. void CalculateGlossySpecularReflections(inout SurfaceStruct surface)
  1095. {
  1096.     float specularity = surface.specularity;
  1097.     float roughness = 0.7f;
  1098.     float spread = 0.02f;
  1099.  
  1100.     specularity *= 1.0f - float(surface.mask.sky);
  1101.  
  1102.     vec4 reflectionSum = vec4(0.0f);
  1103.  
  1104.     surface.fresnelPower = 6.0f;
  1105.     surface.baseSpecularity = 0.0f;
  1106.  
  1107.     if (surface.mask.ironBlock)
  1108.     {
  1109.         roughness = 0.9f;
  1110.         //specularity = 1.0f;
  1111.         //surface.baseSpecularity = 1.0f;
  1112.     }
  1113.  
  1114.     if (surface.mask.goldBlock)
  1115.     {
  1116.         specularity = 0.0f;
  1117.     }
  1118.  
  1119.  
  1120.  
  1121.     if (specularity > 0.01f)
  1122.     {
  1123.         float fresnel = 1.0f - clamp(-dot(normalize(surface.viewSpacePosition.xyz), surface.normal.xyz), 0.0f, 1.0f);
  1124.  
  1125.         for (int i = 1; i <= 10; i++)
  1126.         {
  1127.             vec2 translation = vec2(surface.normal.x, surface.normal.y) * i * spread;
  1128.                  translation *= vec2(1.0f, viewWidth / viewHeight);
  1129.             //vec2 scaling = (4.0f - vec2(fresnel) * 3.0f);
  1130.  
  1131.             float faceFactor = surface.normal.z;
  1132.                   faceFactor *= spread * 13.0f;
  1133.  
  1134.             vec2 scaling = vec2(1.0f + faceFactor * (i / 10.0f) * 2.0f);
  1135.  
  1136.             float r = float(i) + 4.0f;
  1137.                   r *= roughness * 0.8f;
  1138.             int     ri = int(floor(r));
  1139.             float   rf = fract(r);
  1140.  
  1141.             vec2 finalCoord = (((texcoord.st * 2.0f - 1.0f) * scaling) * 0.5f + 0.5f) + translation;
  1142.  
  1143.             float weight = (11 - i + 1) / 10.0f;
  1144.             reflectionSum.rgb += pow(texture2DLod(gcolor, finalCoord, r).rgb, vec3(2.2f));
  1145.         }
  1146.  
  1147.  
  1148.  
  1149.         reflectionSum.rgb /= 10.0f;
  1150.  
  1151.         fresnel *= 0.9;
  1152.         fresnel = pow(fresnel, surface.fresnelPower);
  1153.  
  1154.         surface.color = mix(surface.color, reflectionSum.rgb * 1.0f, vec3(specularity) * fresnel * (1.0f - surface.baseSpecularity) + surface.baseSpecularity);
  1155.         }
  1156.     //surface.color.rgb *= vec3(1.0f) + reflectionSum.rgb * 400000.2f;
  1157. }
  1158.  
  1159. vec4 TextureSmooth(in sampler2D tex, in vec2 coord, in int level)
  1160. {
  1161.     vec2 res = vec2(viewWidth, viewHeight);
  1162.     coord = coord * res + 0.5f;
  1163.     vec2 i = floor(coord);
  1164.     vec2 f = fract(coord);
  1165.     f = f * f * (3.0f - 2.0f * f);
  1166.     coord = i + f;
  1167.     coord = (coord - 0.5f) / res;
  1168.     return texture2D(tex, coord, level);
  1169. }
  1170.  
  1171. void SmoothSky(inout SurfaceStruct surface)
  1172. {
  1173.     const float cloudHeight = 170.0f;
  1174.     const float cloudDepth = 60.0f;
  1175.     const float cloudMaxHeight = cloudHeight + cloudDepth / 2.0f;
  1176.     const float cloudMinHeight = cloudHeight - cloudDepth / 2.0f;
  1177.  
  1178.     float cameraHeight = cameraPosition.y;
  1179.     float surfaceHeight = surface.worldSpacePosition.y;
  1180.  
  1181.     vec3 combined = pow(TextureSmooth(gcolor, texcoord.st, 2).rgb, vec3(2.2f));
  1182.     vec3 original = surface.color;
  1183.  
  1184.     if (surface.cloudAlpha > 0.0001f)
  1185.     {
  1186.         surface.color = combined;
  1187.     }
  1188.  
  1189.     if (cameraHeight < cloudMinHeight && surfaceHeight < cloudMinHeight - 10.0f && surface.mask.land)
  1190.     {
  1191.         surface.color = original;
  1192.     }
  1193.  
  1194.     if (cameraHeight > cloudMaxHeight && surfaceHeight > cloudMaxHeight && surface.mask.land)
  1195.     {
  1196.         surface.color = original;
  1197.     }
  1198. }
  1199.  
  1200.  
  1201. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1202. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1203. /////////////////////////////////////////////////////////////MAIN//////////////////////////////////////////////////////////////////////////////
  1204. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1205. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1206. void main() {
  1207.    
  1208.     surface.color = pow(texture2DLod(gcolor, texcoord.st, 0).rgb, vec3(2.2f));
  1209.     surface.normal = GetNormals(texcoord.st);
  1210.     surface.depth = GetDepth(texcoord.st);
  1211.     surface.linearDepth         = ExpToLinearDepth(surface.depth);              //Get linear scene depth
  1212.     surface.viewSpacePosition = GetViewSpacePosition(texcoord.st);
  1213.     surface.worldSpacePosition = gbufferModelViewInverse * surface.viewSpacePosition;
  1214.     surface.lightVector = lightVector;
  1215.     surface.sunlightVisibility = GetSunlightVisibility(texcoord.st);
  1216.     surface.upVector    = upVector;
  1217.     vec4 wlv                    = shadowModelViewInverse * vec4(0.0f, 0.0f, 0.0f, 1.0f);
  1218.     surface.worldLightVector    = normalize(wlv.xyz);
  1219.  
  1220.     surface.specularity = GetSpecularity(texcoord.st);
  1221.     surface.roughness = 1.0f - GetRoughness(texcoord.st);
  1222.     surface.fresnelPower = 6.0f + surface.roughness * 0.0f;
  1223.     surface.baseSpecularity = 0.02f;
  1224.  
  1225.     surface.mask.matIDs = GetMaterialIDs(texcoord.st);
  1226.     CalculateMasks(surface.mask);
  1227.  
  1228.     surface.cloudAlpha = 0.0f;
  1229.     #ifdef SMOOTH_SKY
  1230.         surface.cloudAlpha = texture2D(composite, texcoord.st, 2).g;
  1231.         SmoothSky(surface);
  1232.     #endif
  1233.  
  1234.     CloudPlane(surface);
  1235.     CalculateSpecularReflections(surface);
  1236.     CalculateSpecularHighlight(surface);
  1237.     //CalculateGlossySpecularReflections(surface);
  1238.  
  1239.  
  1240.     // surface.color = surface.normal * 0.0001f;
  1241.  
  1242.     //surface.color = vec3(fwidth(surface.depth)) * 0.01f;
  1243.  
  1244.     //surface.color.rgb = surface.reflection.rgb;
  1245.  
  1246.  
  1247.     surface.color = pow(surface.color, vec3(1.0f / 2.2f));
  1248.     gl_FragData[0] = vec4(surface.color, 1.0f);
  1249.     //gl_FragData[1] = vec4(texture2D(composite, texcoord.st).r, cloudAlpha, texture2D(composite, texcoord.st).b, 1.0f);
  1250.    
  1251.  
  1252. }
Add Comment
Please, Sign In to add comment