webgl_trackballcamera_earth.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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, 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.TrackballCamera({
  70. fov: 25,
  71. aspect: width / height,
  72. near: 50,
  73. far: 1e7,
  74. rotateSpeed: 1.0,
  75. zoomSpeed: 1.2,
  76. panSpeed: 0.2,
  77. noZoom: false,
  78. noPan: false,
  79. staticMoving: false,
  80. dynamicDampingFactor: 0.3,
  81. minDistance: radius * 1.1,
  82. maxDistance: radius * 100,
  83. keys: [ 65, 83, 68 ], // [ rotateKey, zoomKey, panKey ],
  84. domElement: renderer.domElement,
  85. });
  86. camera.position.z = radius * 7;
  87. dirLight = new THREE.DirectionalLight( 0xFFFFFF );
  88. dirLight.position.set( -1, 0, 1 );
  89. dirLight.position.normalize();
  90. scene.add( dirLight );
  91. var planetTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_atmos_2048.jpg" ),
  92. cloudsTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_clouds_1024.png" ),
  93. normalTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_normal_2048.jpg" ),
  94. specularTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_specular_2048.jpg" ),
  95. moonTexture = THREE.ImageUtils.loadTexture( "textures/planets/moon_1024.jpg" );
  96. var shader = THREE.ShaderUtils.lib[ "normal" ],
  97. uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  98. uniforms[ "tNormal" ].texture = normalTexture;
  99. uniforms[ "uNormalScale" ].value = 0.85;
  100. uniforms[ "tDiffuse" ].texture = planetTexture;
  101. uniforms[ "tSpecular" ].texture = specularTexture;
  102. uniforms[ "enableAO" ].value = false;
  103. uniforms[ "enableDiffuse" ].value = true;
  104. uniforms[ "enableSpecular" ].value = true;
  105. uniforms[ "uDiffuseColor" ].value.setHex( 0xffffff );
  106. uniforms[ "uSpecularColor" ].value.setHex( 0xaaaaaa );
  107. uniforms[ "uAmbientColor" ].value.setHex( 0x000000 );
  108. uniforms[ "uShininess" ].value = 30;
  109. var materialNormalMap = new THREE.ShaderMaterial({
  110. fragmentShader: shader.fragmentShader,
  111. vertexShader: shader.vertexShader,
  112. uniforms: uniforms,
  113. lights: true
  114. });
  115. // planet
  116. geometry = new THREE.SphereGeometry( radius, 100, 50 );
  117. geometry.computeTangents();
  118. meshPlanet = new THREE.Mesh( geometry, materialNormalMap );
  119. meshPlanet.rotation.y = 1.3;
  120. meshPlanet.rotation.z = tilt;
  121. scene.add( meshPlanet );
  122. // clouds
  123. var materialClouds = new THREE.MeshLambertMaterial( { color: 0xffffff, map: cloudsTexture, transparent:true } );
  124. meshClouds = new THREE.Mesh( geometry, materialClouds );
  125. meshClouds.scale.set( cloudsScale, cloudsScale, cloudsScale );
  126. meshClouds.rotation.z = tilt;
  127. scene.add( meshClouds );
  128. // moon
  129. var materialMoon = new THREE.MeshPhongMaterial( { color: 0xffffff, map: moonTexture } );
  130. meshMoon = new THREE.Mesh( geometry, materialMoon );
  131. meshMoon.position.set( radius * 5, 0, 0 );
  132. meshMoon.scale.set( moonScale, moonScale, moonScale );
  133. scene.add( meshMoon );
  134. // stars
  135. var i,
  136. vector,
  137. starsGeometry = new THREE.Geometry();
  138. for ( i = 0; i < 1500; i++ ) {
  139. vector = new THREE.Vector3( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  140. vector.multiplyScalar( radius );
  141. starsGeometry.vertices.push( new THREE.Vertex( vector ) );
  142. }
  143. var stars,
  144. starsMaterials = [
  145. new THREE.ParticleBasicMaterial( { color: 0x555555, size: 2, sizeAttenuation: false } ),
  146. new THREE.ParticleBasicMaterial( { color: 0x555555, size: 1, sizeAttenuation: false } ),
  147. new THREE.ParticleBasicMaterial( { color: 0x333333, size: 2, sizeAttenuation: false } ),
  148. new THREE.ParticleBasicMaterial( { color: 0x3a3a3a, size: 1, sizeAttenuation: false } ),
  149. new THREE.ParticleBasicMaterial( { color: 0x1a1a1a, size: 2, sizeAttenuation: false } ),
  150. new THREE.ParticleBasicMaterial( { color: 0x1a1a1a, size: 1, sizeAttenuation: false } )
  151. ];
  152. for ( i = 10; i < 30; i++ ) {
  153. stars = new THREE.ParticleSystem( starsGeometry, starsMaterials[ i % 6 ] );
  154. stars.rotation.x = Math.random() * 6;
  155. stars.rotation.y = Math.random() * 6;
  156. stars.rotation.z = Math.random() * 6;
  157. var s = i * 10;
  158. stars.scale.set( s, s, s );
  159. stars.matrixAutoUpdate = false;
  160. stars.updateMatrix();
  161. scene.add( stars );
  162. }
  163. stats = new Stats();
  164. stats.domElement.style.position = 'absolute';
  165. stats.domElement.style.top = '0px';
  166. stats.domElement.style.zIndex = 100;
  167. container.appendChild( stats.domElement );
  168. window.addEventListener( 'resize', onWindowResize, false );
  169. };
  170. function onWindowResize( event ) {
  171. width = window.innerWidth;
  172. height = window.innerHeight;
  173. renderer.setSize( width, height );
  174. camera.aspect = width / height;
  175. camera.updateProjectionMatrix();
  176. camera.screen.width = width;
  177. camera.screen.height = height;
  178. camera.radius = ( width + height ) / 4;
  179. };
  180. function animate() {
  181. requestAnimationFrame( animate );
  182. render();
  183. stats.update();
  184. };
  185. function render() {
  186. var t = new Date().getTime(),
  187. dt = ( t - time ) / 1000;
  188. time = t;
  189. meshPlanet.rotation.y += rotationSpeed * dt;
  190. meshClouds.rotation.y += 1.25 * rotationSpeed * dt;
  191. var angle = dt * rotationSpeed;
  192. meshMoon.position = new THREE.Vector3(
  193. Math.cos( angle ) * meshMoon.position.x - Math.sin( angle ) * meshMoon.position.z,
  194. 0,
  195. Math.sin( angle ) * meshMoon.position.x + Math.cos( angle ) * meshMoon.position.z
  196. );
  197. meshMoon.rotation.y -= angle;
  198. renderer.clear();
  199. renderer.render( scene, camera );
  200. };
  201. </script>
  202. </body>
  203. </html>