misc_controls_fly.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 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. <meta property="og:title" content="three.js webgpu - fly controls - earth">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/misc_controls_fly.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/misc_controls_fly.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background:#000;
  15. color: #eee;
  16. }
  17. a {
  18. color: #0080ff;
  19. }
  20. b {
  21. color: orange
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - earth [fly controls]<br/>
  27. <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
  28. </div>
  29. <script type="importmap">
  30. {
  31. "imports": {
  32. "three": "../build/three.webgpu.js",
  33. "three/webgpu": "../build/three.webgpu.js",
  34. "three/tsl": "../build/three.tsl.js",
  35. "three/addons/": "./jsm/"
  36. }
  37. }
  38. </script>
  39. <script type="module">
  40. import * as THREE from 'three/webgpu';
  41. import { pass } from 'three/tsl';
  42. import { film } from 'three/addons/tsl/display/FilmNode.js';
  43. import Stats from 'three/addons/libs/stats.module.js';
  44. import { FlyControls } from 'three/addons/controls/FlyControls.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 renderPipeline;
  57. const textureLoader = new THREE.TextureLoader();
  58. let d, dPlanet, dMoon;
  59. const dMoonVec = new THREE.Vector3();
  60. const timer = new THREE.Timer();
  61. timer.connect( document );
  62. init();
  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.background = new THREE.Color( 0x000000 );
  68. scene.fog = new THREE.FogExp2( 0x000000, 0.00000025 );
  69. dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  70. dirLight.position.set( - 1, 0, 1 ).normalize();
  71. scene.add( dirLight );
  72. const materialNormalMap = new THREE.MeshPhongMaterial( {
  73. specular: 0x7c7c7c,
  74. shininess: 15,
  75. map: textureLoader.load( 'textures/planets/earth_atmos_2048.jpg' ),
  76. specularMap: textureLoader.load( 'textures/planets/earth_specular_2048.jpg' ),
  77. normalMap: textureLoader.load( 'textures/planets/earth_normal_2048.jpg' ),
  78. // y scale is negated to compensate for normal map handedness.
  79. normalScale: new THREE.Vector2( 0.85, - 0.85 )
  80. } );
  81. materialNormalMap.map.colorSpace = THREE.SRGBColorSpace;
  82. // planet
  83. geometry = new THREE.SphereGeometry( radius, 100, 50 );
  84. meshPlanet = new THREE.Mesh( geometry, materialNormalMap );
  85. meshPlanet.rotation.y = 0;
  86. meshPlanet.rotation.z = tilt;
  87. scene.add( meshPlanet );
  88. // clouds
  89. const materialClouds = new THREE.MeshLambertMaterial( {
  90. map: textureLoader.load( 'textures/planets/earth_clouds_1024.png' ),
  91. transparent: true
  92. } );
  93. materialClouds.map.colorSpace = THREE.SRGBColorSpace;
  94. meshClouds = new THREE.Mesh( geometry, materialClouds );
  95. meshClouds.scale.set( cloudsScale, cloudsScale, cloudsScale );
  96. meshClouds.rotation.z = tilt;
  97. scene.add( meshClouds );
  98. // moon
  99. const materialMoon = new THREE.MeshPhongMaterial( {
  100. map: textureLoader.load( 'textures/planets/moon_1024.jpg' )
  101. } );
  102. materialMoon.map.colorSpace = THREE.SRGBColorSpace;
  103. meshMoon = new THREE.Mesh( geometry, materialMoon );
  104. meshMoon.position.set( radius * 5, 0, 0 );
  105. meshMoon.scale.set( moonScale, moonScale, moonScale );
  106. scene.add( meshMoon );
  107. // stars
  108. const r = radius, starsGeometry = [ new THREE.BufferGeometry(), new THREE.BufferGeometry() ];
  109. const vertices1 = [];
  110. const vertices2 = [];
  111. const vertex = new THREE.Vector3();
  112. for ( let i = 0; i < 250; 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. vertices1.push( vertex.x, vertex.y, vertex.z );
  118. }
  119. for ( let i = 0; i < 1500; i ++ ) {
  120. vertex.x = Math.random() * 2 - 1;
  121. vertex.y = Math.random() * 2 - 1;
  122. vertex.z = Math.random() * 2 - 1;
  123. vertex.multiplyScalar( r );
  124. vertices2.push( vertex.x, vertex.y, vertex.z );
  125. }
  126. starsGeometry[ 0 ].setAttribute( 'position', new THREE.Float32BufferAttribute( vertices1, 3 ) );
  127. starsGeometry[ 1 ].setAttribute( 'position', new THREE.Float32BufferAttribute( vertices2, 3 ) );
  128. const starsMaterials = [
  129. new THREE.PointsMaterial( { color: 0x9c9c9c } ),
  130. new THREE.PointsMaterial( { color: 0x838383 } ),
  131. new THREE.PointsMaterial( { color: 0x5a5a5a } )
  132. ];
  133. for ( let i = 10; i < 30; i ++ ) {
  134. const stars = new THREE.Points( starsGeometry[ i % 2 ], starsMaterials[ i % 3 ] );
  135. stars.rotation.x = Math.random() * 6;
  136. stars.rotation.y = Math.random() * 6;
  137. stars.rotation.z = Math.random() * 6;
  138. stars.scale.setScalar( i * 10 );
  139. stars.matrixAutoUpdate = false;
  140. stars.updateMatrix();
  141. scene.add( stars );
  142. }
  143. renderer = new THREE.WebGPURenderer( { antialias: true } );
  144. renderer.setPixelRatio( window.devicePixelRatio );
  145. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  146. renderer.setAnimationLoop( animate );
  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. renderPipeline = new THREE.RenderPipeline( renderer );
  161. const scenePass = pass( scene, camera );
  162. const scenePassColor = scenePass.getTextureNode();
  163. renderPipeline.outputNode = film( scenePassColor );
  164. }
  165. function onWindowResize() {
  166. SCREEN_HEIGHT = window.innerHeight;
  167. SCREEN_WIDTH = window.innerWidth;
  168. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  169. camera.updateProjectionMatrix();
  170. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  171. }
  172. function animate() {
  173. timer.update();
  174. render();
  175. stats.update();
  176. }
  177. function render() {
  178. // rotate the planet and clouds
  179. const delta = timer.getDelta();
  180. meshPlanet.rotation.y += rotationSpeed * delta;
  181. meshClouds.rotation.y += 1.25 * rotationSpeed * delta;
  182. // slow down as we approach the surface
  183. dPlanet = camera.position.length();
  184. dMoonVec.subVectors( camera.position, meshMoon.position );
  185. dMoon = dMoonVec.length();
  186. if ( dMoon < dPlanet ) {
  187. d = ( dMoon - radius * moonScale * 1.01 );
  188. } else {
  189. d = ( dPlanet - radius * 1.01 );
  190. }
  191. controls.movementSpeed = 0.33 * d;
  192. controls.update( delta );
  193. renderPipeline.render();
  194. }
  195. </script>
  196. </body>
  197. </html>
粤ICP备19079148号