webgpu_tsl_earth.html 6.4 KB

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