misc_controls_fly.html 7.5 KB

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