webgpu_tsl_earth.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - earth</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js webgpu</a> - earth
  12. <br>
  13. Based on <a href="https://threejs-journey.com/lessons/earth-shaders" target="_blank" rel="noopener">Three.js Journey</a> lesson
  14. <br>
  15. Earth textures from <a href="https://www.solarsystemscope.com/textures/" target="_blank" rel="noopener">Solar System Scope</a> (resized and merged)
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.webgpu.js",
  21. "three/tsl": "../build/three.webgpu.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { step, normalWorld, output, texture, vec3, vec4, normalize, positionWorld, bumpMap, cameraPosition, color, uniform, mix, uv, max } from 'three/tsl';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  31. let camera, scene, renderer, controls, globe, clock;
  32. init();
  33. function init() {
  34. clock = new THREE.Clock();
  35. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.1, 100 );
  36. camera.position.set( 4.5, 2, 3 );
  37. scene = new THREE.Scene();
  38. // sun
  39. const sun = new THREE.DirectionalLight( '#ffffff', 2 );
  40. sun.position.set( 0, 0, 3 );
  41. scene.add( sun );
  42. // uniforms
  43. const atmosphereDayColor = uniform( color( '#4db2ff' ) );
  44. const atmosphereTwilightColor = uniform( color( '#bc490b' ) );
  45. const roughnessLow = uniform( 0.25 );
  46. const roughnessHigh = uniform( 0.35 );
  47. // textures
  48. const textureLoader = new THREE.TextureLoader();
  49. const dayTexture = textureLoader.load( './textures/planets/earth_day_4096.jpg' );
  50. dayTexture.colorSpace = THREE.SRGBColorSpace;
  51. dayTexture.anisotropy = 8;
  52. const nightTexture = textureLoader.load( './textures/planets/earth_night_4096.jpg' );
  53. nightTexture.colorSpace = THREE.SRGBColorSpace;
  54. nightTexture.anisotropy = 8;
  55. const bumpRoughnessCloudsTexture = textureLoader.load( './textures/planets/earth_bump_roughness_clouds_4096.jpg' );
  56. bumpRoughnessCloudsTexture.anisotropy = 8;
  57. // fresnel
  58. const viewDirection = positionWorld.sub( cameraPosition ).normalize();
  59. const fresnel = viewDirection.dot( normalWorld ).abs().oneMinus().toVar();
  60. // sun orientation
  61. const sunOrientation = normalWorld.dot( normalize( sun.position ) ).toVar();
  62. // atmosphere color
  63. const atmosphereColor = mix( atmosphereTwilightColor, atmosphereDayColor, sunOrientation.smoothstep( - 0.25, 0.75 ) );
  64. // globe
  65. const globeMaterial = new THREE.MeshStandardNodeMaterial();
  66. const cloudsStrength = texture( bumpRoughnessCloudsTexture, uv() ).b.smoothstep( 0.2, 1 );
  67. globeMaterial.colorNode = mix( texture( dayTexture ), vec3( 1 ), cloudsStrength.mul( 2 ) );
  68. const roughness = max(
  69. texture( bumpRoughnessCloudsTexture ).g,
  70. step( 0.01, cloudsStrength )
  71. );
  72. globeMaterial.roughnessNode = roughness.remap( 0, 1, roughnessLow, roughnessHigh );
  73. const night = texture( nightTexture );
  74. const dayStrength = sunOrientation.smoothstep( - 0.25, 0.5 );
  75. const atmosphereDayStrength = sunOrientation.smoothstep( - 0.5, 1 );
  76. const atmosphereMix = atmosphereDayStrength.mul( fresnel.pow( 2 ) ).clamp( 0, 1 );
  77. let finalOutput = mix( night.rgb, output.rgb, dayStrength );
  78. finalOutput = mix( finalOutput, atmosphereColor, atmosphereMix );
  79. globeMaterial.outputNode = vec4( finalOutput, output.a );
  80. const bumpElevation = max(
  81. texture( bumpRoughnessCloudsTexture ).r,
  82. cloudsStrength
  83. );
  84. globeMaterial.normalNode = bumpMap( bumpElevation );
  85. const sphereGeometry = new THREE.SphereGeometry( 1, 64, 64 );
  86. globe = new THREE.Mesh( sphereGeometry, globeMaterial );
  87. scene.add( globe );
  88. // atmosphere
  89. const atmosphereMaterial = new THREE.MeshBasicNodeMaterial( { side: THREE.BackSide, transparent: true } );
  90. let alpha = fresnel.remap( 0.73, 1, 1, 0 ).pow( 3 );
  91. alpha = alpha.mul( sunOrientation.smoothstep( - 0.5, 1 ) );
  92. atmosphereMaterial.outputNode = vec4( atmosphereColor, alpha );
  93. const atmosphere = new THREE.Mesh( sphereGeometry, atmosphereMaterial );
  94. atmosphere.scale.setScalar( 1.04 );
  95. scene.add( atmosphere );
  96. // debug
  97. const gui = new GUI();
  98. gui
  99. .addColor( { color: atmosphereDayColor.value.getHex( THREE.SRGBColorSpace ) }, 'color' )
  100. .onChange( ( value ) => {
  101. atmosphereDayColor.value.set( value );
  102. } )
  103. .name( 'atmosphereDayColor' );
  104. gui
  105. .addColor( { color: atmosphereTwilightColor.value.getHex( THREE.SRGBColorSpace ) }, 'color' )
  106. .onChange( ( value ) => {
  107. atmosphereTwilightColor.value.set( value );
  108. } )
  109. .name( 'atmosphereTwilightColor' );
  110. gui.add( roughnessLow, 'value', 0, 1, 0.001 ).name( 'roughnessLow' );
  111. gui.add( roughnessHigh, 'value', 0, 1, 0.001 ).name( 'roughnessHigh' );
  112. // renderer
  113. renderer = new THREE.WebGPURenderer();
  114. renderer.setPixelRatio( window.devicePixelRatio );
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. renderer.setAnimationLoop( animate );
  117. document.body.appendChild( renderer.domElement );
  118. // controls
  119. controls = new OrbitControls( camera, renderer.domElement );
  120. controls.enableDamping = true;
  121. controls.minDistance = 0.1;
  122. controls.maxDistance = 50;
  123. window.addEventListener( 'resize', onWindowResize );
  124. }
  125. function onWindowResize() {
  126. camera.aspect = window.innerWidth / window.innerHeight;
  127. camera.updateProjectionMatrix();
  128. renderer.setSize( window.innerWidth, window.innerHeight );
  129. }
  130. async function animate() {
  131. const delta = clock.getDelta();
  132. globe.rotation.y += delta * 0.025;
  133. controls.update();
  134. renderer.render( scene, camera );
  135. }
  136. </script>
  137. </body>
  138. </html>