2
0

misc_controls_fly.html 7.8 KB

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