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

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