webgl_trackballcamera_earth.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - trackball camera - 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. <style>
  8. body {
  9. background: #000;
  10. color: #EEE;
  11. padding: 0;
  12. margin: 0;
  13. font-weight: bold;
  14. overflow: hidden;
  15. font-family: Monospace;
  16. font-size: 13px;
  17. text-align: center;
  18. }
  19. #info {
  20. position: absolute;
  21. top: 0px;
  22. width: 100%;
  23. padding: 5px;
  24. z-index: 100;
  25. }
  26. a { color: green; }
  27. b { color: green; }
  28. </style>
  29. <script src="../build/Three.js"></script>
  30. <script src="js/Detector.js"></script>
  31. <script src="js/RequestAnimationFrame.js"></script>
  32. <script src="js/Stats.js"></script>
  33. </head>
  34. <body>
  35. <div id="info">
  36. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - earth [trackball camera]<br/><br/>
  37. <b>MOVE</b> mouse &amp; press <b>LEFT/A:</b> rotate, <b>MIDDLE/S:</b> zoom, <b>RIGHT/D:</b> pan
  38. </div>
  39. <script>
  40. var radius = 6371,
  41. tilt = 0.41,
  42. rotationSpeed = 0.1,
  43. cloudsScale = 1.005,
  44. moonScale = 0.23,
  45. height = window.innerHeight,
  46. width = window.innerWidth,
  47. container, stats,
  48. camera, controls, scene, renderer,
  49. geometry, meshPlanet, meshClouds, meshMoon,
  50. dirLight, ambientLight,
  51. time = new Date().getTime();
  52. window.onload = function() {
  53. if ( !Detector.webgl ) {
  54. Detector.addGetWebGLMessage();
  55. return;
  56. }
  57. init();
  58. animate();
  59. }
  60. function init() {
  61. container = document.createElement( 'div' );
  62. document.body.appendChild( container );
  63. scene = new THREE.Scene();
  64. renderer = new THREE.WebGLRenderer( { clearAlpha: 1, clearColor: 0x000000 } );
  65. renderer.setSize( width, height );
  66. renderer.sortObjects = false;
  67. renderer.autoClear = false;
  68. container.appendChild( renderer.domElement );
  69. camera = new THREE.PerspectiveCamera( 25, width / height, 50, 1e7 );
  70. camera.position.z = radius * 7;
  71. controls = new THREE.TrackballControls( camera, renderer.domElement );
  72. controls.rotateSpeed = 1.0;
  73. controls.zoomSpeed = 1.2;
  74. controls.panSpeed = 0.2;
  75. controls.noZoom = false;
  76. controls.noPan = false;
  77. controls.staticMoving = false;
  78. controls.dynamicDampingFactor = 0.3;
  79. controls.minDistance = radius * 1.1;
  80. controls.maxDistance = radius * 100;
  81. controls.keys = [ 65, 83, 68 ]; // [ rotateKey, zoomKey, panKey ]
  82. dirLight = new THREE.DirectionalLight( 0xFFFFFF );
  83. dirLight.position.set( -1, 0, 1 ).normalize();
  84. scene.add( dirLight );
  85. var planetTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_atmos_2048.jpg" ),
  86. cloudsTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_clouds_1024.png" ),
  87. normalTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_normal_2048.jpg" ),
  88. specularTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_specular_2048.jpg" ),
  89. moonTexture = THREE.ImageUtils.loadTexture( "textures/planets/moon_1024.jpg" );
  90. var shader = THREE.ShaderUtils.lib[ "normal" ],
  91. uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  92. uniforms[ "tNormal" ].texture = normalTexture;
  93. uniforms[ "uNormalScale" ].value = 0.85;
  94. uniforms[ "tDiffuse" ].texture = planetTexture;
  95. uniforms[ "tSpecular" ].texture = specularTexture;
  96. uniforms[ "enableAO" ].value = false;
  97. uniforms[ "enableDiffuse" ].value = true;
  98. uniforms[ "enableSpecular" ].value = true;
  99. uniforms[ "uDiffuseColor" ].value.setHex( 0xffffff );
  100. uniforms[ "uSpecularColor" ].value.setHex( 0x333333 );
  101. uniforms[ "uAmbientColor" ].value.setHex( 0x000000 );
  102. uniforms[ "uShininess" ].value = 15;
  103. var materialNormalMap = new THREE.ShaderMaterial({
  104. fragmentShader: shader.fragmentShader,
  105. vertexShader: shader.vertexShader,
  106. uniforms: uniforms,
  107. lights: true
  108. });
  109. // planet
  110. geometry = new THREE.SphereGeometry( radius, 100, 50 );
  111. geometry.computeTangents();
  112. meshPlanet = new THREE.Mesh( geometry, materialNormalMap );
  113. meshPlanet.rotation.y = 1.3;
  114. meshPlanet.rotation.z = tilt;
  115. scene.add( meshPlanet );
  116. // clouds
  117. var materialClouds = new THREE.MeshLambertMaterial( { color: 0xffffff, map: cloudsTexture, transparent:true } );
  118. meshClouds = new THREE.Mesh( geometry, materialClouds );
  119. meshClouds.scale.set( cloudsScale, cloudsScale, cloudsScale );
  120. meshClouds.rotation.z = tilt;
  121. scene.add( meshClouds );
  122. // moon
  123. var materialMoon = new THREE.MeshPhongMaterial( { color: 0xffffff, map: moonTexture } );
  124. meshMoon = new THREE.Mesh( geometry, materialMoon );
  125. meshMoon.position.set( radius * 5, 0, 0 );
  126. meshMoon.scale.set( moonScale, moonScale, moonScale );
  127. scene.add( meshMoon );
  128. // stars
  129. var i,
  130. vector,
  131. starsGeometry = new THREE.Geometry();
  132. for ( i = 0; i < 1500; i++ ) {
  133. vector = new THREE.Vector3( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  134. vector.multiplyScalar( radius );
  135. starsGeometry.vertices.push( new THREE.Vertex( vector ) );
  136. }
  137. var stars,
  138. starsMaterials = [
  139. new THREE.ParticleBasicMaterial( { color: 0x555555, size: 2, sizeAttenuation: false } ),
  140. new THREE.ParticleBasicMaterial( { color: 0x555555, size: 1, sizeAttenuation: false } ),
  141. new THREE.ParticleBasicMaterial( { color: 0x333333, size: 2, sizeAttenuation: false } ),
  142. new THREE.ParticleBasicMaterial( { color: 0x3a3a3a, size: 1, sizeAttenuation: false } ),
  143. new THREE.ParticleBasicMaterial( { color: 0x1a1a1a, size: 2, sizeAttenuation: false } ),
  144. new THREE.ParticleBasicMaterial( { color: 0x1a1a1a, size: 1, sizeAttenuation: false } )
  145. ];
  146. for ( i = 10; i < 30; i++ ) {
  147. stars = new THREE.ParticleSystem( starsGeometry, starsMaterials[ i % 6 ] );
  148. stars.rotation.x = Math.random() * 6;
  149. stars.rotation.y = Math.random() * 6;
  150. stars.rotation.z = Math.random() * 6;
  151. var s = i * 10;
  152. stars.scale.set( s, s, s );
  153. stars.matrixAutoUpdate = false;
  154. stars.updateMatrix();
  155. scene.add( stars );
  156. }
  157. stats = new Stats();
  158. stats.domElement.style.position = 'absolute';
  159. stats.domElement.style.top = '0px';
  160. stats.domElement.style.zIndex = 100;
  161. container.appendChild( stats.domElement );
  162. window.addEventListener( 'resize', onWindowResize, false );
  163. };
  164. function onWindowResize( event ) {
  165. width = window.innerWidth;
  166. height = window.innerHeight;
  167. renderer.setSize( width, height );
  168. camera.aspect = width / height;
  169. camera.updateProjectionMatrix();
  170. controls.screen.width = width;
  171. controls.screen.height = height;
  172. camera.radius = ( width + height ) / 4;
  173. };
  174. function animate() {
  175. requestAnimationFrame( animate );
  176. render();
  177. stats.update();
  178. };
  179. function render() {
  180. var t = new Date().getTime(),
  181. dt = ( t - time ) / 1000;
  182. time = t;
  183. meshPlanet.rotation.y += rotationSpeed * dt;
  184. meshClouds.rotation.y += 1.25 * rotationSpeed * dt;
  185. var angle = dt * rotationSpeed;
  186. meshMoon.position = new THREE.Vector3(
  187. Math.cos( angle ) * meshMoon.position.x - Math.sin( angle ) * meshMoon.position.z,
  188. 0,
  189. Math.sin( angle ) * meshMoon.position.x + Math.cos( angle ) * meshMoon.position.z
  190. );
  191. meshMoon.rotation.y -= angle;
  192. controls.update();
  193. renderer.clear();
  194. renderer.render( scene, camera );
  195. };
  196. </script>
  197. </body>
  198. </html>