misc_controls_fly.html 7.6 KB

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