css2d_label.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. let camera, scene, renderer, labelRenderer;
  34. const clock = new THREE.Clock();
  35. const textureLoader = new THREE.TextureLoader();
  36. let moon;
  37. init();
  38. animate();
  39. function init() {
  40. const EARTH_RADIUS = 1;
  41. const MOON_RADIUS = 0.27;
  42. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
  43. camera.position.set( 10, 5, 20 );
  44. scene = new THREE.Scene();
  45. const dirLight = new THREE.DirectionalLight( 0xffffff );
  46. dirLight.position.set( 0, 0, 1 );
  47. scene.add( dirLight );
  48. const axesHelper = new THREE.AxesHelper( 5 );
  49. scene.add( axesHelper );
  50. //
  51. const earthGeometry = new THREE.SphereGeometry( EARTH_RADIUS, 16, 16 );
  52. const earthMaterial = new THREE.MeshPhongMaterial( {
  53. specular: 0x333333,
  54. shininess: 5,
  55. map: textureLoader.load( 'textures/planets/earth_atmos_2048.jpg' ),
  56. specularMap: textureLoader.load( 'textures/planets/earth_specular_2048.jpg' ),
  57. normalMap: textureLoader.load( 'textures/planets/earth_normal_2048.jpg' ),
  58. normalScale: new THREE.Vector2( 0.85, 0.85 )
  59. } );
  60. const earth = new THREE.Mesh( earthGeometry, earthMaterial );
  61. scene.add( earth );
  62. const moonGeometry = new THREE.SphereGeometry( MOON_RADIUS, 16, 16 );
  63. const moonMaterial = new THREE.MeshPhongMaterial( {
  64. shininess: 5,
  65. map: textureLoader.load( 'textures/planets/moon_1024.jpg' )
  66. } );
  67. moon = new THREE.Mesh( moonGeometry, moonMaterial );
  68. scene.add( moon );
  69. //
  70. const earthDiv = document.createElement( 'div' );
  71. earthDiv.className = 'label';
  72. earthDiv.textContent = 'Earth';
  73. earthDiv.style.marginTop = '-1em';
  74. const earthLabel = new CSS2DObject( earthDiv );
  75. earthLabel.position.set( 0, EARTH_RADIUS, 0 );
  76. earth.add( earthLabel );
  77. const moonDiv = document.createElement( 'div' );
  78. moonDiv.className = 'label';
  79. moonDiv.textContent = 'Moon';
  80. moonDiv.style.marginTop = '-1em';
  81. const moonLabel = new CSS2DObject( moonDiv );
  82. moonLabel.position.set( 0, MOON_RADIUS, 0 );
  83. moon.add( moonLabel );
  84. //
  85. renderer = new THREE.WebGLRenderer();
  86. renderer.setPixelRatio( window.devicePixelRatio );
  87. renderer.setSize( window.innerWidth, window.innerHeight );
  88. document.body.appendChild( renderer.domElement );
  89. labelRenderer = new CSS2DRenderer();
  90. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  91. labelRenderer.domElement.style.position = 'absolute';
  92. labelRenderer.domElement.style.top = '0px';
  93. document.body.appendChild( labelRenderer.domElement );
  94. const controls = new OrbitControls( camera, labelRenderer.domElement );
  95. controls.minDistance = 5;
  96. controls.maxDistance = 100;
  97. //
  98. window.addEventListener( 'resize', onWindowResize );
  99. }
  100. function onWindowResize() {
  101. camera.aspect = window.innerWidth / window.innerHeight;
  102. camera.updateProjectionMatrix();
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  105. }
  106. function animate() {
  107. requestAnimationFrame( animate );
  108. const elapsed = clock.getElapsedTime();
  109. moon.position.set( Math.sin( elapsed ) * 5, 0, Math.cos( elapsed ) * 5 );
  110. renderer.render( scene, camera );
  111. labelRenderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>