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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. }
  57. }
  58. </script>
  59. <script type="module">
  60. import * as THREE from 'three';
  61. import {GUI} from '../../examples/jsm/libs/lil-gui.module.min.js';
  62. function main() {
  63. const canvas = document.querySelector('#c');
  64. const renderer = new THREE.WebGLRenderer({canvas});
  65. const gui = new GUI();
  66. const fov = 40;
  67. const aspect = 2; // the canvas default
  68. const near = 0.1;
  69. const far = 1000;
  70. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  71. camera.position.set(0, 50, 0);
  72. camera.up.set(0, 0, 1);
  73. camera.lookAt(0, 0, 0);
  74. const scene = new THREE.Scene();
  75. {
  76. const color = 0xFFFFFF;
  77. const intensity = 3;
  78. const light = new THREE.PointLight(color, intensity);
  79. scene.add(light);
  80. }
  81. const objects = [];
  82. const radius = 1;
  83. const widthSegments = 6;
  84. const heightSegments = 6;
  85. const sphereGeometry = new THREE.SphereGeometry(
  86. radius, widthSegments, heightSegments);
  87. const solarSystem = new THREE.Object3D();
  88. scene.add(solarSystem);
  89. objects.push(solarSystem);
  90. const sunMaterial = new THREE.MeshPhongMaterial({emissive: 0xFFFF00});
  91. const sunMesh = new THREE.Mesh(sphereGeometry, sunMaterial);
  92. sunMesh.scale.set(5, 5, 5);
  93. solarSystem.add(sunMesh);
  94. objects.push(sunMesh);
  95. const earthOrbit = new THREE.Object3D();
  96. earthOrbit.position.x = 10;
  97. solarSystem.add(earthOrbit);
  98. objects.push(earthOrbit);
  99. const earthMaterial = new THREE.MeshPhongMaterial({color: 0x2233FF, emissive: 0x112244});
  100. const earthMesh = new THREE.Mesh(sphereGeometry, earthMaterial);
  101. earthOrbit.add(earthMesh);
  102. objects.push(earthMesh);
  103. const moonOrbit = new THREE.Object3D();
  104. moonOrbit.position.x = 2;
  105. earthOrbit.add(moonOrbit);
  106. const moonMaterial = new THREE.MeshPhongMaterial({color: 0x888888, emissive: 0x222222});
  107. const moonMesh = new THREE.Mesh(sphereGeometry, moonMaterial);
  108. moonMesh.scale.set(.5, .5, .5);
  109. moonOrbit.add(moonMesh);
  110. objects.push(moonMesh);
  111. // Turns both axes and grid visible on/off
  112. // GUI requires a property that returns a bool
  113. // to decide to make a checkbox so we make a setter
  114. // can getter for `visible` which we can tell GUI
  115. // to look at.
  116. class AxisGridHelper {
  117. constructor(node, units = 10) {
  118. const axes = new THREE.AxesHelper();
  119. axes.material.depthTest = false;
  120. axes.renderOrder = 2; // after the grid
  121. node.add(axes);
  122. const grid = new THREE.GridHelper(units, units);
  123. grid.material.depthTest = false;
  124. grid.renderOrder = 1;
  125. node.add(grid);
  126. this.grid = grid;
  127. this.axes = axes;
  128. this.visible = false;
  129. }
  130. get visible() {
  131. return this._visible;
  132. }
  133. set visible(v) {
  134. this._visible = v;
  135. this.grid.visible = v;
  136. this.axes.visible = v;
  137. }
  138. }
  139. function makeAxisGrid(node, label, units) {
  140. const helper = new AxisGridHelper(node, units);
  141. gui.add(helper, 'visible').name(label);
  142. }
  143. makeAxisGrid(solarSystem, 'solarSystem', 26);
  144. makeAxisGrid(sunMesh, 'sunMesh');
  145. makeAxisGrid(earthOrbit, 'earthOrbit');
  146. makeAxisGrid(earthMesh, 'earthMesh');
  147. makeAxisGrid(moonOrbit, 'moonOrbit');
  148. makeAxisGrid(moonMesh, 'moonMesh');
  149. function resizeRendererToDisplaySize(renderer) {
  150. const canvas = renderer.domElement;
  151. const width = canvas.clientWidth;
  152. const height = canvas.clientHeight;
  153. const needResize = canvas.width !== width || canvas.height !== height;
  154. if (needResize) {
  155. renderer.setSize(width, height, false);
  156. }
  157. return needResize;
  158. }
  159. function render(time) {
  160. time *= 0.001;
  161. if (resizeRendererToDisplaySize(renderer)) {
  162. const canvas = renderer.domElement;
  163. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  164. camera.updateProjectionMatrix();
  165. }
  166. objects.forEach((obj) => {
  167. obj.rotation.y = time;
  168. });
  169. renderer.render(scene, camera);
  170. requestAnimationFrame(render);
  171. }
  172. requestAnimationFrame(render);
  173. }
  174. main();
  175. </script>
  176. </html>