scenegraph-sun-earth-orbit.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Scenegraph - Sun Earth</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. function main() {
  36. const canvas = document.querySelector('#c');
  37. const renderer = new THREE.WebGLRenderer({canvas});
  38. const fov = 40;
  39. const aspect = 2; // the canvas default
  40. const near = 0.1;
  41. const far = 1000;
  42. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  43. camera.position.set(0, 150, 0);
  44. camera.up.set(0, 0, 1);
  45. camera.lookAt(0, 0, 0);
  46. const scene = new THREE.Scene();
  47. {
  48. const color = 0xFFFFFF;
  49. const intensity = 3;
  50. const light = new THREE.PointLight(color, intensity);
  51. scene.add(light);
  52. }
  53. const objects = [];
  54. const radius = 1;
  55. const widthSegments = 6;
  56. const heightSegments = 6;
  57. const sphereGeometry = new THREE.SphereGeometry(
  58. radius, widthSegments, heightSegments);
  59. const sunMaterial = new THREE.MeshPhongMaterial({emissive: 0xFFFF00});
  60. const sunMesh = new THREE.Mesh(sphereGeometry, sunMaterial);
  61. sunMesh.scale.set(5, 5, 5);
  62. scene.add(sunMesh);
  63. objects.push(sunMesh);
  64. const earthMaterial = new THREE.MeshPhongMaterial({color: 0x2233FF, emissive: 0x112244});
  65. const earthMesh = new THREE.Mesh(sphereGeometry, earthMaterial);
  66. earthMesh.position.x = 10;
  67. sunMesh.add(earthMesh);
  68. objects.push(earthMesh);
  69. function resizeRendererToDisplaySize(renderer) {
  70. const canvas = renderer.domElement;
  71. const width = canvas.clientWidth;
  72. const height = canvas.clientHeight;
  73. const needResize = canvas.width !== width || canvas.height !== height;
  74. if (needResize) {
  75. renderer.setSize(width, height, false);
  76. }
  77. return needResize;
  78. }
  79. function render(time) {
  80. time *= 0.001;
  81. if (resizeRendererToDisplaySize(renderer)) {
  82. const canvas = renderer.domElement;
  83. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  84. camera.updateProjectionMatrix();
  85. }
  86. objects.forEach((obj) => {
  87. obj.rotation.y = time;
  88. });
  89. renderer.render(scene, camera);
  90. requestAnimationFrame(render);
  91. }
  92. requestAnimationFrame(render);
  93. }
  94. main();
  95. </script>
  96. </html>