css2d_label.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. scene = new THREE.Scene();
  63. const dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  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. earthMaterial.map.colorSpace = THREE.SRGBColorSpace;
  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. moonMaterial.map.colorSpace = THREE.SRGBColorSpace;
  89. moon = new THREE.Mesh( moonGeometry, moonMaterial );
  90. scene.add( moon );
  91. //
  92. earth.layers.enableAll();
  93. moon.layers.enableAll();
  94. const earthDiv = document.createElement( 'div' );
  95. earthDiv.className = 'label';
  96. earthDiv.textContent = 'Earth';
  97. earthDiv.style.backgroundColor = 'transparent';
  98. const earthLabel = new CSS2DObject( earthDiv );
  99. earthLabel.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
  100. earthLabel.center.set( 0, 1 );
  101. earth.add( earthLabel );
  102. earthLabel.layers.set( 0 );
  103. const earthMassDiv = document.createElement( 'div' );
  104. earthMassDiv.className = 'label';
  105. earthMassDiv.textContent = '5.97237e24 kg';
  106. earthMassDiv.style.backgroundColor = 'transparent';
  107. const earthMassLabel = new CSS2DObject( earthMassDiv );
  108. earthMassLabel.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
  109. earthMassLabel.center.set( 0, 0 );
  110. earth.add( earthMassLabel );
  111. earthMassLabel.layers.set( 1 );
  112. const moonDiv = document.createElement( 'div' );
  113. moonDiv.className = 'label';
  114. moonDiv.textContent = 'Moon';
  115. moonDiv.style.backgroundColor = 'transparent';
  116. const moonLabel = new CSS2DObject( moonDiv );
  117. moonLabel.position.set( 1.5 * MOON_RADIUS, 0, 0 );
  118. moonLabel.center.set( 0, 1 );
  119. moon.add( moonLabel );
  120. moonLabel.layers.set( 0 );
  121. const moonMassDiv = document.createElement( 'div' );
  122. moonMassDiv.className = 'label';
  123. moonMassDiv.textContent = '7.342e22 kg';
  124. moonMassDiv.style.backgroundColor = 'transparent';
  125. const moonMassLabel = new CSS2DObject( moonMassDiv );
  126. moonMassLabel.position.set( 1.5 * MOON_RADIUS, 0, 0 );
  127. moonMassLabel.center.set( 0, 0 );
  128. moon.add( moonMassLabel );
  129. moonMassLabel.layers.set( 1 );
  130. //
  131. renderer = new THREE.WebGLRenderer();
  132. renderer.setPixelRatio( window.devicePixelRatio );
  133. renderer.setSize( window.innerWidth, window.innerHeight );
  134. document.body.appendChild( renderer.domElement );
  135. labelRenderer = new CSS2DRenderer();
  136. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  137. labelRenderer.domElement.style.position = 'absolute';
  138. labelRenderer.domElement.style.top = '0px';
  139. document.body.appendChild( labelRenderer.domElement );
  140. const controls = new OrbitControls( camera, labelRenderer.domElement );
  141. controls.minDistance = 5;
  142. controls.maxDistance = 100;
  143. //
  144. window.addEventListener( 'resize', onWindowResize );
  145. initGui();
  146. }
  147. function onWindowResize() {
  148. camera.aspect = window.innerWidth / window.innerHeight;
  149. camera.updateProjectionMatrix();
  150. renderer.setSize( window.innerWidth, window.innerHeight );
  151. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  152. }
  153. function animate() {
  154. requestAnimationFrame( animate );
  155. const elapsed = clock.getElapsedTime();
  156. moon.position.set( Math.sin( elapsed ) * 5, 0, Math.cos( elapsed ) * 5 );
  157. renderer.render( scene, camera );
  158. labelRenderer.render( scene, camera );
  159. }
  160. //
  161. function initGui() {
  162. gui = new GUI();
  163. gui.title( 'Camera Layers' );
  164. gui.add( layers, 'Toggle Name' );
  165. gui.add( layers, 'Toggle Mass' );
  166. gui.add( layers, 'Enable All' );
  167. gui.add( layers, 'Disable All' );
  168. gui.open();
  169. }
  170. </script>
  171. </body>
  172. </html>