css2d_label.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <title>three.js css2d - label</title>
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. .label {
  10. color: #FFF;
  11. font-family: sans-serif;
  12. padding: 2px;
  13. background: rgba( 0, 0, 0, .6 );
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> css2d - label</div>
  19. <!-- Import maps polyfill -->
  20. <!-- Remove this when import maps will be widely supported -->
  21. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.module.js"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three';
  31. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  32. import { CSS2DRenderer, CSS2DObject } from './jsm/renderers/CSS2DRenderer.js';
  33. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  34. let gui;
  35. let camera, scene, renderer, labelRenderer;
  36. const layers = {
  37. 'Toggle Name': function () {
  38. camera.layers.toggle( 0 );
  39. },
  40. 'Toggle Mass': function () {
  41. camera.layers.toggle( 1 );
  42. },
  43. 'Enable All': function () {
  44. camera.layers.enableAll();
  45. },
  46. 'Disable All': function () {
  47. camera.layers.disableAll();
  48. }
  49. }
  50. const clock = new THREE.Clock();
  51. const textureLoader = new THREE.TextureLoader();
  52. let moon;
  53. init();
  54. animate();
  55. function init() {
  56. const EARTH_RADIUS = 1;
  57. const MOON_RADIUS = 0.27;
  58. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
  59. camera.position.set( 10, 5, 20 );
  60. camera.layers.enableAll();
  61. camera.layers.toggle( 1 );
  62. scene = new THREE.Scene();
  63. const dirLight = new THREE.DirectionalLight( 0xffffff );
  64. dirLight.position.set( 0, 0, 1 );
  65. dirLight.layers.enableAll();
  66. scene.add( dirLight );
  67. const axesHelper = new THREE.AxesHelper( 5 );
  68. axesHelper.layers.enableAll();
  69. scene.add( axesHelper );
  70. //
  71. const earthGeometry = new THREE.SphereGeometry( EARTH_RADIUS, 16, 16 );
  72. const earthMaterial = new THREE.MeshPhongMaterial( {
  73. specular: 0x333333,
  74. shininess: 5,
  75. map: textureLoader.load( 'textures/planets/earth_atmos_2048.jpg' ),
  76. specularMap: textureLoader.load( 'textures/planets/earth_specular_2048.jpg' ),
  77. normalMap: textureLoader.load( 'textures/planets/earth_normal_2048.jpg' ),
  78. normalScale: new THREE.Vector2( 0.85, 0.85 )
  79. } );
  80. const earth = new THREE.Mesh( earthGeometry, earthMaterial );
  81. scene.add( earth );
  82. const moonGeometry = new THREE.SphereGeometry( MOON_RADIUS, 16, 16 );
  83. const moonMaterial = new THREE.MeshPhongMaterial( {
  84. shininess: 5,
  85. map: textureLoader.load( 'textures/planets/moon_1024.jpg' )
  86. } );
  87. moon = new THREE.Mesh( moonGeometry, moonMaterial );
  88. scene.add( moon );
  89. //
  90. earth.layers.enableAll();
  91. moon.layers.enableAll();
  92. const earthDiv = document.createElement( 'div' );
  93. earthDiv.className = 'label';
  94. earthDiv.textContent = 'Earth';
  95. earthDiv.style.marginTop = '-1em';
  96. const earthLabel = new CSS2DObject( earthDiv );
  97. earthLabel.position.set( 0, EARTH_RADIUS, 0 );
  98. earth.add( earthLabel );
  99. earthLabel.layers.set( 0 );
  100. const earthMassDiv = document.createElement( 'div' );
  101. earthMassDiv.className = 'label';
  102. earthMassDiv.textContent = '5.97237e24 kg';
  103. earthMassDiv.style.marginTop = '-1em';
  104. const earthMassLabel = new CSS2DObject( earthMassDiv );
  105. earthMassLabel.position.set( 0, - 2 * EARTH_RADIUS, 0 );
  106. earth.add( earthMassLabel );
  107. earthMassLabel.layers.set( 1 );
  108. const moonDiv = document.createElement( 'div' );
  109. moonDiv.className = 'label';
  110. moonDiv.textContent = 'Moon';
  111. moonDiv.style.marginTop = '-1em';
  112. const moonLabel = new CSS2DObject( moonDiv );
  113. moonLabel.position.set( 0, MOON_RADIUS, 0 );
  114. moon.add( moonLabel );
  115. moonLabel.layers.set( 0 );
  116. const moonMassDiv = document.createElement( 'div' );
  117. moonMassDiv.className = 'label';
  118. moonMassDiv.textContent = '7.342e22 kg';
  119. moonMassDiv.style.marginTop = '-1em';
  120. const moonMassLabel = new CSS2DObject( moonMassDiv );
  121. moonMassLabel.position.set( 0, - 2 * MOON_RADIUS, 0 );
  122. moon.add( moonMassLabel );
  123. moonMassLabel.layers.set( 1 );
  124. //
  125. renderer = new THREE.WebGLRenderer();
  126. renderer.setPixelRatio( window.devicePixelRatio );
  127. renderer.setSize( window.innerWidth, window.innerHeight );
  128. document.body.appendChild( renderer.domElement );
  129. labelRenderer = new CSS2DRenderer();
  130. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  131. labelRenderer.domElement.style.position = 'absolute';
  132. labelRenderer.domElement.style.top = '0px';
  133. document.body.appendChild( labelRenderer.domElement );
  134. const controls = new OrbitControls( camera, labelRenderer.domElement );
  135. controls.minDistance = 5;
  136. controls.maxDistance = 100;
  137. //
  138. window.addEventListener( 'resize', onWindowResize );
  139. initGui();
  140. }
  141. function onWindowResize() {
  142. camera.aspect = window.innerWidth / window.innerHeight;
  143. camera.updateProjectionMatrix();
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  146. }
  147. function animate() {
  148. requestAnimationFrame( animate );
  149. const elapsed = clock.getElapsedTime();
  150. moon.position.set( Math.sin( elapsed ) * 5, 0, Math.cos( elapsed ) * 5 );
  151. renderer.render( scene, camera );
  152. labelRenderer.render( scene, camera );
  153. }
  154. //
  155. function initGui() {
  156. gui = new GUI();
  157. gui.add( layers, 'Toggle Name' );
  158. gui.add( layers, 'Toggle Mass' );
  159. gui.add( layers, 'Enable All' );
  160. gui.add( layers, 'Disable All' );
  161. }
  162. </script>
  163. </body>
  164. </html>