webgl_trackballcamera_earth.html 7.1 KB

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