Advertisement
Guest User

Untitled

a guest
May 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.52 KB | None | 0 0
  1. #version 120
  2.  
  3.  
  4.  
  5. #include "Common.inc"
  6.  
  7.  
  8. /*
  9. _______ _________ _______ _______ _
  10. ( ____ ¥¥__ __/( ___ )( ____ )( )
  11. | ( ¥/ ) ( | ( ) || ( )|| |
  12. | (_____ | | | | | || (____)|| |
  13. (_____ ) | | | | | || _____)| |
  14. ) | | | | | | || ( (_)
  15. /¥____) | | | | (___) || ) _
  16. ¥_______) )_( (_______)|/ (_)
  17.  
  18. Do not modify this code until you have read the LICENSE.txt contained in the root directory of this shaderpack!
  19.  
  20. */
  21. #define SHADOW_MAP_BIAS 0.90
  22.  
  23. /////////ADJUSTABLE VARIABLES//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  24. /////////ADJUSTABLE VARIABLES//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  25.  
  26.  
  27.  
  28.  
  29. #define ENABLE_SSAO // Screen space ambient occlusion.
  30. #define GI // Indirect lighting from sunlight.
  31.  
  32. #define GI_QUALITY 0.5 // Number of GI samples. More samples=smoother GI. High performance impact! [0.5 1.0 2.0]
  33. //#define GI_ARTIFACT_REDUCTION // Reduces artifacts on back edges of blocks at the cost of performance.
  34. #define GI_RENDER_RESOLUTION 1 // Render resolution of GI. 0 = High. 1 = Low. Set to 1 for faster but blurrier GI. [0 1]
  35. #define GI_RADIUS 1.0 // How far indirect light can spread. Can help to reduce artifacts with low GI samples. [0.5 0.75 1.0 1.5 2.0]
  36.  
  37. /////////INTERNAL VARIABLES////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. /////////INTERNAL VARIABLES////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  39. //Do not change the name of these variables or their type. The Shaders Mod reads these lines and determines values to send to the inner-workings
  40. //of the shaders mod. The shaders mod only reads these lines and doesn't actually know the real value assigned to these variables in GLSL.
  41. //Some of these variables are critical for proper operation. Change at your own risk.
  42.  
  43. const float shadowDistance = 120.0; // Shadow distance. Set lower if you prefer nicer close shadows. Set higher if you prefer nicer distant shadows. [80.0 120.0 180.0 240.0]
  44. const bool shadowHardwareFiltering0 = true;
  45.  
  46. const bool shadowtex1Mipmap = true;
  47. const bool shadowtex1Nearest = false;
  48. const bool shadowcolor0Mipmap = true;
  49. const bool shadowcolor0Nearest = false;
  50. const bool shadowcolor1Mipmap = true;
  51. const bool shadowcolor1Nearest = false;
  52.  
  53. const int noiseTextureResolution = 64;
  54.  
  55.  
  56. //END OF INTERNAL VARIABLES//
  57.  
  58. /* DRAWBUFFERS:46 */
  59.  
  60. uniform sampler2D gnormal;
  61. uniform sampler2D depthtex1;
  62. uniform sampler2D composite;
  63. uniform sampler2D gdepth;
  64. uniform sampler2D shadowcolor1;
  65. uniform sampler2D shadowcolor;
  66. uniform sampler2D shadowtex1;
  67. uniform sampler2D noisetex;
  68. uniform sampler2D gaux1;
  69. uniform sampler2D gaux2;
  70. uniform sampler2D gaux3;
  71.  
  72. uniform mat4 gbufferModelView;
  73. uniform mat4 gbufferModelViewInverse;
  74. uniform mat4 shadowModelView;
  75. uniform mat4 shadowModelViewInverse;
  76. uniform mat4 shadowProjection;
  77. uniform mat4 gbufferProjectionInverse;
  78. uniform mat4 gbufferProjection;
  79.  
  80. varying vec4 texcoord;
  81. varying vec3 lightVector;
  82.  
  83. varying float timeSunriseSunset;
  84. varying float timeNoon;
  85. varying float timeMidnight;
  86. varying float timeSkyDark;
  87.  
  88. varying vec3 colorSunlight;
  89. varying vec3 colorSkylight;
  90. varying vec3 colorSunglow;
  91. varying vec3 colorBouncedSunlight;
  92. varying vec3 colorScatteredSunlight;
  93. varying vec3 colorTorchlight;
  94. varying vec3 colorWaterMurk;
  95. varying vec3 colorWaterBlue;
  96. varying vec3 colorSkyTint;
  97.  
  98. uniform float near;
  99. uniform float far;
  100. uniform float viewWidth;
  101. uniform float viewHeight;
  102. uniform float rainStrength;
  103. uniform float wetness;
  104. uniform float aspectRatio;
  105. uniform float frameTimeCounter;
  106. uniform float sunAngle;
  107. uniform vec3 skyColor;
  108. uniform vec3 cameraPosition;
  109.  
  110. varying vec3 upVector;
  111.  
  112. /////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  113. /////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  114.  
  115. vec3 GetNormals(in vec2 coord) { //Function that retrieves the screen space surface normals. Used for lighting calculations
  116. return DecodeNormal(texture2D(gnormal, coord).xy);
  117. }
  118.  
  119. float GetDepth(in vec2 coord) {
  120. return texture2D(depthtex1, coord.st).x;
  121. }
  122.  
  123. vec4 GetScreenSpacePosition(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
  124. float depth = GetDepth(coord);
  125. vec4 fragposition = gbufferProjectionInverse * vec4(coord.s * 2.0f - 1.0f, coord.t * 2.0f - 1.0f, 2.0f * depth - 1.0f, 1.0f);
  126. fragposition /= fragposition.w;
  127.  
  128. return fragposition;
  129. }
  130.  
  131. vec4 GetScreenSpacePosition(in vec2 coord, in float depth) { //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
  132. vec4 fragposition = gbufferProjectionInverse * vec4(coord.s * 2.0f - 1.0f, coord.t * 2.0f - 1.0f, 2.0f * depth - 1.0f, 1.0f);
  133. fragposition /= fragposition.w;
  134.  
  135. return fragposition;
  136. }
  137.  
  138.  
  139. vec3 CalculateNoisePattern1(vec2 offset, float size) {
  140. vec2 coord = texcoord.st;
  141.  
  142. coord *= vec2(viewWidth, viewHeight);
  143. coord = mod(coord + offset, vec2(size));
  144. coord /= noiseTextureResolution;
  145.  
  146. return texture2D(noisetex, coord).xyz;
  147. }
  148.  
  149. vec2 DistortShadowSpace(in vec2 pos)
  150. {
  151. vec2 signedPos = pos * 2.0f - 1.0f;
  152.  
  153. float dist = sqrt(signedPos.x * signedPos.x + signedPos.y * signedPos.y);
  154. float distortFactor = (1.0f - SHADOW_MAP_BIAS) + dist * SHADOW_MAP_BIAS;
  155. signedPos.xy *= 0.95 / distortFactor;
  156.  
  157. pos = signedPos * 0.5f + 0.5f;
  158.  
  159. return pos;
  160. }
  161.  
  162. vec3 Contrast(in vec3 color, in float contrast)
  163. {
  164. float colorLength = length(color);
  165. vec3 nColor = color / colorLength;
  166.  
  167. colorLength = pow(colorLength, contrast);
  168.  
  169. return nColor * colorLength;
  170. }
  171.  
  172. float GetMaterialIDs(in vec2 coord) { //Function that retrieves the texture that has all material IDs stored in it
  173. return texture2D(composite, coord).b;
  174. }
  175.  
  176. float GetSkylight(in vec2 coord)
  177. {
  178. return texture2DLod(gdepth, coord, 0).g;
  179. }
  180.  
  181. float GetMaterialMask(in vec2 coord, const in int ID) {
  182. float matID = (GetMaterialIDs(coord) * 255.0f);
  183.  
  184. //Catch last part of sky
  185. if (matID > 254.0f) {
  186. matID = 0.0f;
  187. }
  188.  
  189. if (matID == ID) {
  190. return 1.0f;
  191. } else {
  192. return 0.0f;
  193. }
  194. }
  195.  
  196. bool GetSkyMask(in vec2 coord)
  197. {
  198. float matID = GetMaterialIDs(coord);
  199. matID = floor(matID * 255.0f);
  200.  
  201. if (matID < 1.0f || matID > 254.0f)
  202. {
  203. return true;
  204. } else {
  205. return false;
  206. }
  207. }
  208.  
  209. float GetAO2(in vec4 screenSpacePosition, in vec3 normal, in vec2 coord, in vec3 dither)
  210. {
  211. //Determine origin position
  212. vec3 origin = screenSpacePosition.xyz;
  213.  
  214. vec3 randomRotation = normalize(dither.xyz * vec3(2.0f, 2.0f, 1.0f) - vec3(1.0f, 1.0f, 0.0f));
  215.  
  216. vec3 tangent = normalize(randomRotation - normal * dot(randomRotation, normal));
  217. vec3 bitangent = cross(normal, tangent);
  218. mat3 tbn = mat3(tangent, bitangent, normal);
  219.  
  220. float aoRadius = 0.15f * -screenSpacePosition.z;
  221. //aoRadius = 0.8f;
  222. float zThickness = 0.25f * -screenSpacePosition.z;
  223. //zThickness = 1.2f;
  224.  
  225. vec3 samplePosition = vec3(0.0f);
  226. float intersect = 0.0f;
  227. vec4 sampleScreenSpace = vec4(0.0f);
  228. float sampleDepth = 0.0f;
  229. float distanceWeight = 0.0f;
  230. float finalRadius = 0.0f;
  231.  
  232. int numRaysPassed = 0;
  233.  
  234. float ao = 0.0f;
  235.  
  236. for (int i = 0; i < 4; i++)
  237. {
  238. vec3 kernel = vec3(texture2D(noisetex, vec2(0.1f + (i * 1.0f) / 64.0f)).r * 2.0f - 1.0f,
  239. texture2D(noisetex, vec2(0.1f + (i * 1.0f) / 64.0f)).g * 2.0f - 1.0f,
  240. texture2D(noisetex, vec2(0.1f + (i * 1.0f) / 64.0f)).b * 1.0f);
  241. kernel = normalize(kernel);
  242. kernel *= pow(dither.x + 0.01f, 1.0f);
  243.  
  244. samplePosition = tbn * kernel;
  245. samplePosition = samplePosition * aoRadius + origin;
  246.  
  247. sampleScreenSpace = gbufferProjection * vec4(samplePosition, 0.0f);
  248. sampleScreenSpace.xyz /= sampleScreenSpace.w;
  249. sampleScreenSpace.xyz = sampleScreenSpace.xyz * 0.5f + 0.5f;
  250.  
  251. //Check depth at sample point
  252. sampleDepth = GetScreenSpacePosition(sampleScreenSpace.xy).z;
  253.  
  254. //If point is behind geometry, buildup AO
  255. if (sampleDepth >= samplePosition.z && sampleDepth - samplePosition.z < zThickness)
  256. {
  257. ao += 1.0f;
  258. } else {
  259.  
  260. }
  261. }
  262. ao /= 4;
  263. ao = 1.0f - ao;
  264. ao = pow(ao, 1.0f);
  265.  
  266. return ao;
  267. }
  268.  
  269. vec3 ProjectBack(vec3 cameraSpace)
  270. {
  271. vec4 clipSpace = gbufferProjection * vec4(cameraSpace, 1.0);
  272. vec3 NDCSpace = clipSpace.xyz / clipSpace.w;
  273. vec3 screenSpace = 0.5 * NDCSpace + 0.5;
  274. //screenSpace.z = 0.1f;
  275. return screenSpace;
  276. }
  277.  
  278. float ExpToLinearDepth(in float depth)
  279. {
  280. return 2.0f * near * far / (far + near - (2.0f * depth - 1.0f) * (far - near));
  281. }
  282.  
  283. float GetAO(vec2 coord, vec3 normal, float dither)
  284. {
  285. const int numRays = 8;
  286.  
  287. const float phi = 1.618033988;
  288. const float gAngle = phi * 3.14159265 * 1.0003;
  289.  
  290. float depth = GetDepth(coord);
  291. float linDepth = ExpToLinearDepth(depth);
  292. vec3 origin = GetScreenSpacePosition(coord, depth).xyz;
  293.  
  294. float aoAccum = 0.0;
  295.  
  296. float radius = 0.15 * -origin.z;
  297. radius = mix(radius, 0.8, 0.5);
  298. float zThickness = 0.15 * -origin.z;
  299. zThickness = mix(zThickness, 1.0, 0.5);
  300.  
  301. float aoMul = 1.0;
  302.  
  303. for (int i = 0; i < numRays; i++)
  304. {
  305. float fi = float(i) + dither;
  306. float fiN = fi / float(numRays);
  307. float lon = gAngle * fi * 6.0;
  308. float lat = asin(fiN * 2.0 - 1.0) * 1.0;
  309.  
  310. vec3 kernel;
  311. kernel.x = cos(lat) * cos(lon);
  312. kernel.z = cos(lat) * sin(lon);
  313. kernel.y = sin(lat);
  314.  
  315. kernel.xyz = normalize(kernel.xyz + normal.xyz);
  316.  
  317. float sampleLength = radius * mod(fiN, 0.07) / 0.07;
  318.  
  319. vec3 samplePos = origin + kernel * sampleLength;
  320.  
  321. vec3 samplePosProj = ProjectBack(samplePos);
  322.  
  323. /*
  324. float sampleDepth = ExpToLinearDepth(GetDepth(samplePosProj.xy));
  325.  
  326. float kernelAngle = dot(kernel, normal);
  327.  
  328. if (sampleDepth < linDepth && kernelAngle > 0.0)
  329. {
  330. aoAccum += 1.0 * saturate(kernelAngle) * saturate(abs(sampleDepth - linDepth) * 50.0);
  331. }
  332. */
  333.  
  334. vec3 actualSamplePos = GetScreenSpacePosition(samplePosProj.xy, GetDepth(samplePosProj.xy)).xyz;
  335.  
  336. vec3 sampleVector = normalize(samplePos - origin);
  337.  
  338. float depthDiff = actualSamplePos.z - samplePos.z;
  339.  
  340. if (depthDiff > 0.0 && depthDiff < zThickness)
  341. {
  342. //aoAccum += 1.0 * saturate(depthDiff * 100.0) * saturate(1.0 - depthDiff * 0.25 / (sampleLength + 0.001));
  343. float aow = 1.35 * saturate(dot(sampleVector, normal));
  344. //aow *= saturate(dot(sampleVector, upVector) * 0.5 + 0.5) * 1.5 + 0.0;
  345. aoAccum += aow;
  346. //aoAccum += 1.0 * saturate(dot(kernel, upVector));
  347. //aoMul *= mix(1.0, 0.0, saturate(dot(kernel, upVector)));
  348. //aoMul *= 0.45;
  349. }
  350. }
  351.  
  352. aoAccum /= numRays;
  353.  
  354. float ao = 1.0 - aoAccum;
  355. //ao = aoMul;
  356. ao = pow(ao, 1.5);
  357.  
  358. return ao;
  359. }
  360.  
  361. vec4 GetLight(in int LOD, in vec2 offset, in float range, in float quality, vec3 noisePattern)
  362. {
  363. float scale = pow(2.0f, float(LOD));
  364.  
  365. float padding = 0.002f;
  366.  
  367. if ( texcoord.s - offset.s + padding < 1.0f / scale + (padding * 2.0f)
  368. && texcoord.t - offset.t + padding < 1.0f / scale + (padding * 2.0f)
  369. && texcoord.s - offset.s + padding > 0.0f
  370. && texcoord.t - offset.t + padding > 0.0f)
  371. {
  372.  
  373. vec2 coord = (texcoord.st - offset.st) * scale;
  374.  
  375. vec3 normal = GetNormals(coord.st); //Gets the screen-space normals
  376.  
  377. vec4 gn = gbufferModelViewInverse * vec4(normal.xyz, 0.0f);
  378. gn = shadowModelView * gn;
  379. gn.xyz = normalize(gn.xyz);
  380.  
  381. vec3 shadowSpaceNormal = gn.xyz;
  382.  
  383. vec4 screenSpacePosition = GetScreenSpacePosition(coord.st); //Gets the screen-space position
  384. vec3 viewVector = normalize(screenSpacePosition.xyz);
  385.  
  386.  
  387. float distance = sqrt( screenSpacePosition.x * screenSpacePosition.x //Get surface distance in meters
  388. + screenSpacePosition.y * screenSpacePosition.y
  389. + screenSpacePosition.z * screenSpacePosition.z);
  390.  
  391. float materialIDs = texture2D(composite, coord).b * 255.0f;
  392.  
  393. vec4 upVectorShadowSpace = shadowModelView * vec4(0.0f, 1.0, 0.0, 0.0);
  394.  
  395.  
  396. vec4 worldposition = gbufferModelViewInverse * screenSpacePosition; //Transform from screen space to world space
  397. worldposition = shadowModelView * worldposition; //Transform from world space to shadow space
  398. float comparedepth = -worldposition.z; //Surface distance from sun to be compared to the shadow map
  399.  
  400. worldposition = shadowProjection * worldposition; //Transform from shadow space to shadow projection space
  401. worldposition /= worldposition.w;
  402.  
  403. float d = sqrt(worldposition.x * worldposition.x + worldposition.y * worldposition.y);
  404. float distortFactor = (1.0f - SHADOW_MAP_BIAS) + d * SHADOW_MAP_BIAS;
  405. //worldposition.xy /= distortFactor;
  406. //worldposition.z = mix(worldposition.z, 0.5, 0.8);
  407. worldposition = worldposition * 0.5f + 0.5f; //Transform from shadow projection space to shadow map coordinates
  408.  
  409. float shadowMult = 0.0f; //Multiplier used to fade out shadows at distance
  410. float shad = 0.0f;
  411. vec3 fakeIndirect = vec3(0.0f);
  412.  
  413. float fakeLargeAO = 0.0;
  414.  
  415.  
  416. float mcSkylight = GetSkylight(coord) * 0.8 + 0.2;
  417.  
  418. float fademult = 0.15f;
  419.  
  420. shadowMult = clamp((shadowDistance * 41.4f * fademult) - (distance * fademult), 0.0f, 1.0f); //Calculate shadowMult to fade shadows out
  421.  
  422. float compare = sin(frameTimeCounter) > -0.2 ? 1.0 : 0.0;
  423.  
  424. if ( shadowMult > 0.0)
  425. {
  426.  
  427.  
  428. //big shadow
  429. float rad = range;
  430.  
  431. int c = 0;
  432. float s = 2.0f * rad / 2048;
  433.  
  434. vec2 dither = noisePattern.xy - 0.5f;
  435. //vec2 dither = vec2(0.0f);
  436.  
  437. float step = 1.0f / quality;
  438.  
  439. for (float i = -2.0f; i <= 2.0f; i += step) {
  440. for (float j = -2.0f; j <= 2.0f; j += step) {
  441.  
  442. vec2 offset = (vec2(i, j) + dither * step) * s;
  443.  
  444. offset *= length(offset) * 15.0;
  445. offset *= GI_RADIUS * 1.0;
  446.  
  447. //offset += shadowSpaceNormal.xz * 0.01;
  448. //offset = normalize(normalize(offset) + shadowSpaceNormal.xz * vec2(1.0, 1.0) * compare) * length(offset);
  449.  
  450. vec2 coord = worldposition.st + offset;
  451. vec2 lookupCoord = DistortShadowSpace(coord);
  452.  
  453. #ifdef GI_ARTIFACT_REDUCTION
  454. float depthSample = texture2DLod(shadowtex1, lookupCoord, 0).x;
  455. #else
  456. float depthSample = texture2DLod(shadowtex1, lookupCoord, 3).x;
  457. #endif
  458.  
  459. /*
  460. depthSample = depthSample * 2.0 - 1.0;
  461. depthSample -= 0.4;
  462. depthSample /= 0.2;
  463. depthSample = depthSample * 0.5 + 0.5;
  464. */
  465.  
  466. depthSample = -3 + 5.0 * depthSample;
  467. vec3 samplePos = vec3(coord.x, coord.y, depthSample);
  468.  
  469.  
  470. fakeLargeAO += saturate((worldposition.z - samplePos.z) * 1000.0);
  471. //fakeLargeAO += saturate((worldposition.z - samplePos.z) * 1000.0) * saturate(1.0 - abs(worldposition.z - samplePos.z) * 5.0);
  472.  
  473.  
  474. vec3 lightVector = normalize(samplePos.xyz - worldposition.xyz);
  475.  
  476. vec4 normalSample = texture2DLod(shadowcolor1, lookupCoord, 6);
  477. vec3 surfaceNormal = normalSample.rgb * 2.0f - 1.0f;
  478. surfaceNormal.x = -surfaceNormal.x;
  479. surfaceNormal.y = -surfaceNormal.y;
  480.  
  481. float surfaceSkylight = normalSample.a;
  482.  
  483. if (surfaceSkylight < 0.2)
  484. {
  485. surfaceSkylight = mcSkylight;
  486. }
  487.  
  488. float NdotL = max(0.0f, dot(shadowSpaceNormal.xyz, lightVector * vec3(1.0, 1.0, -1.0)));
  489. NdotL = NdotL * 0.9f + 0.2f;
  490.  
  491. if (abs(materialIDs - 3.0f) < 0.1f || abs(materialIDs - 2.0f) < 0.1f || abs(materialIDs - 11.0f) < 0.1f)
  492. {
  493. NdotL = 0.5f;
  494. }
  495.  
  496. if (NdotL > 0.0)
  497. {
  498. bool isTranslucent = length(surfaceNormal) < 0.5f;
  499.  
  500. if (isTranslucent)
  501. {
  502. surfaceNormal.xyz = vec3(0.0f, 0.0f, 1.0f);
  503. }
  504.  
  505. //float leafMix = clamp(-surfaceNormal.b * 10.0f, 0.0f, 1.0f);
  506.  
  507.  
  508. float weight = dot(lightVector, surfaceNormal);
  509. float rawdot = weight;
  510. // float aoWeight = abs(weight);
  511. // aoWeight *= clamp(dot(lightVector, upVectorShadowSpace.xyz), 0.0, 1.0);
  512. //weight = mix(weight, 1.0f, leafMix);
  513. if (isTranslucent)
  514. {
  515. weight = abs(weight) * 0.85f;
  516. }
  517.  
  518. if (normalSample.a < 0.2)
  519. {
  520. weight = 0.5;
  521. }
  522.  
  523. weight = max(weight, 0.0f);
  524.  
  525. float dist = length(samplePos.xyz - worldposition.xyz - vec3(0.0f, 0.0f, 0.0f));
  526. if (dist < 0.0005f)
  527. {
  528. dist = 10000000.0f;
  529. }
  530.  
  531. const float falloffPower = 1.9f;
  532. float distanceWeight = (1.0f / (pow(dist * (62260.0f / rad), falloffPower) + 100.1f));
  533. //distanceWeight = max(0.0f, distanceWeight - 0.000009f);
  534. distanceWeight *= pow(length(offset), 2.0) * 50000.0 + 1.01;
  535.  
  536.  
  537. //Leaves self-occlusion
  538. if (rawdot < 0.0f)
  539. {
  540. distanceWeight = max(distanceWeight * 30.0f - 0.13f, 0.0f);
  541. distanceWeight *= 0.04f;
  542. }
  543.  
  544.  
  545. //float skylightWeight = clamp(1.0 - abs(surfaceSkylight - mcSkylight) * 10.0, 0.0, 1.0);
  546. float skylightWeight = 1.0 / (max(0.0, surfaceSkylight - mcSkylight) * 15.0 + 1.0);
  547.  
  548.  
  549. vec3 colorSample = pow(texture2DLod(shadowcolor, lookupCoord, 6).rgb, vec3(2.2f));
  550.  
  551. colorSample /= surfaceSkylight;
  552. //colorSample = pow(colorSample, vec3(1.4f));
  553.  
  554. //colorSample = Contrast(colorSample, 0.8f);
  555.  
  556. colorSample = normalize(colorSample) * pow(length(colorSample), 1.1f);
  557.  
  558. //colorSample = mix(colorSample, vec3(dot(colorSample, vec3(0.3333f))), vec3(0.035f));
  559. //float colorMagnitude = dot(colorSample, vec3(0.3333f));
  560. //vec3 normalized = normalize(colorSample);
  561. // if (surfaceNormal.b < -0.1f && abs(materialIDs - 3.0f) < 0.1f)
  562. // {
  563. // float crossfade = clamp(1.0f - dist / 5.0f, 0.0f, 1.0f);
  564. // normalized = pow(normalized, vec3(mix(1.0f, 2.0f, crossfade)));
  565. // }
  566. //colorSample = normalized * colorMagnitude;
  567.  
  568.  
  569. fakeIndirect += colorSample * weight * distanceWeight * NdotL * skylightWeight;
  570. //fakeIndirect += skylightWeight * weight * distanceWeight * NdotL;
  571. //fakeLargeAO += aoDistanceWeight * NdotL;
  572. }
  573. c += 1;
  574. }
  575. }
  576.  
  577. fakeIndirect /= c;
  578. fakeLargeAO /= c;
  579. fakeLargeAO = clamp(1.0 - fakeLargeAO * 0.8, 0.0, 1.0);
  580. // fakeLargeAO = pow(fakeLargeAO, 2.0);
  581. }
  582.  
  583. fakeIndirect = mix(vec3(0.0f), fakeIndirect, vec3(shadowMult));
  584.  
  585. //fakeIndirect /= fakeLargeAO;
  586.  
  587. float ao = 1.0f;
  588. bool isSky = GetSkyMask(coord.st);
  589. #ifdef ENABLE_SSAO
  590. if (!isSky)
  591. {
  592. ao *= GetAO(coord.st, normal.xyz, noisePattern.x);
  593. }
  594. #endif
  595.  
  596. fakeIndirect.rgb *= ao;
  597.  
  598. //ao *= fakeLargeAO;
  599.  
  600.  
  601. //fakeIndirect.rgb = vec3(mcSkylight / 1150.0);
  602.  
  603. return vec4(fakeIndirect.rgb * 500.0f * GI_RADIUS, ao);
  604. }
  605. else {
  606. return vec4(0.0f);
  607. }
  608. }
  609.  
  610.  
  611. float CalculateDitherPattern1() {
  612. const int[16] ditherPattern = int[16] (0 , 8 , 2 , 10,
  613. 12, 4 , 14, 6 ,
  614. 3 , 11, 1, 9 ,
  615. 15, 7 , 13, 5 );
  616.  
  617. vec2 count = vec2(0.0f);
  618. count.x = floor(mod(texcoord.s * viewWidth, 4.0f));
  619. count.y = floor(mod(texcoord.t * viewHeight, 4.0f));
  620.  
  621. int dither = ditherPattern[int(count.x) + int(count.y) * 4];
  622.  
  623. return float(dither) / 16.0f;
  624. }
  625.  
  626. void DoNightEye(inout vec3 color) { //Desaturates any color input at night, simulating the rods in the human eye
  627.  
  628. float amount = 0.8f; //How much will the new desaturated and tinted image be mixed with the original image
  629. vec3 rodColor = vec3(0.2f, 0.4f, 1.0f); //Cyan color that humans percieve when viewing extremely low light levels via rod cells in the eye
  630. float colorDesat = dot(color, vec3(1.0f)); //Desaturated color
  631.  
  632. color = mix(color, vec3(colorDesat) * rodColor, timeMidnight * amount);
  633. //color.rgb = color.rgb;
  634. }
  635.  
  636. vec4 textureSmooth(in sampler2D tex, in vec2 coord)
  637. {
  638. vec2 res = vec2(64.0f, 64.0f);
  639.  
  640. coord *= res;
  641. coord += 0.5f;
  642.  
  643. vec2 whole = floor(coord);
  644. vec2 part = fract(coord);
  645.  
  646. part.x = part.x * part.x * (3.0f - 2.0f * part.x);
  647. part.y = part.y * part.y * (3.0f - 2.0f * part.y);
  648. // part.x = 1.0f - (cos(part.x * 3.1415f) * 0.5f + 0.5f);
  649. // part.y = 1.0f - (cos(part.y * 3.1415f) * 0.5f + 0.5f);
  650.  
  651. coord = whole + part;
  652.  
  653. coord -= 0.5f;
  654. coord /= res;
  655.  
  656. return texture2D(tex, coord);
  657. }
  658.  
  659. float AlmostIdentity(in float x, in float m, in float n)
  660. {
  661. if (x > m) return x;
  662.  
  663. float a = 2.0f * n - m;
  664. float b = 2.0f * m - 3.0f * n;
  665. float t = x / m;
  666.  
  667. return (a * t + b) * t * t + n;
  668. }
  669.  
  670.  
  671. float GetWaves(vec3 position) {
  672. float speed = 0.9f;
  673.  
  674. vec2 p = position.xz / 20.0f;
  675.  
  676. p.xy -= position.y / 20.0f;
  677.  
  678. p.x = -p.x;
  679.  
  680. p.x += (frameTimeCounter / 40.0f) * speed;
  681. p.y -= (frameTimeCounter / 40.0f) * speed;
  682.  
  683. float weight = 1.0f;
  684. float weights = weight;
  685.  
  686. float allwaves = 0.0f;
  687.  
  688. float wave = 0.0;
  689. //wave = textureSmooth(noisetex, (p * vec2(2.0f, 1.2f)) + vec2(0.0f, p.x * 2.1f) ).x;
  690. p /= 2.1f; /*p *= pow(2.0f, 1.0f);*/ p.y -= (frameTimeCounter / 20.0f) * speed; p.x -= (frameTimeCounter / 30.0f) * speed;
  691. //allwaves += wave;
  692.  
  693. weight = 4.1f;
  694. weights += weight;
  695. wave = textureSmooth(noisetex, (p * vec2(2.0f, 1.4f)) + vec2(0.0f, -p.x * 2.1f) ).x;
  696. p /= 1.5f;/*p *= pow(2.0f, 2.0f);*/ p.x += (frameTimeCounter / 20.0f) * speed;
  697. wave *= weight;
  698. allwaves += wave;
  699.  
  700. weight = 17.25f;
  701. weights += weight;
  702. wave = (textureSmooth(noisetex, (p * vec2(1.0f, 0.75f)) + vec2(0.0f, p.x * 1.1f) ).x); p /= 1.5f; p.x -= (frameTimeCounter / 55.0f) * speed;
  703. wave *= weight;
  704. allwaves += wave;
  705.  
  706. weight = 15.25f;
  707. weights += weight;
  708. wave = (textureSmooth(noisetex, (p * vec2(1.0f, 0.75f)) + vec2(0.0f, -p.x * 1.7f) ).x); p /= 1.9f; p.x += (frameTimeCounter / 155.0f) * speed;
  709. wave *= weight;
  710. allwaves += wave;
  711.  
  712. weight = 29.25f;
  713. weights += weight;
  714. wave = abs(textureSmooth(noisetex, (p * vec2(1.0f, 0.8f)) + vec2(0.0f, -p.x * 1.7f) ).x * 2.0f - 1.0f); p /= 2.0f; p.x += (frameTimeCounter / 155.0f) * speed;
  715. wave = 1.0f - AlmostIdentity(wave, 0.2f, 0.1f);
  716. wave *= weight;
  717. allwaves += wave;
  718.  
  719. weight = 15.25f;
  720. weights += weight;
  721. wave = abs(textureSmooth(noisetex, (p * vec2(1.0f, 0.8f)) + vec2(0.0f, p.x * 1.7f) ).x * 2.0f - 1.0f);
  722. wave = 1.0f - AlmostIdentity(wave, 0.2f, 0.1f);
  723. wave *= weight;
  724. allwaves += wave;
  725.  
  726. // weight = 10.0f;
  727. // weights += weight;
  728. // wave = sin(length(position.xz * 5.0 + frameTimeCounter));
  729. // wave *= weight;
  730. // allwaves += wave;
  731.  
  732. allwaves /= weights;
  733.  
  734. return allwaves;
  735. }
  736.  
  737.  
  738. vec3 GetWavesNormal(vec3 position) {
  739.  
  740. float WAVE_HEIGHT = 1.5;
  741.  
  742. const float sampleDistance = 11.0f;
  743.  
  744. position -= vec3(0.005f, 0.0f, 0.005f) * sampleDistance;
  745.  
  746. float wavesCenter = GetWaves(position);
  747. float wavesLeft = GetWaves(position + vec3(0.01f * sampleDistance, 0.0f, 0.0f));
  748. float wavesUp = GetWaves(position + vec3(0.0f, 0.0f, 0.01f * sampleDistance));
  749.  
  750. vec3 wavesNormal;
  751. wavesNormal.r = wavesCenter - wavesLeft;
  752. wavesNormal.g = wavesCenter - wavesUp;
  753.  
  754. wavesNormal.r *= 30.0f * WAVE_HEIGHT / sampleDistance;
  755. wavesNormal.g *= 30.0f * WAVE_HEIGHT / sampleDistance;
  756.  
  757. // wavesNormal.b = sqrt(1.0f - wavesNormal.r * wavesNormal.r - wavesNormal.g * wavesNormal.g);
  758. wavesNormal.b = 1.0;
  759. wavesNormal.rgb = normalize(wavesNormal.rgb);
  760.  
  761.  
  762.  
  763. return wavesNormal.rgb;
  764. }
  765.  
  766. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  767. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  768. /////////////////////////////////////////////////////////////MAIN//////////////////////////////////////////////////////////////////////////////
  769. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  770. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  771. void main() {
  772.  
  773. vec3 noisePattern = CalculateNoisePattern1(vec2(0.0f), 4);
  774. vec4 screenSpacePosition = GetScreenSpacePosition(texcoord.st);
  775. vec4 worldSpacePosition = gbufferModelViewInverse * screenSpacePosition;
  776. vec4 worldLightVector = shadowModelViewInverse * vec4(0.0f, 0.0f, 1.0f, 0.0f);
  777. vec3 normal = GetNormals(texcoord.st);
  778.  
  779. vec4 light = vec4(0.0, 0.0, 0.0, 1.0);
  780. #ifdef GI
  781. light = GetLight(GI_RENDER_RESOLUTION, vec2(0.0f ), 16.0, GI_QUALITY, noisePattern);
  782. #endif
  783. //light += GetLight(0, vec2(0.0f), 2.0f, 0.5f);
  784.  
  785. // if (light.r >= 1.0f)
  786. // {
  787. // light.r = 0.0f;
  788. // }
  789.  
  790. // if (light.g >= 1.0f)
  791. // {
  792. // light.g = 0.0f;
  793. // }
  794.  
  795. // if (light.b >= 1.0f)
  796. // {
  797. // light.b = 0.0f;
  798. // }
  799.  
  800. light.a = mix(light.a, 1.0, GetMaterialMask(texcoord.st * (GI_RENDER_RESOLUTION + 1.0), 4));
  801.  
  802.  
  803.  
  804. vec2 wavesNormal = EncodeNormal(GetWavesNormal(vec3(texcoord.s * 50.0, 1.0, texcoord.t * 50.0)).xyz);
  805.  
  806. vec4 gaux1Color = texture2D(gaux1, texcoord.st);
  807.  
  808. gl_FragData[0] = vec4(gaux1Color.xy, wavesNormal.xy);
  809. gl_FragData[1] = vec4(pow(light.rgb, vec3(1.0 / 2.2)), light.a);
  810. //gl_FragData[1] = vec4(GetWavesNormal(vec3(texcoord.s * 50.0, 1.0, texcoord.t * 50.0)).xyz * 0.5 + 0.5, 1.0);
  811. // gl_FragData[1] = vec4(0.0, 0.0, 0.0, 0.0);
  812. }
  813.  
  814. //change GetWavesNormal
  815. //change material id getting of transparent blocks
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement