canvas_geometry_earth.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - geometry - 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. color: #808080;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #ffffff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. a {
  23. color: #0080ff;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="container"></div>
  29. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - earth demo</div>
  30. <script src="../build/three.min.js"></script>
  31. <script src="js/libs/stats.min.js"></script>
  32. <script>
  33. var container, stats;
  34. var camera, scene, renderer;
  35. var group;
  36. var mouseX = 0, mouseY = 0;
  37. var windowHalfX = window.innerWidth / 2;
  38. var windowHalfY = window.innerHeight / 2;
  39. init();
  40. animate();
  41. function init() {
  42. container = document.getElementById( 'container' );
  43. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  44. camera.position.z = 500;
  45. scene = new THREE.Scene();
  46. group = new THREE.Object3D();
  47. scene.add( group );
  48. // earth
  49. var earthTexture = new THREE.Texture();
  50. var loader = new THREE.ImageLoader();
  51. loader.load( 'textures/land_ocean_ice_cloud_2048.jpg', function ( image ) {
  52. earthTexture.image = image;
  53. earthTexture.needsUpdate = true;
  54. } );
  55. var geometry = new THREE.SphereGeometry( 200, 20, 20 );
  56. var material = new THREE.MeshBasicMaterial( { map: earthTexture, overdraw: true } );
  57. var mesh = new THREE.Mesh( geometry, material );
  58. group.add( mesh );
  59. // shadow
  60. var canvas = document.createElement( 'canvas' );
  61. canvas.width = 128;
  62. canvas.height = 128;
  63. var context = canvas.getContext( '2d' );
  64. var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  65. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  66. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  67. context.fillStyle = gradient;
  68. context.fillRect( 0, 0, canvas.width, canvas.height );
  69. var texture = new THREE.Texture( canvas );
  70. texture.needsUpdate = true;
  71. var geometry = new THREE.PlaneGeometry( 300, 300, 3, 3 );
  72. var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: true } );
  73. var mesh = new THREE.Mesh( geometry, material );
  74. mesh.position.y = - 250;
  75. mesh.rotation.x = - Math.PI / 2;
  76. group.add( mesh );
  77. renderer = new THREE.CanvasRenderer();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. container.appendChild( renderer.domElement );
  80. stats = new Stats();
  81. stats.domElement.style.position = 'absolute';
  82. stats.domElement.style.top = '0px';
  83. container.appendChild( stats.domElement );
  84. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  85. //
  86. window.addEventListener( 'resize', onWindowResize, false );
  87. }
  88. function onWindowResize() {
  89. windowHalfX = window.innerWidth / 2;
  90. windowHalfY = window.innerHeight / 2;
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. }
  95. function onDocumentMouseMove( event ) {
  96. mouseX = ( event.clientX - windowHalfX );
  97. mouseY = ( event.clientY - windowHalfY );
  98. }
  99. //
  100. function animate() {
  101. requestAnimationFrame( animate );
  102. render();
  103. stats.update();
  104. }
  105. function render() {
  106. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  107. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  108. camera.lookAt( scene.position );
  109. group.rotation.y -= 0.005;
  110. renderer.render( scene, camera );
  111. }
  112. </script>
  113. </body>
  114. </html>
粤ICP备19079148号