css2d_label.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. <meta property="og:title" content="three.js css2d - label">
  7. <meta property="og:type" content="website">
  8. <meta property="og:url" content="https://threejs.org/examples/css2d_label.html">
  9. <meta property="og:image" content="https://threejs.org/examples/screenshots/css2d_label.jpg">
  10. <title>three.js css2d - label</title>
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. .label {
  14. color: #FFF;
  15. font-family: sans-serif;
  16. padding: 2px;
  17. background: rgba( 0, 0, 0, .6 );
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> css2d - label</div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.module.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. import { CSS2DRenderer, CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js';
  35. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  36. let gui;
  37. let camera, scene, renderer, labelRenderer;
  38. const layers = {
  39. 'Toggle Name': function () {
  40. camera.layers.toggle( 0 );
  41. },
  42. 'Toggle Mass': function () {
  43. camera.layers.toggle( 1 );
  44. },
  45. 'Enable All': function () {
  46. camera.layers.enableAll();
  47. },
  48. 'Disable All': function () {
  49. camera.layers.disableAll();
  50. }
  51. };
  52. const timer = new THREE.Timer();
  53. timer.connect( document );
  54. const textureLoader = new THREE.TextureLoader();
  55. let moon;
  56. init();
  57. animate();
  58. function init() {
  59. const EARTH_RADIUS = 1;
  60. const MOON_RADIUS = 0.27;
  61. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
  62. camera.position.set( 10, 5, 20 );
  63. camera.layers.enableAll();
  64. scene = new THREE.Scene();
  65. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  66. dirLight.position.set( 0, 0, 1 );
  67. dirLight.layers.enableAll();
  68. scene.add( dirLight );
  69. const axesHelper = new THREE.AxesHelper( 5 );
  70. axesHelper.layers.enableAll();
  71. scene.add( axesHelper );
  72. //
  73. const earthGeometry = new THREE.SphereGeometry( EARTH_RADIUS, 16, 16 );
  74. const earthMaterial = new THREE.MeshPhongMaterial( {
  75. specular: 0x333333,
  76. shininess: 5,
  77. map: textureLoader.load( 'textures/planets/earth_atmos_2048.jpg' ),
  78. specularMap: textureLoader.load( 'textures/planets/earth_specular_2048.jpg' ),
  79. normalMap: textureLoader.load( 'textures/planets/earth_normal_2048.jpg' ),
  80. normalScale: new THREE.Vector2( 0.85, 0.85 )
  81. } );
  82. earthMaterial.map.colorSpace = THREE.SRGBColorSpace;
  83. const earth = new THREE.Mesh( earthGeometry, earthMaterial );
  84. scene.add( earth );
  85. const moonGeometry = new THREE.SphereGeometry( MOON_RADIUS, 16, 16 );
  86. const moonMaterial = new THREE.MeshPhongMaterial( {
  87. shininess: 5,
  88. map: textureLoader.load( 'textures/planets/moon_1024.jpg' )
  89. } );
  90. moonMaterial.map.colorSpace = THREE.SRGBColorSpace;
  91. moon = new THREE.Mesh( moonGeometry, moonMaterial );
  92. scene.add( moon );
  93. //
  94. earth.layers.enableAll();
  95. moon.layers.enableAll();
  96. const earthDiv = document.createElement( 'div' );
  97. earthDiv.className = 'label';
  98. earthDiv.textContent = 'Earth';
  99. earthDiv.style.backgroundColor = 'transparent';
  100. const earthLabel = new CSS2DObject( earthDiv );
  101. earthLabel.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
  102. earthLabel.center.set( 0, 1 );
  103. earth.add( earthLabel );
  104. earthLabel.layers.set( 0 );
  105. const earthMassDiv = document.createElement( 'div' );
  106. earthMassDiv.className = 'label';
  107. earthMassDiv.textContent = '5.97237e24 kg';
  108. earthMassDiv.style.backgroundColor = 'transparent';
  109. const earthMassLabel = new CSS2DObject( earthMassDiv );
  110. earthMassLabel.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
  111. earthMassLabel.center.set( 0, 0 );
  112. earth.add( earthMassLabel );
  113. earthMassLabel.layers.set( 1 );
  114. const moonDiv = document.createElement( 'div' );
  115. moonDiv.className = 'label';
  116. moonDiv.textContent = 'Moon';
  117. moonDiv.style.backgroundColor = 'transparent';
  118. const moonLabel = new CSS2DObject( moonDiv );
  119. moonLabel.position.set( 1.5 * MOON_RADIUS, 0, 0 );
  120. moonLabel.center.set( 0, 1 );
  121. moon.add( moonLabel );
  122. moonLabel.layers.set( 0 );
  123. const moonMassDiv = document.createElement( 'div' );
  124. moonMassDiv.className = 'label';
  125. moonMassDiv.textContent = '7.342e22 kg';
  126. moonMassDiv.style.backgroundColor = 'transparent';
  127. const moonMassLabel = new CSS2DObject( moonMassDiv );
  128. moonMassLabel.position.set( 1.5 * MOON_RADIUS, 0, 0 );
  129. moonMassLabel.center.set( 0, 0 );
  130. moon.add( moonMassLabel );
  131. moonMassLabel.layers.set( 1 );
  132. //
  133. renderer = new THREE.WebGLRenderer();
  134. renderer.setPixelRatio( window.devicePixelRatio );
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. document.body.appendChild( renderer.domElement );
  137. labelRenderer = new CSS2DRenderer();
  138. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  139. labelRenderer.domElement.style.position = 'absolute';
  140. labelRenderer.domElement.style.top = '0px';
  141. document.body.appendChild( labelRenderer.domElement );
  142. const controls = new OrbitControls( camera, labelRenderer.domElement );
  143. controls.minDistance = 5;
  144. controls.maxDistance = 100;
  145. //
  146. window.addEventListener( 'resize', onWindowResize );
  147. initGui();
  148. }
  149. function onWindowResize() {
  150. camera.aspect = window.innerWidth / window.innerHeight;
  151. camera.updateProjectionMatrix();
  152. renderer.setSize( window.innerWidth, window.innerHeight );
  153. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  154. }
  155. function animate() {
  156. timer.update();
  157. requestAnimationFrame( animate );
  158. const elapsed = timer.getElapsed();
  159. moon.position.set( Math.sin( elapsed ) * 5, 0, Math.cos( elapsed ) * 5 );
  160. renderer.render( scene, camera );
  161. labelRenderer.render( scene, camera );
  162. }
  163. //
  164. function initGui() {
  165. gui = new GUI();
  166. gui.title( 'Camera Layers' );
  167. gui.add( layers, 'Toggle Name' );
  168. gui.add( layers, 'Toggle Mass' );
  169. gui.add( layers, 'Enable All' );
  170. gui.add( layers, 'Disable All' );
  171. gui.open();
  172. }
  173. </script>
  174. </body>
  175. </html>
粤ICP备19079148号