misc_controls_fly.html 7.0 KB

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