sky.fragment.fx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. precision highp float;
  2. // Input
  3. varying vec3 vPositionW;
  4. #ifdef VERTEXCOLOR
  5. varying vec4 vColor;
  6. #endif
  7. #ifdef CLIPPLANE
  8. varying float fClipDistance;
  9. #endif
  10. // Sky
  11. uniform float luminance;
  12. uniform float turbidity;
  13. uniform float rayleigh;
  14. uniform float mieCoefficient;
  15. uniform float mieDirectionalG;
  16. uniform vec3 sunPosition;
  17. // Fog
  18. #ifdef FOG
  19. #define FOGMODE_NONE 0.
  20. #define FOGMODE_EXP 1.
  21. #define FOGMODE_EXP2 2.
  22. #define FOGMODE_LINEAR 3.
  23. #define E 2.71828
  24. uniform vec4 vFogInfos;
  25. uniform vec3 vFogColor;
  26. varying float fFogDistance;
  27. float CalcFogFactor()
  28. {
  29. float fogCoeff = 1.0;
  30. float fogStart = vFogInfos.y;
  31. float fogEnd = vFogInfos.z;
  32. float fogDensity = vFogInfos.w;
  33. if (FOGMODE_LINEAR == vFogInfos.x)
  34. {
  35. fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
  36. }
  37. else if (FOGMODE_EXP == vFogInfos.x)
  38. {
  39. fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
  40. }
  41. else if (FOGMODE_EXP2 == vFogInfos.x)
  42. {
  43. fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
  44. }
  45. return clamp(fogCoeff, 0.0, 1.0);
  46. }
  47. #endif
  48. // Constants
  49. const float e = 2.71828182845904523536028747135266249775724709369995957;
  50. const float pi = 3.141592653589793238462643383279502884197169;
  51. const float n = 1.0003;
  52. const float N = 2.545E25;
  53. const float pn = 0.035;
  54. const vec3 lambda = vec3(680E-9, 550E-9, 450E-9);
  55. const vec3 K = vec3(0.686, 0.678, 0.666);
  56. const float v = 4.0;
  57. const float rayleighZenithLength = 8.4E3;
  58. const float mieZenithLength = 1.25E3;
  59. const vec3 up = vec3(0.0, 1.0, 0.0);
  60. const float EE = 1000.0;
  61. const float sunAngularDiameterCos = 0.999956676946448443553574619906976478926848692873900859324;
  62. const float cutoffAngle = pi/1.95;
  63. const float steepness = 1.5;
  64. vec3 totalRayleigh(vec3 lambda)
  65. {
  66. return (8.0 * pow(pi, 3.0) * pow(pow(n, 2.0) - 1.0, 2.0) * (6.0 + 3.0 * pn)) / (3.0 * N * pow(lambda, vec3(4.0)) * (6.0 - 7.0 * pn));
  67. }
  68. vec3 simplifiedRayleigh()
  69. {
  70. return 0.0005 / vec3(94, 40, 18);
  71. }
  72. float rayleighPhase(float cosTheta)
  73. {
  74. return (3.0 / (16.0*pi)) * (1.0 + pow(cosTheta, 2.0));
  75. }
  76. vec3 totalMie(vec3 lambda, vec3 K, float T)
  77. {
  78. float c = (0.2 * T ) * 10E-18;
  79. return 0.434 * c * pi * pow((2.0 * pi) / lambda, vec3(v - 2.0)) * K;
  80. }
  81. float hgPhase(float cosTheta, float g)
  82. {
  83. return (1.0 / (4.0*pi)) * ((1.0 - pow(g, 2.0)) / pow(1.0 - 2.0*g*cosTheta + pow(g, 2.0), 1.5));
  84. }
  85. float sunIntensity(float zenithAngleCos)
  86. {
  87. return EE * max(0.0, 1.0 - exp(-((cutoffAngle - acos(zenithAngleCos))/steepness)));
  88. }
  89. float A = 0.15;
  90. float B = 0.50;
  91. float C = 0.10;
  92. float D = 0.20;
  93. float E = 0.02;
  94. float F = 0.30;
  95. float W = 1000.0;
  96. vec3 Uncharted2Tonemap(vec3 x)
  97. {
  98. return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
  99. }
  100. void main(void) {
  101. // Clip plane
  102. #ifdef CLIPPLANE
  103. if (fClipDistance > 0.0)
  104. discard;
  105. #endif
  106. /**
  107. *--------------------------------------------------------------------------------------------------
  108. * Sky Color
  109. *--------------------------------------------------------------------------------------------------
  110. */
  111. const vec3 cameraPos = vec3(0.0, 0.0, 0.0);
  112. float sunfade = 1.0 - clamp(1.0 - exp((sunPosition.y / 450000.0)), 0.0, 1.0);
  113. float rayleighCoefficient = rayleigh - (1.0 * (1.0 - sunfade));
  114. vec3 sunDirection = normalize(sunPosition);
  115. float sunE = sunIntensity(dot(sunDirection, up));
  116. vec3 betaR = simplifiedRayleigh() * rayleighCoefficient;
  117. vec3 betaM = totalMie(lambda, K, turbidity) * mieCoefficient;
  118. float zenithAngle = acos(max(0.0, dot(up, normalize(vPositionW - cameraPos))));
  119. float sR = rayleighZenithLength / (cos(zenithAngle) + 0.15 * pow(93.885 - ((zenithAngle * 180.0) / pi), -1.253));
  120. float sM = mieZenithLength / (cos(zenithAngle) + 0.15 * pow(93.885 - ((zenithAngle * 180.0) / pi), -1.253));
  121. vec3 Fex = exp(-(betaR * sR + betaM * sM));
  122. float cosTheta = dot(normalize(vPositionW - cameraPos), sunDirection);
  123. float rPhase = rayleighPhase(cosTheta*0.5+0.5);
  124. vec3 betaRTheta = betaR * rPhase;
  125. float mPhase = hgPhase(cosTheta, mieDirectionalG);
  126. vec3 betaMTheta = betaM * mPhase;
  127. vec3 Lin = pow(sunE * ((betaRTheta + betaMTheta) / (betaR + betaM)) * (1.0 - Fex),vec3(1.5));
  128. Lin *= mix(vec3(1.0), pow(sunE * ((betaRTheta + betaMTheta) / (betaR + betaM)) * Fex, vec3(1.0 / 2.0)), clamp(pow(1.0-dot(up, sunDirection), 5.0), 0.0, 1.0));
  129. vec3 direction = normalize(vPositionW - cameraPos);
  130. float theta = acos(direction.y);
  131. float phi = atan(direction.z, direction.x);
  132. vec2 uv = vec2(phi, theta) / vec2(2.0 * pi, pi) + vec2(0.5, 0.0);
  133. vec3 L0 = vec3(0.1) * Fex;
  134. float sundisk = smoothstep(sunAngularDiameterCos, sunAngularDiameterCos + 0.00002, cosTheta);
  135. L0 += (sunE * 19000.0 * Fex) * sundisk;
  136. vec3 whiteScale = 1.0/Uncharted2Tonemap(vec3(W));
  137. vec3 texColor = (Lin+L0);
  138. texColor *= 0.04 ;
  139. texColor += vec3(0.0,0.001,0.0025)*0.3;
  140. float g_fMaxLuminance = 1.0;
  141. float fLumScaled = 0.1 / luminance;
  142. float fLumCompressed = (fLumScaled * (1.0 + (fLumScaled / (g_fMaxLuminance * g_fMaxLuminance)))) / (1.0 + fLumScaled);
  143. float ExposureBias = fLumCompressed;
  144. vec3 curr = Uncharted2Tonemap((log2(2.0/pow(luminance,4.0)))*texColor);
  145. vec3 skyColor = curr * whiteScale;
  146. vec3 retColor = pow(skyColor,vec3(1.0/(1.2+(1.2*sunfade))));
  147. vec4 baseColor = vec4(retColor, 1.0);
  148. /**
  149. *--------------------------------------------------------------------------------------------------
  150. * Sky Color
  151. *--------------------------------------------------------------------------------------------------
  152. */
  153. // Alpha
  154. float alpha = 1.0;
  155. #ifdef VERTEXCOLOR
  156. baseColor.rgb *= vColor.rgb;
  157. #endif
  158. // Lighting
  159. vec3 diffuseBase = vec3(1.0, 1.0, 1.0);
  160. #ifdef VERTEXALPHA
  161. alpha *= vColor.a;
  162. #endif
  163. // Composition
  164. vec4 color = vec4(baseColor.rgb, alpha);
  165. #ifdef FOG
  166. float fog = CalcFogFactor();
  167. color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
  168. #endif
  169. gl_FragColor = color;
  170. }