SkyMesh.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import {
  2. BackSide,
  3. BoxGeometry,
  4. Mesh,
  5. Vector3
  6. } from 'three';
  7. import { Fn, NodeMaterial, float, vec3, acos, add, mul, clamp, cos, dot, exp, max, mix, modelViewProjection, normalize, positionWorld, pow, smoothstep, sub, varying, varyingProperty, vec4, uniform, cameraPosition } from 'three/tsl';
  8. /**
  9. * Based on "A Practical Analytic Model for Daylight"
  10. * aka The Preetham Model, the de facto standard analytic skydome model
  11. * https://www.researchgate.net/publication/220720443_A_Practical_Analytic_Model_for_Daylight
  12. *
  13. * First implemented by Simon Wallner
  14. * http://simonwallner.at/project/atmospheric-scattering/
  15. *
  16. * Improved by Martin Upitis
  17. * http://blenderartists.org/forum/showthread.php?245954-preethams-sky-impementation-HDR
  18. *
  19. * Three.js integration by zz85 http://twitter.com/blurspline
  20. */
  21. class SkyMesh extends Mesh {
  22. constructor() {
  23. const material = new NodeMaterial();
  24. super( new BoxGeometry( 1, 1, 1 ), material );
  25. this.turbidity = uniform( 2 );
  26. this.rayleigh = uniform( 1 );
  27. this.mieCoefficient = uniform( 0.005 );
  28. this.mieDirectionalG = uniform( 0.8 );
  29. this.sunPosition = uniform( new Vector3() );
  30. this.upUniform = uniform( new Vector3( 0, 1, 0 ) );
  31. this.isSky = true;
  32. const vertexNode = /*@__PURE__*/ Fn( () => {
  33. // constants for atmospheric scattering
  34. const e = float( 2.71828182845904523536028747135266249775724709369995957 );
  35. // const pi = float( 3.141592653589793238462643383279502884197169 );
  36. // wavelength of used primaries, according to preetham
  37. // const lambda = vec3( 680E-9, 550E-9, 450E-9 );
  38. // this pre-calcuation replaces older TotalRayleigh(vec3 lambda) function:
  39. // (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))
  40. const totalRayleigh = vec3( 5.804542996261093E-6, 1.3562911419845635E-5, 3.0265902468824876E-5 );
  41. // mie stuff
  42. // K coefficient for the primaries
  43. // const v = float( 4.0 );
  44. // const K = vec3( 0.686, 0.678, 0.666 );
  45. // MieConst = pi * pow( ( 2.0 * pi ) / lambda, vec3( v - 2.0 ) ) * K
  46. const MieConst = vec3( 1.8399918514433978E14, 2.7798023919660528E14, 4.0790479543861094E14 );
  47. // earth shadow hack
  48. // cutoffAngle = pi / 1.95;
  49. const cutoffAngle = float( 1.6110731556870734 );
  50. const steepness = float( 1.5 );
  51. const EE = float( 1000.0 );
  52. // varying sun position
  53. const vSunDirection = normalize( this.sunPosition );
  54. varyingProperty( 'vec3', 'vSunDirection' ).assign( vSunDirection );
  55. // varying sun intensity
  56. const angle = dot( vSunDirection, this.upUniform );
  57. const zenithAngleCos = clamp( angle, - 1, 1 );
  58. const sunIntensity = EE.mul( max( 0.0, float( 1.0 ).sub( pow( e, cutoffAngle.sub( acos( zenithAngleCos ) ).div( steepness ).negate() ) ) ) );
  59. varyingProperty( 'float', 'vSunE' ).assign( sunIntensity );
  60. // varying sun fade
  61. const vSunfade = float( 1.0 ).sub( clamp( float( 1.0 ).sub( exp( this.sunPosition.y.div( 450000.0 ) ) ), 0, 1 ) );
  62. varyingProperty( 'float', 'vSunfade' ).assign( vSunfade );
  63. // varying vBetaR
  64. const rayleighCoefficient = this.rayleigh.sub( float( 1.0 ).mul( float( 1.0 ).sub( vSunfade ) ) );
  65. // extinction (absorbtion + out scattering)
  66. // rayleigh coefficients
  67. varyingProperty( 'vec3', 'vBetaR' ).assign( totalRayleigh.mul( rayleighCoefficient ) );
  68. // varying vBetaM
  69. const c = float( 0.2 ).mul( this.turbidity ).mul( 10E-18 );
  70. const totalMie = float( 0.434 ).mul( c ).mul( MieConst );
  71. varyingProperty( 'vec3', 'vBetaM' ).assign( totalMie.mul( this.mieCoefficient ) );
  72. // position
  73. const position = modelViewProjection();
  74. position.z.assign( position.w ); // set z to camera.far
  75. return position;
  76. } )();
  77. const fragmentNode = /*@__PURE__*/ Fn( () => {
  78. const vSunDirection = varying( vec3(), 'vSunDirection' );
  79. const vSunE = varying( float(), 'vSunE' );
  80. const vSunfade = varying( float(), 'vSunfade' );
  81. const vBetaR = varying( vec3(), 'vBetaR' );
  82. const vBetaM = varying( vec3(), 'vBetaM' );
  83. // constants for atmospheric scattering
  84. const pi = float( 3.141592653589793238462643383279502884197169 );
  85. // optical length at zenith for molecules
  86. const rayleighZenithLength = float( 8.4E3 );
  87. const mieZenithLength = float( 1.25E3 );
  88. // 66 arc seconds -> degrees, and the cosine of that
  89. const sunAngularDiameterCos = float( 0.999956676946448443553574619906976478926848692873900859324 );
  90. // 3.0 / ( 16.0 * pi )
  91. const THREE_OVER_SIXTEENPI = float( 0.05968310365946075 );
  92. // 1.0 / ( 4.0 * pi )
  93. const ONE_OVER_FOURPI = float( 0.07957747154594767 );
  94. //
  95. const direction = normalize( positionWorld.sub( cameraPosition ) );
  96. // optical length
  97. // cutoff angle at 90 to avoid singularity in next formula.
  98. const zenithAngle = acos( max( 0.0, dot( this.upUniform, direction ) ) );
  99. const inverse = float( 1.0 ).div( cos( zenithAngle ).add( float( 0.15 ).mul( pow( float( 93.885 ).sub( zenithAngle.mul( 180.0 ).div( pi ) ), - 1.253 ) ) ) );
  100. const sR = rayleighZenithLength.mul( inverse );
  101. const sM = mieZenithLength.mul( inverse );
  102. // combined extinction factor
  103. const Fex = exp( mul( vBetaR, sR ).add( mul( vBetaM, sM ) ).negate() );
  104. // in scattering
  105. const cosTheta = dot( direction, vSunDirection );
  106. // betaRTheta
  107. const c = cosTheta.mul( 0.5 ).add( 0.5 );
  108. const rPhase = THREE_OVER_SIXTEENPI.mul( float( 1.0 ).add( pow( c, 2.0 ) ) );
  109. const betaRTheta = vBetaR.mul( rPhase );
  110. // betaMTheta
  111. const g2 = pow( this.mieDirectionalG, 2.0 );
  112. const inv = float( 1.0 ).div( pow( float( 1.0 ).sub( float( 2.0 ).mul( this.mieDirectionalG ).mul( cosTheta ) ).add( g2 ), 1.5 ) );
  113. const mPhase = ONE_OVER_FOURPI.mul( float( 1.0 ).sub( g2 ) ).mul( inv );
  114. const betaMTheta = vBetaM.mul( mPhase );
  115. const Lin = pow( vSunE.mul( add( betaRTheta, betaMTheta ).div( add( vBetaR, vBetaM ) ) ).mul( sub( 1.0, Fex ) ), vec3( 1.5 ) );
  116. Lin.mulAssign( mix( vec3( 1.0 ), pow( vSunE.mul( add( betaRTheta, betaMTheta ).div( add( vBetaR, vBetaM ) ) ).mul( Fex ), vec3( 1.0 / 2.0 ) ), clamp( pow( sub( 1.0, dot( this.upUniform, vSunDirection ) ), 5.0 ), 0.0, 1.0 ) ) );
  117. // nightsky
  118. const L0 = vec3( 0.1 ).mul( Fex );
  119. // composition + solar disc
  120. const sundisk = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos.add( 0.00002 ), cosTheta );
  121. L0.addAssign( vSunE.mul( 19000.0 ).mul( Fex ).mul( sundisk ) );
  122. const texColor = add( Lin, L0 ).mul( 0.04 ).add( vec3( 0.0, 0.0003, 0.00075 ) );
  123. const retColor = pow( texColor, vec3( float( 1.0 ).div( float( 1.2 ).add( vSunfade.mul( 1.2 ) ) ) ) );
  124. return vec4( retColor, 1.0 );
  125. } )();
  126. material.side = BackSide;
  127. material.depthWrite = false;
  128. material.vertexNode = vertexNode;
  129. material.fragmentNode = fragmentNode;
  130. }
  131. }
  132. export { SkyMesh };
粤ICP备19079148号