scenegraph-sun-earth-moon-axes-grids.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. color: white;
  13. font-family: monospace;
  14. }
  15. #c {
  16. width: 100%;
  17. height: 100%;
  18. display: block;
  19. }
  20. #ui {
  21. position: absolute;
  22. right: 1em;
  23. top: 1em;
  24. background: rgba(64, 64, 64, 0.8);
  25. padding: .5em;
  26. }
  27. #ui .axisgrid {
  28. display: flex;
  29. align-items: center;
  30. }
  31. #ui .axisgrid:nth-child(odd) {
  32. background: rgba(72, 72, 72, 0.8);
  33. }
  34. #ui .label {
  35. width: 8em;
  36. overflow: hidden;
  37. text-overflow: ellipsis;
  38. }
  39. #ui .checkbox {
  40. display: flex;
  41. align-items: center;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <canvas id="c"></canvas>
  47. <div id="ui"></div>
  48. </body>
  49. <!-- Import maps polyfill -->
  50. <!-- Remove this when import maps will be widely supported -->
  51. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  52. <script type="importmap">
  53. {
  54. "imports": {
  55. "three": "../../build/three.module.js",
  56. "three/addons/": "../../examples/jsm/"
  57. }
  58. }
  59. </script>
  60. <script type="module">
  61. import * as THREE from 'three';
  62. import {GUI} from 'three/addons/libs/lil-gui.module.min.js';
  63. function main() {
  64. const canvas = document.querySelector('#c');
  65. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  66. const gui = new GUI();
  67. const fov = 40;
  68. const aspect = 2; // the canvas default
  69. const near = 0.1;
  70. const far = 1000;
  71. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  72. camera.position.set(0, 50, 0);
  73. camera.up.set(0, 0, 1);
  74. camera.lookAt(0, 0, 0);
  75. const scene = new THREE.Scene();
  76. {
  77. const color = 0xFFFFFF;
  78. const intensity = 3;
  79. const light = new THREE.PointLight(color, intensity);
  80. scene.add(light);
  81. }
  82. const objects = [];
  83. const radius = 1;
  84. const widthSegments = 6;
  85. const heightSegments = 6;
  86. const sphereGeometry = new THREE.SphereGeometry(
  87. radius, widthSegments, heightSegments);
  88. const solarSystem = new THREE.Object3D();
  89. scene.add(solarSystem);
  90. objects.push(solarSystem);
  91. const sunMaterial = new THREE.MeshPhongMaterial({emissive: 0xFFFF00});
  92. const sunMesh = new THREE.Mesh(sphereGeometry, sunMaterial);
  93. sunMesh.scale.set(5, 5, 5);
  94. solarSystem.add(sunMesh);
  95. objects.push(sunMesh);
  96. const earthOrbit = new THREE.Object3D();
  97. earthOrbit.position.x = 10;
  98. solarSystem.add(earthOrbit);
  99. objects.push(earthOrbit);
  100. const earthMaterial = new THREE.MeshPhongMaterial({color: 0x2233FF, emissive: 0x112244});
  101. const earthMesh = new THREE.Mesh(sphereGeometry, earthMaterial);
  102. earthOrbit.add(earthMesh);
  103. objects.push(earthMesh);
  104. const moonOrbit = new THREE.Object3D();
  105. moonOrbit.position.x = 2;
  106. earthOrbit.add(moonOrbit);
  107. const moonMaterial = new THREE.MeshPhongMaterial({color: 0x888888, emissive: 0x222222});
  108. const moonMesh = new THREE.Mesh(sphereGeometry, moonMaterial);
  109. moonMesh.scale.set(.5, .5, .5);
  110. moonOrbit.add(moonMesh);
  111. objects.push(moonMesh);
  112. // Turns both axes and grid visible on/off
  113. // GUI requires a property that returns a bool
  114. // to decide to make a checkbox so we make a setter
  115. // can getter for `visible` which we can tell GUI
  116. // to look at.
  117. class AxisGridHelper {
  118. constructor(node, units = 10) {
  119. const axes = new THREE.AxesHelper();
  120. axes.material.depthTest = false;
  121. axes.renderOrder = 2; // after the grid
  122. node.add(axes);
  123. const grid = new THREE.GridHelper(units, units);
  124. grid.material.depthTest = false;
  125. grid.renderOrder = 1;
  126. node.add(grid);
  127. this.grid = grid;
  128. this.axes = axes;
  129. this.visible = false;
  130. }
  131. get visible() {
  132. return this._visible;
  133. }
  134. set visible(v) {
  135. this._visible = v;
  136. this.grid.visible = v;
  137. this.axes.visible = v;
  138. }
  139. }
  140. function makeAxisGrid(node, label, units) {
  141. const helper = new AxisGridHelper(node, units);
  142. gui.add(helper, 'visible').name(label);
  143. }
  144. makeAxisGrid(solarSystem, 'solarSystem', 26);
  145. makeAxisGrid(sunMesh, 'sunMesh');
  146. makeAxisGrid(earthOrbit, 'earthOrbit');
  147. makeAxisGrid(earthMesh, 'earthMesh');
  148. makeAxisGrid(moonOrbit, 'moonOrbit');
  149. makeAxisGrid(moonMesh, 'moonMesh');
  150. function resizeRendererToDisplaySize(renderer) {
  151. const canvas = renderer.domElement;
  152. const width = canvas.clientWidth;
  153. const height = canvas.clientHeight;
  154. const needResize = canvas.width !== width || canvas.height !== height;
  155. if (needResize) {
  156. renderer.setSize(width, height, false);
  157. }
  158. return needResize;
  159. }
  160. function render(time) {
  161. time *= 0.001;
  162. if (resizeRendererToDisplaySize(renderer)) {
  163. const canvas = renderer.domElement;
  164. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  165. camera.updateProjectionMatrix();
  166. }
  167. objects.forEach((obj) => {
  168. obj.rotation.y = time;
  169. });
  170. renderer.render(scene, camera);
  171. requestAnimationFrame(render);
  172. }
  173. requestAnimationFrame(render);
  174. }
  175. main();
  176. </script>
  177. </html>