align-html-to-3d-w-sorting.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Align HTML Elements w/hiding</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%; /* let our container decide our size */
  15. height: 100%;
  16. display: block;
  17. }
  18. #container {
  19. position: relative; /* makes this the origin of its children */
  20. width: 100%;
  21. height: 100%;
  22. overflow: hidden;
  23. }
  24. #labels {
  25. position: absolute; /* let us position ourself inside the container */
  26. z-index: 0; /* make a new stacking context so children don't sort with rest of page */
  27. left: 0; /* make our position the top left of the container */
  28. top: 0;
  29. color: white;
  30. }
  31. #labels>div {
  32. white-space: nowrap;
  33. position: absolute; /* let us position them inside the container */
  34. left: 0; /* make their default position the top left of the container */
  35. top: 0;
  36. cursor: pointer; /* change the cursor to a hand when over us */
  37. font-size: large;
  38. user-select: none; /* don't let the text get selected */
  39. text-shadow: /* create a black outline */
  40. -1px -1px 0 #000,
  41. 0 -1px 0 #000,
  42. 1px -1px 0 #000,
  43. 1px 0 0 #000,
  44. 1px 1px 0 #000,
  45. 0 1px 0 #000,
  46. -1px 1px 0 #000,
  47. -1px 0 0 #000;
  48. }
  49. #labels>div:hover {
  50. color: red;
  51. }
  52. </style>
  53. </head>
  54. <body>
  55. <div id="container">
  56. <canvas id="c"></canvas>
  57. <div id="labels"></div>
  58. </div>
  59. </body>
  60. <!-- Import maps polyfill -->
  61. <!-- Remove this when import maps will be widely supported -->
  62. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  63. <script type="importmap">
  64. {
  65. "imports": {
  66. "three": "../../build/three.module.js",
  67. "three/addons/": "../../examples/jsm/"
  68. }
  69. }
  70. </script>
  71. <script type="module">
  72. import * as THREE from 'three';
  73. import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
  74. function main() {
  75. const canvas = document.querySelector('#c');
  76. const renderer = new THREE.WebGLRenderer({canvas});
  77. const fov = 75;
  78. const aspect = 2; // the canvas default
  79. const near = 1.1;
  80. const far = 20;
  81. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  82. camera.position.z = 7;
  83. const controls = new OrbitControls(camera, canvas);
  84. controls.target.set(0, 0, 0);
  85. controls.update();
  86. const scene = new THREE.Scene();
  87. {
  88. const color = 0xFFFFFF;
  89. const intensity = 1;
  90. const light = new THREE.DirectionalLight(color, intensity);
  91. light.position.set(-1, 2, 4);
  92. scene.add(light);
  93. }
  94. const boxWidth = 1;
  95. const boxHeight = 1;
  96. const boxDepth = 1;
  97. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  98. const labelContainerElem = document.querySelector('#labels');
  99. function makeInstance(geometry, color, x, name) {
  100. const material = new THREE.MeshPhongMaterial({color});
  101. const cube = new THREE.Mesh(geometry, material);
  102. scene.add(cube);
  103. cube.position.x = x;
  104. const elem = document.createElement('div');
  105. elem.textContent = name;
  106. labelContainerElem.appendChild(elem);
  107. return {cube, elem};
  108. }
  109. const cubes = [
  110. makeInstance(geometry, 0x44aa88, 0, 'Aqua Colored Box'),
  111. makeInstance(geometry, 0x8844aa, -2, 'Purple Colored Box'),
  112. makeInstance(geometry, 0xaa8844, 2, 'Gold Colored Box'),
  113. ];
  114. function resizeRendererToDisplaySize(renderer) {
  115. const canvas = renderer.domElement;
  116. const width = canvas.clientWidth;
  117. const height = canvas.clientHeight;
  118. const needResize = canvas.width !== width || canvas.height !== height;
  119. if (needResize) {
  120. renderer.setSize(width, height, false);
  121. }
  122. return needResize;
  123. }
  124. const tempV = new THREE.Vector3();
  125. const raycaster = new THREE.Raycaster();
  126. function render(time) {
  127. time *= 0.001;
  128. if (resizeRendererToDisplaySize(renderer)) {
  129. const canvas = renderer.domElement;
  130. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  131. camera.updateProjectionMatrix();
  132. }
  133. cubes.forEach((cubeInfo, ndx) => {
  134. const {cube, elem} = cubeInfo;
  135. const speed = 1 + ndx * .1;
  136. const rot = time * speed;
  137. cube.rotation.x = rot;
  138. cube.rotation.y = rot;
  139. // get the position of the center of the cube
  140. cube.updateWorldMatrix(true, false);
  141. cube.getWorldPosition(tempV);
  142. // get the normalized screen coordinate of that position
  143. // x and y will be in the -1 to +1 range with x = -1 being
  144. // on the left and y = -1 being on the bottom
  145. tempV.project(camera);
  146. // ask the raycaster for all the objects that intersect
  147. // from the eye toward this object's position
  148. raycaster.setFromCamera(tempV, camera);
  149. const intersectedObjects = raycaster.intersectObjects(scene.children);
  150. // We're visible if the first intersection is this object.
  151. const show = intersectedObjects.length && cube === intersectedObjects[0].object;
  152. if (!show || Math.abs(tempV.z) > 1) {
  153. // hide the label
  154. elem.style.display = 'none';
  155. } else {
  156. // unhide the label
  157. elem.style.display = '';
  158. // convert the normalized position to CSS coordinates
  159. const x = (tempV.x * .5 + .5) * canvas.clientWidth;
  160. const y = (tempV.y * -.5 + .5) * canvas.clientHeight;
  161. // move the elem to that position
  162. elem.style.transform = `translate(-50%, -50%) translate(${x}px,${y}px)`;
  163. // set the zIndex for sorting
  164. elem.style.zIndex = (-tempV.z * .5 + .5) * 100000 | 0;
  165. }
  166. });
  167. renderer.render(scene, camera);
  168. requestAnimationFrame(render);
  169. }
  170. requestAnimationFrame(render);
  171. }
  172. main();
  173. </script>
  174. </html>