align-html-elements-to-3d.html 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Aligning HTML Elements to 3D</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – Aligning HTML Elements to 3D">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>Aligning HTML Elements to 3D</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p>This article is part of a series of articles about three.js. The first article
  29. is <a href="fundamentals.html">three.js fundamentals</a>. If you haven't read that
  30. yet and you're new to three.js you might want to consider starting there. </p>
  31. <p>Sometimes you'd like to display some text in your 3D scene. You have many options
  32. each with pluses and minuses.</p>
  33. <ul>
  34. <li><p>Use 3D text</p>
  35. <p>If you look at the <a href="primitives.html">primitives article</a> you'll see <a href="/docs/#api/en/geometries/TextGeometry"><code class="notranslate" translate="no">TextGeometry</code></a> which
  36. makes 3D text. This might be useful for flying logos but probably not so useful for stats, info,
  37. or labelling lots of things.</p>
  38. </li>
  39. <li><p>Use a texture with 2D text drawn into it.</p>
  40. <p>The article on <a href="canvas-textures.html">using a Canvas as a texture</a> shows using
  41. a canvas as a texture. You can draw text into a canvas and <a href="billboards.html">display it as a billboard</a>.
  42. The plus here might be that the text is integrated into the 3D scene. For something like a computer terminal
  43. shown in a 3D scene this might be perfect.</p>
  44. </li>
  45. <li><p>Use HTML Elements and position them to match the 3D</p>
  46. <p>The benefits to this approach is you can use all of HTML. Your HTML can have multiple elements. It can
  47. by styled with CSS. It can also be selected by the user as it is actual text. </p>
  48. </li>
  49. </ul>
  50. <p>This article will cover this last approach.</p>
  51. <p>Let's start simple. We'll make a 3D scene with a few primitives and then add a label to each primitive. We'll start
  52. with an example from <a href="responsive.html">the article on responsive pages</a> </p>
  53. <p>We'll add some <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a> like we did in <a href="lights.html">the article on lighting</a>.</p>
  54. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import * as THREE from 'three';
  55. +import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
  56. </pre>
  57. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const controls = new OrbitControls(camera, canvas);
  58. controls.target.set(0, 0, 0);
  59. controls.update();
  60. </pre>
  61. <p>We need to provide an HTML element to contain our label elements</p>
  62. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;body&gt;
  63. - &lt;canvas id="c"&gt;&lt;/canvas&gt;
  64. + &lt;div id="container"&gt;
  65. + &lt;canvas id="c"&gt;&lt;/canvas&gt;
  66. + &lt;div id="labels"&gt;&lt;/div&gt;
  67. + &lt;/div&gt;
  68. &lt;/body&gt;
  69. </pre>
  70. <p>By putting both the canvas and the <code class="notranslate" translate="no">&lt;div id="labels"&gt;</code> inside a
  71. parent container we can make them overlap with this CSS</p>
  72. <pre class="prettyprint showlinemods notranslate lang-css" translate="no">#c {
  73. - width: 100%;
  74. - height: 100%;
  75. + width: 100%; /* let our container decide our size */
  76. + height: 100%;
  77. display: block;
  78. }
  79. +#container {
  80. + position: relative; /* makes this the origin of its children */
  81. + width: 100%;
  82. + height: 100%;
  83. + overflow: hidden;
  84. +}
  85. +#labels {
  86. + position: absolute; /* let us position ourself inside the container */
  87. + left: 0; /* make our position the top left of the container */
  88. + top: 0;
  89. + color: white;
  90. +}
  91. </pre>
  92. <p>let's also add some CSS for the labels themselves</p>
  93. <pre class="prettyprint showlinemods notranslate lang-css" translate="no">#labels&gt;div {
  94. position: absolute; /* let us position them inside the container */
  95. left: 0; /* make their default position the top left of the container */
  96. top: 0;
  97. cursor: pointer; /* change the cursor to a hand when over us */
  98. font-size: large;
  99. user-select: none; /* don't let the text get selected */
  100. text-shadow: /* create a black outline */
  101. -1px -1px 0 #000,
  102. 0 -1px 0 #000,
  103. 1px -1px 0 #000,
  104. 1px 0 0 #000,
  105. 1px 1px 0 #000,
  106. 0 1px 0 #000,
  107. -1px 1px 0 #000,
  108. -1px 0 0 #000;
  109. }
  110. #labels&gt;div:hover {
  111. color: red;
  112. }
  113. </pre>
  114. <p>Now into our code we don't have to add too much. We had a function
  115. <code class="notranslate" translate="no">makeInstance</code> that we used to generate cubes. Let's make it
  116. so it also adds a label element.</p>
  117. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">+const labelContainerElem = document.querySelector('#labels');
  118. -function makeInstance(geometry, color, x) {
  119. +function makeInstance(geometry, color, x, name) {
  120. const material = new THREE.MeshPhongMaterial({color});
  121. const cube = new THREE.Mesh(geometry, material);
  122. scene.add(cube);
  123. cube.position.x = x;
  124. + const elem = document.createElement('div');
  125. + elem.textContent = name;
  126. + labelContainerElem.appendChild(elem);
  127. - return cube;
  128. + return {cube, elem};
  129. }
  130. </pre>
  131. <p>As you can see we're adding a <code class="notranslate" translate="no">&lt;div&gt;</code> to the container, one for each cube. We're
  132. also returning an object with both the <code class="notranslate" translate="no">cube</code> and the <code class="notranslate" translate="no">elem</code> for the label.</p>
  133. <p>Calling it we need to provide a name for each</p>
  134. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const cubes = [
  135. - makeInstance(geometry, 0x44aa88, 0),
  136. - makeInstance(geometry, 0x8844aa, -2),
  137. - makeInstance(geometry, 0xaa8844, 2),
  138. + makeInstance(geometry, 0x44aa88, 0, 'Aqua'),
  139. + makeInstance(geometry, 0x8844aa, -2, 'Purple'),
  140. + makeInstance(geometry, 0xaa8844, 2, 'Gold'),
  141. ];
  142. </pre>
  143. <p>What remains is positioning the label elements at render time</p>
  144. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const tempV = new THREE.Vector3();
  145. ...
  146. -cubes.forEach((cube, ndx) =&gt; {
  147. +cubes.forEach((cubeInfo, ndx) =&gt; {
  148. + const {cube, elem} = cubeInfo;
  149. const speed = 1 + ndx * .1;
  150. const rot = time * speed;
  151. cube.rotation.x = rot;
  152. cube.rotation.y = rot;
  153. + // get the position of the center of the cube
  154. + cube.updateWorldMatrix(true, false);
  155. + cube.getWorldPosition(tempV);
  156. +
  157. + // get the normalized screen coordinate of that position
  158. + // x and y will be in the -1 to +1 range with x = -1 being
  159. + // on the left and y = -1 being on the bottom
  160. + tempV.project(camera);
  161. +
  162. + // convert the normalized position to CSS coordinates
  163. + const x = (tempV.x * .5 + .5) * canvas.clientWidth;
  164. + const y = (tempV.y * -.5 + .5) * canvas.clientHeight;
  165. +
  166. + // move the elem to that position
  167. + elem.style.transform = `translate(-50%, -50%) translate(${x}px,${y}px)`;
  168. });
  169. </pre>
  170. <p>And with that we have labels aligned to their corresponding objects.</p>
  171. <p></p><div translate="no" class="threejs_example_container notranslate">
  172. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/align-html-to-3d.html"></iframe></div>
  173. <a class="threejs_center" href="/manual/examples/align-html-to-3d.html" target="_blank">click here to open in a separate window</a>
  174. </div>
  175. <p></p>
  176. <p>There are a couple of issues we probably want to deal with.</p>
  177. <p>One is that if we rotate the objects so they overlap all the labels
  178. overlap as well.</p>
  179. <div class="threejs_center"><img src="../resources/images/overlapping-labels.png" style="width: 307px;"></div>
  180. <p>Another is that if we zoom way out so that the objects go outside
  181. the frustum the labels will still appear.</p>
  182. <p>A possible solution to the problem of overlapping objects is to use
  183. the <a href="picking.html">picking code from the article on picking</a>.
  184. We'll pass in the position of the object on the screen and then
  185. ask the <code class="notranslate" translate="no">RayCaster</code> to tell us which objects were intersected.
  186. If our object is not the first one then we are not in the front.</p>
  187. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const tempV = new THREE.Vector3();
  188. +const raycaster = new THREE.Raycaster();
  189. ...
  190. cubes.forEach((cubeInfo, ndx) =&gt; {
  191. const {cube, elem} = cubeInfo;
  192. const speed = 1 + ndx * .1;
  193. const rot = time * speed;
  194. cube.rotation.x = rot;
  195. cube.rotation.y = rot;
  196. // get the position of the center of the cube
  197. cube.updateWorldMatrix(true, false);
  198. cube.getWorldPosition(tempV);
  199. // get the normalized screen coordinate of that position
  200. // x and y will be in the -1 to +1 range with x = -1 being
  201. // on the left and y = -1 being on the bottom
  202. tempV.project(camera);
  203. + // ask the raycaster for all the objects that intersect
  204. + // from the eye toward this object's position
  205. + raycaster.setFromCamera(tempV, camera);
  206. + const intersectedObjects = raycaster.intersectObjects(scene.children);
  207. + // We're visible if the first intersection is this object.
  208. + const show = intersectedObjects.length &amp;&amp; cube === intersectedObjects[0].object;
  209. +
  210. + if (!show) {
  211. + // hide the label
  212. + elem.style.display = 'none';
  213. + } else {
  214. + // un-hide the label
  215. + elem.style.display = '';
  216. // convert the normalized position to CSS coordinates
  217. const x = (tempV.x * .5 + .5) * canvas.clientWidth;
  218. const y = (tempV.y * -.5 + .5) * canvas.clientHeight;
  219. // move the elem to that position
  220. elem.style.transform = `translate(-50%, -50%) translate(${x}px,${y}px)`;
  221. + }
  222. });
  223. </pre>
  224. <p>This handles overlapping.</p>
  225. <p>To handle going outside the frustum we can add this check if the origin of
  226. the object is outside the frustum by checking <code class="notranslate" translate="no">tempV.z</code></p>
  227. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">- if (!show) {
  228. + if (!show || Math.abs(tempV.z) &gt; 1) {
  229. // hide the label
  230. elem.style.display = 'none';
  231. </pre>
  232. <p>This <em>kind of</em> works because the normalized coordinates we computed include a <code class="notranslate" translate="no">z</code>
  233. value that goes from -1 when at the <code class="notranslate" translate="no">near</code> part of our camera frustum to +1 when
  234. at the <code class="notranslate" translate="no">far</code> part of our camera frustum.</p>
  235. <p></p><div translate="no" class="threejs_example_container notranslate">
  236. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/align-html-to-3d-w-hiding.html"></iframe></div>
  237. <a class="threejs_center" href="/manual/examples/align-html-to-3d-w-hiding.html" target="_blank">click here to open in a separate window</a>
  238. </div>
  239. <p></p>
  240. <p>For the frustum check, the solution above fails as we're only checking the origin of the object. For a large
  241. object. That origin might go outside the frustum but half of the object might still be in the frustum.</p>
  242. <p>A more correct solution would be to check if the object itself is in the frustum
  243. or not. Unfortunate that check is slow. For 3 cubes it will not be a problem
  244. but for many objects it might be.</p>
  245. <p>Three.js provides some functions to check if an object's bounding sphere is
  246. in a frustum</p>
  247. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">// at init time
  248. const frustum = new THREE.Frustum();
  249. const viewProjection = new THREE.Matrix4();
  250. ...
  251. // before checking
  252. camera.updateMatrix();
  253. camera.updateMatrixWorld();
  254. camera.matrixWorldInverse.copy(camera.matrixWorld).invert();
  255. ...
  256. // then for each mesh
  257. someMesh.updateMatrix();
  258. someMesh.updateMatrixWorld();
  259. viewProjection.multiplyMatrices(
  260. camera.projectionMatrix, camera.matrixWorldInverse);
  261. frustum.setFromProjectionMatrix(viewProjection);
  262. const inFrustum = frustum.contains(someMesh));
  263. </pre>
  264. <p>Our current overlapping solution has similar issues. Picking is slow. We could
  265. use gpu based picking like we covered in the <a href="picking.html">picking
  266. article</a> but that is also not free. Which solution you
  267. chose depends on your needs.</p>
  268. <p>Another issue is the order the labels appear. If we change the code to have
  269. longer labels</p>
  270. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const cubes = [
  271. - makeInstance(geometry, 0x44aa88, 0, 'Aqua'),
  272. - makeInstance(geometry, 0x8844aa, -2, 'Purple'),
  273. - makeInstance(geometry, 0xaa8844, 2, 'Gold'),
  274. + makeInstance(geometry, 0x44aa88, 0, 'Aqua Colored Box'),
  275. + makeInstance(geometry, 0x8844aa, -2, 'Purple Colored Box'),
  276. + makeInstance(geometry, 0xaa8844, 2, 'Gold Colored Box'),
  277. ];
  278. </pre>
  279. <p>and set the CSS so these don't wrap</p>
  280. <pre class="prettyprint showlinemods notranslate lang-css" translate="no">#labels&gt;div {
  281. + white-space: nowrap;
  282. </pre>
  283. <p>Then we can run into this issue</p>
  284. <div class="threejs_center"><img src="../resources/images/label-sorting-issue.png" style="width: 401px;"></div>
  285. <p>You can see above the purple box is in the back but its label is in front of the aqua box.</p>
  286. <p>We can fix this by setting the <code class="notranslate" translate="no">zIndex</code> of each element. The projected position has a <code class="notranslate" translate="no">z</code> value
  287. that goes from -1 in front to positive 1 in back. <code class="notranslate" translate="no">zIndex</code> is required to be an integer and goes the
  288. opposite direction meaning for <code class="notranslate" translate="no">zIndex</code> greater values are in front so the following code should work.</p>
  289. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">// convert the normalized position to CSS coordinates
  290. const x = (tempV.x * .5 + .5) * canvas.clientWidth;
  291. const y = (tempV.y * -.5 + .5) * canvas.clientHeight;
  292. // move the elem to that position
  293. elem.style.transform = `translate(-50%, -50%) translate(${x}px,${y}px)`;
  294. +// set the zIndex for sorting
  295. +elem.style.zIndex = (-tempV.z * .5 + .5) * 100000 | 0;
  296. </pre>
  297. <p>Because of the way the projected z value works we need to pick a large number to spread out the values
  298. otherwise many will have the same value. To make sure the labels don't overlap with other parts of
  299. the page we can tell the browser to create a new <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context">stacking context</a>
  300. by setting the <code class="notranslate" translate="no">z-index</code> of the container of the labels</p>
  301. <pre class="prettyprint showlinemods notranslate lang-css" translate="no">#labels {
  302. position: absolute; /* let us position ourself inside the container */
  303. + z-index: 0; /* make a new stacking context so children don't sort with rest of page */
  304. left: 0; /* make our position the top left of the container */
  305. top: 0;
  306. color: white;
  307. z-index: 0;
  308. }
  309. </pre>
  310. <p>and now the labels should always be in the correct order.</p>
  311. <p></p><div translate="no" class="threejs_example_container notranslate">
  312. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/align-html-to-3d-w-sorting.html"></iframe></div>
  313. <a class="threejs_center" href="/manual/examples/align-html-to-3d-w-sorting.html" target="_blank">click here to open in a separate window</a>
  314. </div>
  315. <p></p>
  316. <p>While we're at it let's do one more example to show one more issue.
  317. Let's draw a globe like Google Maps and label the countries.</p>
  318. <p>I found <a href="http://thematicmapping.org/downloads/world_borders.php">this data</a>
  319. which contains the borders of countries. It's licensed as
  320. <a href="http://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>.</p>
  321. <p>I <a href="https://github.com/mrdoob/three.js/blob/master/manual/resources/tools/geo-picking/">wrote some code</a>
  322. to load the data, and generate country outlines and some JSON data with the names
  323. of the countries and their locations.</p>
  324. <div class="threejs_center"><img src="../examples/resources/data/world/country-outlines-4k.png" style="background: black; width: 700px"></div>
  325. <p>The JSON data is an array of entries something like this</p>
  326. <pre class="prettyprint showlinemods notranslate lang-json" translate="no">[
  327. {
  328. "name": "Algeria",
  329. "min": [
  330. -8.667223,
  331. 18.976387
  332. ],
  333. "max": [
  334. 11.986475,
  335. 37.091385
  336. ],
  337. "area": 238174,
  338. "lat": 28.163,
  339. "lon": 2.632,
  340. "population": {
  341. "2005": 32854159
  342. }
  343. },
  344. ...
  345. </pre>
  346. <p>where min, max, lat, lon, are all in latitude and longitude degrees.</p>
  347. <p>Let's load it up. The code is based on the examples from <a href="optimize-lots-of-objects.html">optimizing lots of
  348. objects</a> though we are not drawing lots
  349. of objects we'll be using the same solutions for <a href="rendering-on-demand.html">rendering on
  350. demand</a>.</p>
  351. <p>The first thing is to make a sphere and use the outline texture.</p>
  352. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  353. const loader = new THREE.TextureLoader();
  354. const texture = loader.load('resources/data/world/country-outlines-4k.png', render);
  355. const geometry = new THREE.SphereGeometry(1, 64, 32);
  356. const material = new THREE.MeshBasicMaterial({map: texture});
  357. scene.add(new THREE.Mesh(geometry, material));
  358. }
  359. </pre>
  360. <p>Then let's load the JSON file by first making a loader</p>
  361. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">async function loadJSON(url) {
  362. const req = await fetch(url);
  363. return req.json();
  364. }
  365. </pre>
  366. <p>and then calling it</p>
  367. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">let countryInfos;
  368. async function loadCountryData() {
  369. countryInfos = await loadJSON('resources/data/world/country-info.json');
  370. ...
  371. }
  372. requestRenderIfNotRequested();
  373. }
  374. loadCountryData();
  375. </pre>
  376. <p>Now let's use that data to generate and place the labels.</p>
  377. <p>In the article on <a href="optimize-lots-of-objects.html">optimizing lots of objects</a>
  378. we had setup a small scene graph of helper objects to make it easy to
  379. compute latitude and longitude positions on our globe. See that article
  380. for an explanation of how they work.</p>
  381. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const lonFudge = Math.PI * 1.5;
  382. const latFudge = Math.PI;
  383. // these helpers will make it easy to position the boxes
  384. // We can rotate the lon helper on its Y axis to the longitude
  385. const lonHelper = new THREE.Object3D();
  386. // We rotate the latHelper on its X axis to the latitude
  387. const latHelper = new THREE.Object3D();
  388. lonHelper.add(latHelper);
  389. // The position helper moves the object to the edge of the sphere
  390. const positionHelper = new THREE.Object3D();
  391. positionHelper.position.z = 1;
  392. latHelper.add(positionHelper);
  393. </pre>
  394. <p>We'll use that to compute a position for each label</p>
  395. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const labelParentElem = document.querySelector('#labels');
  396. for (const countryInfo of countryInfos) {
  397. const {lat, lon, name} = countryInfo;
  398. // adjust the helpers to point to the latitude and longitude
  399. lonHelper.rotation.y = THREE.MathUtils.degToRad(lon) + lonFudge;
  400. latHelper.rotation.x = THREE.MathUtils.degToRad(lat) + latFudge;
  401. // get the position of the lat/lon
  402. positionHelper.updateWorldMatrix(true, false);
  403. const position = new THREE.Vector3();
  404. positionHelper.getWorldPosition(position);
  405. countryInfo.position = position;
  406. // add an element for each country
  407. const elem = document.createElement('div');
  408. elem.textContent = name;
  409. labelParentElem.appendChild(elem);
  410. countryInfo.elem = elem;
  411. </pre>
  412. <p>The code above looks very similar to the code we wrote for making cube labels
  413. making an element per label. When we're done we have an array, <code class="notranslate" translate="no">countryInfos</code>,
  414. with one entry for each country to which we've added an <code class="notranslate" translate="no">elem</code> property for
  415. the label element for that country and a <code class="notranslate" translate="no">position</code> with its position on the
  416. globe.</p>
  417. <p>Just like we did for the cubes we need to update the position of the
  418. labels and render time.</p>
  419. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const tempV = new THREE.Vector3();
  420. function updateLabels() {
  421. // exit if we have not yet loaded the JSON file
  422. if (!countryInfos) {
  423. return;
  424. }
  425. for (const countryInfo of countryInfos) {
  426. const {position, elem} = countryInfo;
  427. // get the normalized screen coordinate of that position
  428. // x and y will be in the -1 to +1 range with x = -1 being
  429. // on the left and y = -1 being on the bottom
  430. tempV.copy(position);
  431. tempV.project(camera);
  432. // convert the normalized position to CSS coordinates
  433. const x = (tempV.x * .5 + .5) * canvas.clientWidth;
  434. const y = (tempV.y * -.5 + .5) * canvas.clientHeight;
  435. // move the elem to that position
  436. elem.style.transform = `translate(-50%, -50%) translate(${x}px,${y}px)`;
  437. // set the zIndex for sorting
  438. elem.style.zIndex = (-tempV.z * .5 + .5) * 100000 | 0;
  439. }
  440. }
  441. </pre>
  442. <p>You can see the code above is substantially similar to the cube example before.
  443. The only major difference is we pre-computed the label positions at init time.
  444. We can do this because the globe never moves. Only our camera moves.</p>
  445. <p>Lastly we need to call <code class="notranslate" translate="no">updateLabels</code> in our render loop</p>
  446. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render() {
  447. renderRequested = false;
  448. if (resizeRendererToDisplaySize(renderer)) {
  449. const canvas = renderer.domElement;
  450. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  451. camera.updateProjectionMatrix();
  452. }
  453. controls.update();
  454. + updateLabels();
  455. renderer.render(scene, camera);
  456. }
  457. </pre>
  458. <p>And this is what we get</p>
  459. <p></p><div translate="no" class="threejs_example_container notranslate">
  460. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/align-html-elements-to-3d-globe-too-many-labels.html"></iframe></div>
  461. <a class="threejs_center" href="/manual/examples/align-html-elements-to-3d-globe-too-many-labels.html" target="_blank">click here to open in a separate window</a>
  462. </div>
  463. <p></p>
  464. <p>That is way too many labels!</p>
  465. <p>We have 2 problems.</p>
  466. <ol>
  467. <li><p>Labels facing away from us are showing up.</p>
  468. </li>
  469. <li><p>There are too many labels.</p>
  470. </li>
  471. </ol>
  472. <p>For issue #1 we can't really use the <code class="notranslate" translate="no">RayCaster</code> like we did above as there is
  473. nothing to intersect except the sphere. Instead what we can do is check if that
  474. particular country is facing away from us or not. This works because the label
  475. positions are around a sphere. In fact we're using a unit sphere, a sphere with
  476. a radius of 1.0. That means the positions are already unit directions making
  477. the math relatively easy.</p>
  478. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const tempV = new THREE.Vector3();
  479. +const cameraToPoint = new THREE.Vector3();
  480. +const cameraPosition = new THREE.Vector3();
  481. +const normalMatrix = new THREE.Matrix3();
  482. function updateLabels() {
  483. // exit if we have not yet loaded the JSON file
  484. if (!countryInfos) {
  485. return;
  486. }
  487. + const minVisibleDot = 0.2;
  488. + // get a matrix that represents a relative orientation of the camera
  489. + normalMatrix.getNormalMatrix(camera.matrixWorldInverse);
  490. + // get the camera's position
  491. + camera.getWorldPosition(cameraPosition);
  492. for (const countryInfo of countryInfos) {
  493. const {position, elem} = countryInfo;
  494. + // Orient the position based on the camera's orientation.
  495. + // Since the sphere is at the origin and the sphere is a unit sphere
  496. + // this gives us a camera relative direction vector for the position.
  497. + tempV.copy(position);
  498. + tempV.applyMatrix3(normalMatrix);
  499. +
  500. + // compute the direction to this position from the camera
  501. + cameraToPoint.copy(position);
  502. + cameraToPoint.applyMatrix4(camera.matrixWorldInverse).normalize();
  503. +
  504. + // get the dot product of camera relative direction to this position
  505. + // on the globe with the direction from the camera to that point.
  506. + // 1 = facing directly towards the camera
  507. + // 0 = exactly on tangent of the sphere from the camera
  508. + // &lt; 0 = facing away
  509. + const dot = tempV.dot(cameraToPoint);
  510. +
  511. + // if the orientation is not facing us hide it.
  512. + if (dot &lt; minVisibleDot) {
  513. + elem.style.display = 'none';
  514. + continue;
  515. + }
  516. +
  517. + // restore the element to its default display style
  518. + elem.style.display = '';
  519. // get the normalized screen coordinate of that position
  520. // x and y will be in the -1 to +1 range with x = -1 being
  521. // on the left and y = -1 being on the bottom
  522. tempV.copy(position);
  523. tempV.project(camera);
  524. // convert the normalized position to CSS coordinates
  525. const x = (tempV.x * .5 + .5) * canvas.clientWidth;
  526. const y = (tempV.y * -.5 + .5) * canvas.clientHeight;
  527. // move the elem to that position
  528. countryInfo.elem.style.transform = `translate(-50%, -50%) translate(${x}px,${y}px)`;
  529. // set the zIndex for sorting
  530. elem.style.zIndex = (-tempV.z * .5 + .5) * 100000 | 0;
  531. }
  532. }
  533. </pre>
  534. <p>Above we use the positions as a direction and get that direction relative to the
  535. camera. Then we get the camera relative direction from the camera to that
  536. position on the globe and take the <em>dot product</em>. The dot product returns the cosine
  537. of the angle between the to vectors. This gives us a value from -1
  538. to +1 where -1 means the label is facing the camera, 0 means the label is directly
  539. on the edge of the sphere relative to the camera, and anything greater than zero is
  540. behind. We then use that value to show or hide the element.</p>
  541. <div class="spread">
  542. <div>
  543. <div data-diagram="dotProduct" style="height: 400px"></div>
  544. </div>
  545. </div>
  546. <p>In the diagram above we can see the dot product of the direction the label is
  547. facing to direction from the camera to that position. If you rotate the
  548. direction you'll see the dot product is -1.0 when the direction is directly
  549. facing the camera, it's 0.0 when exactly on the tangent of the sphere relative
  550. to the camera or to put it another way it's 0 when the 2 vectors are
  551. perpendicular to each other, 90 degrees It's greater than zero with the label is
  552. behind the sphere.</p>
  553. <p>For issue #2, too many labels we need some way to decide which labels
  554. to show. One way would be to only show labels for large countries.
  555. The data we're loading contains min and max values for the area a
  556. country covers. From that we can compute an area and then use that
  557. area to decide whether or not to display the country.</p>
  558. <p>At init time let's compute the area</p>
  559. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const labelParentElem = document.querySelector('#labels');
  560. for (const countryInfo of countryInfos) {
  561. const {lat, lon, min, max, name} = countryInfo;
  562. // adjust the helpers to point to the latitude and longitude
  563. lonHelper.rotation.y = THREE.MathUtils.degToRad(lon) + lonFudge;
  564. latHelper.rotation.x = THREE.MathUtils.degToRad(lat) + latFudge;
  565. // get the position of the lat/lon
  566. positionHelper.updateWorldMatrix(true, false);
  567. const position = new THREE.Vector3();
  568. positionHelper.getWorldPosition(position);
  569. countryInfo.position = position;
  570. + // compute the area for each country
  571. + const width = max[0] - min[0];
  572. + const height = max[1] - min[1];
  573. + const area = width * height;
  574. + countryInfo.area = area;
  575. // add an element for each country
  576. const elem = document.createElement('div');
  577. elem.textContent = name;
  578. labelParentElem.appendChild(elem);
  579. countryInfo.elem = elem;
  580. }
  581. </pre>
  582. <p>Then at render time let's use the area to decide to display the label
  583. or not</p>
  584. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">+const large = 20 * 20;
  585. const maxVisibleDot = 0.2;
  586. // get a matrix that represents a relative orientation of the camera
  587. normalMatrix.getNormalMatrix(camera.matrixWorldInverse);
  588. // get the camera's position
  589. camera.getWorldPosition(cameraPosition);
  590. for (const countryInfo of countryInfos) {
  591. - const {position, elem} = countryInfo;
  592. + const {position, elem, area} = countryInfo;
  593. + // large enough?
  594. + if (area &lt; large) {
  595. + elem.style.display = 'none';
  596. + continue;
  597. + }
  598. ...
  599. </pre>
  600. <p>Finally, since I'm not sure what good values are for these settings lets
  601. add a GUI so we can play with them</p>
  602. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import * as THREE from 'three';
  603. import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
  604. +import {GUI} from 'three/addons/libs/lil-gui.module.min.js';
  605. </pre>
  606. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">+const settings = {
  607. + minArea: 20,
  608. + maxVisibleDot: -0.2,
  609. +};
  610. +const gui = new GUI({width: 300});
  611. +gui.add(settings, 'minArea', 0, 50).onChange(requestRenderIfNotRequested);
  612. +gui.add(settings, 'maxVisibleDot', -1, 1, 0.01).onChange(requestRenderIfNotRequested);
  613. function updateLabels() {
  614. if (!countryInfos) {
  615. return;
  616. }
  617. - const large = 20 * 20;
  618. - const maxVisibleDot = -0.2;
  619. + const large = settings.minArea * settings.minArea;
  620. // get a matrix that represents a relative orientation of the camera
  621. normalMatrix.getNormalMatrix(camera.matrixWorldInverse);
  622. // get the camera's position
  623. camera.getWorldPosition(cameraPosition);
  624. for (const countryInfo of countryInfos) {
  625. ...
  626. // if the orientation is not facing us hide it.
  627. - if (dot &gt; maxVisibleDot) {
  628. + if (dot &gt; settings.maxVisibleDot) {
  629. elem.style.display = 'none';
  630. continue;
  631. }
  632. </pre>
  633. <p>and here's the result</p>
  634. <p></p><div translate="no" class="threejs_example_container notranslate">
  635. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/align-html-elements-to-3d-globe.html"></iframe></div>
  636. <a class="threejs_center" href="/manual/examples/align-html-elements-to-3d-globe.html" target="_blank">click here to open in a separate window</a>
  637. </div>
  638. <p></p>
  639. <p>You can see as you rotate the earth labels that go behind disappear.
  640. Adjust the <code class="notranslate" translate="no">minVisibleDot</code> to see the cutoff change.
  641. You can also adjust the <code class="notranslate" translate="no">minArea</code> value to see larger or smaller countries
  642. appear.</p>
  643. <p>The more I worked on this the more I realized just how much
  644. work is put into Google Maps. They have also have to decide which labels to
  645. show. I'm pretty sure they use all kinds of criteria. For example your current
  646. location, your default language setting, your account settings if you have an
  647. account, they probably use population or popularity, they might give priority
  648. to the countries in the center of the view, etc ... Lots to think about.</p>
  649. <p>In any case I hope these examples gave you some idea of how to align HTML
  650. elements with your 3D. A few things I might change.</p>
  651. <p>Next up let's make it so you can <a href="indexed-textures.html">pick and highlight a country</a>.</p>
  652. <p><link rel="stylesheet" href="../resources/threejs-align-html-elements-to-3d.css"></p>
  653. <script type="module" src="../resources/threejs-align-html-elements-to-3d.js"></script>
  654. </div>
  655. </div>
  656. </div>
  657. <script src="../resources/prettify.js"></script>
  658. <script src="../resources/lesson.js"></script>
  659. </body></html>