Sky.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Based on "A Practical Analytic Model for Daylight"
  3. * aka The Preetham Model, the de facto standard analytic skydome model
  4. * https://www.researchgate.net/publication/220720443_A_Practical_Analytic_Model_for_Daylight
  5. *
  6. * First implemented by Simon Wallner
  7. * http://www.simonwallner.at/projects/atmospheric-scattering
  8. *
  9. * Improved by Martin Upitis
  10. * http://blenderartists.org/forum/showthread.php?245954-preethams-sky-impementation-HDR
  11. *
  12. * Three.js integration by zz85 http://twitter.com/blurspline
  13. */
  14. THREE.Sky = function () {
  15. var shader = THREE.Sky.SkyShader;
  16. var material = new THREE.ShaderMaterial( {
  17. name: 'SkyShader',
  18. fragmentShader: shader.fragmentShader,
  19. vertexShader: shader.vertexShader,
  20. uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
  21. side: THREE.BackSide,
  22. depthWrite: false
  23. } );
  24. THREE.Mesh.call( this, new THREE.BoxGeometry( 1, 1, 1 ), material );
  25. };
  26. THREE.Sky.prototype = Object.create( THREE.Mesh.prototype );
  27. THREE.Sky.SkyShader = {
  28. uniforms: {
  29. 'turbidity': { value: 2 },
  30. 'rayleigh': { value: 1 },
  31. 'mieCoefficient': { value: 0.005 },
  32. 'mieDirectionalG': { value: 0.8 },
  33. 'sunPosition': { value: new THREE.Vector3() },
  34. 'up': { value: new THREE.Vector3( 0, 1, 0 ) }
  35. },
  36. vertexShader: [
  37. 'uniform vec3 sunPosition;',
  38. 'uniform float rayleigh;',
  39. 'uniform float turbidity;',
  40. 'uniform float mieCoefficient;',
  41. 'uniform vec3 up;',
  42. 'varying vec3 vWorldPosition;',
  43. 'varying vec3 vSunDirection;',
  44. 'varying float vSunfade;',
  45. 'varying vec3 vBetaR;',
  46. 'varying vec3 vBetaM;',
  47. 'varying float vSunE;',
  48. // constants for atmospheric scattering
  49. 'const float e = 2.71828182845904523536028747135266249775724709369995957;',
  50. 'const float pi = 3.141592653589793238462643383279502884197169;',
  51. // wavelength of used primaries, according to preetham
  52. 'const vec3 lambda = vec3( 680E-9, 550E-9, 450E-9 );',
  53. // this pre-calcuation replaces older TotalRayleigh(vec3 lambda) function:
  54. // (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))
  55. 'const vec3 totalRayleigh = vec3( 5.804542996261093E-6, 1.3562911419845635E-5, 3.0265902468824876E-5 );',
  56. // mie stuff
  57. // K coefficient for the primaries
  58. 'const float v = 4.0;',
  59. 'const vec3 K = vec3( 0.686, 0.678, 0.666 );',
  60. // MieConst = pi * pow( ( 2.0 * pi ) / lambda, vec3( v - 2.0 ) ) * K
  61. 'const vec3 MieConst = vec3( 1.8399918514433978E14, 2.7798023919660528E14, 4.0790479543861094E14 );',
  62. // earth shadow hack
  63. // cutoffAngle = pi / 1.95;
  64. 'const float cutoffAngle = 1.6110731556870734;',
  65. 'const float steepness = 1.5;',
  66. 'const float EE = 1000.0;',
  67. 'float sunIntensity( float zenithAngleCos ) {',
  68. ' zenithAngleCos = clamp( zenithAngleCos, -1.0, 1.0 );',
  69. ' return EE * max( 0.0, 1.0 - pow( e, -( ( cutoffAngle - acos( zenithAngleCos ) ) / steepness ) ) );',
  70. '}',
  71. 'vec3 totalMie( float T ) {',
  72. ' float c = ( 0.2 * T ) * 10E-18;',
  73. ' return 0.434 * c * MieConst;',
  74. '}',
  75. 'void main() {',
  76. ' vec4 worldPosition = modelMatrix * vec4( position, 1.0 );',
  77. ' vWorldPosition = worldPosition.xyz;',
  78. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  79. ' gl_Position.z = gl_Position.w;', // set z to camera.far
  80. ' vSunDirection = normalize( sunPosition );',
  81. ' vSunE = sunIntensity( dot( vSunDirection, up ) );',
  82. ' vSunfade = 1.0 - clamp( 1.0 - exp( ( sunPosition.y / 450000.0 ) ), 0.0, 1.0 );',
  83. ' float rayleighCoefficient = rayleigh - ( 1.0 * ( 1.0 - vSunfade ) );',
  84. // extinction (absorbtion + out scattering)
  85. // rayleigh coefficients
  86. ' vBetaR = totalRayleigh * rayleighCoefficient;',
  87. // mie coefficients
  88. ' vBetaM = totalMie( turbidity ) * mieCoefficient;',
  89. '}'
  90. ].join( '\n' ),
  91. fragmentShader: [
  92. 'varying vec3 vWorldPosition;',
  93. 'varying vec3 vSunDirection;',
  94. 'varying float vSunfade;',
  95. 'varying vec3 vBetaR;',
  96. 'varying vec3 vBetaM;',
  97. 'varying float vSunE;',
  98. 'uniform float mieDirectionalG;',
  99. 'uniform vec3 up;',
  100. 'const vec3 cameraPos = vec3( 0.0, 0.0, 0.0 );',
  101. // constants for atmospheric scattering
  102. 'const float pi = 3.141592653589793238462643383279502884197169;',
  103. 'const float n = 1.0003;', // refractive index of air
  104. 'const float N = 2.545E25;', // number of molecules per unit volume for air at 288.15K and 1013mb (sea level -45 celsius)
  105. // optical length at zenith for molecules
  106. 'const float rayleighZenithLength = 8.4E3;',
  107. 'const float mieZenithLength = 1.25E3;',
  108. // 66 arc seconds -> degrees, and the cosine of that
  109. 'const float sunAngularDiameterCos = 0.999956676946448443553574619906976478926848692873900859324;',
  110. // 3.0 / ( 16.0 * pi )
  111. 'const float THREE_OVER_SIXTEENPI = 0.05968310365946075;',
  112. // 1.0 / ( 4.0 * pi )
  113. 'const float ONE_OVER_FOURPI = 0.07957747154594767;',
  114. 'float rayleighPhase( float cosTheta ) {',
  115. ' return THREE_OVER_SIXTEENPI * ( 1.0 + pow( cosTheta, 2.0 ) );',
  116. '}',
  117. 'float hgPhase( float cosTheta, float g ) {',
  118. ' float g2 = pow( g, 2.0 );',
  119. ' float inverse = 1.0 / pow( 1.0 - 2.0 * g * cosTheta + g2, 1.5 );',
  120. ' return ONE_OVER_FOURPI * ( ( 1.0 - g2 ) * inverse );',
  121. '}',
  122. 'void main() {',
  123. ' vec3 direction = normalize( vWorldPosition - cameraPos );',
  124. // optical length
  125. // cutoff angle at 90 to avoid singularity in next formula.
  126. ' float zenithAngle = acos( max( 0.0, dot( up, direction ) ) );',
  127. ' float inverse = 1.0 / ( cos( zenithAngle ) + 0.15 * pow( 93.885 - ( ( zenithAngle * 180.0 ) / pi ), -1.253 ) );',
  128. ' float sR = rayleighZenithLength * inverse;',
  129. ' float sM = mieZenithLength * inverse;',
  130. // combined extinction factor
  131. ' vec3 Fex = exp( -( vBetaR * sR + vBetaM * sM ) );',
  132. // in scattering
  133. ' float cosTheta = dot( direction, vSunDirection );',
  134. ' float rPhase = rayleighPhase( cosTheta * 0.5 + 0.5 );',
  135. ' vec3 betaRTheta = vBetaR * rPhase;',
  136. ' float mPhase = hgPhase( cosTheta, mieDirectionalG );',
  137. ' vec3 betaMTheta = vBetaM * mPhase;',
  138. ' vec3 Lin = pow( vSunE * ( ( betaRTheta + betaMTheta ) / ( vBetaR + vBetaM ) ) * ( 1.0 - Fex ), vec3( 1.5 ) );',
  139. ' 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 ) );',
  140. // nightsky
  141. ' float theta = acos( direction.y ); // elevation --> y-axis, [-pi/2, pi/2]',
  142. ' float phi = atan( direction.z, direction.x ); // azimuth --> x-axis [-pi/2, pi/2]',
  143. ' vec2 uv = vec2( phi, theta ) / vec2( 2.0 * pi, pi ) + vec2( 0.5, 0.0 );',
  144. ' vec3 L0 = vec3( 0.1 ) * Fex;',
  145. // composition + solar disc
  146. ' float sundisk = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos + 0.00002, cosTheta );',
  147. ' L0 += ( vSunE * 19000.0 * Fex ) * sundisk;',
  148. ' vec3 texColor = ( Lin + L0 ) * 0.04 + vec3( 0.0, 0.0003, 0.00075 );',
  149. ' vec3 retColor = pow( texColor, vec3( 1.0 / ( 1.2 + ( 1.2 * vSunfade ) ) ) );',
  150. ' gl_FragColor = vec4( retColor, 1.0 );',
  151. '#include <tonemapping_fragment>',
  152. '#include <encodings_fragment>',
  153. '}'
  154. ].join( '\n' )
  155. };