Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.12 KB | None | 0 0
  1. #version 120
  2.  
  3.  
  4. #define MAX_COLOR_RANGE 48.0
  5. /*
  6. Read my terms of mofification/sharing before changing something below please!
  7. Chocapic13' shaders, derived from SonicEther v10 rc6.
  8. Place two leading Slashes in front of the following '#define' lines in order to disable an option.
  9. */
  10.  
  11.  
  12. #define Godrays
  13. const float exposure = 6.0;
  14. const float density = 1.0;
  15. const int NUM_SAMPLES = 2;
  16. const float grnoise = 1.0;
  17.  
  18. #define WaterReflection
  19. #define WaterReflectionStrength 1.0
  20.  
  21. #define UnderwaterWaterCaustics
  22. #define UnderwaterWaterCausticsStrength 10.5
  23. #define UnderwaterWaterCausticsSize 0.11
  24. #define UnderwaterWaterCausticsSpeed 6.5
  25.  
  26. #define VolumeLight
  27. #define VolumeLightMultiplier 3.0
  28. #define VolumeLightMultiplierDay 1.0
  29. #define VolumeLightMultiplierNight 0.0
  30. #define VolumeLightMultiplierSunsetSunrise 1.0
  31. #define VL_STRENGTH_INSIDE 1.5
  32. const bool gdepthMipmapEnabled = true;
  33.  
  34. //#define Specular
  35.  
  36. #define WATER_REFRACT
  37. #define REFRACT_MULT 7.0
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. //don't touch these lines if you don't know what you do!
  65. const int maxf = 8; //number of refinements
  66. const float stp = 1.0; //size of one step for raytracing algorithm
  67. const float ref = 0.05; //refinement multiplier
  68. const float inc = 2.2; //increasement factor at each step
  69.  
  70. //ground constants (lower quality)
  71. const int Gmaxf = 3; //number of refinements
  72. const float Gstp = 1.2; //size of one step for raytracing algorithm
  73. const float Gref = 0.11; //refinement multiplier
  74. const float Ginc = 3.0; //increasement factor at each step
  75.  
  76. varying vec4 texcoord;
  77.  
  78. varying vec3 lightVector;
  79. varying vec3 sunVec;
  80. varying vec3 moonVec;
  81. varying vec3 upVec;
  82.  
  83. varying vec3 sunlight;
  84. varying vec3 moonlight;
  85. varying vec3 ambient_color;
  86.  
  87. varying float handItemLight;
  88.  
  89. varying float SdotU;
  90. varying float MdotU;
  91. varying float sunVisibility;
  92. varying float moonVisibility;
  93.  
  94. uniform sampler2D composite;
  95. uniform sampler2D gaux4;
  96. uniform sampler2D gaux3;
  97. uniform sampler2D gaux1;
  98. uniform sampler2D depthtex0;
  99. uniform sampler2D depthtex1;
  100. uniform sampler2D gnormal;
  101. uniform sampler2D gdepth;
  102. uniform sampler2D noisetex;
  103. uniform sampler2D gcolor;
  104. uniform sampler2DShadow shadow;
  105. uniform sampler2D gdepthtex;
  106.  
  107.  
  108. uniform vec3 sunPosition;
  109. uniform vec3 moonPosition;
  110. uniform vec3 upPosition;
  111. uniform vec3 cameraPosition;
  112. uniform vec3 skyColor;
  113.  
  114. uniform mat4 gbufferProjection;
  115. uniform mat4 gbufferProjectionInverse;
  116. uniform mat4 gbufferModelViewInverse;
  117. uniform mat4 shadowModelView;
  118. uniform mat4 shadowProjection;
  119. uniform mat4 shadowProjectionInverse;
  120. uniform mat4 shadowModelViewInverse;
  121.  
  122. uniform int isEyeInWater;
  123. uniform int worldTime;
  124. uniform float far;
  125. uniform float near;
  126. uniform float aspectRatio;
  127. uniform float viewWidth;
  128. uniform float viewHeight;
  129. uniform float rainStrength;
  130. uniform float wetness;
  131. uniform float frameTimeCounter;
  132. uniform int fogMode;
  133. uniform ivec2 eyeBrightnessSmooth;
  134.  
  135. float pw = 1.0/ viewWidth;
  136. float ph = 1.0/ viewHeight;
  137. float matflag = texture2D(gaux1,texcoord.xy).g;
  138. int iswater = int(matflag > 0.04 && matflag < 0.07);
  139.  
  140.  
  141.  
  142. vec3 fogclr = pow(mix(vec3(0.5,0.5,1.0),vec3(0.3,0.3,0.3),rainStrength)*ambient_color,vec3(2.2));
  143. vec3 fragpos = vec3(texcoord.st, texture2D(depthtex0, texcoord.st).r);
  144. vec3 normal = texture2D(gnormal, texcoord.st).rgb * 2.0 - 1.0;
  145.  
  146. float time = float(worldTime);
  147. float transition_fading = 1.0-(clamp((time-12000.0)/300.0,0.0,1.0)-clamp((time-13000.0)/300.0,0.0,1.0) + clamp((time-22000.0)/200.0,0.0,1.0)-clamp((time-23400.0)/200.0,0.0,1.0));
  148.  
  149. float night = clamp((time-12000.0)/300.0,0.0,1.0)-clamp((time-22800.0)/200.0,0.0,1.0);
  150.  
  151. float sky_lightmap = texture2D(gaux1,texcoord.xy).r;
  152. float iswet = wetness*pow(sky_lightmap,5.0)*sqrt(0.5+max(dot(normal,normalize(upPosition)),0.0));
  153.  
  154. vec3 specular = pow(texture2D(gaux3,texcoord.xy).rgb,vec3(2.2));
  155. float specmap = (specular.r+specular.g*(iswet));
  156.  
  157. vec4 color = texture2D(composite,texcoord.xy);
  158.  
  159. float ld(float depth) {
  160. return (2.0 * near) / (far + near - depth * (far - near));
  161. }
  162. float timefract = worldTime;
  163. float rainx = clamp(rainStrength, 0.0f, 1.0f)/1.0f;
  164.  
  165.  
  166.  
  167.  
  168.  
  169. //Calculate Time of Day
  170. float TimeSunrise = ((clamp(timefract, 23000.0, 24000.0) - 23000.0) / 1000.0) + (1.0 - (clamp(timefract, 0.0, 4000.0)/4000.0));
  171. float TimeNoon = ((clamp(timefract, 0.0, 4000.0)) / 4000.0) - ((clamp(timefract, 8000.0, 12000.0) - 8000.0) / 4000.0);
  172. float TimeSunset = ((clamp(timefract, 8000.0, 12000.0) - 8000.0) / 4000.0) - ((clamp(timefract, 12000.0, 12750.0) - 12000.0) / 750.0);
  173. float TimeMidnight = ((clamp(timefract, 12000.0, 12750.0) - 12000.0) / 750.0) - ((clamp(timefract, 23000.0, 24000.0) - 23000.0) / 1000.0);
  174.  
  175. float dynamicExposure()
  176. {
  177. return mix(1.0,0.0,(pow(eyeBrightnessSmooth.y / 240.0f, 3.0f)));
  178. }
  179.  
  180. vec3 nvec3(vec4 pos) {
  181. return pos.xyz/pos.w;
  182. }
  183.  
  184. vec4 nvec4(vec3 pos) {
  185. return vec4(pos.xyz, 1.0);
  186. }
  187.  
  188. float cdist(vec2 coord) {
  189. return max(abs(coord.s-0.5),abs(coord.t-0.5))*2.0;
  190. }
  191.  
  192. float luma(vec3 color) {
  193. return dot(color.rgb,vec3(0.299, 0.587, 0.114));
  194. }
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. float getnoise(vec2 pos) {
  202. return abs(fract(sin(dot(pos ,vec2(18.9898f,28.633f))) * 4378.5453f));
  203. }
  204.  
  205.  
  206.  
  207. vec3 skyLightIntegral (vec3 fposition) {
  208. vec3 skycoaa = ivec3(28,180,255)/255.0;
  209. vec3 sky_color = mix(moonlight*2.0,pow(skycoaa,vec3(2.2)),sunVisibility)*2.0;
  210. sky_color = mix(sky_color,vec3(0.25,0.3,0.4)*length(ambient_color),rainStrength);
  211.  
  212. vec3 sVector = normalize(fposition);
  213. vec3 upVector = normalize(upPosition);
  214.  
  215. const float PI = 3.14159265359;
  216.  
  217. float Lz = 1.0;
  218. float T = max(acos(dot(sVector,upVec)),0.0);
  219. float Y = max(acos(dot(sunVec,sVector)),0.0);
  220. float M = max(acos(dot(moonVec,sVector)),0.0);
  221.  
  222. float blueDif = (1+2.0*cos(T));
  223. float sunDif = (1.0+2.0*max(cos(Y),0.0));
  224.  
  225. float hemisphereIntegral = PI + 2.0*(sin(T+PI/2.0)-sin(T-PI/2.0));
  226. float sunIntegral = PI + 2.0*max(sin(Y+PI/2.0)*sin(Y+PI/2.0)*sin(Y+PI/2.0)-sin(Y-PI/2.0)*sin(Y-PI/2.0)*sin(Y-PI/2.0),0.0);
  227. float moonIntegral = PI + 2.0*max(sin(M+PI/2.0)*sin(M+PI/2.0)*sin(M+PI/2.0)-sin(M-PI/2.0)*sin(M-PI/2.0)*sin(M-PI/2.0),0.0);
  228.  
  229. return hemisphereIntegral*sky_color*Lz + sunIntegral*(1-rainStrength*0.9) * sunVisibility + moonIntegral * moonlight * moonVisibility;
  230. }
  231.  
  232. float subSurfaceScattering(vec3 pos, float N) {
  233.  
  234. return pow(max(dot(lightVector,normalize(pos)),0.0),N)*(N+1)/6.28;
  235.  
  236. }
  237.  
  238. vec3 getSkyColor(vec3 fposition) {
  239. //sky gradient
  240. /*----------*/
  241. vec3 skycoaa = ivec3(30,170,255)/255.0;
  242. vec3 sky_color = pow(skycoaa,vec3(2.2));
  243. vec3 nsunlight = normalize(pow(sunlight,vec3(2.2)));
  244. vec3 sVector = normalize(fposition);
  245. vec3 upVector = normalize(upPosition);
  246.  
  247. sky_color = normalize(mix(sky_color,vec3(0.25,0.3,0.4)*length(ambient_color),rainStrength)); //normalize colors in order to don't change luminance
  248.  
  249.  
  250. float Lz = 1.0;
  251. float cosT = dot(sVector,upVector); //T=S-Y
  252. float absCosT = abs(cosT);
  253. float cosS = dot(sunVec,upVector);
  254. float S = acos(cosS); //S=Y+T -> cos(Y+T)=cos(S) -> cos(Y)*cos(T) - sin(Y)*sin(T) = cos(S)
  255. float cosY = dot(sunVec,sVector);
  256. float Y = acos(cosY); //Y=S-T
  257.  
  258.  
  259. float a = -0.7;
  260. float b = -0.2;
  261. float c = 12.0;
  262. float d = -0.9;
  263. float e = 3.0;
  264.  
  265.  
  266.  
  267.  
  268. //sun sky color
  269. float L = (1+a*exp(b/(absCosT+0.01)))*(1+c*exp(d*Y)+e*cosY*cosY);
  270. //vec3 Lc = 1 + c*exp(d*Y)*sky_color + e*cosY*cosY*sky_color + a*exp(b/(absCosT+0.01))*sky_color + a*exp(b/(absCosT+0.01))*c*exp(d*Y) *nsunlight+ a*exp(b/(absCosT+0.01))*e*cosY*cosY*nsunlight;
  271. //= 1 + f(Y) + g(Y) + h(T) + i(T,Y) + j(T,Y)
  272. //S is constant on the frame
  273. //float T = 2.0*S-Y;
  274. // cosT2 = abs(cos(S+Y));
  275. //L = 1 + c*exp(d*Y) + e*cosY*cosY + a*exp(b/(cosT2+0.01)) + a*exp(b/(cosT2+0.01))*c*exp(d*Y) + a*exp(b/(cosT2+0.01))*e*cosY*cosY;
  276. //1 + c*exp(d*Y) + e*cosY*cosY + a*exp(b/cos(T)) + a*exp(b/cos(T))*c*exp(d*Y) + a*exp(b/cos(T)))*e*cosY*cosY;
  277. L = pow(L,1.0-rainStrength*0.8)*(1.0-rainStrength*0.2); //modulate intensity when raining
  278. //vec3 skyColorSun = mix(sky_color,,1-exp(-0.3*L*(1-rainStrength*0.8)));
  279. vec3 skyColorSun = mix(sky_color, nsunlight,1-exp(-0.13*L*(1-rainStrength*0.8)))*L ; //affect color based on luminance (0% physically accurate)
  280. skyColorSun *= sunVisibility;
  281.  
  282.  
  283. //moon sky color
  284. float McosS = dot(moonVec,upVector);
  285. float MS = acos(McosS);
  286. float McosY = dot(moonVec,sVector);
  287. float MY = acos(McosY);
  288.  
  289. float L2 = (1+a*exp(b/(absCosT+0.01)))*(1+c*exp(d*MY)+e*McosY*McosY);
  290. L2 = pow(L2,1.0-rainStrength*0.8)*(1.0-rainStrength*0.2); //modulate intensity when raining
  291. vec3 skyColormoon = mix(moonlight,normalize(vec3(0.25,0.3,0.4))*length(moonlight),rainStrength)*L2*0.4 ; //affect color based on luminance (0% physically accurate)
  292. skyColormoon *= moonVisibility;
  293.  
  294. sky_color = skyColormoon+skyColorSun/2.0;
  295. //sky_color = vec3(Lc);
  296. /*----------*/
  297.  
  298.  
  299. return sky_color;
  300. }
  301.  
  302.  
  303.  
  304. vec3 getFogColor(vec3 fposition) {
  305.  
  306. vec3 skycoaa = ivec3(30,170,255)/255.0;
  307. vec3 sky_color = pow(skycoaa,vec3(2.2));
  308. vec3 nsunlight = normalize(pow(sunlight,vec3(2.2)));
  309. vec3 sVector = normalize(fposition);
  310. vec3 upVector = normalize(upPosition);
  311.  
  312. sky_color = normalize(mix(sky_color,vec3(0.25,0.3,0.4)*length(ambient_color),rainStrength)); //normalize colors in order to don't change luminance
  313.  
  314.  
  315. float Lz = 1.0;
  316. float cosT = dot(sVector,upVector); //T=S-Y
  317. float absCosT = abs(cosT);
  318. float cosS = dot(sunVec,upVector);
  319. float S = acos(cosS); //S=Y+T -> cos(Y+T)=cos(S) -> cos(Y)*cos(T) - sin(Y)*sin(T) = cos(S)
  320. float cosY = dot(sunVec,sVector);
  321. float Y = acos(cosY); //Y=S-T
  322. //float L = ((0.91+10*exp(-3*Y)+0.45*cosY*cosY)*(1.0-exp(-0.32/(absCosT+0.01))))/((0.91+10*exp(-3*S)+0.45*cosS*cosS)*(1.0-exp(-0.32))); //CIE clear sky
  323.  
  324. float a = -0.7;
  325. float b = -0.2;
  326. float c = 12.0;
  327. float d = -0.9;
  328. float e = 3.0;
  329.  
  330. //sun sky color
  331. float L = (1+a*exp(b/(absCosT+0.01)))*(1+c*exp(d*Y)+e*cosY*cosY);
  332. //vec3 Lc = 1 + c*exp(d*Y)*sky_color + e*cosY*cosY*sky_color + a*exp(b/(absCosT+0.01))*sky_color + a*exp(b/(absCosT+0.01))*c*exp(d*Y) *nsunlight+ a*exp(b/(absCosT+0.01))*e*cosY*cosY*nsunlight;
  333. //= 1 + f(Y) + g(Y) + h(T) + i(T,Y) + j(T,Y)
  334. //S is constant on the frame
  335. //float T = 2.0*S-Y;
  336. //float cosT2 = abs(cos(S+Y));
  337. //L = 1 + c*exp(d*Y) + e*cosY*cosY + a*exp(b/(cosT2+0.01)) + a*exp(b/(cosT2+0.01))*c*exp(d*Y) + a*exp(b/(cosT2+0.01))*e*cosY*cosY;
  338. L = pow(L,1.0-rainStrength*0.8)*(1.0-rainStrength*0.2); //modulate intensity when raining
  339. //vec3 skyColorSun = mix(sky_color,,1-exp(-0.3*L*(1-rainStrength*0.8)));
  340. vec3 skyColorSun = mix(sky_color, nsunlight,1-exp(-0.05*L*(1-rainStrength*0.8)))*sqrt(L) ; //affect color based on luminance (0% physically accurate)
  341. skyColorSun *= sunVisibility;
  342.  
  343.  
  344. //moon sky color
  345. float McosS = dot(moonVec,upVector);
  346. float MS = acos(McosS);
  347. float McosY = dot(moonVec,sVector);
  348. float MY = acos(McosY);
  349.  
  350. float L2 = (1+a*exp(b/absCosT))*(1+c*exp(d*MY)+e*McosY*McosY);
  351. L2 = pow(L2,1.0-rainStrength*0.8)*(1.0-rainStrength*0.2); //modulate intensity when raining
  352. vec3 skyColormoon = mix(moonlight,normalize(vec3(0.25,0.25,0.25))*length(moonlight),rainStrength)*sqrt(L2) ;
  353. skyColormoon *= moonVisibility;
  354.  
  355. sky_color = skyColormoon+skyColorSun/2.0;
  356. //sky_color = vec3(Lc);
  357. /*----------*/
  358.  
  359.  
  360. return sky_color;
  361.  
  362. }
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375. float pixeldepth = texture2D(depthtex0,texcoord.xy).x;
  376.  
  377. float getDepth(float depth) {
  378. return 2.0 * near * far / (far + near - (2.0 * depth - 1.0) * (far - near));
  379. }
  380.  
  381.  
  382. float waterH(vec2 posxz, float visibility) {
  383.  
  384. vec2 movement = vec2(-abs(frameTimeCounter/2000.-0.5),-abs(frameTimeCounter/2000.-0.5));
  385. vec2 movement2 = vec2(abs(frameTimeCounter/2000.-0.5),abs(frameTimeCounter/2000.-0.5));
  386.  
  387. vec2 coord = (posxz/600)+(movement);
  388. vec2 coord1 = (posxz/599)+(movement2);
  389. vec2 coord2 = (posxz/598)+(movement);
  390. vec2 coord3 = (posxz/597)+(movement2);
  391.  
  392. float noise = texture2D(noisetex,fract(coord.xy/2.0)).x/8;
  393. noise += texture2D(noisetex,fract(coord1.xy)).x/16.0;
  394. noise += texture2D(noisetex,fract(coord2.xy*2.0)).x/8;
  395. noise += texture2D(noisetex,fract(coord3.xy*2.0)).x/8;
  396.  
  397. return noise * visibility;
  398. }
  399.  
  400. vec3 skyGradient (vec3 fposition, vec3 color, vec3 fogclr) {
  401.  
  402. return (fogclr*3.+color)/4.;
  403.  
  404.  
  405. }
  406.  
  407.  
  408. vec3 getWaveHeight(vec2 posxz){
  409.  
  410. vec2 coord = (posxz);
  411.  
  412. float deltaPos = 0.3;
  413.  
  414. //float fc = (find_closest(texcoord.st,deltaPos));
  415. float h0 = waterH(coord, 1.0f);
  416. float h1 = waterH(coord + vec2(deltaPos,0.0), 1.0f);
  417. float h2 = waterH(coord + vec2(-deltaPos,0.0), 1.0f);
  418. float h3 = waterH(coord + vec2(0.0,deltaPos), 1.0f);
  419. float h4 = waterH(coord + vec2(0.0,-deltaPos), 1.0f);
  420.  
  421. float xDelta = ((h1-h0)+(h0-h2))/deltaPos;
  422. float yDelta = ((h3-h0)+(h0-h4))/deltaPos;
  423.  
  424. vec3 wave = normalize(vec3(xDelta,yDelta,1.0-pow(abs(xDelta+yDelta),2.0)));
  425.  
  426. return wave;
  427. }
  428.  
  429.  
  430. float getWaterDepth(){
  431.  
  432. vec2 getCustomTc = texcoord.st;
  433.  
  434. vec3 uPos = vec3(.0);
  435.  
  436. float uDepth = texture2D(depthtex1,getCustomTc).x;
  437. uPos = nvec3(gbufferProjectionInverse * vec4(vec3(getCustomTc.xy,uDepth) * 2.0 - 1.0, 1.0));
  438.  
  439. vec3 uVec = fragpos-uPos;
  440.  
  441. float UNdotUP = abs(dot(normalize(uVec),normal));
  442. float depth = sqrt(dot(uVec,uVec))*UNdotUP;
  443.  
  444. //depth *= refractMask;
  445.  
  446. return depth;
  447. }
  448.  
  449. float refractmask(in vec2 coord){
  450.  
  451. float mask = texture2D(gaux1, coord.st).g;
  452. mask = float(mask > 0.04 && mask < 0.07);
  453. return mask;
  454.  
  455. }
  456.  
  457.  
  458.  
  459. void waterRefraction(inout vec3 color, in vec4 worldposition, in vec4 fragposition) {
  460. vec3 posxz = worldposition.xyz + cameraPosition.xyz;
  461.  
  462. float chromaticAbborationAmount = 0.1;
  463.  
  464. vec3 refraction = getWaveHeight(posxz.xz + posxz.y);
  465.  
  466. float depth = getDepth(texture2D(depthtex1, texcoord.st).x);
  467. float depth2 = getDepth(pixeldepth);
  468.  
  469. float wDepth = depth - depth2;
  470.  
  471. float refMult = 1.0;
  472. refMult = clamp(wDepth,0.0,1.0);
  473. refMult /= (depth2);
  474. refMult *= (REFRACT_MULT/75);
  475.  
  476. chromaticAbborationAmount *= refMult;
  477.  
  478. vec2 coord1 = texcoord.st + refraction.xy * (refMult);
  479. vec2 coord2 = texcoord.st + refraction.xy * (refMult + chromaticAbborationAmount);
  480. vec2 coord3 = texcoord.st + refraction.xy * (refMult + chromaticAbborationAmount * 2.0);
  481.  
  482. vec3 rA;
  483. rA.x = texture2D(gcolor, coord1).x;
  484. rA.y = texture2D(gcolor, coord2).y;
  485. rA.z = texture2D(gcolor, coord3).z;
  486.  
  487. float maskCoord1 = refractmask(coord1);
  488. float maskCoord2 = refractmask(coord2);
  489. float maskCoord3 = refractmask(coord3);
  490.  
  491. vec3 rB = color.rgb;
  492.  
  493. refraction.r = rA.r*maskCoord1 + rB.r*(1-maskCoord1);
  494. refraction.g = rA.g*maskCoord2 + rB.g*(1-maskCoord2);
  495. refraction.b = rA.b*maskCoord3 + rB.b*(1-maskCoord3);
  496.  
  497. if (iswater > 0.9)
  498. color = refraction;
  499. //color += abs(1-clamp(pixeldepth / 1.25,0.0,1.0));
  500. }
  501.  
  502.  
  503. vec3 calcFog(vec3 fposition, vec3 color) {
  504. float fog = exp(-pow(length(fposition)/256.0,4.0-(2.7*rainStrength))*40.0);
  505. float fogfactor = clamp(fog,0.0,1.0);
  506. fogclr = getFogColor(fposition.xyz);
  507. return mix((fogclr+color.rgb*2.0*(1-rainStrength*0.9))/(1+2.0*(1-rainStrength*0.9)),color.rgb,fogfactor);
  508. }
  509.  
  510.  
  511.  
  512. vec4 raytraceGround(vec3 fragpos, vec3 normal,vec3 fogclr) {
  513. vec4 color = vec4(0.0);
  514. vec3 start = fragpos;
  515. vec3 rvector = normalize(reflect(normalize(fragpos), normalize(normal)));
  516.  
  517. vec3 vector = Gstp * rvector;
  518.  
  519. vec3 oldpos = fragpos;
  520. fragpos += vector;
  521. vec3 tvector = vector;
  522. int sr = 0;
  523. for(int i=0;i<30;i++){
  524. vec3 pos = nvec3(gbufferProjection * nvec4(fragpos)) * 0.5 + 0.5;
  525. if(pos.x < 0 || pos.x > 1 || pos.y < 0 || pos.y > 1 || pos.z < 0 || pos.z > 1.0) break;
  526. vec3 spos = vec3(pos.st, texture2D(depthtex1, pos.st).r);
  527. spos = nvec3(gbufferProjectionInverse * nvec4(spos * 2.0 - 1.0));
  528. float err = distance(fragpos.xyz,spos.xyz);
  529. if(err < pow(length(vector)*1.8,1.15)){
  530.  
  531. sr++;
  532. if(sr >= maxf){
  533. float border = clamp(1.0 - pow(cdist(pos.st), 20.0), 0.0, 1.0);
  534. color = texture2D(composite, pos.st,0);
  535. float land = texture2D(gaux1, pos.st).g;
  536. land = float(land < 0.03);
  537.  
  538. spos.z = mix(spos.z,2000.0*(0.25+sunVisibility*0.75),land);
  539. if (land > 0.0) color.rgb = skyGradient(spos,pow(color.rgb,vec3(2.2))*MAX_COLOR_RANGE,fogclr);
  540. else color.rgb = vec3(calcFog)(spos,pow(color.rgb,vec3(2.2))*MAX_COLOR_RANGE,fogclr);
  541. color.a = 1.0;
  542. color.a *= border;
  543. break;
  544. }
  545. tvector -=vector;
  546. vector *=Gref;
  547.  
  548.  
  549. }
  550. vector *= Ginc;
  551. oldpos = fragpos;
  552. tvector += vector;
  553. fragpos = start + tvector;
  554. }
  555. return color;
  556. }
  557.  
  558.  
  559.  
  560.  
  561. vec3 drawSun(vec3 fposition,vec3 color,int land) {
  562. vec3 sVector = normalize(fposition);
  563. float sun = max(pow(clamp(dot(sVector,normalize(sunPosition))+0.002,0.0,1.0),750.0)-0.002,0.0)*land*(1-rainStrength*0.75)*sunVisibility*20;
  564. vec3 sunlight = mix(sunlight,vec3(0.25,0.3,0.4)*length(ambient_color),rainStrength*0.8);
  565.  
  566. return mix(color,sunlight*30.0,sun);
  567.  
  568. }
  569.  
  570.  
  571.  
  572.  
  573. vec4 raytrace(vec3 fragpos, vec3 normal) {
  574. float mult = clamp(texture2D(noisetex, vec2(frameTimeCounter / 10.0)).x,0.8,1.9);
  575.  
  576. vec4 color = vec4(0.0);
  577. vec3 start = fragpos;
  578. vec3 rvector = normalize(reflect(normalize(fragpos), normalize(normal)));
  579. vec3 vector = stp * rvector;
  580. vec3 oldpos = fragpos;
  581. fragpos += vector;
  582. vec3 tvector = vector;
  583. int sr = 0;
  584. for(int i=0;i<40;i++){
  585. vec3 pos = nvec3(gbufferProjection * nvec4(fragpos)) * 0.5 + 0.5;
  586. if(pos.x < 0 || pos.x > 1 || pos.y < 0 || pos.y > 1 || pos.z < 0 || pos.z > 1.0) break;
  587. vec3 spos = vec3(pos.st, texture2D(depthtex1, pos.st).r);
  588. spos = nvec3(gbufferProjectionInverse * nvec4(spos * 2.0 - 1.0));
  589. float err = abs(fragpos.z-spos.z);
  590. if(err < pow(length(vector)*1.85,1.15)){
  591.  
  592. sr++;
  593. if(sr >= maxf){
  594. float border = clamp(1.0 - pow(cdist(pos.st), 20.0), 0.0, 1.0);
  595. color = texture2D(composite, pos.st);
  596. color.rgb = calcFog(spos,pow(color.rgb,vec3(2.2))*MAX_COLOR_RANGE);
  597. color.a = 1.0;
  598. color.a *= border;
  599. break;
  600. }
  601. tvector -=vector;
  602. vector *=ref;
  603.  
  604.  
  605. }
  606. vector *= inc;
  607. oldpos = fragpos;
  608. tvector += vector;
  609. fragpos = start + tvector;
  610. }
  611. return color;
  612. }
  613.  
  614.  
  615.  
  616.  
  617. vec3 drawCloud(vec3 fposition,vec3 color) {
  618. vec3 sVector = normalize(fposition);
  619. float cosT = dot(sVector,upVec);
  620. float McosY = dot(moonVec,sVector);
  621. float cosY = dot(sunVec,sVector);
  622. float MY = acos(McosY);
  623. //cloud generation
  624. /*----------*/
  625. float mult = texture2D(noisetex, vec2(frameTimeCounter / 10.0)).x;
  626.  
  627. vec3 tpos = vec3(gbufferModelViewInverse * vec4(fposition,1.0))*mult;
  628. vec3 wvec = normalize(tpos);
  629. vec3 wVector = normalize(tpos);
  630. vec3 intersection = wVector*(0.0/(wVector.y)) + wVector*(wVector.y*0)*mult;
  631.  
  632.  
  633.  
  634. float canHit = length(intersection)-length(tpos);
  635.  
  636.  
  637. vec2 wind = vec2(frameTimeCounter*(cos(frameTimeCounter/100.0)+0.5),frameTimeCounter*(sin(frameTimeCounter/100.0)+0.5))*mult;
  638.  
  639. vec3 wpos = tpos.xyz+cameraPosition;
  640. vec2 coord = (intersection.xz+ 2.0*cosT*intersection.xz+wind)/512.0*mult;
  641. float noise = texture2D(noisetex,fract(coord.xy/2.0)).x*mult;
  642.  
  643.  
  644.  
  645.  
  646. float coverageVariance = (sin(-intersection.x*0.015+frameTimeCounter*0.1)+cos(intersection.z*0.01-frameTimeCounter*0.04))*(1+sin(frameTimeCounter*0.008))*0.5*mult+(10-rainStrength);
  647. vec2 tc = vec2(cos(coord.x*0.25-frameTimeCounter*0.003),cos(coord.y*0.25+frameTimeCounter*0.003));
  648. coverageVariance = (texture2D(noisetex,tc).x*2.0-1.0)*0.1*mult;
  649. float cl = max(noise-1.02-coverageVariance+rainStrength*0.7,0.0)*mult;
  650.  
  651.  
  652. float cloud = pow((1.0 - (pow(0.2-rainStrength*0.19,cl)))*max(cosT,0.0),2.2)*mult;
  653. float N = 8.0;
  654. vec3 cloud_color = moonlight*4.0*moonVisibility*(1-rainStrength*0.9) + sunlight*4.0*sunVisibility*(1-rainStrength*0.9) + ambient_color*5.0 + sunlight*24.0*pow(max(cosY,0.0),N)*(N+1)/6.28 * (1-cloud) * (1-rainStrength)*sunVisibility + moonlight*48.0*pow(max(McosY,0.0),N)*(N+1)/6.28 * (cloud*0.5+0.5) * (1-rainStrength)*moonVisibility ; //coloring clouds
  655. /*----------*/
  656. return mix(color,cloud_color*2.0,cloud); //mix up sky color and clouds
  657. //return cloud*cloud_color;
  658. }
  659.  
  660. float convertVec3ToFloat(in vec3 invec){
  661.  
  662. float mixing;
  663. mixing += invec.x;
  664. mixing += invec.y;
  665. mixing += invec.z;
  666. mixing /= 3.0;
  667.  
  668. return mixing;
  669. }
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676. vec4 worldposition = vec4(0.0);
  677.  
  678.  
  679. vec4 raytraceGround(vec3 fragpos, vec3 normal) {
  680. vec4 color = vec4(0.0);
  681. vec3 start = fragpos;
  682. vec3 rvector = normalize(reflect(normalize(fragpos), normalize(normal)));
  683. vec3 vector = Gstp * rvector;
  684. vec3 oldpos = fragpos;
  685. fragpos += vector;
  686. vec3 tvector = vector;
  687. int sr = 0;
  688. for(int i=0;i<30;i++){
  689. vec3 pos = nvec3(gbufferProjection * nvec4(fragpos)) * 0.5 + 0.5;
  690. if(pos.x < 0 || pos.x > 1 || pos.y < 0 || pos.y > 1 || pos.z < 0 || pos.z > 1.0) break;
  691. vec3 spos = vec3(pos.st, texture2D(depthtex1, pos.st).r);
  692. spos = nvec3(gbufferProjectionInverse * nvec4(spos * 2.0 - 1.0));
  693. float err = distance(fragpos.xyz,spos.xyz);
  694. if(err < length(vector)){
  695.  
  696. sr++;
  697. if(sr >= maxf){
  698. float border = clamp(1.0 - pow(cdist(pos.st), 20.0), 0.0, 1.0);
  699. color = texture2D(composite, pos.st);
  700. color.rgb = calcFog(spos,pow(color.rgb,vec3(2.2))*MAX_COLOR_RANGE);
  701. color.a = 1.0;
  702. color.a *= border;
  703. break;
  704. }
  705. tvector -=vector;
  706. vector *=Gref;
  707.  
  708.  
  709. }
  710. vector *= Ginc;
  711. oldpos = fragpos;
  712. tvector += vector;
  713. fragpos = start + tvector;
  714. }
  715. return color;
  716. }
  717.  
  718. vec3 waterCaustic(vec3 fposition,vec3 color) {
  719.  
  720. vec2 movement = vec2(abs(frameTimeCounter/1000.-0.5),abs(frameTimeCounter/1000.-0.5));
  721. vec2 movement2 = vec2(abs(frameTimeCounter/1000.-0.5),-abs(frameTimeCounter/1000.-0.5));
  722. vec2 movement3 = vec2(-abs(frameTimeCounter/1000.-0.5),abs(frameTimeCounter/1000.-0.5));
  723. vec2 movement4 = vec2(-abs(frameTimeCounter/1000.-0.5),-abs(frameTimeCounter/1000.-0.5));
  724.  
  725. vec3 underwaterpos = vec3(texcoord.st, texture2D(depthtex1, texcoord.st).r);
  726. underwaterpos = nvec3(gbufferProjectionInverse * nvec4(underwaterpos * 2.0 - 1.0));
  727. vec4 worldpositionuw = gbufferModelViewInverse * vec4(underwaterpos,1.0);
  728. vec3 wpos = (worldpositionuw.xyz + cameraPosition.xyz);
  729.  
  730. vec2 coord = (wpos.xz/(270*UnderwaterWaterCausticsSize))+(movement*(4*UnderwaterWaterCausticsSpeed));
  731. vec2 coord1 = (wpos.xz/(269.9*UnderwaterWaterCausticsSize))+(movement2*(4*UnderwaterWaterCausticsSpeed));
  732. vec2 coord2 = (wpos.xz/(269.8*UnderwaterWaterCausticsSize))+(movement3*(4*UnderwaterWaterCausticsSpeed));
  733. vec2 coord3 = (wpos.xz/(269.7*UnderwaterWaterCausticsSize))+(movement4*(4*UnderwaterWaterCausticsSpeed));
  734.  
  735.  
  736.  
  737. float noise = texture2D(noisetex,fract(coord.xy/2.0)).x/2.0;
  738. noise += texture2D(noisetex,fract(coord2.xy/2.0)).x/2.0;
  739. noise += texture2D(noisetex,fract(coord3.xy/2.0)).x/2.0;
  740. noise += texture2D(noisetex,fract(coord.xy/2.0)).x/2.0;
  741. noise += texture2D(noisetex,fract(coord.xy)).x/2.0;
  742. noise += texture2D(noisetex,fract(coord2.xy)).x/2.0;
  743. noise += texture2D(noisetex,fract(coord3.xy)).x/2.0;
  744. noise += texture2D(noisetex,fract(coord.xy)).x/2.0;
  745.  
  746. float noise2 = texture2D(noisetex,fract(coord.xy/2.0+0.01)).x/2.0;
  747. noise2 += texture2D(noisetex,fract(coord2.xy/2.0+0.01)).x/2.0;
  748. noise2 += texture2D(noisetex,fract(coord3.xy/2.0+0.01)).x/2.0;
  749. noise2 += texture2D(noisetex,fract(coord.xy/2.0+0.01)).x/2.0;
  750. noise2 += texture2D(noisetex,fract(coord.xy+0.01)).x/2.0;
  751. noise2 += texture2D(noisetex,fract(coord2.xy+0.01)).x/2.0;
  752. noise2 += texture2D(noisetex,fract(coord3.xy+0.01)).x/2.0;
  753. noise2 += texture2D(noisetex,fract(coord.xy+0.01)).x/2.0;
  754.  
  755.  
  756. float causticstrength = (max(abs(noise-1.95),0.0)+max(abs(noise2-1.95),0.0));
  757. float wca = (0.1/UnderwaterWaterCausticsStrength);
  758. float caustic = (1.0 - (pow(wca,causticstrength)));
  759. vec3 caustic_color = color*(10.0-(0.7-moonVisibility*0.4))*(0.1*moonVisibility); //coloring caustics
  760. vec3 wc = mix(color,caustic_color,caustic);
  761.  
  762. return wc;
  763. }
  764.  
  765.  
  766.  
  767. float distx(float dist){
  768. float d = ((far * (dist - near)) / (dist * (far - near)));
  769. return d;
  770. }
  771.  
  772.  
  773.  
  774. float getnoise2(vec2 pos) {
  775. return mod((dot(pos.xy, vec2(18.9898f,28.633f)) * 4378.5453f), 1.0);
  776. }
  777.  
  778.  
  779.  
  780. #ifdef VolumeLight
  781. vec3 vlColor(vec3 fogcolor,in vec3 color, in vec2 pos) {
  782.  
  783. float VolumeSample = texture2D(gdepth, pos.xy, 3.0).r;
  784.  
  785. float iN = (VolumeLightMultiplierNight*moonVisibility);
  786. float iD = (VolumeLightMultiplierDay*TimeNoon);
  787. float iSSSR = (VolumeLightMultiplierSunsetSunrise*(1-TimeNoon)*(1-moonVisibility));
  788.  
  789. float i = (iN + iD + iSSSR);
  790.  
  791. float eBS = mix(1.0,0.0,(pow(eyeBrightnessSmooth.y / 240.0f, 1.0f)));
  792.  
  793. float Glow = pow(max(dot(normalize(fragpos),lightVector),0.0),2.5*15);
  794. float vlGlow = (1-Glow*-(2.5*(1-TimeNoon*-5)*(1+(TimeSunrise + TimeSunset)*15.)*(1-eBS))) * 0.5;
  795.  
  796. float vlInside = ((eBS*10.0*VL_STRENGTH_INSIDE*(1-moonVisibility)));
  797. float vlInsideNight = ((eBS*10.0*VL_STRENGTH_INSIDE*(moonVisibility)));
  798. float vlFinalInside = (vlInside + vlInsideNight);
  799.  
  800. vec3 vlDay = vec3(10.0,10.0,10.0)/50*VolumeLightMultiplierDay;
  801. vec3 vlNight = vec3(10.0,10.0,10.0)/50 * VolumeLightMultiplierNight;
  802. vec3 vlSSSR = vec3(10.0,10.0,10.0)/50*(VolumeLightMultiplierSunsetSunrise * (1 - TimeNoon) * (1 - moonVisibility));
  803. vec3 combined = (vlDay + vlNight + vlSSSR);
  804.  
  805. vec3 vlcolor = combined;
  806. vlcolor = mix(vlcolor,pow(vlcolor,vec3(1.5)) * 2.2,(TimeSunrise + TimeSunset));
  807. vlcolor *= (1 + (vlFinalInside));
  808. vlcolor *= (1 + (vlGlow));
  809.  
  810. vlcolor = mix(color, vlcolor, VolumeSample / 5.0 * VolumeLightMultiplier * (1-isEyeInWater) * (1-rainx) * transition_fading);
  811. return vlcolor;
  812. }
  813. #endif
  814.  
  815.  
  816.  
  817.  
  818. //////////////////////////////VOID MAIN//////////////////////////////
  819. //////////////////////////////VOID MAIN//////////////////////////////
  820. //////////////////////////////VOID MAIN//////////////////////////////
  821. //////////////////////////////VOID MAIN//////////////////////////////
  822. //////////////////////////////VOID MAIN//////////////////////////////
  823. void main() {
  824. color.rgb = pow(color.rgb,vec3(2.2))*MAX_COLOR_RANGE;
  825. int land = int(matflag < 0.03);
  826. int iswater = int(matflag > 0.04 && matflag < 0.07);
  827. int hand = int(matflag > 0.75 && matflag < 0.85);
  828.  
  829. fragpos = nvec3(gbufferProjectionInverse * nvec4(fragpos * 2.0 - 1.0));
  830. float depth = getWaterDepth();
  831.  
  832. float depthMap = clamp(exp(-depth / 2.5),0.0,1.0);
  833. if (iswater > 0.9)color.rgb = mix(color.rgb,getSkyColor(fragpos.rgb)/954,1-depthMap);
  834.  
  835. color.rgb = drawSun(fragpos,color.rgb,land);
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842. #ifdef UnderwaterWaterCaustics
  843. if (isEyeInWater > 0.9)
  844. color.rgb += waterCaustic(fragpos, color.rgb)*(depthMap)*1.65;
  845. else
  846. color.rgb += waterCaustic(fragpos, color.rgb)*(depthMap)*1.65*iswater;
  847. #endif
  848.  
  849.  
  850. float fresnel_pow = 5.0;
  851. float normalDotEye = dot(normal, normalize(fragpos));
  852. float fresnel = clamp(pow(1.0 + normalDotEye, fresnel_pow),0.0,1.0);
  853. float fmult = 0.95;
  854. fresnel = fresnel*fmult + (1-fmult);
  855. vec4 reflection = vec4(0.0);
  856. vec3 lc = mix(vec3(0.0),sunlight,sunVisibility);
  857. if (iswater > 0.9) {
  858.  
  859. //compute skybox at reflected position
  860.  
  861. vec3 npos = normalize(fragpos);
  862. vec3 reflectedVector = reflect(normalize(fragpos), normalize(normal));
  863. reflectedVector = fragpos + reflectedVector * (far-fragpos.z);
  864. vec3 sky_color = calcFog(reflectedVector,drawCloud(reflectedVector,getSkyColor(reflectedVector)))*clamp(sky_lightmap*2.0-2/16.0,0.0,1.0);
  865.  
  866.  
  867. reflection = raytrace(fragpos, normal);
  868. reflection.rgb = mix(sky_color, reflection.rgb, reflection.a)*0.75+(color.a*50)*lc*(1.0-rainStrength)*48.0; //fake sky reflection, avoid empty spaces
  869. reflection.a = min(reflection.a,1.0);
  870. color.rgb = reflection.rgb*WaterReflectionStrength *fresnel + (1-fresnel)*color.rgb;
  871.  
  872. }
  873. if (land < 0.9 && hand < 0.1) {
  874.  
  875.  
  876.  
  877. vec3 npos = normalize(fragpos);
  878. vec3 reflectedVector = reflect(normalize(fragpos), normalize(normal));
  879. reflectedVector = fragpos + reflectedVector * (far-length(fragpos));
  880. vec3 sky_color = calcFog(reflectedVector,drawCloud(reflectedVector,getSkyColor(reflectedVector)))*clamp(sky_lightmap*2.0-2/16.0,0.0,1.0);
  881.  
  882.  
  883. #ifdef Specular
  884. if (specmap*fresnel > 0.005) reflection = raytraceGround(fragpos, normal, sky_color);
  885.  
  886. reflection.rgb = mix(sky_color, reflection.rgb, reflection.a)+(color.a)*lc*(1.0-rainStrength)*24.;
  887. reflection.rgb = mix(reflection.rgb,reflection.rgb*normalize(color.rgb),0.0);
  888. reflection.rgb = reflection.rgb*3.;
  889. color.rgb = specmap*fresnel*reflection.rgb + (1-fresnel*specmap)*color.rgb;;
  890. #endif
  891.  
  892. }
  893.  
  894. /*
  895. vec3 npos = normalize(fragpos);
  896. vec3 reflectedVector = reflect(normalize(fragpos), normalize(normal));
  897. reflectedVector = fragpos + reflectedVector * (far-length(fragpos));
  898. vec3 sky_color = calcFog(reflectedVector,drawCloud(reflectedVector,getSkyColor(reflectedVector)))*clamp(sky_lightmap*2.0-2/16.0,0.0,1.0);
  899.  
  900. reflection = raytraceGround(fragpos, normal);
  901. reflection.rgb = mix(sky_color, reflection.rgb, reflection.a)* WaterReflectionStrength + (color.a)*pow(lc,vec3(1/2.2))*(1.0-rainStrength)*256.0; //fake sky reflection, avoid empty spaces
  902. //reflection.rgb = mix(reflection.rgb,reflection.rgb*normalize(color.rgb),0.5);
  903. reflection.a = min(reflection.a,1.0);
  904. reflection.rgb = reflection.rgb;
  905. color.rgb = reflection.rgb;
  906. */
  907.  
  908. //color.rgb += fresnel*torchcolor*torch_lightmap*specmap;
  909.  
  910. vec3 colmult = mix(vec3(1.0),vec3(0.1,0.25,0.45),isEyeInWater);
  911. float depth_diff = clamp(pow(ld(texture2D(depthtex1, texcoord.st).r)*3.4,2.0),0.0,1.0);
  912. color.rgb = mix(color.rgb*colmult,vec3(0.05,0.1,0.15),depth_diff*isEyeInWater);
  913.  
  914.  
  915. color.rgb = calcFog(fragpos.xyz,color.rgb);
  916.  
  917.  
  918. float transition_fading = 1.0-(clamp((time-12000.0)/300.0,0.0,1.0)-clamp((time-13500.0)/300.0,0.0,1.0) + clamp((time-22500.0)/300.0,0.0,1.0)-clamp((time-23400.0)/300.0,0.0,1.0)); //fading between sun/moon shadows
  919.  
  920. /* DRAWBUFFERS:5 */
  921.  
  922. //draw rain
  923. //color.rgb = texture2D(gaux4,texcoord.xy).rgb*texture2D(gaux4,texcoord.xy).a;
  924.  
  925. vec4 tpos = vec4(sunPosition,1.0)*gbufferProjection;
  926. tpos = vec4(tpos.xyz/tpos.w,1.0);
  927. vec2 pos1 = tpos.xy/tpos.z;
  928. vec2 lightPos = pos1*0.5+0.5;
  929. float gr = 0.0;
  930.  
  931.  
  932. float visiblesun = 0.0;
  933. float temp;
  934. float nb = 0;
  935.  
  936. //calculate sun occlusion (only on one pixel)
  937. if (texcoord.x < pw && texcoord.x < ph) {
  938. for (int i = 0; i < 15;i++) {
  939. for (int j = 0; j < 15 ;j++) {
  940. temp = texture2D(gaux1,clamp(lightPos + vec2(pw*(i-7.0),ph*(j-7.0))*15.0,0.0001,0.9999)).g;
  941. visiblesun += 1.0-float(temp > 0.01) ;
  942. nb += 1.0;
  943. }
  944. }
  945. visiblesun /= nb;
  946. }
  947.  
  948. #ifdef VolumeLight
  949. color.rgb = vec3(vlColor(vec3(fogclr), color.rgb, texcoord.st));
  950. #endif
  951.  
  952.  
  953. /*
  954. if (texcoord.x > 1.0-pw && texcoord.x > 1.0-ph) {
  955. float avglum = 0.0;
  956. for (int i = 0; i < 5;i++) {
  957. for (int j = 0; j < 5 ;j++) {
  958. float distFallof = 1.0-sqrt(max(i*1.0,j*1.0)+1.0f)/9.0;
  959. avglum += luma(texture2D(composite,vec2(0.5) + vec2(pw*(i-2.0),ph*(j-2.0))*vec2(1.0,aspectRatio)*150.0).rgb);
  960. nb += distFallof;
  961. }
  962. }
  963. visiblesun = avglum/nb;
  964. }
  965.  
  966.  
  967. if (isEyeInWater < 0.9) color.rgb += (reflect*0.2)*iswet;
  968.  
  969.  
  970. */
  971.  
  972. color.rgb = clamp(pow(color.rgb/MAX_COLOR_RANGE,vec3(1.0/2.2)),0.0,1.0);
  973.  
  974.  
  975. gl_FragData[0] = vec4(color.rgb,visiblesun*float(sign(sunPosition.z)<0.0));
  976. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement