1
0

css2d_label.html 6.1 KB

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