123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <!DOCTYPE html>
- <html>
- <title>three.js webgl - trackball 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: green; }
- b { color: green; }
- </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 [trackball camera]<br/><br/>
- <b>MOVE</b> mouse & press <b>LEFT/A:</b> rotate, <b>MIDDLE/S:</b> zoom, <b>RIGHT/D:</b> pan
- </div>
- <script type="text/javascript">
- var radius = 6371,
- tilt = 0.41,
- rotationSpeed = 0.1,
- cloudsScale = 1.005,
- moonScale = 0.23,
- height = window.innerHeight,
- width = window.innerWidth,
- container, stats,
- camera, scene, renderer,
- geometry, meshPlanet, meshClouds, meshMoon,
- dirLight, ambientLight,
- time = new Date().getTime();
- window.onload = function() {
- if ( !Detector.webgl ) {
- Detector.addGetWebGLMessage();
- return;
- }
- init();
- animate();
- }
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- scene = new THREE.Scene();
- renderer = new THREE.WebGLRenderer( { clearAlpha: 1, clearColor: 0x000000 } );
- renderer.setSize( width, height );
- renderer.sortObjects = false;
- renderer.autoClear = false;
- container.appendChild( renderer.domElement );
- camera = new THREE.TrackballCamera({
- fov: 25,
- aspect: width / height,
- near: 50,
- far: 1e7,
- rotateSpeed: 1.0,
- zoomSpeed: 1.2,
- panSpeed: 0.2,
- noZoom: false,
- noPan: false,
- staticMoving: false,
- dynamicDampingFactor: 0.3,
- minDistance: radius * 1.1,
- maxDistance: radius * 100,
- keys: [ 65, 83, 68 ], // [ rotateKey, zoomKey, panKey ],
- domElement: renderer.domElement,
- });
- camera.position.z = radius * 7;
- dirLight = new THREE.DirectionalLight( 0xFFFFFF );
- dirLight.position.set( -1, 0, 1 );
- dirLight.position.normalize();
- scene.addLight( dirLight );
- ambientLight = new THREE.AmbientLight( 0xFFFFFF );
- //scene.addLight( ambientLight );
- var planetTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_atmos_2048.jpg" ),
- cloudsTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_clouds_1024.png" ),
- normalTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_normal_2048.jpg" ),
- specularTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_specular_2048.jpg" ),
- moonTexture = THREE.ImageUtils.loadTexture( "textures/planets/moon_1024.jpg" );
- var shader = THREE.ShaderUtils.lib[ "normal" ],
- 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[ "uAmbientLightColor" ].value = ambientLight.color;
- uniforms[ "uDiffuseColor" ].value.setHex( 0xffffff );
- uniforms[ "uSpecularColor" ].value.setHex( 0xaaaaaa );
- uniforms[ "uAmbientColor" ].value.setHex( 0x000000 );
- uniforms[ "uShininess" ].value = 30;
- var materialNormalMap = new THREE.MeshShaderMaterial({
- fragmentShader: shader.fragmentShader,
- vertexShader: shader.vertexShader,
- uniforms: uniforms
- });
-
- // 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,
- vector,
- starsGeometry = new THREE.Geometry();
- for ( i = 0; i < 1500; i++ ) {
- vector = new THREE.Vector3( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
- vector.multiplyScalar( radius );
- starsGeometry.vertices.push( new THREE.Vertex( vector ) );
- }
- var stars,
- 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, starsMaterials[ i % 6 ] );
- stars.rotation.x = Math.random() * 6;
- stars.rotation.y = Math.random() * 6;
- stars.rotation.z = Math.random() * 6;
- var s = i * 10;
- stars.scale.set( s, s, s );
- stars.matrixAutoUpdate = false;
- stars.updateMatrix();
- scene.addObject( stars );
- }
- 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 ) {
- width = window.innerWidth;
- height = window.innerHeight;
- renderer.setSize( width, height );
- camera.aspect = width / height;
- camera.updateProjectionMatrix();
- camera.screen.width = width;
- camera.screen.height = height;
- camera.radius = ( width + height ) / 4;
- };
- function animate() {
- requestAnimationFrame( animate );
-
- render();
- stats.update();
- };
- function render() {
- var t = new Date().getTime(),
- dt = ( t - time ) / 1000;
- time = t;
- meshPlanet.rotation.y += rotationSpeed * dt;
- meshClouds.rotation.y += 1.25 * rotationSpeed * dt;
- var angle = dt * rotationSpeed;
- meshMoon.position = new THREE.Vector3(
- Math.cos( angle ) * meshMoon.position.x - Math.sin( angle ) * meshMoon.position.z,
- 0,
- Math.sin( angle ) * meshMoon.position.x + Math.cos( angle ) * meshMoon.position.z
- );
- meshMoon.rotation.y -= angle;
- renderer.clear();
- renderer.render( scene, camera );
- };
- </script>
- </body>
- </html>
|