scenegraph-sun-earth-orbit.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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, 150, 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 sunMaterial = new THREE.MeshPhongMaterial( { emissive: 0xFFFF00 } );
  57. const sunMesh = new THREE.Mesh( sphereGeometry, sunMaterial );
  58. sunMesh.scale.set( 5, 5, 5 );
  59. scene.add( sunMesh );
  60. objects.push( sunMesh );
  61. const earthMaterial = new THREE.MeshPhongMaterial( { color: 0x2233FF, emissive: 0x112244 } );
  62. const earthMesh = new THREE.Mesh( sphereGeometry, earthMaterial );
  63. earthMesh.position.x = 10;
  64. sunMesh.add( earthMesh );
  65. objects.push( earthMesh );
  66. function resizeRendererToDisplaySize( renderer ) {
  67. const canvas = renderer.domElement;
  68. const width = canvas.clientWidth;
  69. const height = canvas.clientHeight;
  70. const needResize = canvas.width !== width || canvas.height !== height;
  71. if ( needResize ) {
  72. renderer.setSize( width, height, false );
  73. }
  74. return needResize;
  75. }
  76. function render( time ) {
  77. time *= 0.001;
  78. if ( resizeRendererToDisplaySize( renderer ) ) {
  79. const canvas = renderer.domElement;
  80. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  81. camera.updateProjectionMatrix();
  82. }
  83. objects.forEach( ( obj ) => {
  84. obj.rotation.y = time;
  85. } );
  86. renderer.render( scene, camera );
  87. requestAnimationFrame( render );
  88. }
  89. requestAnimationFrame( render );
  90. }
  91. main();
  92. </script>
  93. </html>