webgpu_tsl_earth.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/webgpu": "../build/three.webgpu.js",
  22. "three/tsl": "../build/three.tsl.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import { step, normalWorldGeometry, output, texture, vec3, vec4, normalize, positionWorld, bumpMap, cameraPosition, color, uniform, mix, uv, max } from 'three/tsl';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. let camera, scene, renderer, controls, globe, clock;
  33. init();
  34. function init() {
  35. clock = new THREE.Clock();
  36. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.1, 100 );
  37. camera.position.set( 4.5, 2, 3 );
  38. scene = new THREE.Scene();
  39. // sun
  40. const sun = new THREE.DirectionalLight( '#ffffff', 2 );
  41. sun.position.set( 0, 0, 3 );
  42. scene.add( sun );
  43. // uniforms
  44. const atmosphereDayColor = uniform( color( '#4db2ff' ) );
  45. const atmosphereTwilightColor = uniform( color( '#bc490b' ) );
  46. const roughnessLow = uniform( 0.25 );
  47. const roughnessHigh = uniform( 0.35 );
  48. // textures
  49. const textureLoader = new THREE.TextureLoader();
  50. const dayTexture = textureLoader.load( './textures/planets/earth_day_4096.jpg' );
  51. dayTexture.colorSpace = THREE.SRGBColorSpace;
  52. dayTexture.anisotropy = 8;
  53. const nightTexture = textureLoader.load( './textures/planets/earth_night_4096.jpg' );
  54. nightTexture.colorSpace = THREE.SRGBColorSpace;
  55. nightTexture.anisotropy = 8;
  56. const bumpRoughnessCloudsTexture = textureLoader.load( './textures/planets/earth_bump_roughness_clouds_4096.jpg' );
  57. bumpRoughnessCloudsTexture.anisotropy = 8;
  58. // fresnel
  59. const viewDirection = positionWorld.sub( cameraPosition ).normalize();
  60. const fresnel = viewDirection.dot( normalWorldGeometry ).abs().oneMinus().toVar();
  61. // sun orientation
  62. const sunOrientation = normalWorldGeometry.dot( normalize( sun.position ) ).toVar();
  63. // atmosphere color
  64. const atmosphereColor = mix( atmosphereTwilightColor, atmosphereDayColor, sunOrientation.smoothstep( - 0.25, 0.75 ) );
  65. // globe
  66. const globeMaterial = new THREE.MeshStandardNodeMaterial();
  67. const cloudsStrength = texture( bumpRoughnessCloudsTexture, uv() ).b.smoothstep( 0.2, 1 );
  68. globeMaterial.colorNode = mix( texture( dayTexture ), vec3( 1 ), cloudsStrength.mul( 2 ) );
  69. const roughness = max(
  70. texture( bumpRoughnessCloudsTexture ).g,
  71. step( 0.01, cloudsStrength )
  72. );
  73. globeMaterial.roughnessNode = roughness.remap( 0, 1, roughnessLow, roughnessHigh );
  74. const night = texture( nightTexture );
  75. const dayStrength = sunOrientation.smoothstep( - 0.25, 0.5 );
  76. const atmosphereDayStrength = sunOrientation.smoothstep( - 0.5, 1 );
  77. const atmosphereMix = atmosphereDayStrength.mul( fresnel.pow( 2 ) ).clamp( 0, 1 );
  78. let finalOutput = mix( night.rgb, output.rgb, dayStrength );
  79. finalOutput = mix( finalOutput, atmosphereColor, atmosphereMix );
  80. globeMaterial.outputNode = vec4( finalOutput, output.a );
  81. const bumpElevation = max(
  82. texture( bumpRoughnessCloudsTexture ).r,
  83. cloudsStrength
  84. );
  85. globeMaterial.normalNode = bumpMap( bumpElevation );
  86. const sphereGeometry = new THREE.SphereGeometry( 1, 64, 64 );
  87. globe = new THREE.Mesh( sphereGeometry, globeMaterial );
  88. scene.add( globe );
  89. // atmosphere
  90. const atmosphereMaterial = new THREE.MeshBasicNodeMaterial( { side: THREE.BackSide, transparent: true } );
  91. let alpha = fresnel.remap( 0.73, 1, 1, 0 ).pow( 3 );
  92. alpha = alpha.mul( sunOrientation.smoothstep( - 0.5, 1 ) );
  93. atmosphereMaterial.outputNode = vec4( atmosphereColor, alpha );
  94. const atmosphere = new THREE.Mesh( sphereGeometry, atmosphereMaterial );
  95. atmosphere.scale.setScalar( 1.04 );
  96. scene.add( atmosphere );
  97. // debug
  98. const gui = new GUI();
  99. gui
  100. .addColor( { color: atmosphereDayColor.value.getHex( THREE.SRGBColorSpace ) }, 'color' )
  101. .onChange( ( value ) => {
  102. atmosphereDayColor.value.set( value );
  103. } )
  104. .name( 'atmosphereDayColor' );
  105. gui
  106. .addColor( { color: atmosphereTwilightColor.value.getHex( THREE.SRGBColorSpace ) }, 'color' )
  107. .onChange( ( value ) => {
  108. atmosphereTwilightColor.value.set( value );
  109. } )
  110. .name( 'atmosphereTwilightColor' );
  111. gui.add( roughnessLow, 'value', 0, 1, 0.001 ).name( 'roughnessLow' );
  112. gui.add( roughnessHigh, 'value', 0, 1, 0.001 ).name( 'roughnessHigh' );
  113. // renderer
  114. renderer = new THREE.WebGPURenderer();
  115. renderer.setPixelRatio( window.devicePixelRatio );
  116. renderer.setSize( window.innerWidth, window.innerHeight );
  117. renderer.setAnimationLoop( animate );
  118. document.body.appendChild( renderer.domElement );
  119. // controls
  120. controls = new OrbitControls( camera, renderer.domElement );
  121. controls.enableDamping = true;
  122. controls.minDistance = 0.1;
  123. controls.maxDistance = 50;
  124. window.addEventListener( 'resize', onWindowResize );
  125. }
  126. function onWindowResize() {
  127. camera.aspect = window.innerWidth / window.innerHeight;
  128. camera.updateProjectionMatrix();
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. }
  131. async function animate() {
  132. const delta = clock.getDelta();
  133. globe.rotation.y += delta * 0.025;
  134. controls.update();
  135. renderer.render( scene, camera );
  136. }
  137. </script>
  138. </body>
  139. </html>
粤ICP备19079148号