webgl_trackballcamera_earth.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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[ "uDirLightPos" ].value = dirLight.position;
  105. uniforms[ "uDirLightColor" ].value = dirLight.color;
  106. uniforms[ "uAmbientLightColor" ].value = ambientLight.color;
  107. uniforms[ "uDiffuseColor" ].value.setHex( 0xffffff );
  108. uniforms[ "uSpecularColor" ].value.setHex( 0xaaaaaa );
  109. uniforms[ "uAmbientColor" ].value.setHex( 0x000000 );
  110. uniforms[ "uShininess" ].value = 30;
  111. var materialNormalMap = new THREE.MeshShaderMaterial({
  112. fragmentShader: shader.fragmentShader,
  113. vertexShader: shader.vertexShader,
  114. uniforms: uniforms
  115. });
  116. // planet
  117. geometry = new THREE.Sphere( radius, 100, 50 );
  118. geometry.computeTangents();
  119. meshPlanet = new THREE.Mesh( geometry, materialNormalMap );
  120. meshPlanet.rotation.y = 1.3;
  121. meshPlanet.rotation.z = tilt;
  122. scene.addObject( meshPlanet );
  123. // clouds
  124. var materialClouds = new THREE.MeshLambertMaterial( { color: 0xffffff, map: cloudsTexture, transparent:true } );
  125. meshClouds = new THREE.Mesh( geometry, materialClouds );
  126. meshClouds.scale.set( cloudsScale, cloudsScale, cloudsScale );
  127. meshClouds.rotation.z = tilt;
  128. scene.addObject( meshClouds );
  129. // moon
  130. var materialMoon = new THREE.MeshPhongMaterial( { color: 0xffffff, map: moonTexture } );
  131. meshMoon = new THREE.Mesh( geometry, materialMoon );
  132. meshMoon.position.set( radius * 5, 0, 0 );
  133. meshMoon.scale.set( moonScale, moonScale, moonScale );
  134. scene.addObject( meshMoon );
  135. // stars
  136. var i,
  137. vector,
  138. starsGeometry = new THREE.Geometry();
  139. for ( i = 0; i < 1500; i++ ) {
  140. vector = new THREE.Vector3( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  141. vector.multiplyScalar( radius );
  142. starsGeometry.vertices.push( new THREE.Vertex( vector ) );
  143. }
  144. var stars,
  145. starsMaterials = [
  146. new THREE.ParticleBasicMaterial( { color: 0x555555, size: 2, sizeAttenuation: false } ),
  147. new THREE.ParticleBasicMaterial( { color: 0x555555, size: 1, sizeAttenuation: false } ),
  148. new THREE.ParticleBasicMaterial( { color: 0x333333, size: 2, sizeAttenuation: false } ),
  149. new THREE.ParticleBasicMaterial( { color: 0x3a3a3a, size: 1, sizeAttenuation: false } ),
  150. new THREE.ParticleBasicMaterial( { color: 0x1a1a1a, size: 2, sizeAttenuation: false } ),
  151. new THREE.ParticleBasicMaterial( { color: 0x1a1a1a, size: 1, sizeAttenuation: false } )
  152. ];
  153. for ( i = 10; i < 30; i++ ) {
  154. stars = new THREE.ParticleSystem( starsGeometry, starsMaterials[ i % 6 ] );
  155. stars.rotation.x = Math.random() * 6;
  156. stars.rotation.y = Math.random() * 6;
  157. stars.rotation.z = Math.random() * 6;
  158. var s = i * 10;
  159. stars.scale.set( s, s, s );
  160. stars.matrixAutoUpdate = false;
  161. stars.updateMatrix();
  162. scene.addObject( stars );
  163. }
  164. stats = new Stats();
  165. stats.domElement.style.position = 'absolute';
  166. stats.domElement.style.top = '0px';
  167. stats.domElement.style.zIndex = 100;
  168. container.appendChild( stats.domElement );
  169. window.addEventListener( 'resize', onWindowResize, false );
  170. };
  171. function onWindowResize( event ) {
  172. width = window.innerWidth;
  173. height = window.innerHeight;
  174. renderer.setSize( width, height );
  175. camera.aspect = width / height;
  176. camera.updateProjectionMatrix();
  177. camera.screen.width = width;
  178. camera.screen.height = height;
  179. camera.radius = ( width + height ) / 4;
  180. };
  181. function animate() {
  182. requestAnimationFrame( animate );
  183. render();
  184. stats.update();
  185. };
  186. function render() {
  187. var t = new Date().getTime(),
  188. dt = ( t - time ) / 1000;
  189. time = t;
  190. meshPlanet.rotation.y += rotationSpeed * dt;
  191. meshClouds.rotation.y += 1.25 * rotationSpeed * dt;
  192. var angle = dt * rotationSpeed;
  193. meshMoon.position = new THREE.Vector3(
  194. Math.cos( angle ) * meshMoon.position.x - Math.sin( angle ) * meshMoon.position.z,
  195. 0,
  196. Math.sin( angle ) * meshMoon.position.x + Math.cos( angle ) * meshMoon.position.z
  197. );
  198. meshMoon.rotation.y -= angle;
  199. renderer.clear();
  200. renderer.render( scene, camera );
  201. };
  202. </script>
  203. </body>
  204. </html>