scenegraph-sun-earth-moon.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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( { antialias: true, canvas } );
  38. renderer.useLegacyLights = false;
  39. const fov = 40;
  40. const aspect = 2; // the canvas default
  41. const near = 0.1;
  42. const far = 1000;
  43. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  44. camera.position.set( 0, 50, 0 );
  45. camera.up.set( 0, 0, 1 );
  46. camera.lookAt( 0, 0, 0 );
  47. const scene = new THREE.Scene();
  48. {
  49. const color = 0xFFFFFF;
  50. const intensity = 500;
  51. const light = new THREE.PointLight( color, intensity );
  52. scene.add( light );
  53. }
  54. const objects = [];
  55. const radius = 1;
  56. const widthSegments = 6;
  57. const heightSegments = 6;
  58. const sphereGeometry = new THREE.SphereGeometry(
  59. radius, widthSegments, heightSegments );
  60. const solarSystem = new THREE.Object3D();
  61. scene.add( solarSystem );
  62. objects.push( solarSystem );
  63. const sunMaterial = new THREE.MeshPhongMaterial( { emissive: 0xFFFF00 } );
  64. const sunMesh = new THREE.Mesh( sphereGeometry, sunMaterial );
  65. sunMesh.scale.set( 5, 5, 5 );
  66. solarSystem.add( sunMesh );
  67. objects.push( sunMesh );
  68. const earthOrbit = new THREE.Object3D();
  69. earthOrbit.position.x = 10;
  70. solarSystem.add( earthOrbit );
  71. objects.push( earthOrbit );
  72. const earthMaterial = new THREE.MeshPhongMaterial( { color: 0x2233FF, emissive: 0x112244 } );
  73. const earthMesh = new THREE.Mesh( sphereGeometry, earthMaterial );
  74. earthOrbit.add( earthMesh );
  75. objects.push( earthMesh );
  76. const moonOrbit = new THREE.Object3D();
  77. moonOrbit.position.x = 2;
  78. earthOrbit.add( moonOrbit );
  79. const moonMaterial = new THREE.MeshPhongMaterial( { color: 0x888888, emissive: 0x222222 } );
  80. const moonMesh = new THREE.Mesh( sphereGeometry, moonMaterial );
  81. moonMesh.scale.set( .5, .5, .5 );
  82. moonOrbit.add( moonMesh );
  83. objects.push( moonMesh );
  84. function resizeRendererToDisplaySize( renderer ) {
  85. const canvas = renderer.domElement;
  86. const width = canvas.clientWidth;
  87. const height = canvas.clientHeight;
  88. const needResize = canvas.width !== width || canvas.height !== height;
  89. if ( needResize ) {
  90. renderer.setSize( width, height, false );
  91. }
  92. return needResize;
  93. }
  94. function render( time ) {
  95. time *= 0.001;
  96. if ( resizeRendererToDisplaySize( renderer ) ) {
  97. const canvas = renderer.domElement;
  98. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  99. camera.updateProjectionMatrix();
  100. }
  101. objects.forEach( ( obj ) => {
  102. obj.rotation.y = time;
  103. } );
  104. renderer.render( scene, camera );
  105. requestAnimationFrame( render );
  106. }
  107. requestAnimationFrame( render );
  108. }
  109. main();
  110. </script>
  111. </html>