webgl_trackballcamera_earth.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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.min.js"></script>
  30. <script src="js/Detector.js"></script>
  31. <script src="js/Stats.js"></script>
  32. </head>
  33. <body>
  34. <div id="info">
  35. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - earth [trackball camera]<br/><br/>
  36. <b>MOVE</b> mouse &amp; press <b>LEFT/A:</b> rotate, <b>MIDDLE/S:</b> zoom, <b>RIGHT/D:</b> pan
  37. </div>
  38. <script>
  39. var radius = 6371,
  40. tilt = 0.41,
  41. rotationSpeed = 0.1,
  42. cloudsScale = 1.005,
  43. moonScale = 0.23,
  44. height = window.innerHeight,
  45. width = window.innerWidth,
  46. container, stats,
  47. camera, controls, scene, renderer,
  48. geometry, meshPlanet, meshClouds, meshMoon,
  49. dirLight, ambientLight,
  50. clock = new THREE.Clock();
  51. window.onload = function() {
  52. if ( !Detector.webgl ) {
  53. Detector.addGetWebGLMessage();
  54. return;
  55. }
  56. init();
  57. animate();
  58. }
  59. function init() {
  60. container = document.createElement( 'div' );
  61. document.body.appendChild( container );
  62. scene = new THREE.Scene();
  63. renderer = new THREE.WebGLRenderer( { clearAlpha: 1, clearColor: 0x000000, antialias: true } );
  64. renderer.setSize( width, height );
  65. renderer.sortObjects = false;
  66. renderer.autoClear = false;
  67. //
  68. renderer.gammaInput = true;
  69. renderer.gammaOutput = true;
  70. //
  71. container.appendChild( renderer.domElement );
  72. camera = new THREE.PerspectiveCamera( 25, width / height, 50, 1e7 );
  73. camera.position.z = radius * 7;
  74. controls = new THREE.TrackballControls( camera, renderer.domElement );
  75. controls.rotateSpeed = 1.0;
  76. controls.zoomSpeed = 1.2;
  77. controls.panSpeed = 0.2;
  78. controls.noZoom = false;
  79. controls.noPan = false;
  80. controls.staticMoving = false;
  81. controls.dynamicDampingFactor = 0.3;
  82. controls.minDistance = radius * 1.1;
  83. controls.maxDistance = radius * 100;
  84. controls.keys = [ 65, 83, 68 ]; // [ rotateKey, zoomKey, panKey ]
  85. dirLight = new THREE.DirectionalLight( 0xFFFFFF );
  86. dirLight.position.set( -1, 0, 1 ).normalize();
  87. scene.add( dirLight );
  88. var planetTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_atmos_2048.jpg" ),
  89. cloudsTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_clouds_1024.png" ),
  90. normalTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_normal_2048.jpg" ),
  91. specularTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_specular_2048.jpg" ),
  92. moonTexture = THREE.ImageUtils.loadTexture( "textures/planets/moon_1024.jpg" );
  93. var shader = THREE.ShaderUtils.lib[ "normal" ],
  94. uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  95. uniforms[ "tNormal" ].value = normalTexture;
  96. uniforms[ "uNormalScale" ].value.set( 0.85, 0.85 );
  97. uniforms[ "tDiffuse" ].value = planetTexture;
  98. uniforms[ "tSpecular" ].value = specularTexture;
  99. uniforms[ "enableAO" ].value = false;
  100. uniforms[ "enableDiffuse" ].value = true;
  101. uniforms[ "enableSpecular" ].value = true;
  102. uniforms[ "uDiffuseColor" ].value.setHex( 0xffffff );
  103. uniforms[ "uSpecularColor" ].value.setHex( 0x666666 );
  104. uniforms[ "uAmbientColor" ].value.setHex( 0x000000 );
  105. uniforms[ "uShininess" ].value = 20;
  106. uniforms[ "uDiffuseColor" ].value.convertGammaToLinear();
  107. uniforms[ "uSpecularColor" ].value.convertGammaToLinear();
  108. uniforms[ "uAmbientColor" ].value.convertGammaToLinear();
  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 = 0;
  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 starsGeometry = new THREE.Geometry();
  136. for ( var i = 0; i < 1500; i ++ ) {
  137. var vertex = new THREE.Vector3();
  138. vertex.x = Math.random() * 2 - 1;
  139. vertex.y = Math.random() * 2 - 1;
  140. vertex.z = Math.random() * 2 - 1;
  141. vertex.multiplyScalar( radius );
  142. starsGeometry.vertices.push( vertex );
  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.add( 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. controls.handleResize();
  178. };
  179. function animate() {
  180. requestAnimationFrame( animate );
  181. render();
  182. stats.update();
  183. };
  184. function render() {
  185. var delta = clock.getDelta();
  186. meshPlanet.rotation.y += rotationSpeed * delta;
  187. meshClouds.rotation.y += 1.25 * rotationSpeed * delta;
  188. var angle = delta * rotationSpeed;
  189. meshMoon.position = new THREE.Vector3(
  190. Math.cos( angle ) * meshMoon.position.x - Math.sin( angle ) * meshMoon.position.z,
  191. 0,
  192. Math.sin( angle ) * meshMoon.position.x + Math.cos( angle ) * meshMoon.position.z
  193. );
  194. meshMoon.rotation.y -= angle;
  195. controls.update();
  196. renderer.clear();
  197. renderer.render( scene, camera );
  198. };
  199. </script>
  200. </body>
  201. </html>