2
0

misc_controls_fly.html 7.4 KB

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