css2d_label.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  33. import { CSS2DRenderer, CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js';
  34. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  35. let gui;
  36. let camera, scene, renderer, labelRenderer;
  37. const layers = {
  38. 'Toggle Name': function () {
  39. camera.layers.toggle( 0 );
  40. },
  41. 'Toggle Mass': function () {
  42. camera.layers.toggle( 1 );
  43. },
  44. 'Enable All': function () {
  45. camera.layers.enableAll();
  46. },
  47. 'Disable All': function () {
  48. camera.layers.disableAll();
  49. }
  50. };
  51. const clock = new THREE.Clock();
  52. const textureLoader = new THREE.TextureLoader();
  53. let moon;
  54. init();
  55. animate();
  56. function init() {
  57. const EARTH_RADIUS = 1;
  58. const MOON_RADIUS = 0.27;
  59. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
  60. camera.position.set( 10, 5, 20 );
  61. camera.layers.enableAll();
  62. camera.layers.toggle( 1 );
  63. scene = new THREE.Scene();
  64. const dirLight = new THREE.DirectionalLight( 0xffffff );
  65. dirLight.position.set( 0, 0, 1 );
  66. dirLight.layers.enableAll();
  67. scene.add( dirLight );
  68. const axesHelper = new THREE.AxesHelper( 5 );
  69. axesHelper.layers.enableAll();
  70. scene.add( axesHelper );
  71. //
  72. const earthGeometry = new THREE.SphereGeometry( EARTH_RADIUS, 16, 16 );
  73. const earthMaterial = new THREE.MeshPhongMaterial( {
  74. specular: 0x333333,
  75. shininess: 5,
  76. map: textureLoader.load( 'textures/planets/earth_atmos_2048.jpg' ),
  77. specularMap: textureLoader.load( 'textures/planets/earth_specular_2048.jpg' ),
  78. normalMap: textureLoader.load( 'textures/planets/earth_normal_2048.jpg' ),
  79. normalScale: new THREE.Vector2( 0.85, 0.85 )
  80. } );
  81. const earth = new THREE.Mesh( earthGeometry, earthMaterial );
  82. scene.add( earth );
  83. const moonGeometry = new THREE.SphereGeometry( MOON_RADIUS, 16, 16 );
  84. const moonMaterial = new THREE.MeshPhongMaterial( {
  85. shininess: 5,
  86. map: textureLoader.load( 'textures/planets/moon_1024.jpg' )
  87. } );
  88. moon = new THREE.Mesh( moonGeometry, moonMaterial );
  89. scene.add( moon );
  90. //
  91. earth.layers.enableAll();
  92. moon.layers.enableAll();
  93. const earthDiv = document.createElement( 'div' );
  94. earthDiv.className = 'label';
  95. earthDiv.textContent = 'Earth';
  96. earthDiv.style.marginTop = '-1em';
  97. const earthLabel = new CSS2DObject( earthDiv );
  98. earthLabel.position.set( 0, EARTH_RADIUS, 0 );
  99. earth.add( earthLabel );
  100. earthLabel.layers.set( 0 );
  101. const earthMassDiv = document.createElement( 'div' );
  102. earthMassDiv.className = 'label';
  103. earthMassDiv.textContent = '5.97237e24 kg';
  104. earthMassDiv.style.marginTop = '-1em';
  105. const earthMassLabel = new CSS2DObject( earthMassDiv );
  106. earthMassLabel.position.set( 0, - 2 * EARTH_RADIUS, 0 );
  107. earth.add( earthMassLabel );
  108. earthMassLabel.layers.set( 1 );
  109. const moonDiv = document.createElement( 'div' );
  110. moonDiv.className = 'label';
  111. moonDiv.textContent = 'Moon';
  112. moonDiv.style.marginTop = '-1em';
  113. const moonLabel = new CSS2DObject( moonDiv );
  114. moonLabel.position.set( 0, MOON_RADIUS, 0 );
  115. moon.add( moonLabel );
  116. moonLabel.layers.set( 0 );
  117. const moonMassDiv = document.createElement( 'div' );
  118. moonMassDiv.className = 'label';
  119. moonMassDiv.textContent = '7.342e22 kg';
  120. moonMassDiv.style.marginTop = '-1em';
  121. const moonMassLabel = new CSS2DObject( moonMassDiv );
  122. moonMassLabel.position.set( 0, - 2 * MOON_RADIUS, 0 );
  123. moon.add( moonMassLabel );
  124. moonMassLabel.layers.set( 1 );
  125. //
  126. renderer = new THREE.WebGLRenderer();
  127. renderer.setPixelRatio( window.devicePixelRatio );
  128. renderer.setSize( window.innerWidth, window.innerHeight );
  129. document.body.appendChild( renderer.domElement );
  130. labelRenderer = new CSS2DRenderer();
  131. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  132. labelRenderer.domElement.style.position = 'absolute';
  133. labelRenderer.domElement.style.top = '0px';
  134. document.body.appendChild( labelRenderer.domElement );
  135. const controls = new OrbitControls( camera, labelRenderer.domElement );
  136. controls.minDistance = 5;
  137. controls.maxDistance = 100;
  138. //
  139. window.addEventListener( 'resize', onWindowResize );
  140. initGui();
  141. }
  142. function onWindowResize() {
  143. camera.aspect = window.innerWidth / window.innerHeight;
  144. camera.updateProjectionMatrix();
  145. renderer.setSize( window.innerWidth, window.innerHeight );
  146. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  147. }
  148. function animate() {
  149. requestAnimationFrame( animate );
  150. const elapsed = clock.getElapsedTime();
  151. moon.position.set( Math.sin( elapsed ) * 5, 0, Math.cos( elapsed ) * 5 );
  152. renderer.render( scene, camera );
  153. labelRenderer.render( scene, camera );
  154. }
  155. //
  156. function initGui() {
  157. gui = new GUI();
  158. gui.add( layers, 'Toggle Name' );
  159. gui.add( layers, 'Toggle Mass' );
  160. gui.add( layers, 'Enable All' );
  161. gui.add( layers, 'Disable All' );
  162. }
  163. </script>
  164. </body>
  165. </html>