threejs-align-html-to-3d-w-sorting.html 5.6 KB

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