misc_controls_fly.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - fly controls - 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. <style>
  9. body {
  10. background:#000;
  11. color: #eee;
  12. }
  13. a {
  14. color: #0080ff;
  15. }
  16. b {
  17. color: orange
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - earth [fly controls]<br/>
  23. <b>WASD</b> move, <b>R|F</b> up | down, <b>Q|E</b> roll, <b>up|down</b> pitch, <b>left|right</b> yaw
  24. </div>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.webgpu.js",
  29. "three/tsl": "../build/three.webgpu.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import { pass, film } from 'three/tsl';
  37. import Stats from 'three/addons/libs/stats.module.js';
  38. import { FlyControls } from 'three/addons/controls/FlyControls.js';
  39. const radius = 6371;
  40. const tilt = 0.41;
  41. const rotationSpeed = 0.02;
  42. const cloudsScale = 1.005;
  43. const moonScale = 0.23;
  44. const MARGIN = 0;
  45. let SCREEN_HEIGHT = window.innerHeight - MARGIN * 2;
  46. let SCREEN_WIDTH = window.innerWidth;
  47. let camera, controls, scene, renderer, stats;
  48. let geometry, meshPlanet, meshClouds, meshMoon;
  49. let dirLight;
  50. let postProcessing;
  51. const textureLoader = new THREE.TextureLoader();
  52. let d, dPlanet, dMoon;
  53. const dMoonVec = new THREE.Vector3();
  54. const clock = new THREE.Clock();
  55. init();
  56. function init() {
  57. camera = new THREE.PerspectiveCamera( 25, SCREEN_WIDTH / SCREEN_HEIGHT, 50, 1e7 );
  58. camera.position.z = radius * 5;
  59. scene = new THREE.Scene();
  60. scene.fog = new THREE.FogExp2( 0x000000, 0.00000025 );
  61. dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  62. dirLight.position.set( - 1, 0, 1 ).normalize();
  63. scene.add( dirLight );
  64. const materialNormalMap = new THREE.MeshPhongMaterial( {
  65. specular: 0x7c7c7c,
  66. shininess: 15,
  67. map: textureLoader.load( 'textures/planets/earth_atmos_2048.jpg' ),
  68. specularMap: textureLoader.load( 'textures/planets/earth_specular_2048.jpg' ),
  69. normalMap: textureLoader.load( 'textures/planets/earth_normal_2048.jpg' ),
  70. // y scale is negated to compensate for normal map handedness.
  71. normalScale: new THREE.Vector2( 0.85, - 0.85 )
  72. } );
  73. materialNormalMap.map.colorSpace = THREE.SRGBColorSpace;
  74. // planet
  75. geometry = new THREE.SphereGeometry( radius, 100, 50 );
  76. meshPlanet = new THREE.Mesh( geometry, materialNormalMap );
  77. meshPlanet.rotation.y = 0;
  78. meshPlanet.rotation.z = tilt;
  79. scene.add( meshPlanet );
  80. // clouds
  81. const materialClouds = new THREE.MeshLambertMaterial( {
  82. map: textureLoader.load( 'textures/planets/earth_clouds_1024.png' ),
  83. transparent: true
  84. } );
  85. materialClouds.map.colorSpace = THREE.SRGBColorSpace;
  86. meshClouds = new THREE.Mesh( geometry, materialClouds );
  87. meshClouds.scale.set( cloudsScale, cloudsScale, cloudsScale );
  88. meshClouds.rotation.z = tilt;
  89. scene.add( meshClouds );
  90. // moon
  91. const materialMoon = new THREE.MeshPhongMaterial( {
  92. map: textureLoader.load( 'textures/planets/moon_1024.jpg' )
  93. } );
  94. materialMoon.map.colorSpace = THREE.SRGBColorSpace;
  95. meshMoon = new THREE.Mesh( geometry, materialMoon );
  96. meshMoon.position.set( radius * 5, 0, 0 );
  97. meshMoon.scale.set( moonScale, moonScale, moonScale );
  98. scene.add( meshMoon );
  99. // stars
  100. const r = radius, starsGeometry = [ new THREE.BufferGeometry(), new THREE.BufferGeometry() ];
  101. const vertices1 = [];
  102. const vertices2 = [];
  103. const vertex = new THREE.Vector3();
  104. for ( let i = 0; i < 250; i ++ ) {
  105. vertex.x = Math.random() * 2 - 1;
  106. vertex.y = Math.random() * 2 - 1;
  107. vertex.z = Math.random() * 2 - 1;
  108. vertex.multiplyScalar( r );
  109. vertices1.push( vertex.x, vertex.y, vertex.z );
  110. }
  111. for ( let i = 0; i < 1500; i ++ ) {
  112. vertex.x = Math.random() * 2 - 1;
  113. vertex.y = Math.random() * 2 - 1;
  114. vertex.z = Math.random() * 2 - 1;
  115. vertex.multiplyScalar( r );
  116. vertices2.push( vertex.x, vertex.y, vertex.z );
  117. }
  118. starsGeometry[ 0 ].setAttribute( 'position', new THREE.Float32BufferAttribute( vertices1, 3 ) );
  119. starsGeometry[ 1 ].setAttribute( 'position', new THREE.Float32BufferAttribute( vertices2, 3 ) );
  120. const starsMaterials = [
  121. new THREE.PointsMaterial( { color: 0x9c9c9c } ),
  122. new THREE.PointsMaterial( { color: 0x838383 } ),
  123. new THREE.PointsMaterial( { color: 0x5a5a5a } )
  124. ];
  125. for ( let i = 10; i < 30; i ++ ) {
  126. const stars = new THREE.Points( starsGeometry[ i % 2 ], starsMaterials[ i % 3 ] );
  127. stars.rotation.x = Math.random() * 6;
  128. stars.rotation.y = Math.random() * 6;
  129. stars.rotation.z = Math.random() * 6;
  130. stars.scale.setScalar( i * 10 );
  131. stars.matrixAutoUpdate = false;
  132. stars.updateMatrix();
  133. scene.add( stars );
  134. }
  135. renderer = new THREE.WebGPURenderer( { antialias: true } );
  136. renderer.setPixelRatio( window.devicePixelRatio );
  137. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  138. renderer.setAnimationLoop( animate );
  139. document.body.appendChild( renderer.domElement );
  140. //
  141. controls = new FlyControls( camera, renderer.domElement );
  142. controls.movementSpeed = 1000;
  143. controls.domElement = renderer.domElement;
  144. controls.rollSpeed = Math.PI / 24;
  145. controls.autoForward = false;
  146. controls.dragToLook = false;
  147. //
  148. stats = new Stats();
  149. document.body.appendChild( stats.dom );
  150. window.addEventListener( 'resize', onWindowResize );
  151. // postprocessing
  152. postProcessing = new THREE.PostProcessing( renderer );
  153. const scenePass = pass( scene, camera );
  154. const scenePassColor = scenePass.getTextureNode();
  155. postProcessing.outputNode = film( scenePassColor );
  156. }
  157. function onWindowResize() {
  158. SCREEN_HEIGHT = window.innerHeight;
  159. SCREEN_WIDTH = window.innerWidth;
  160. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  161. camera.updateProjectionMatrix();
  162. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  163. }
  164. function animate() {
  165. render();
  166. stats.update();
  167. }
  168. function render() {
  169. // rotate the planet and clouds
  170. const delta = clock.getDelta();
  171. meshPlanet.rotation.y += rotationSpeed * delta;
  172. meshClouds.rotation.y += 1.25 * rotationSpeed * delta;
  173. // slow down as we approach the surface
  174. dPlanet = camera.position.length();
  175. dMoonVec.subVectors( camera.position, meshMoon.position );
  176. dMoon = dMoonVec.length();
  177. if ( dMoon < dPlanet ) {
  178. d = ( dMoon - radius * moonScale * 1.01 );
  179. } else {
  180. d = ( dPlanet - radius * 1.01 );
  181. }
  182. controls.movementSpeed = 0.33 * d;
  183. controls.update( delta );
  184. postProcessing.render();
  185. }
  186. </script>
  187. </body>
  188. </html>