misc_controls_fly.html 7.9 KB

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