SkyMesh.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import {
  2. BackSide,
  3. BoxGeometry,
  4. Mesh,
  5. Vector3,
  6. NodeMaterial
  7. } from 'three/webgpu';
  8. import { Fn, float, vec3, acos, add, mul, clamp, cos, dot, exp, max, mix, modelViewProjection, normalize, positionWorld, pow, smoothstep, sub, varyingProperty, vec4, uniform, cameraPosition } from 'three/tsl';
  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. * ```js
  22. * const sky = new SkyMesh();
  23. * sky.scale.setScalar( 10000 );
  24. * scene.add( sky );
  25. * ```
  26. *
  27. * @augments Mesh
  28. * @three_import import { SkyMesh } from 'three/addons/objects/SkyMesh.js';
  29. */
  30. class SkyMesh extends Mesh {
  31. /**
  32. * Constructs a new skydome.
  33. */
  34. constructor() {
  35. const material = new NodeMaterial();
  36. super( new BoxGeometry( 1, 1, 1 ), material );
  37. /**
  38. * The turbidity uniform.
  39. *
  40. * @type {UniformNode<float>}
  41. */
  42. this.turbidity = uniform( 2 );
  43. /**
  44. * The rayleigh uniform.
  45. *
  46. * @type {UniformNode<float>}
  47. */
  48. this.rayleigh = uniform( 1 );
  49. /**
  50. * The mieCoefficient uniform.
  51. *
  52. * @type {UniformNode<float>}
  53. */
  54. this.mieCoefficient = uniform( 0.005 );
  55. /**
  56. * The mieDirectionalG uniform.
  57. *
  58. * @type {UniformNode<float>}
  59. */
  60. this.mieDirectionalG = uniform( 0.8 );
  61. /**
  62. * The sun position uniform.
  63. *
  64. * @type {UniformNode<vec3>}
  65. */
  66. this.sunPosition = uniform( new Vector3() );
  67. /**
  68. * The up position.
  69. *
  70. * @type {UniformNode<vec3>}
  71. */
  72. this.upUniform = uniform( new Vector3( 0, 1, 0 ) );
  73. /**
  74. * This flag can be used for type testing.
  75. *
  76. * @type {boolean}
  77. * @readonly
  78. * @default true
  79. */
  80. this.isSky = true;
  81. // Varyings
  82. const vSunDirection = varyingProperty( 'vec3' );
  83. const vSunE = varyingProperty( 'float' );
  84. const vSunfade = varyingProperty( 'float' );
  85. const vBetaR = varyingProperty( 'vec3' );
  86. const vBetaM = varyingProperty( 'vec3' );
  87. const vertexNode = /*@__PURE__*/ Fn( () => {
  88. // constants for atmospheric scattering
  89. const e = float( 2.71828182845904523536028747135266249775724709369995957 );
  90. // const pi = float( 3.141592653589793238462643383279502884197169 );
  91. // wavelength of used primaries, according to preetham
  92. // const lambda = vec3( 680E-9, 550E-9, 450E-9 );
  93. // this pre-calculation replaces older TotalRayleigh(vec3 lambda) function:
  94. // (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))
  95. const totalRayleigh = vec3( 5.804542996261093E-6, 1.3562911419845635E-5, 3.0265902468824876E-5 );
  96. // mie stuff
  97. // K coefficient for the primaries
  98. // const v = float( 4.0 );
  99. // const K = vec3( 0.686, 0.678, 0.666 );
  100. // MieConst = pi * pow( ( 2.0 * pi ) / lambda, vec3( v - 2.0 ) ) * K
  101. const MieConst = vec3( 1.8399918514433978E14, 2.7798023919660528E14, 4.0790479543861094E14 );
  102. // earth shadow hack
  103. // cutoffAngle = pi / 1.95;
  104. const cutoffAngle = float( 1.6110731556870734 );
  105. const steepness = float( 1.5 );
  106. const EE = float( 1000.0 );
  107. // varying sun position
  108. const sunDirection = normalize( this.sunPosition );
  109. vSunDirection.assign( sunDirection );
  110. // varying sun intensity
  111. const angle = dot( sunDirection, this.upUniform );
  112. const zenithAngleCos = clamp( angle, - 1, 1 );
  113. const sunIntensity = EE.mul( max( 0.0, float( 1.0 ).sub( pow( e, cutoffAngle.sub( acos( zenithAngleCos ) ).div( steepness ).negate() ) ) ) );
  114. vSunE.assign( sunIntensity );
  115. // varying sun fade
  116. const sunfade = float( 1.0 ).sub( clamp( float( 1.0 ).sub( exp( this.sunPosition.y.div( 450000.0 ) ) ), 0, 1 ) );
  117. vSunfade.assign( sunfade );
  118. // varying vBetaR
  119. const rayleighCoefficient = this.rayleigh.sub( float( 1.0 ).mul( float( 1.0 ).sub( sunfade ) ) );
  120. // extinction (absorption + out scattering)
  121. // rayleigh coefficients
  122. vBetaR.assign( totalRayleigh.mul( rayleighCoefficient ) );
  123. // varying vBetaM
  124. const c = float( 0.2 ).mul( this.turbidity ).mul( 10E-18 );
  125. const totalMie = float( 0.434 ).mul( c ).mul( MieConst );
  126. vBetaM.assign( totalMie.mul( this.mieCoefficient ) );
  127. // position
  128. const position = modelViewProjection;
  129. position.z.assign( position.w ); // set z to camera.far
  130. return position;
  131. } )();
  132. const fragmentNode = /*@__PURE__*/ Fn( () => {
  133. // constants for atmospheric scattering
  134. const pi = float( 3.141592653589793238462643383279502884197169 );
  135. // optical length at zenith for molecules
  136. const rayleighZenithLength = float( 8.4E3 );
  137. const mieZenithLength = float( 1.25E3 );
  138. // 66 arc seconds -> degrees, and the cosine of that
  139. const sunAngularDiameterCos = float( 0.999956676946448443553574619906976478926848692873900859324 );
  140. // 3.0 / ( 16.0 * pi )
  141. const THREE_OVER_SIXTEENPI = float( 0.05968310365946075 );
  142. // 1.0 / ( 4.0 * pi )
  143. const ONE_OVER_FOURPI = float( 0.07957747154594767 );
  144. //
  145. const direction = normalize( positionWorld.sub( cameraPosition ) );
  146. // optical length
  147. // cutoff angle at 90 to avoid singularity in next formula.
  148. const zenithAngle = acos( max( 0.0, dot( this.upUniform, direction ) ) );
  149. 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 ) ) ) );
  150. const sR = rayleighZenithLength.mul( inverse );
  151. const sM = mieZenithLength.mul( inverse );
  152. // combined extinction factor
  153. const Fex = exp( mul( vBetaR, sR ).add( mul( vBetaM, sM ) ).negate() );
  154. // in scattering
  155. const cosTheta = dot( direction, vSunDirection );
  156. // betaRTheta
  157. const c = cosTheta.mul( 0.5 ).add( 0.5 );
  158. const rPhase = THREE_OVER_SIXTEENPI.mul( float( 1.0 ).add( pow( c, 2.0 ) ) );
  159. const betaRTheta = vBetaR.mul( rPhase );
  160. // betaMTheta
  161. const g2 = pow( this.mieDirectionalG, 2.0 );
  162. const inv = float( 1.0 ).div( pow( float( 1.0 ).sub( float( 2.0 ).mul( this.mieDirectionalG ).mul( cosTheta ) ).add( g2 ), 1.5 ) );
  163. const mPhase = ONE_OVER_FOURPI.mul( float( 1.0 ).sub( g2 ) ).mul( inv );
  164. const betaMTheta = vBetaM.mul( mPhase );
  165. const Lin = pow( vSunE.mul( add( betaRTheta, betaMTheta ).div( add( vBetaR, vBetaM ) ) ).mul( sub( 1.0, Fex ) ), vec3( 1.5 ) );
  166. 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 ) ) );
  167. // nightsky
  168. const L0 = vec3( 0.1 ).mul( Fex );
  169. // composition + solar disc
  170. const sundisk = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos.add( 0.00002 ), cosTheta );
  171. L0.addAssign( vSunE.mul( 19000.0 ).mul( Fex ).mul( sundisk ) );
  172. const texColor = add( Lin, L0 ).mul( 0.04 ).add( vec3( 0.0, 0.0003, 0.00075 ) );
  173. const retColor = pow( texColor, vec3( float( 1.0 ).div( float( 1.2 ).add( vSunfade.mul( 1.2 ) ) ) ) );
  174. return vec4( retColor, 1.0 );
  175. } )();
  176. material.side = BackSide;
  177. material.depthWrite = false;
  178. material.vertexNode = vertexNode;
  179. material.fragmentNode = fragmentNode;
  180. }
  181. }
  182. export { SkyMesh };
粤ICP备19079148号