Sky.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {
  2. BackSide,
  3. BoxGeometry,
  4. Mesh,
  5. ShaderMaterial,
  6. UniformsUtils,
  7. Vector3
  8. } from 'three';
  9. /**
  10. * Represents a skydome for scene backgrounds. Based on [A Practical Analytic Model for Daylight]{@link https://www.researchgate.net/publication/220720443_A_Practical_Analytic_Model_for_Daylight}
  11. * aka The Preetham Model, the de facto standard for analytical skydomes.
  12. *
  13. * Note that this class can only be used with {@link WebGLRenderer}.
  14. * When using {@link WebGPURenderer}, use {@link SkyMesh}.
  15. *
  16. * More references:
  17. *
  18. * - {@link http://simonwallner.at/project/atmospheric-scattering/}
  19. * - {@link http://blenderartists.org/forum/showthread.php?245954-preethams-sky-impementation-HDR}
  20. *
  21. *
  22. * ```js
  23. * const sky = new Sky();
  24. * sky.scale.setScalar( 10000 );
  25. * scene.add( sky );
  26. * ```
  27. *
  28. * @augments Mesh
  29. * @three_import import { Sky } from 'three/addons/objects/Sky.js';
  30. */
  31. class Sky extends Mesh {
  32. /**
  33. * Constructs a new skydome.
  34. */
  35. constructor() {
  36. const shader = Sky.SkyShader;
  37. const material = new ShaderMaterial( {
  38. name: shader.name,
  39. uniforms: UniformsUtils.clone( shader.uniforms ),
  40. vertexShader: shader.vertexShader,
  41. fragmentShader: shader.fragmentShader,
  42. side: BackSide,
  43. depthWrite: false
  44. } );
  45. super( new BoxGeometry( 1, 1, 1 ), material );
  46. /**
  47. * This flag can be used for type testing.
  48. *
  49. * @type {boolean}
  50. * @readonly
  51. * @default true
  52. */
  53. this.isSky = true;
  54. }
  55. }
  56. Sky.SkyShader = {
  57. name: 'SkyShader',
  58. uniforms: {
  59. 'turbidity': { value: 2 },
  60. 'rayleigh': { value: 1 },
  61. 'mieCoefficient': { value: 0.005 },
  62. 'mieDirectionalG': { value: 0.8 },
  63. 'sunPosition': { value: new Vector3() },
  64. 'up': { value: new Vector3( 0, 1, 0 ) }
  65. },
  66. vertexShader: /* glsl */`
  67. uniform vec3 sunPosition;
  68. uniform float rayleigh;
  69. uniform float turbidity;
  70. uniform float mieCoefficient;
  71. uniform vec3 up;
  72. varying vec3 vWorldPosition;
  73. varying vec3 vSunDirection;
  74. varying float vSunfade;
  75. varying vec3 vBetaR;
  76. varying vec3 vBetaM;
  77. varying float vSunE;
  78. // constants for atmospheric scattering
  79. const float e = 2.71828182845904523536028747135266249775724709369995957;
  80. const float pi = 3.141592653589793238462643383279502884197169;
  81. // wavelength of used primaries, according to preetham
  82. const vec3 lambda = vec3( 680E-9, 550E-9, 450E-9 );
  83. // this pre-calculation replaces older TotalRayleigh(vec3 lambda) function:
  84. // (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))
  85. const vec3 totalRayleigh = vec3( 5.804542996261093E-6, 1.3562911419845635E-5, 3.0265902468824876E-5 );
  86. // mie stuff
  87. // K coefficient for the primaries
  88. const float v = 4.0;
  89. const vec3 K = vec3( 0.686, 0.678, 0.666 );
  90. // MieConst = pi * pow( ( 2.0 * pi ) / lambda, vec3( v - 2.0 ) ) * K
  91. const vec3 MieConst = vec3( 1.8399918514433978E14, 2.7798023919660528E14, 4.0790479543861094E14 );
  92. // earth shadow hack
  93. // cutoffAngle = pi / 1.95;
  94. const float cutoffAngle = 1.6110731556870734;
  95. const float steepness = 1.5;
  96. const float EE = 1000.0;
  97. float sunIntensity( float zenithAngleCos ) {
  98. zenithAngleCos = clamp( zenithAngleCos, -1.0, 1.0 );
  99. return EE * max( 0.0, 1.0 - pow( e, -( ( cutoffAngle - acos( zenithAngleCos ) ) / steepness ) ) );
  100. }
  101. vec3 totalMie( float T ) {
  102. float c = ( 0.2 * T ) * 10E-18;
  103. return 0.434 * c * MieConst;
  104. }
  105. void main() {
  106. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  107. vWorldPosition = worldPosition.xyz;
  108. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  109. gl_Position.z = gl_Position.w; // set z to camera.far
  110. vSunDirection = normalize( sunPosition );
  111. vSunE = sunIntensity( dot( vSunDirection, up ) );
  112. vSunfade = 1.0 - clamp( 1.0 - exp( ( sunPosition.y / 450000.0 ) ), 0.0, 1.0 );
  113. float rayleighCoefficient = rayleigh - ( 1.0 * ( 1.0 - vSunfade ) );
  114. // extinction (absorption + out scattering)
  115. // rayleigh coefficients
  116. vBetaR = totalRayleigh * rayleighCoefficient;
  117. // mie coefficients
  118. vBetaM = totalMie( turbidity ) * mieCoefficient;
  119. }`,
  120. fragmentShader: /* glsl */`
  121. varying vec3 vWorldPosition;
  122. varying vec3 vSunDirection;
  123. varying float vSunfade;
  124. varying vec3 vBetaR;
  125. varying vec3 vBetaM;
  126. varying float vSunE;
  127. uniform float mieDirectionalG;
  128. uniform vec3 up;
  129. // constants for atmospheric scattering
  130. const float pi = 3.141592653589793238462643383279502884197169;
  131. const float n = 1.0003; // refractive index of air
  132. const float N = 2.545E25; // number of molecules per unit volume for air at 288.15K and 1013mb (sea level -45 celsius)
  133. // optical length at zenith for molecules
  134. const float rayleighZenithLength = 8.4E3;
  135. const float mieZenithLength = 1.25E3;
  136. // 66 arc seconds -> degrees, and the cosine of that
  137. const float sunAngularDiameterCos = 0.999956676946448443553574619906976478926848692873900859324;
  138. // 3.0 / ( 16.0 * pi )
  139. const float THREE_OVER_SIXTEENPI = 0.05968310365946075;
  140. // 1.0 / ( 4.0 * pi )
  141. const float ONE_OVER_FOURPI = 0.07957747154594767;
  142. float rayleighPhase( float cosTheta ) {
  143. return THREE_OVER_SIXTEENPI * ( 1.0 + pow( cosTheta, 2.0 ) );
  144. }
  145. float hgPhase( float cosTheta, float g ) {
  146. float g2 = pow( g, 2.0 );
  147. float inverse = 1.0 / pow( 1.0 - 2.0 * g * cosTheta + g2, 1.5 );
  148. return ONE_OVER_FOURPI * ( ( 1.0 - g2 ) * inverse );
  149. }
  150. void main() {
  151. vec3 direction = normalize( vWorldPosition - cameraPosition );
  152. // optical length
  153. // cutoff angle at 90 to avoid singularity in next formula.
  154. float zenithAngle = acos( max( 0.0, dot( up, direction ) ) );
  155. float inverse = 1.0 / ( cos( zenithAngle ) + 0.15 * pow( 93.885 - ( ( zenithAngle * 180.0 ) / pi ), -1.253 ) );
  156. float sR = rayleighZenithLength * inverse;
  157. float sM = mieZenithLength * inverse;
  158. // combined extinction factor
  159. vec3 Fex = exp( -( vBetaR * sR + vBetaM * sM ) );
  160. // in scattering
  161. float cosTheta = dot( direction, vSunDirection );
  162. float rPhase = rayleighPhase( cosTheta * 0.5 + 0.5 );
  163. vec3 betaRTheta = vBetaR * rPhase;
  164. float mPhase = hgPhase( cosTheta, mieDirectionalG );
  165. vec3 betaMTheta = vBetaM * mPhase;
  166. vec3 Lin = pow( vSunE * ( ( betaRTheta + betaMTheta ) / ( vBetaR + vBetaM ) ) * ( 1.0 - Fex ), vec3( 1.5 ) );
  167. 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 ) );
  168. // nightsky
  169. float theta = acos( direction.y ); // elevation --> y-axis, [-pi/2, pi/2]
  170. float phi = atan( direction.z, direction.x ); // azimuth --> x-axis [-pi/2, pi/2]
  171. vec2 uv = vec2( phi, theta ) / vec2( 2.0 * pi, pi ) + vec2( 0.5, 0.0 );
  172. vec3 L0 = vec3( 0.1 ) * Fex;
  173. // composition + solar disc
  174. float sundisk = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos + 0.00002, cosTheta );
  175. L0 += ( vSunE * 19000.0 * Fex ) * sundisk;
  176. vec3 texColor = ( Lin + L0 ) * 0.04 + vec3( 0.0, 0.0003, 0.00075 );
  177. vec3 retColor = pow( texColor, vec3( 1.0 / ( 1.2 + ( 1.2 * vSunfade ) ) ) );
  178. gl_FragColor = vec4( retColor, 1.0 );
  179. #include <tonemapping_fragment>
  180. #include <colorspace_fragment>
  181. }`
  182. };
  183. export { Sky };