SkyShader.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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: { value: 1 },
  19. turbidity: { value: 2 },
  20. rayleigh: { value: 1 },
  21. mieCoefficient: { value: 0.005 },
  22. mieDirectionalG: { value: 0.8 },
  23. sunPosition: { value: new THREE.Vector3() }
  24. },
  25. vertexShader: [
  26. "uniform vec3 sunPosition;",
  27. "uniform float rayleigh;",
  28. "uniform float turbidity;",
  29. "uniform float mieCoefficient;",
  30. "varying vec3 vWorldPosition;",
  31. "varying vec3 vSunDirection;",
  32. "varying float vSunfade;",
  33. "varying vec3 vBetaR;",
  34. "varying vec3 vBetaM;",
  35. "varying float vSunE;",
  36. "const vec3 up = vec3(0.0, 1.0, 0.0);",
  37. // constants for atmospheric scattering
  38. "const float e = 2.71828182845904523536028747135266249775724709369995957;",
  39. "const float pi = 3.141592653589793238462643383279502884197169;",
  40. // mie stuff
  41. // K coefficient for the primaries
  42. "const float v = 4.0;",
  43. "const vec3 K = vec3(0.686, 0.678, 0.666);",
  44. // see http://blenderartists.org/forum/showthread.php?321110-Shaders-and-Skybox-madness
  45. // A simplied version of the total Reayleigh scattering to works on browsers that use ANGLE
  46. "const vec3 simplifiedRayleigh = 0.0005 / vec3(94, 40, 18);",
  47. // wavelength of used primaries, according to preetham
  48. "const vec3 lambda = vec3(680E-9, 550E-9, 450E-9);",
  49. // earth shadow hack
  50. "const float cutoffAngle = pi/1.95;",
  51. "const float steepness = 1.5;",
  52. "const float EE = 1000.0;",
  53. "float sunIntensity(float zenithAngleCos)",
  54. "{",
  55. "zenithAngleCos = clamp(zenithAngleCos, -1.0, 1.0);",
  56. "return EE * max(0.0, 1.0 - pow(e, -((cutoffAngle - acos(zenithAngleCos))/steepness)));",
  57. "}",
  58. "vec3 totalMie(vec3 lambda, float T)",
  59. "{",
  60. "float c = (0.2 * T ) * 10E-18;",
  61. "return 0.434 * c * pi * pow((2.0 * pi) / lambda, vec3(v - 2.0)) * K;",
  62. "}",
  63. "void main() {",
  64. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  65. "vWorldPosition = worldPosition.xyz;",
  66. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  67. "vSunDirection = normalize(sunPosition);",
  68. "vSunE = sunIntensity(dot(vSunDirection, up));",
  69. "vSunfade = 1.0-clamp(1.0-exp((sunPosition.y/450000.0)),0.0,1.0);",
  70. "float rayleighCoefficient = rayleigh - (1.0 * (1.0-vSunfade));",
  71. // extinction (absorbtion + out scattering)
  72. // rayleigh coefficients
  73. "vBetaR = simplifiedRayleigh * rayleighCoefficient;",
  74. // mie coefficients
  75. "vBetaM = totalMie(lambda, turbidity) * mieCoefficient;",
  76. "}"
  77. ].join( "\n" ),
  78. fragmentShader: [
  79. "varying vec3 vWorldPosition;",
  80. "varying vec3 vSunDirection;",
  81. "varying float vSunfade;",
  82. "varying vec3 vBetaR;",
  83. "varying vec3 vBetaM;",
  84. "varying float vSunE;",
  85. "uniform float luminance;",
  86. "uniform float mieDirectionalG;",
  87. "const vec3 cameraPos = vec3(0., 0., 0.);",
  88. // constants for atmospheric scattering
  89. "const float pi = 3.141592653589793238462643383279502884197169;",
  90. "const float n = 1.0003;", // refractive index of air
  91. "const float N = 2.545E25;", // number of molecules per unit volume for air at
  92. // 288.15K and 1013mb (sea level -45 celsius)
  93. // optical length at zenith for molecules
  94. "const float rayleighZenithLength = 8.4E3;",
  95. "const float mieZenithLength = 1.25E3;",
  96. "const vec3 up = vec3(0.0, 1.0, 0.0);",
  97. "const float sunAngularDiameterCos = 0.999956676946448443553574619906976478926848692873900859324;",
  98. // 66 arc seconds -> degrees, and the cosine of that
  99. "float rayleighPhase(float cosTheta)",
  100. "{",
  101. "return (3.0 / (16.0*pi)) * (1.0 + pow(cosTheta, 2.0));",
  102. "}",
  103. "float hgPhase(float cosTheta, float g)",
  104. "{",
  105. "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));",
  106. "}",
  107. // Filmic ToneMapping http://filmicgames.com/archives/75
  108. "const float A = 0.15;",
  109. "const float B = 0.50;",
  110. "const float C = 0.10;",
  111. "const float D = 0.20;",
  112. "const float E = 0.02;",
  113. "const float F = 0.30;",
  114. "const float whiteScale = 1.0748724675633854;", // 1.0 / Uncharted2Tonemap(1000.0)
  115. "vec3 Uncharted2Tonemap(vec3 x)",
  116. "{",
  117. "return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;",
  118. "}",
  119. "void main() ",
  120. "{",
  121. // optical length
  122. // cutoff angle at 90 to avoid singularity in next formula.
  123. "float zenithAngle = acos(max(0.0, dot(up, normalize(vWorldPosition - cameraPos))));",
  124. "float sR = rayleighZenithLength / (cos(zenithAngle) + 0.15 * pow(93.885 - ((zenithAngle * 180.0) / pi), -1.253));",
  125. "float sM = mieZenithLength / (cos(zenithAngle) + 0.15 * pow(93.885 - ((zenithAngle * 180.0) / pi), -1.253));",
  126. // combined extinction factor
  127. "vec3 Fex = exp(-(vBetaR * sR + vBetaM * sM));",
  128. // in scattering
  129. "float cosTheta = dot(normalize(vWorldPosition - cameraPos), vSunDirection);",
  130. "float rPhase = rayleighPhase(cosTheta*0.5+0.5);",
  131. "vec3 betaRTheta = vBetaR * rPhase;",
  132. "float mPhase = hgPhase(cosTheta, mieDirectionalG);",
  133. "vec3 betaMTheta = vBetaM * mPhase;",
  134. "vec3 Lin = pow(vSunE * ((betaRTheta + betaMTheta) / (vBetaR + vBetaM)) * (1.0 - Fex),vec3(1.5));",
  135. "Lin *= mix(vec3(1.0),pow(vSunE * ((betaRTheta + betaMTheta) / (vBetaR + vBetaM)) * Fex,vec3(1.0/2.0)),clamp(pow(1.0-dot(up, vSunDirection),5.0),0.0,1.0));",
  136. //nightsky
  137. "vec3 direction = normalize(vWorldPosition - cameraPos);",
  138. "float theta = acos(direction.y); // elevation --> y-axis, [-pi/2, pi/2]",
  139. "float phi = atan(direction.z, direction.x); // azimuth --> x-axis [-pi/2, pi/2]",
  140. "vec2 uv = vec2(phi, theta) / vec2(2.0*pi, pi) + vec2(0.5, 0.0);",
  141. "vec3 L0 = vec3(0.1) * Fex;",
  142. // composition + solar disc
  143. "float sundisk = smoothstep(sunAngularDiameterCos,sunAngularDiameterCos+0.00002,cosTheta);",
  144. "L0 += (vSunE * 19000.0 * Fex)*sundisk;",
  145. "vec3 texColor = (Lin+L0) * 0.04 + vec3(0.0, 0.0003, 0.00075);",
  146. "vec3 curr = Uncharted2Tonemap((log2(2.0/pow(luminance,4.0)))*texColor);",
  147. "vec3 color = curr*whiteScale;",
  148. "vec3 retColor = pow(color,vec3(1.0/(1.2+(1.2*vSunfade))));",
  149. "gl_FragColor.rgb = retColor;",
  150. "gl_FragColor.a = 1.0;",
  151. "}"
  152. ].join( "\n" )
  153. };
  154. THREE.Sky = function () {
  155. var skyShader = THREE.ShaderLib[ "sky" ];
  156. var skyUniforms = Object.assign( {}, skyShader.uniforms );
  157. var skyMat = new THREE.ShaderMaterial( {
  158. fragmentShader: skyShader.fragmentShader,
  159. vertexShader: skyShader.vertexShader,
  160. uniforms: skyUniforms,
  161. side: THREE.BackSide
  162. } );
  163. var skyGeo = new THREE.SphereBufferGeometry( 450000, 32, 15 );
  164. var skyMesh = new THREE.Mesh( skyGeo, skyMat );
  165. // Expose variables
  166. this.mesh = skyMesh;
  167. this.uniforms = skyUniforms;
  168. };