misc_controls_fly.html 7.1 KB

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