1
0

webgpu_tsl_earth.html 6.5 KB

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