Sky.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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](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. 'cloudScale': { value: 0.0002 },
  66. 'cloudSpeed': { value: 0.0001 },
  67. 'cloudCoverage': { value: 0.4 },
  68. 'cloudDensity': { value: 0.4 },
  69. 'cloudElevation': { value: 0.5 },
  70. 'time': { value: 0.0 }
  71. },
  72. vertexShader: /* glsl */`
  73. uniform vec3 sunPosition;
  74. uniform float rayleigh;
  75. uniform float turbidity;
  76. uniform float mieCoefficient;
  77. uniform vec3 up;
  78. varying vec3 vWorldPosition;
  79. varying vec3 vSunDirection;
  80. varying float vSunfade;
  81. varying vec3 vBetaR;
  82. varying vec3 vBetaM;
  83. varying float vSunE;
  84. // constants for atmospheric scattering
  85. const float e = 2.71828182845904523536028747135266249775724709369995957;
  86. const float pi = 3.141592653589793238462643383279502884197169;
  87. // wavelength of used primaries, according to preetham
  88. const vec3 lambda = vec3( 680E-9, 550E-9, 450E-9 );
  89. // this pre-calculation replaces older TotalRayleigh(vec3 lambda) function:
  90. // (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))
  91. const vec3 totalRayleigh = vec3( 5.804542996261093E-6, 1.3562911419845635E-5, 3.0265902468824876E-5 );
  92. // mie stuff
  93. // K coefficient for the primaries
  94. const float v = 4.0;
  95. const vec3 K = vec3( 0.686, 0.678, 0.666 );
  96. // MieConst = pi * pow( ( 2.0 * pi ) / lambda, vec3( v - 2.0 ) ) * K
  97. const vec3 MieConst = vec3( 1.8399918514433978E14, 2.7798023919660528E14, 4.0790479543861094E14 );
  98. // earth shadow hack
  99. // cutoffAngle = pi / 1.95;
  100. const float cutoffAngle = 1.6110731556870734;
  101. const float steepness = 1.5;
  102. const float EE = 1000.0;
  103. float sunIntensity( float zenithAngleCos ) {
  104. zenithAngleCos = clamp( zenithAngleCos, -1.0, 1.0 );
  105. return EE * max( 0.0, 1.0 - pow( e, -( ( cutoffAngle - acos( zenithAngleCos ) ) / steepness ) ) );
  106. }
  107. vec3 totalMie( float T ) {
  108. float c = ( 0.2 * T ) * 10E-18;
  109. return 0.434 * c * MieConst;
  110. }
  111. void main() {
  112. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  113. vWorldPosition = worldPosition.xyz;
  114. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  115. gl_Position.z = gl_Position.w; // set z to camera.far
  116. vSunDirection = normalize( sunPosition );
  117. vSunE = sunIntensity( dot( vSunDirection, up ) );
  118. vSunfade = 1.0 - clamp( 1.0 - exp( ( sunPosition.y / 450000.0 ) ), 0.0, 1.0 );
  119. float rayleighCoefficient = rayleigh - ( 1.0 * ( 1.0 - vSunfade ) );
  120. // extinction (absorption + out scattering)
  121. // rayleigh coefficients
  122. vBetaR = totalRayleigh * rayleighCoefficient;
  123. // mie coefficients
  124. vBetaM = totalMie( turbidity ) * mieCoefficient;
  125. }`,
  126. fragmentShader: /* glsl */`
  127. varying vec3 vWorldPosition;
  128. varying vec3 vSunDirection;
  129. varying vec3 vBetaR;
  130. varying vec3 vBetaM;
  131. varying float vSunE;
  132. uniform float mieDirectionalG;
  133. uniform vec3 up;
  134. uniform float cloudScale;
  135. uniform float cloudSpeed;
  136. uniform float cloudCoverage;
  137. uniform float cloudDensity;
  138. uniform float cloudElevation;
  139. uniform float time;
  140. // Cloud noise functions
  141. float hash( vec2 p ) {
  142. return fract( sin( dot( p, vec2( 127.1, 311.7 ) ) ) * 43758.5453123 );
  143. }
  144. float noise( vec2 p ) {
  145. vec2 i = floor( p );
  146. vec2 f = fract( p );
  147. f = f * f * ( 3.0 - 2.0 * f );
  148. float a = hash( i );
  149. float b = hash( i + vec2( 1.0, 0.0 ) );
  150. float c = hash( i + vec2( 0.0, 1.0 ) );
  151. float d = hash( i + vec2( 1.0, 1.0 ) );
  152. return mix( mix( a, b, f.x ), mix( c, d, f.x ), f.y );
  153. }
  154. float fbm( vec2 p ) {
  155. float value = 0.0;
  156. float amplitude = 0.5;
  157. for ( int i = 0; i < 5; i ++ ) {
  158. value += amplitude * noise( p );
  159. p *= 2.0;
  160. amplitude *= 0.5;
  161. }
  162. return value;
  163. }
  164. // constants for atmospheric scattering
  165. const float pi = 3.141592653589793238462643383279502884197169;
  166. const float n = 1.0003; // refractive index of air
  167. const float N = 2.545E25; // number of molecules per unit volume for air at 288.15K and 1013mb (sea level -45 celsius)
  168. // optical length at zenith for molecules
  169. const float rayleighZenithLength = 8.4E3;
  170. const float mieZenithLength = 1.25E3;
  171. // 66 arc seconds -> degrees, and the cosine of that
  172. const float sunAngularDiameterCos = 0.999956676946448443553574619906976478926848692873900859324;
  173. // 3.0 / ( 16.0 * pi )
  174. const float THREE_OVER_SIXTEENPI = 0.05968310365946075;
  175. // 1.0 / ( 4.0 * pi )
  176. const float ONE_OVER_FOURPI = 0.07957747154594767;
  177. float rayleighPhase( float cosTheta ) {
  178. return THREE_OVER_SIXTEENPI * ( 1.0 + pow( cosTheta, 2.0 ) );
  179. }
  180. float hgPhase( float cosTheta, float g ) {
  181. float g2 = pow( g, 2.0 );
  182. float inverse = 1.0 / pow( 1.0 - 2.0 * g * cosTheta + g2, 1.5 );
  183. return ONE_OVER_FOURPI * ( ( 1.0 - g2 ) * inverse );
  184. }
  185. void main() {
  186. vec3 direction = normalize( vWorldPosition - cameraPosition );
  187. // optical length
  188. // cutoff angle at 90 to avoid singularity in next formula.
  189. float zenithAngle = acos( max( 0.0, dot( up, direction ) ) );
  190. float inverse = 1.0 / ( cos( zenithAngle ) + 0.15 * pow( 93.885 - ( ( zenithAngle * 180.0 ) / pi ), -1.253 ) );
  191. float sR = rayleighZenithLength * inverse;
  192. float sM = mieZenithLength * inverse;
  193. // combined extinction factor
  194. vec3 Fex = exp( -( vBetaR * sR + vBetaM * sM ) );
  195. // in scattering
  196. float cosTheta = dot( direction, vSunDirection );
  197. float rPhase = rayleighPhase( cosTheta * 0.5 + 0.5 );
  198. vec3 betaRTheta = vBetaR * rPhase;
  199. float mPhase = hgPhase( cosTheta, mieDirectionalG );
  200. vec3 betaMTheta = vBetaM * mPhase;
  201. vec3 Lin = pow( vSunE * ( ( betaRTheta + betaMTheta ) / ( vBetaR + vBetaM ) ) * ( 1.0 - Fex ), vec3( 1.5 ) );
  202. 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 ) );
  203. // nightsky
  204. float theta = acos( direction.y ); // elevation --> y-axis, [-pi/2, pi/2]
  205. float phi = atan( direction.z, direction.x ); // azimuth --> x-axis [-pi/2, pi/2]
  206. vec2 uv = vec2( phi, theta ) / vec2( 2.0 * pi, pi ) + vec2( 0.5, 0.0 );
  207. vec3 L0 = vec3( 0.1 ) * Fex;
  208. // composition + solar disc
  209. float sundisk = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos + 0.00002, cosTheta );
  210. L0 += ( vSunE * 19000.0 * Fex ) * sundisk;
  211. vec3 texColor = ( Lin + L0 ) * 0.04 + vec3( 0.0, 0.0003, 0.00075 );
  212. // Clouds
  213. if ( direction.y > 0.0 && cloudCoverage > 0.0 ) {
  214. // Project to cloud plane (higher elevation = clouds appear lower/closer)
  215. float elevation = mix( 1.0, 0.1, cloudElevation );
  216. vec2 cloudUV = direction.xz / ( direction.y * elevation );
  217. cloudUV *= cloudScale;
  218. cloudUV += time * cloudSpeed;
  219. // Multi-octave noise for fluffy clouds
  220. float cloudNoise = fbm( cloudUV * 1000.0 );
  221. cloudNoise += 0.5 * fbm( cloudUV * 2000.0 + 3.7 );
  222. cloudNoise = cloudNoise * 0.5 + 0.5;
  223. // Apply coverage threshold
  224. float cloudMask = smoothstep( 1.0 - cloudCoverage, 1.0 - cloudCoverage + 0.3, cloudNoise );
  225. // Fade clouds near horizon (adjusted by elevation)
  226. float horizonFade = smoothstep( 0.0, 0.1 + 0.2 * cloudElevation, direction.y );
  227. cloudMask *= horizonFade;
  228. // Cloud lighting based on sun position
  229. float sunInfluence = dot( direction, vSunDirection ) * 0.5 + 0.5;
  230. float daylight = max( 0.0, vSunDirection.y * 2.0 );
  231. // Base cloud color affected by atmosphere
  232. vec3 atmosphereColor = Lin * 0.04;
  233. vec3 cloudColor = mix( vec3( 0.3 ), vec3( 1.0 ), daylight );
  234. cloudColor = mix( cloudColor, atmosphereColor + vec3( 1.0 ), sunInfluence * 0.5 );
  235. cloudColor *= vSunE * 0.00002;
  236. // Blend clouds with sky
  237. texColor = mix( texColor, cloudColor, cloudMask * cloudDensity );
  238. }
  239. gl_FragColor = vec4( texColor, 1.0 );
  240. #include <tonemapping_fragment>
  241. #include <colorspace_fragment>
  242. }`
  243. };
  244. export { Sky };
粤ICP备19079148号