css2d_label.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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="module">
  20. import * as THREE from '../build/three.module.js';
  21. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  22. import { CSS2DRenderer, CSS2DObject } from './jsm/renderers/CSS2DRenderer.js';
  23. var camera, scene, renderer, labelRenderer;
  24. var clock = new THREE.Clock();
  25. var textureLoader = new THREE.TextureLoader();
  26. var earth, moon;
  27. init();
  28. animate();
  29. function init() {
  30. var EARTH_RADIUS = 1;
  31. var MOON_RADIUS = 0.27;
  32. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  33. camera.position.set( 10, 5, 20 );
  34. scene = new THREE.Scene();
  35. var dirLight = new THREE.DirectionalLight( 0xffffff );
  36. dirLight.position.set( 0, 0, 1 );
  37. scene.add( dirLight );
  38. var axesHelper = new THREE.AxesHelper( 5 );
  39. scene.add( axesHelper );
  40. //
  41. var earthGeometry = new THREE.SphereBufferGeometry( EARTH_RADIUS, 16, 16 );
  42. var earthMaterial = new THREE.MeshPhongMaterial( {
  43. specular: 0x333333,
  44. shininess: 5,
  45. map: textureLoader.load( 'textures/planets/earth_atmos_2048.jpg' ),
  46. specularMap: textureLoader.load( 'textures/planets/earth_specular_2048.jpg' ),
  47. normalMap: textureLoader.load( 'textures/planets/earth_normal_2048.jpg' ),
  48. normalScale: new THREE.Vector2( 0.85, 0.85 )
  49. } );
  50. earth = new THREE.Mesh( earthGeometry, earthMaterial );
  51. scene.add( earth );
  52. var moonGeometry = new THREE.SphereBufferGeometry( MOON_RADIUS, 16, 16 );
  53. var moonMaterial = new THREE.MeshPhongMaterial( {
  54. shininess: 5,
  55. map: textureLoader.load( 'textures/planets/moon_1024.jpg' )
  56. } );
  57. moon = new THREE.Mesh( moonGeometry, moonMaterial );
  58. scene.add( moon );
  59. //
  60. var earthDiv = document.createElement( 'div' );
  61. earthDiv.className = 'label';
  62. earthDiv.textContent = 'Earth';
  63. earthDiv.style.marginTop = '-1em';
  64. var earthLabel = new CSS2DObject( earthDiv );
  65. earthLabel.position.set( 0, EARTH_RADIUS, 0 );
  66. earth.add( earthLabel );
  67. var moonDiv = document.createElement( 'div' );
  68. moonDiv.className = 'label';
  69. moonDiv.textContent = 'Moon';
  70. moonDiv.style.marginTop = '-1em';
  71. var moonLabel = new CSS2DObject( moonDiv );
  72. moonLabel.position.set( 0, MOON_RADIUS, 0 );
  73. moon.add( moonLabel );
  74. //
  75. renderer = new THREE.WebGLRenderer();
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. document.body.appendChild( renderer.domElement );
  79. labelRenderer = new CSS2DRenderer();
  80. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  81. labelRenderer.domElement.style.position = 'absolute';
  82. labelRenderer.domElement.style.top = 0;
  83. document.body.appendChild( labelRenderer.domElement );
  84. var controls = new OrbitControls( camera, labelRenderer.domElement );
  85. }
  86. function animate() {
  87. requestAnimationFrame( animate );
  88. var elapsed = clock.getElapsedTime();
  89. moon.position.set( Math.sin( elapsed ) * 5, 0, Math.cos( elapsed ) * 5 );
  90. renderer.render( scene, camera );
  91. labelRenderer.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>