SkyMesh.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. import {
  2. BackSide,
  3. BoxGeometry,
  4. Mesh,
  5. Vector3,
  6. NodeMaterial
  7. } from 'three/webgpu';
  8. import { Fn, float, floor, fract, vec2, vec3, acos, add, mul, clamp, cos, dot, exp, max, mix, modelViewProjection, normalize, positionWorld, pow, smoothstep, sub, varyingProperty, vec4, uniform, cameraPosition, time, If, Loop } from 'three/tsl';
  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 WebGPURenderer}.
  14. * When using {@link WebGLRenderer}, use {@link Sky}.
  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. * It can be useful to hide the sun disc when generating an environment map to avoid artifacts
  28. *
  29. * ```js
  30. * // disable before rendering environment map
  31. * sky.showSunDisc.value = false;
  32. * // ...
  33. * // re-enable before scene sky box rendering
  34. * sky.showSunDisc.value = true;
  35. * ```
  36. *
  37. * @augments Mesh
  38. * @three_import import { SkyMesh } from 'three/addons/objects/SkyMesh.js';
  39. */
  40. class SkyMesh extends Mesh {
  41. /**
  42. * Constructs a new skydome.
  43. */
  44. constructor() {
  45. const material = new NodeMaterial();
  46. super( new BoxGeometry( 1, 1, 1 ), material );
  47. /**
  48. * The turbidity uniform.
  49. *
  50. * @type {UniformNode<float>}
  51. */
  52. this.turbidity = uniform( 2 );
  53. /**
  54. * The rayleigh uniform.
  55. *
  56. * @type {UniformNode<float>}
  57. */
  58. this.rayleigh = uniform( 1 );
  59. /**
  60. * The mieCoefficient uniform.
  61. *
  62. * @type {UniformNode<float>}
  63. */
  64. this.mieCoefficient = uniform( 0.005 );
  65. /**
  66. * The mieDirectionalG uniform.
  67. *
  68. * @type {UniformNode<float>}
  69. */
  70. this.mieDirectionalG = uniform( 0.8 );
  71. /**
  72. * The sun position uniform.
  73. *
  74. * @type {UniformNode<vec3>}
  75. */
  76. this.sunPosition = uniform( new Vector3() );
  77. /**
  78. * The up position.
  79. *
  80. * @type {UniformNode<vec3>}
  81. */
  82. this.upUniform = uniform( new Vector3( 0, 1, 0 ) );
  83. /**
  84. * The cloud scale uniform.
  85. *
  86. * @type {UniformNode<float>}
  87. */
  88. this.cloudScale = uniform( 0.0002 );
  89. /**
  90. * The cloud speed uniform.
  91. *
  92. * @type {UniformNode<float>}
  93. */
  94. this.cloudSpeed = uniform( 0.0001 );
  95. /**
  96. * The cloud coverage uniform.
  97. *
  98. * @type {UniformNode<float>}
  99. */
  100. this.cloudCoverage = uniform( 0.4 );
  101. /**
  102. * The cloud density uniform.
  103. *
  104. * @type {UniformNode<float>}
  105. */
  106. this.cloudDensity = uniform( 0.4 );
  107. /**
  108. * The cloud elevation uniform.
  109. *
  110. * @type {UniformNode<float>}
  111. */
  112. this.cloudElevation = uniform( 0.5 );
  113. /**
  114. * Whether to render the solar disc.
  115. *
  116. * @type {UniformNode<float>}
  117. */
  118. this.showSunDisc = uniform( 1 );
  119. /**
  120. * This flag can be used for type testing.
  121. *
  122. * @type {boolean}
  123. * @readonly
  124. * @default true
  125. * @deprecated Use isSkyMesh instead.
  126. */
  127. this.isSky = true; // @deprecated, r182
  128. /**
  129. * This flag can be used for type testing.
  130. *
  131. * @type {boolean}
  132. * @readonly
  133. * @default true
  134. */
  135. this.isSkyMesh = true;
  136. // Varyings
  137. const vSunDirection = varyingProperty( 'vec3' );
  138. const vSunE = varyingProperty( 'float' );
  139. const vBetaR = varyingProperty( 'vec3' );
  140. const vBetaM = varyingProperty( 'vec3' );
  141. const vertexNode = /*@__PURE__*/ Fn( () => {
  142. // constants for atmospheric scattering
  143. const e = float( 2.718281828459045 );
  144. // const pi = float( 3.141592653589793 );
  145. // wavelength of used primaries, according to preetham
  146. // const lambda = vec3( 680E-9, 550E-9, 450E-9 );
  147. // this pre-calculation replaces older TotalRayleigh(vec3 lambda) function:
  148. // (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))
  149. const totalRayleigh = vec3( 5.804542996261093E-6, 1.3562911419845635E-5, 3.0265902468824876E-5 );
  150. // mie stuff
  151. // K coefficient for the primaries
  152. // const v = float( 4.0 );
  153. // const K = vec3( 0.686, 0.678, 0.666 );
  154. // MieConst = pi * pow( ( 2.0 * pi ) / lambda, vec3( v - 2.0 ) ) * K
  155. const MieConst = vec3( 1.8399918514433978E14, 2.7798023919660528E14, 4.0790479543861094E14 );
  156. // earth shadow hack
  157. // cutoffAngle = pi / 1.95;
  158. const cutoffAngle = float( 1.6110731556870734 );
  159. const steepness = float( 1.5 );
  160. const EE = float( 1000.0 );
  161. // varying sun position
  162. const sunDirection = normalize( this.sunPosition );
  163. vSunDirection.assign( sunDirection );
  164. // varying sun intensity
  165. const angle = dot( sunDirection, this.upUniform );
  166. const zenithAngleCos = clamp( angle, - 1, 1 );
  167. const sunIntensity = EE.mul( max( 0.0, float( 1.0 ).sub( pow( e, cutoffAngle.sub( acos( zenithAngleCos ) ).div( steepness ).negate() ) ) ) );
  168. vSunE.assign( sunIntensity );
  169. // sun fade
  170. const sunfade = float( 1.0 ).sub( clamp( float( 1.0 ).sub( exp( this.sunPosition.y.div( 450000.0 ) ) ), 0, 1 ) );
  171. // varying vBetaR
  172. const rayleighCoefficient = this.rayleigh.sub( float( 1.0 ).mul( float( 1.0 ).sub( sunfade ) ) );
  173. // extinction (absorption + out scattering)
  174. // rayleigh coefficients
  175. vBetaR.assign( totalRayleigh.mul( rayleighCoefficient ) );
  176. // varying vBetaM
  177. const c = float( 0.2 ).mul( this.turbidity ).mul( 10E-18 );
  178. const totalMie = float( 0.434 ).mul( c ).mul( MieConst );
  179. vBetaM.assign( totalMie.mul( this.mieCoefficient ) );
  180. // position
  181. const position = modelViewProjection;
  182. position.z.assign( position.w ); // set z to camera.far
  183. return position;
  184. } )();
  185. const colorNode = /*@__PURE__*/ Fn( () => {
  186. // constants for atmospheric scattering
  187. const pi = float( 3.141592653589793 );
  188. // optical length at zenith for molecules
  189. const rayleighZenithLength = float( 8.4E3 );
  190. const mieZenithLength = float( 1.25E3 );
  191. // 66 arc seconds -> degrees, and the cosine of that
  192. const sunAngularDiameterCos = float( 0.9999566769464484 );
  193. // 3.0 / ( 16.0 * pi )
  194. const THREE_OVER_SIXTEENPI = float( 0.05968310365946075 );
  195. // 1.0 / ( 4.0 * pi )
  196. const ONE_OVER_FOURPI = float( 0.07957747154594767 );
  197. //
  198. const direction = normalize( positionWorld.sub( cameraPosition ) );
  199. // optical length
  200. // cutoff angle at 90 to avoid singularity in next formula.
  201. const zenithAngle = acos( max( 0.0, dot( this.upUniform, direction ) ) );
  202. 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 ) ) ) );
  203. const sR = rayleighZenithLength.mul( inverse );
  204. const sM = mieZenithLength.mul( inverse );
  205. // combined extinction factor
  206. const Fex = exp( mul( vBetaR, sR ).add( mul( vBetaM, sM ) ).negate() );
  207. // in scattering
  208. const cosTheta = dot( direction, vSunDirection );
  209. // betaRTheta
  210. const c = cosTheta.mul( 0.5 ).add( 0.5 );
  211. const rPhase = THREE_OVER_SIXTEENPI.mul( float( 1.0 ).add( pow( c, 2.0 ) ) );
  212. const betaRTheta = vBetaR.mul( rPhase );
  213. // betaMTheta
  214. const g2 = pow( this.mieDirectionalG, 2.0 );
  215. const inv = float( 1.0 ).div( pow( float( 1.0 ).sub( float( 2.0 ).mul( this.mieDirectionalG ).mul( cosTheta ) ).add( g2 ), 1.5 ) );
  216. const mPhase = ONE_OVER_FOURPI.mul( float( 1.0 ).sub( g2 ) ).mul( inv );
  217. const betaMTheta = vBetaM.mul( mPhase );
  218. const Lin = pow( vSunE.mul( add( betaRTheta, betaMTheta ).div( add( vBetaR, vBetaM ) ) ).mul( sub( 1.0, Fex ) ), vec3( 1.5 ) );
  219. 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 ) ) );
  220. // nightsky
  221. const L0 = vec3( 0.1 ).mul( Fex );
  222. // composition + solar disc
  223. const sundisc = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos.add( 0.00002 ), cosTheta ).mul( this.showSunDisc );
  224. L0.addAssign( vSunE.mul( 19000.0 ).mul( Fex ).mul( sundisc ) );
  225. const texColor = add( Lin, L0 ).mul( 0.04 ).add( vec3( 0.0, 0.0003, 0.00075 ) ).toVar();
  226. // gradient at a lattice corner; sinless hash so every GPU produces the same clouds
  227. const gradient = Fn( ( [ i ] ) => {
  228. const p = fract( i.xyx.mul( vec3( 0.1031, 0.1030, 0.0973 ) ) ).toVar();
  229. p.addAssign( dot( p, p.yzx.add( 33.33 ) ) );
  230. return fract( p.xx.add( p.yz ).mul( p.zy ) ).mul( 2.0 ).sub( 1.0 );
  231. } );
  232. // 2D gradient noise: isotropic lobes like Perlin at value-noise cost
  233. const noise = Fn( ( [ p ] ) => {
  234. const i = floor( p );
  235. const f = fract( p );
  236. const u = f.mul( f ).mul( f ).mul( f.mul( f.mul( 6.0 ).sub( 15.0 ) ).add( 10.0 ) ); // quintic fade
  237. const a = dot( gradient( i ), f );
  238. const b = dot( gradient( i.add( vec2( 1.0, 0.0 ) ) ), f.sub( vec2( 1.0, 0.0 ) ) );
  239. const c = dot( gradient( i.add( vec2( 0.0, 1.0 ) ) ), f.sub( vec2( 0.0, 1.0 ) ) );
  240. const d = dot( gradient( i.add( vec2( 1.0, 1.0 ) ) ), f.sub( vec2( 1.0, 1.0 ) ) );
  241. return mix( mix( a, b, u.x ), mix( c, d, u.x ), u.y ).mul( 1.6 ); // ~[-1,1]
  242. } );
  243. // fbm; per-octave drift makes clouds billow instead of scrolling as a rigid stamp
  244. const fbm = Fn( ( [ position, drift ] ) => {
  245. const p = vec2( position ).toVar();
  246. const result = float( 0.0 ).toVar();
  247. const amplitude = float( 1.0 ).toVar();
  248. Loop( 4, () => {
  249. result.addAssign( amplitude.mul( noise( p ) ) );
  250. amplitude.mulAssign( 0.5 );
  251. p.mulAssign( 2.0 ).addAssign( drift );
  252. } );
  253. return result;
  254. } );
  255. // Clouds
  256. If( direction.y.greaterThan( 0.0 ).and( this.cloudCoverage.greaterThan( 0.0 ) ), () => {
  257. // Project to cloud plane (higher elevation = clouds appear lower/closer)
  258. const elevation = mix( 1.0, 0.1, this.cloudElevation );
  259. const cloudUV = direction.xz.div( direction.y.mul( elevation ) ).toVar();
  260. cloudUV.mulAssign( this.cloudScale );
  261. cloudUV.addAssign( time.mul( this.cloudSpeed ) );
  262. // Cloud density field
  263. const evolve = time.mul( this.cloudSpeed ).mul( 300.0 );
  264. const cloudNoise = fbm( cloudUV.mul( 1000.0 ), evolve ).mul( 0.7 ).add( 0.5 ).clamp( 0.0, 1.0 ).toVar();
  265. // Large-scale coverage variation: clear gaps next to dense banks
  266. const region = noise( cloudUV.mul( 300.0 ) ).mul( 0.37 ).add( 0.5 );
  267. const cov = clamp( this.cloudCoverage.add( region.sub( 0.5 ).mul( 0.6 ) ), 0.0, 1.0 );
  268. // Carve clouds where noise rises above the coverage level
  269. const threshold = sub( 1.0, cov ).toVar();
  270. const cloudMask = smoothstep( threshold, threshold.add( 0.3 ), cloudNoise ).toVar();
  271. // Fade clouds near horizon (adjusted by elevation)
  272. const horizonFade = smoothstep( 0.0, add( 0.03, mul( 0.06, this.cloudElevation ) ), direction.y );
  273. cloudMask.mulAssign( horizonFade );
  274. // Cloud lighting from the sky's own radiance
  275. const dayFactor = smoothstep( - 0.08, 0.3, vSunDirection.y );
  276. const sunColor = vSunE.mul( Fex ).mul( 0.22 ).mul( 0.04 ).toVar(); // 0.22 ~ albedo/pi, 0.04 = exposure; the aerial composite adds the eye-leg extinction
  277. const skyAmbient = Lin.mul( 0.04 ).add( vec3( 0.0, 0.0003, 0.00075 ) );
  278. // Beer-powder self-shadow from the sampled density
  279. const depth = max( 0.0, cloudNoise.sub( threshold ) ).toVar();
  280. const beer = exp( depth.mul( - 4.0 ) ).toVar();
  281. const powder = sub( 1.0, beer.mul( beer ) ); // beer*beer == exp(-8*depth)
  282. const shade = mix( 0.45, 1.0, beer.mul( powder ).mul( 2.6 ).clamp( 0.0, 1.0 ) ); // 2.6 = 1/0.385, normalizes beer*powder peak to 1
  283. // Henyey-Greenstein forward lobe ( g = 0.7 ): silver lining on rims toward the sun
  284. const silver = float( 0.51 ).div( pow( sub( 1.49, cosTheta.mul( 1.4 ) ), 1.5 ) ).clamp( 0.0, 3.0 ); // 0.51=1-g^2, 1.49=1+g^2, 1.4=2g
  285. const edge = cloudMask.mul( sub( 1.0, cloudMask ) ).mul( 4.0 );
  286. const cloudColor = skyAmbient.add( sunColor.mul( shade ) ).toVar();
  287. cloudColor.addAssign( sunColor.mul( silver ).mul( edge ).mul( 0.6 ) );
  288. cloudColor.mulAssign( dayFactor.max( 0.03 ) );
  289. // Cloud opacity via Beer's law: density sets how solid the clouds get
  290. const alpha = sub( 1.0, exp( depth.mul( this.cloudDensity ).mul( - 12.0 ) ) ).mul( horizonFade ).toVar();
  291. // Occlude the sun disc/glow behind opaque cloud
  292. texColor.subAssign( L0.mul( 0.04 ).mul( alpha ) );
  293. // Composite through the atmosphere so distant clouds dissolve into haze
  294. const cloudAerial = mix( texColor, cloudColor, Fex );
  295. texColor.assign( mix( texColor, cloudAerial, alpha ) );
  296. } );
  297. return vec4( texColor, 1.0 );
  298. } )();
  299. material.side = BackSide;
  300. material.depthWrite = false;
  301. material.vertexNode = vertexNode;
  302. material.colorNode = colorNode;
  303. }
  304. }
  305. export { SkyMesh };
粤ICP备19079148号