SkyShader.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * @author zz85 / https://github.com/zz85
  3. *
  4. * Based on "A Practical Analytic Model for Daylight"
  5. * aka The Preetham Model, the de facto standard analytic skydome model
  6. * http://www.cs.utah.edu/~shirley/papers/sunsky/sunsky.pdf
  7. *
  8. * First implemented by Simon Wallner
  9. * http://www.simonwallner.at/projects/atmospheric-scattering
  10. *
  11. * Improved by Martin Upitis
  12. * http://blenderartists.org/forum/showthread.php?245954-preethams-sky-impementation-HDR
  13. *
  14. * Three.js integration by zz85 http://twitter.com/blurspline
  15. */
  16. THREE.ShaderLib['sky'] = {
  17. uniforms: {
  18. luminance: { type: "f", value:1 },
  19. turbidity: { type: "f", value:2 },
  20. reileigh: { type: "f", value:1 },
  21. mieCoefficient: { type: "f", value:0.005 },
  22. mieDirectionalG: { type: "f", value:0.8 },
  23. sunPosition: { type: "v3", value: new THREE.Vector3() }
  24. },
  25. vertexShader: [
  26. "varying vec3 vWorldPosition;",
  27. "varying vec2 vUv;",
  28. "void main() {",
  29. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  30. "vWorldPosition = worldPosition.xyz;",
  31. "vUv = uv;",
  32. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  33. "}",
  34. ].join("\n"),
  35. fragmentShader: [
  36. "uniform sampler2D skySampler;",
  37. "uniform vec3 sunPosition;",
  38. "varying vec3 vWorldPosition;",
  39. "varying vec2 vUv;",
  40. "vec3 cameraPos = vec3(0., 0., 0.);",
  41. "// uniform sampler2D sDiffuse;",
  42. "// const float turbidity = 10.0; //",
  43. "// const float reileigh = 2.; //",
  44. "// const float luminance = 1.0; //",
  45. "// const float mieCoefficient = 0.005;",
  46. "// const float mieDirectionalG = 0.8;",
  47. "uniform float luminance;",
  48. "uniform float turbidity;",
  49. "uniform float reileigh;",
  50. "uniform float mieCoefficient;",
  51. "uniform float mieDirectionalG;",
  52. "vec3 sunDirection = normalize(sunPosition);",
  53. "float reileighCoefficient = reileigh;",
  54. "// constants for atmospheric scattering",
  55. "const float e = 2.71828182845904523536028747135266249775724709369995957;",
  56. "const float pi = 3.141592653589793238462643383279502884197169;",
  57. "const float n = 1.0003; // refractive index of air",
  58. "const float N = 2.545E25; // number of molecules per unit volume for air at",
  59. "// 288.15K and 1013mb (sea level -45 celsius)",
  60. "const float pn = 0.035; // depolatization factor for standard air",
  61. "// wavelength of used primaries, according to preetham",
  62. "const vec3 lambda = vec3(680E-9, 550E-9, 450E-9);",
  63. "// mie stuff",
  64. "// K coefficient for the primaries",
  65. "const vec3 K = vec3(0.686, 0.678, 0.666);",
  66. "const float v = 4.0;",
  67. "// optical length at zenith for molecules",
  68. "const float rayleighZenithLength = 8.4E3;",
  69. "const float mieZenithLength = 1.25E3;",
  70. "const vec3 up = vec3(0.0, 1.0, 0.0);",
  71. "const float EE = 1000.0;",
  72. "const float sunAngularDiameterCos = 0.999956676946448443553574619906976478926848692873900859324;",
  73. "// 66 arc seconds -> degrees, and the cosine of that",
  74. "// earth shadow hack",
  75. "const float cutoffAngle = pi/1.95;",
  76. "const float steepness = 1.5;",
  77. "vec3 totalRayleigh(vec3 lambda)",
  78. "{",
  79. "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));",
  80. "}",
  81. // see http://blenderartists.org/forum/showthread.php?321110-Shaders-and-Skybox-madness
  82. "// A simplied version of the total Reayleigh scattering to works on browsers that use ANGLE",
  83. "vec3 simplifiedRayleigh()",
  84. "{",
  85. "return 0.0005 / vec3(94, 40, 18);",
  86. // return 0.00054532832366 / (3.0 * 2.545E25 * pow(vec3(680E-9, 550E-9, 450E-9), vec3(4.0)) * 6.245);
  87. "}",
  88. "float rayleighPhase(float cosTheta)",
  89. "{ ",
  90. "return (3.0 / (16.0*pi)) * (1.0 + pow(cosTheta, 2.0));",
  91. "// return (1.0 / (3.0*pi)) * (1.0 + pow(cosTheta, 2.0));",
  92. "// return (3.0 / 4.0) * (1.0 + pow(cosTheta, 2.0));",
  93. "}",
  94. "vec3 totalMie(vec3 lambda, vec3 K, float T)",
  95. "{",
  96. "float c = (0.2 * T ) * 10E-18;",
  97. "return 0.434 * c * pi * pow((2.0 * pi) / lambda, vec3(v - 2.0)) * K;",
  98. "}",
  99. "float hgPhase(float cosTheta, float g)",
  100. "{",
  101. "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));",
  102. "}",
  103. "float sunIntensity(float zenithAngleCos)",
  104. "{",
  105. "return EE * max(0.0, 1.0 - exp(-((cutoffAngle - acos(zenithAngleCos))/steepness)));",
  106. "}",
  107. "// float logLuminance(vec3 c)",
  108. "// {",
  109. "// return log(c.r * 0.2126 + c.g * 0.7152 + c.b * 0.0722);",
  110. "// }",
  111. "// Filmic ToneMapping http://filmicgames.com/archives/75",
  112. "float A = 0.15;",
  113. "float B = 0.50;",
  114. "float C = 0.10;",
  115. "float D = 0.20;",
  116. "float E = 0.02;",
  117. "float F = 0.30;",
  118. "float W = 1000.0;",
  119. "vec3 Uncharted2Tonemap(vec3 x)",
  120. "{",
  121. "return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;",
  122. "}",
  123. "void main() ",
  124. "{",
  125. "float sunfade = 1.0-clamp(1.0-exp((sunPosition.y/450000.0)),0.0,1.0);",
  126. "// luminance = 1.0 ;// vWorldPosition.y / 450000. + 0.5; //sunPosition.y / 450000. * 1. + 0.5;",
  127. "// gl_FragColor = vec4(sunfade, sunfade, sunfade, 1.0);",
  128. "reileighCoefficient = reileighCoefficient - (1.0* (1.0-sunfade));",
  129. "float sunE = sunIntensity(dot(sunDirection, up));",
  130. "// extinction (absorbtion + out scattering) ",
  131. "// rayleigh coefficients",
  132. // "vec3 betaR = totalRayleigh(lambda) * reileighCoefficient;",
  133. "vec3 betaR = simplifiedRayleigh() * reileighCoefficient;",
  134. "// mie coefficients",
  135. "vec3 betaM = totalMie(lambda, K, turbidity) * mieCoefficient;",
  136. "// optical length",
  137. "// cutoff angle at 90 to avoid singularity in next formula.",
  138. "float zenithAngle = acos(max(0.0, dot(up, normalize(vWorldPosition - cameraPos))));",
  139. "float sR = rayleighZenithLength / (cos(zenithAngle) + 0.15 * pow(93.885 - ((zenithAngle * 180.0) / pi), -1.253));",
  140. "float sM = mieZenithLength / (cos(zenithAngle) + 0.15 * pow(93.885 - ((zenithAngle * 180.0) / pi), -1.253));",
  141. "// combined extinction factor ",
  142. "vec3 Fex = exp(-(betaR * sR + betaM * sM));",
  143. "// in scattering",
  144. "float cosTheta = dot(normalize(vWorldPosition - cameraPos), sunDirection);",
  145. "float rPhase = rayleighPhase(cosTheta*0.5+0.5);",
  146. "vec3 betaRTheta = betaR * rPhase;",
  147. "float mPhase = hgPhase(cosTheta, mieDirectionalG);",
  148. "vec3 betaMTheta = betaM * mPhase;",
  149. "vec3 Lin = pow(sunE * ((betaRTheta + betaMTheta) / (betaR + betaM)) * (1.0 - Fex),vec3(1.5));",
  150. "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));",
  151. "//nightsky",
  152. "vec3 direction = normalize(vWorldPosition - cameraPos);",
  153. "float theta = acos(direction.y); // elevation --> y-axis, [-pi/2, pi/2]",
  154. "float phi = atan(direction.z, direction.x); // azimuth --> x-axis [-pi/2, pi/2]",
  155. "vec2 uv = vec2(phi, theta) / vec2(2.0*pi, pi) + vec2(0.5, 0.0);",
  156. "// vec3 L0 = texture2D(skySampler, uv).rgb+0.1 * Fex;",
  157. "vec3 L0 = vec3(0.1) * Fex;",
  158. "// composition + solar disc",
  159. "//if (cosTheta > sunAngularDiameterCos)",
  160. "float sundisk = smoothstep(sunAngularDiameterCos,sunAngularDiameterCos+0.00002,cosTheta);",
  161. "// if (normalize(vWorldPosition - cameraPos).y>0.0)",
  162. "L0 += (sunE * 19000.0 * Fex)*sundisk;",
  163. "vec3 whiteScale = 1.0/Uncharted2Tonemap(vec3(W));",
  164. "vec3 texColor = (Lin+L0); ",
  165. "texColor *= 0.04 ;",
  166. "texColor += vec3(0.0,0.001,0.0025)*0.3;",
  167. "float g_fMaxLuminance = 1.0;",
  168. "float fLumScaled = 0.1 / luminance; ",
  169. "float fLumCompressed = (fLumScaled * (1.0 + (fLumScaled / (g_fMaxLuminance * g_fMaxLuminance)))) / (1.0 + fLumScaled); ",
  170. "float ExposureBias = fLumCompressed;",
  171. "vec3 curr = Uncharted2Tonemap((log2(2.0/pow(luminance,4.0)))*texColor);",
  172. "vec3 color = curr*whiteScale;",
  173. "vec3 retColor = pow(color,vec3(1.0/(1.2+(1.2*sunfade))));",
  174. "gl_FragColor.rgb = retColor;",
  175. "gl_FragColor.a = 1.0;",
  176. "}",
  177. ].join("\n")
  178. };
  179. THREE.Sky = function () {
  180. var skyShader = THREE.ShaderLib[ "sky" ];
  181. var skyUniforms = THREE.UniformsUtils.clone( skyShader.uniforms );
  182. var skyMat = new THREE.ShaderMaterial( {
  183. fragmentShader: skyShader.fragmentShader,
  184. vertexShader: skyShader.vertexShader,
  185. uniforms: skyUniforms,
  186. side: THREE.BackSide
  187. } );
  188. var skyGeo = new THREE.SphereGeometry( 450000, 32, 15 );
  189. var skyMesh = new THREE.Mesh( skyGeo, skyMat );
  190. // Expose variables
  191. this.mesh = skyMesh;
  192. this.uniforms = skyUniforms;
  193. };