software_geometry_earth.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js software - 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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. a {
  10. color: #08f;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="container"></div>
  16. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - earth demo</div>
  17. <script type="module">
  18. import {
  19. CanvasTexture,
  20. Group,
  21. Mesh,
  22. MeshBasicMaterial,
  23. MeshLambertMaterial,
  24. PerspectiveCamera,
  25. PlaneBufferGeometry,
  26. Scene,
  27. SphereBufferGeometry,
  28. TextureLoader
  29. } from "../build/three.module.js";
  30. import Stats from './jsm/libs/stats.module.js';
  31. import { SoftwareRenderer } from './jsm/renderers/SoftwareRenderer.js';
  32. var container, stats;
  33. var camera, scene, renderer;
  34. var group;
  35. var mouseX = 0, mouseY = 0;
  36. var windowHalfX = window.innerWidth / 2;
  37. var windowHalfY = window.innerHeight / 2;
  38. init();
  39. animate();
  40. function init() {
  41. container = document.getElementById( 'container' );
  42. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 2000 );
  43. camera.position.z = 500;
  44. scene = new Scene();
  45. group = new Group();
  46. scene.add( group );
  47. // earth
  48. var loader = new TextureLoader();
  49. loader.load( 'textures/land_ocean_ice_cloud_2048.jpg', function ( texture ) {
  50. var geometry = new SphereBufferGeometry( 200, 20, 20 );
  51. var material = new MeshLambertMaterial( { map: texture } );
  52. var mesh = new Mesh( geometry, material );
  53. group.add( mesh );
  54. } );
  55. // shadow
  56. var canvas = document.createElement( 'canvas' );
  57. canvas.width = 128;
  58. canvas.height = 128;
  59. var context = canvas.getContext( '2d' );
  60. var gradient = context.createRadialGradient(
  61. canvas.width / 2,
  62. canvas.height / 2,
  63. 0,
  64. canvas.width / 2,
  65. canvas.height / 2,
  66. canvas.width / 2
  67. );
  68. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  69. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  70. context.fillStyle = gradient;
  71. context.fillRect( 0, 0, canvas.width, canvas.height );
  72. var texture = new CanvasTexture( canvas );
  73. var geometry = new PlaneBufferGeometry( 300, 300, 3, 3 );
  74. var material = new MeshBasicMaterial( { map: texture } );
  75. var mesh = new Mesh( geometry, material );
  76. mesh.position.y = - 250;
  77. mesh.rotation.x = - Math.PI / 2;
  78. group.add( mesh );
  79. renderer = new SoftwareRenderer();
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. container.appendChild( renderer.domElement );
  82. stats = new Stats();
  83. container.appendChild( stats.dom );
  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>