scenegraph-sun-earth-moon.html 3.2 KB

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