123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - fly controls - earth</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background:#000;
- color: #eee;
- }
- a {
- color: #0080ff;
- }
- b {
- color: orange
- }
- </style>
- </head>
- <body>
- <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - earth [fly controls]<br/>
- <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
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { FlyControls } from './jsm/controls/FlyControls.js';
- import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
- import { RenderPass } from './jsm/postprocessing/RenderPass.js';
- import { FilmPass } from './jsm/postprocessing/FilmPass.js';
- var radius = 6371;
- var tilt = 0.41;
- var rotationSpeed = 0.02;
- var cloudsScale = 1.005;
- var moonScale = 0.23;
- var MARGIN = 0;
- var SCREEN_HEIGHT = window.innerHeight - MARGIN * 2;
- var SCREEN_WIDTH = window.innerWidth;
- var camera, controls, scene, renderer, stats;
- var geometry, meshPlanet, meshClouds, meshMoon;
- var dirLight;
- var composer;
- var textureLoader = new THREE.TextureLoader();
- var d, dPlanet, dMoon, dMoonVec = new THREE.Vector3();
- var clock = new THREE.Clock();
- init();
- animate();
- function init() {
- camera = new THREE.PerspectiveCamera( 25, SCREEN_WIDTH / SCREEN_HEIGHT, 50, 1e7 );
- camera.position.z = radius * 5;
- scene = new THREE.Scene();
- scene.fog = new THREE.FogExp2( 0x000000, 0.00000025 );
- dirLight = new THREE.DirectionalLight( 0xffffff );
- dirLight.position.set( - 1, 0, 1 ).normalize();
- scene.add( dirLight );
- var materialNormalMap = new THREE.MeshPhongMaterial( {
- specular: 0x333333,
- shininess: 15,
- map: textureLoader.load( "textures/planets/earth_atmos_2048.jpg" ),
- specularMap: textureLoader.load( "textures/planets/earth_specular_2048.jpg" ),
- normalMap: textureLoader.load( "textures/planets/earth_normal_2048.jpg" ),
- normalScale: new THREE.Vector2( 0.85, 0.85 )
- } );
- // planet
- geometry = new THREE.SphereBufferGeometry( radius, 100, 50 );
- meshPlanet = new THREE.Mesh( geometry, materialNormalMap );
- meshPlanet.rotation.y = 0;
- meshPlanet.rotation.z = tilt;
- scene.add( meshPlanet );
- // clouds
- var materialClouds = new THREE.MeshLambertMaterial( {
- map: textureLoader.load( "textures/planets/earth_clouds_1024.png" ),
- transparent: true
- } );
- meshClouds = new THREE.Mesh( geometry, materialClouds );
- meshClouds.scale.set( cloudsScale, cloudsScale, cloudsScale );
- meshClouds.rotation.z = tilt;
- scene.add( meshClouds );
- // moon
- var materialMoon = new THREE.MeshPhongMaterial( {
- map: textureLoader.load( "textures/planets/moon_1024.jpg" )
- } );
- meshMoon = new THREE.Mesh( geometry, materialMoon );
- meshMoon.position.set( radius * 5, 0, 0 );
- meshMoon.scale.set( moonScale, moonScale, moonScale );
- scene.add( meshMoon );
- // stars
- var i, r = radius, starsGeometry = [ new THREE.BufferGeometry(), new THREE.BufferGeometry() ];
- var vertices1 = [];
- var vertices2 = [];
- var vertex = new THREE.Vector3();
- for ( i = 0; i < 250; i ++ ) {
- vertex.x = Math.random() * 2 - 1;
- vertex.y = Math.random() * 2 - 1;
- vertex.z = Math.random() * 2 - 1;
- vertex.multiplyScalar( r );
- vertices1.push( vertex.x, vertex.y, vertex.z );
- }
- for ( i = 0; i < 1500; i ++ ) {
- vertex.x = Math.random() * 2 - 1;
- vertex.y = Math.random() * 2 - 1;
- vertex.z = Math.random() * 2 - 1;
- vertex.multiplyScalar( r );
- vertices2.push( vertex.x, vertex.y, vertex.z );
- }
- starsGeometry[ 0 ].setAttribute( 'position', new THREE.Float32BufferAttribute( vertices1, 3 ) );
- starsGeometry[ 1 ].setAttribute( 'position', new THREE.Float32BufferAttribute( vertices2, 3 ) );
- var stars;
- var starsMaterials = [
- new THREE.PointsMaterial( { color: 0x555555, size: 2, sizeAttenuation: false } ),
- new THREE.PointsMaterial( { color: 0x555555, size: 1, sizeAttenuation: false } ),
- new THREE.PointsMaterial( { color: 0x333333, size: 2, sizeAttenuation: false } ),
- new THREE.PointsMaterial( { color: 0x3a3a3a, size: 1, sizeAttenuation: false } ),
- new THREE.PointsMaterial( { color: 0x1a1a1a, size: 2, sizeAttenuation: false } ),
- new THREE.PointsMaterial( { color: 0x1a1a1a, size: 1, sizeAttenuation: false } )
- ];
- for ( i = 10; i < 30; i ++ ) {
- stars = new THREE.Points( starsGeometry[ i % 2 ], starsMaterials[ i % 6 ] );
- stars.rotation.x = Math.random() * 6;
- stars.rotation.y = Math.random() * 6;
- stars.rotation.z = Math.random() * 6;
- stars.scale.setScalar( i * 10 );
- stars.matrixAutoUpdate = false;
- stars.updateMatrix();
- scene.add( stars );
- }
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
- document.body.appendChild( renderer.domElement );
- //
- controls = new FlyControls( camera, renderer.domElement );
- controls.movementSpeed = 1000;
- controls.domElement = renderer.domElement;
- controls.rollSpeed = Math.PI / 24;
- controls.autoForward = false;
- controls.dragToLook = false;
- //
- stats = new Stats();
- document.body.appendChild( stats.dom );
- window.addEventListener( 'resize', onWindowResize, false );
- // postprocessing
- var renderModel = new RenderPass( scene, camera );
- var effectFilm = new FilmPass( 0.35, 0.75, 2048, false );
- composer = new EffectComposer( renderer );
- composer.addPass( renderModel );
- composer.addPass( effectFilm );
- }
- function onWindowResize() {
- SCREEN_HEIGHT = window.innerHeight;
- SCREEN_WIDTH = window.innerWidth;
- camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
- camera.updateProjectionMatrix();
- renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
- composer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- // rotate the planet and clouds
- var delta = clock.getDelta();
- meshPlanet.rotation.y += rotationSpeed * delta;
- meshClouds.rotation.y += 1.25 * rotationSpeed * delta;
- // slow down as we approach the surface
- dPlanet = camera.position.length();
- dMoonVec.subVectors( camera.position, meshMoon.position );
- dMoon = dMoonVec.length();
- if ( dMoon < dPlanet ) {
- d = ( dMoon - radius * moonScale * 1.01 );
- } else {
- d = ( dPlanet - radius * 1.01 );
- }
- controls.movementSpeed = 0.33 * d;
- controls.update( delta );
- composer.render( delta );
- }
- </script>
- </body>
- </html>
|