123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- <!DOCTYPE html>
- <html>
- <title>three.js webgl - fly camera - earth</title>
- <style type="text/css">
- body {
- background:#000;
- color: #eee;
- padding:0;
- margin:0;
- font-weight:bold;
- overflow:hidden;
- font-family:Monospace;
- font-size:13px;
- text-align:center;
- }
- #info {
- position: absolute;
- top: 0px; width: 100%;
- padding: 5px;
- z-index:100;
- }
- a {
- color: #0080ff;
- }
-
- b { color:orange }
-
- </style>
- <script type="text/javascript" src="../build/Three.js"></script>
- <script type="text/javascript" src="js/Detector.js"></script>
- <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
- <script type="text/javascript" src="js/Stats.js"></script>
- </head>
- <body>
- <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - earth [fly camera]<br/><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<br/>
- </div>
- <script type="text/javascript">
- if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
- 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 ENABLE_LENSFLARES = false;
- var lensFlare, lensFlareRotate;
-
- var container, stats;
- var camera, scene, sceneCube, renderer;
- var geometry, meshPlanet, meshClouds, meshMoon;
- var dirLight, pointLight, ambientLight;
- var lastUpdate = new Date().getTime();
- var t, d, dPlanet, dMoon, dMoonVec = new THREE.Vector3();
- var postprocessing = { enabled : true, bloom: false };
-
- init();
- animate();
- function init() {
-
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.FlyCamera({
-
- fov: 25,
- aspect: SCREEN_WIDTH / SCREEN_HEIGHT,
- movementSpeed: 1000,
- domElement: container,
- rollSpeed: Math.PI / 24,
- autoForward: false,
- dragToLook: false,
- near: 50,
- far: 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 );
- dirLight.position.normalize();
- scene.addLight( dirLight );
- pointLight = new THREE.PointLight( 0x000000 );
- pointLight.position.set( -5000, 0, 5000 );
- //scene.addLight( pointLight );
-
- ambientLight = new THREE.AmbientLight( 0x000000 );
- scene.addLight( ambientLight );
- var planetTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_atmos_2048.jpg" );
- var cloudsTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_clouds_1024.png" );
- var normalTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_normal_2048.jpg" );
- var specularTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_specular_2048.jpg" );
- var moonTexture = THREE.ImageUtils.loadTexture( "textures/planets/moon_1024.jpg" );
- var shader = THREE.ShaderUtils.lib[ "normal" ];
- var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
- uniforms[ "tNormal" ].texture = normalTexture;
- uniforms[ "uNormalScale" ].value = 0.85;
- uniforms[ "tDiffuse" ].texture = planetTexture;
- uniforms[ "tSpecular" ].texture = specularTexture;
- uniforms[ "enableAO" ].value = false;
- uniforms[ "enableDiffuse" ].value = true;
- uniforms[ "enableSpecular" ].value = true;
- uniforms[ "uDirLightPos" ].value = dirLight.position;
- uniforms[ "uDirLightColor" ].value = dirLight.color;
- uniforms[ "uPointLightPos" ].value = pointLight.position;
- uniforms[ "uPointLightColor" ].value = pointLight.color;
- uniforms[ "uAmbientLightColor" ].value = ambientLight.color;
- uniforms[ "uDiffuseColor" ].value.setHex( 0xffffff );
- uniforms[ "uSpecularColor" ].value.setHex( 0xaaaaaa );
- uniforms[ "uAmbientColor" ].value.setHex( 0x000000 );
- uniforms[ "uShininess" ].value = 30;
- var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms };
- var materialNormalMap = new THREE.MeshShaderMaterial( parameters );
- // planet
-
- geometry = new THREE.Sphere( radius, 100, 50 );
- geometry.computeTangents();
- meshPlanet = new THREE.Mesh( geometry, materialNormalMap );
- meshPlanet.rotation.y = 1.3;
- meshPlanet.rotation.z = tilt;
- scene.addObject( meshPlanet );
- // clouds
- var materialClouds = new THREE.MeshLambertMaterial( { color: 0xffffff, map: cloudsTexture, transparent:true } );
- meshClouds = new THREE.Mesh( geometry, materialClouds );
- meshClouds.scale.set( cloudsScale, cloudsScale, cloudsScale );
- meshClouds.rotation.z = tilt;
- scene.addObject( meshClouds );
- // moon
- var materialMoon = new THREE.MeshPhongMaterial( { color: 0xffffff, map: moonTexture } );
-
- meshMoon = new THREE.Mesh( geometry, materialMoon );
- meshMoon.position.set( radius * 5, 0, 0 );
- meshMoon.scale.set( moonScale, moonScale, moonScale );
- scene.addObject( meshMoon );
- // stars
-
- var i, r = radius,
- starsGeometry = [ new THREE.Geometry(), new THREE.Geometry() ];
-
- for ( i = 0; i < 250; ++i ) {
- vector1 = new THREE.Vector3( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
- vector1.multiplyScalar( r );
- starsGeometry[ 0 ].vertices.push( new THREE.Vertex( vector1 ) );
- }
- for ( i = 0; i < 1500; ++i ) {
- vector1 = new THREE.Vector3( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
- vector1.multiplyScalar( r );
- starsGeometry[ 1 ].vertices.push( new THREE.Vertex( vector1 ) );
- }
- var stars;
- var starsMaterials = [ new THREE.ParticleBasicMaterial( { color: 0x555555, size: 2, sizeAttenuation: false } ),
- new THREE.ParticleBasicMaterial( { color: 0x555555, size: 1, sizeAttenuation: false } ),
- new THREE.ParticleBasicMaterial( { color: 0x333333, size: 2, sizeAttenuation: false } ),
- new THREE.ParticleBasicMaterial( { color: 0x3a3a3a, size: 1, sizeAttenuation: false } ),
- new THREE.ParticleBasicMaterial( { color: 0x1a1a1a, size: 2, sizeAttenuation: false } ),
- new THREE.ParticleBasicMaterial( { color: 0x1a1a1a, size: 1, sizeAttenuation: false } )
- ];
-
- for ( i = 10; i < 30; i ++) {
-
- stars = new THREE.ParticleSystem( starsGeometry[ i % 2 ], starsMaterials[ i % 6 ] );
- stars.rotation.x = Math.random() * 6;
- stars.rotation.y = Math.random() * 6;
- stars.rotation.z = Math.random() * 6;
-
- s = i * 10;
- stars.scale.set( s, s, s );
-
- stars.matrixAutoUpdate = false;
- stars.updateMatrix();
-
- scene.addObject( stars );
-
- }
- if ( ENABLE_LENSFLARES ) {
-
- lensFlare = new THREE.LensFlare( THREE.ImageUtils.loadTexture( "textures/lensflare/lensflare0.png" ), 700, 0.0, THREE.AdditiveBlending );
-
- lensFlare.add( THREE.ImageUtils.loadTexture( "textures/lensflare/lensflare2.png" ), 512, 0.0, THREE.AdditiveBlending );
- lensFlare.add( lensFlare.lensFlares[ 1 ].texture, 512, 0.0, THREE.AdditiveBlending );
- lensFlare.add( lensFlare.lensFlares[ 1 ].texture, 512, 0.0, THREE.AdditiveBlending );
-
- lensFlare.add( THREE.ImageUtils.loadTexture( "textures/lensflare/lensflare3.png" ), 60, 0.6, THREE.AdditiveBlending );
- lensFlare.add( lensFlare.lensFlares[ 4 ].texture, 70, 0.7, THREE.AdditiveBlending );
- lensFlare.add( lensFlare.lensFlares[ 4 ].texture, 120, 0.9, THREE.AdditiveBlending );
- lensFlare.add( lensFlare.lensFlares[ 4 ].texture, 70, 1.0, THREE.AdditiveBlending );
-
- lensFlare.customUpdateCallback = lensFlareUpdateCallback;
- lensFlare.position.set( 0, 0, -99000 );
-
-
- lensFlareRotate = new THREE.Object3D();
- lensFlareRotate.addChild( lensFlare );
-
- scene.addChild( lensFlareRotate );
-
- lensFlareRotate.rotation.x = Math.PI;
- lensFlareRotate.rotation.y = Math.PI / 2;
-
- }
- renderer = new THREE.WebGLRenderer( { clearAlpha: 1, clearColor: 0x000000 } );
- renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
- renderer.sortObjects = false;
- initPostprocessing();
- renderer.autoClear = false;
- container.appendChild( renderer.domElement );
- stats = new Stats();
- stats.domElement.style.position = 'absolute';
- stats.domElement.style.top = '0px';
- stats.domElement.style.zIndex = 100;
- //container.appendChild( stats.domElement );
- window.addEventListener( 'resize', onWindowResize, false );
- };
- function onWindowResize( event ) {
- SCREEN_HEIGHT = window.innerHeight;
- SCREEN_WIDTH = window.innerWidth;
- renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
- camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
- camera.updateProjectionMatrix();
- initPostprocessing();
- }
- function initPostprocessing() {
- postprocessing.scene = new THREE.Scene();
- postprocessing.camera = new THREE.Camera();
- postprocessing.camera.projectionMatrix = THREE.Matrix4.makeOrtho( SCREEN_WIDTH / - 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / - 2, -10000, 10000 );
- postprocessing.camera.position.z = 100;
- var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat };
- postprocessing.rtTexture1 = new THREE.WebGLRenderTarget( SCREEN_WIDTH, SCREEN_HEIGHT, pars );
- postprocessing.rtTexture2 = new THREE.WebGLRenderTarget( 512, 512, pars );
- postprocessing.rtTexture3 = new THREE.WebGLRenderTarget( 512, 512, pars );
- var screen_shader = THREE.ShaderUtils.lib["screen"];
- var screen_uniforms = THREE.UniformsUtils.clone( screen_shader.uniforms );
- screen_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
- screen_uniforms["opacity"].value = 1.0;
- postprocessing.materialScreen = new THREE.MeshShaderMaterial( {
- uniforms: screen_uniforms,
- vertexShader: screen_shader.vertexShader,
- fragmentShader: screen_shader.fragmentShader,
- blending: THREE.AdditiveBlending,
- transparent: true
- } );
- var convolution_shader = THREE.ShaderUtils.lib["convolution"];
- var convolution_uniforms = THREE.UniformsUtils.clone( convolution_shader.uniforms );
- postprocessing.blurx = new THREE.Vector2( 0.001953125, 0.0 ),
- postprocessing.blury = new THREE.Vector2( 0.0, 0.001953125 );
- convolution_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
- convolution_uniforms["uImageIncrement"].value = postprocessing.blurx;
- convolution_uniforms["cKernel"].value = THREE.ShaderUtils.buildKernel( 4.0 );
- postprocessing.materialConvolution = new THREE.MeshShaderMaterial( {
- uniforms: convolution_uniforms,
- vertexShader: "#define KERNEL_SIZE 25.0\n" + convolution_shader.vertexShader,
- fragmentShader: "#define KERNEL_SIZE 25\n" + convolution_shader.fragmentShader
- } );
- var film_shader = THREE.ShaderUtils.lib["film"];
- var film_uniforms = THREE.UniformsUtils.clone( film_shader.uniforms );
- film_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
- postprocessing.materialFilm = new THREE.MeshShaderMaterial( { uniforms: film_uniforms, vertexShader: film_shader.vertexShader, fragmentShader: film_shader.fragmentShader } );
- postprocessing.materialFilm.uniforms.grayscale.value = 0;
- postprocessing.materialFilm.uniforms.nIntensity.value = 0.35;
- postprocessing.materialFilm.uniforms.sIntensity.value = 0.75;
- postprocessing.materialFilm.uniforms.sCount.value = 2048;
- postprocessing.quad = new THREE.Mesh( new THREE.Plane( SCREEN_WIDTH, SCREEN_HEIGHT ), postprocessing.materialConvolution );
- postprocessing.quad.position.z = - 500;
- postprocessing.scene.addObject( postprocessing.quad );
- };
- function cap_bottom( val, bottom ) {
-
- return val < bottom ? bottom : val;
- };
-
- function lensFlareUpdateCallback( object ) {
- var f, fl = object.lensFlares.length;
- var flare;
- var vecX = -object.positionScreen.x * 2;
- var vecY = -object.positionScreen.y * 2;
- for( f = 0; f < fl; f++ ) {
-
- flare = object.lensFlares[ f ];
-
- flare.x = object.positionScreen.x + vecX * flare.distance;
- flare.y = object.positionScreen.y + vecY * flare.distance;
- flare.rotation = 0;
-
- flare.opacity = cap_bottom( 1 - 0.01 * d / radius, 0 );
- }
- // hard coded stuff
- object.lensFlares[ 2 ].y += 0.025;
- object.lensFlares[ 3 ].rotation = object.positionScreen.x * 0.5 + 45 * Math.PI / 180;
- };
- function animate() {
- requestAnimationFrame( animate );
-
- render();
- stats.update();
- };
-
- function cap( val, bottom ) {
-
- return val > bottom ? val : bottom;
- };
- function render() {
- // rotate the planet and clouds
- t = this.getFrametime();
- meshPlanet.rotation.y += rotationSpeed * t;
- meshClouds.rotation.y += 1.25 * rotationSpeed * t;
-
- // slow down as we approach the surface
- dPlanet = camera.position.length();
- dMoonVec.sub( camera.position, meshMoon.position );
- dMoon = dMoonVec.length();
-
- if ( dMoon < dPlanet ) {
- d = ( dMoon - radius * moonScale * 1.01 );
- } else {
-
- d = ( dPlanet - radius * 1.01 );
- }
-
- camera.movementSpeed = 0.33 * d;
- if ( ENABLE_LENSFLARES ) {
-
- lensFlareRotate.position.set( camera.position.x, camera.position.y, camera.position.z );
-
- }
-
- if ( postprocessing.enabled ) {
- renderer.clear();
- // Render scene into texture
- renderer.render( scene, camera, postprocessing.rtTexture1, true );
- if ( postprocessing.bloom ) {
-
- // Render quad with blured scene into texture (convolution pass 1)
- postprocessing.quad.materials = [ postprocessing.materialConvolution ];
- postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
- postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blurx;
- renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture2, true );
- // Render quad with blured scene into texture (convolution pass 2)
- postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture2;
- postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blury;
- renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture3, true );
- // Render original scene with superimposed blur to texture
- postprocessing.quad.materials = [ postprocessing.materialScreen ];
- postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture3;
- postprocessing.materialScreen.uniforms.opacity.value = 0.75;
- renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture1, false );
- }
-
- // Render to screen
- postprocessing.materialFilm.uniforms.time.value += 0.01;
- postprocessing.quad.materials = [ postprocessing.materialFilm ];
- postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
- renderer.render( postprocessing.scene, postprocessing.camera );
- } else {
- renderer.clear();
- renderer.render( scene, camera );
- }
- };
-
- function getFrametime() {
- var now = new Date().getTime();
- var tdiff = ( now - lastUpdate ) / 1000;
- lastUpdate = now;
- return tdiff;
- };
- </script>
- </body>
- </html>
|