webgpu_tsl_earth.html 6.5 KB

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