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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 src="resources/threejs/r102/three.min.js"></script>
  60. <script src="resources/threejs/r102/js/controls/OrbitControls.js"></script>
  61. <script>
  62. 'use strict';
  63. /* global THREE */
  64. function main() {
  65. const canvas = document.querySelector('#c');
  66. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  67. const fov = 75;
  68. const aspect = 2; // the canvas default
  69. const near = 1.1;
  70. const far = 20;
  71. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  72. camera.position.z = 7;
  73. const controls = new THREE.OrbitControls(camera, canvas);
  74. controls.target.set(0, 0, 0);
  75. controls.update();
  76. const scene = new THREE.Scene();
  77. {
  78. const color = 0xFFFFFF;
  79. const intensity = 1;
  80. const light = new THREE.DirectionalLight(color, intensity);
  81. light.position.set(-1, 2, 4);
  82. scene.add(light);
  83. }
  84. const boxWidth = 1;
  85. const boxHeight = 1;
  86. const boxDepth = 1;
  87. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  88. const labelContainerElem = document.querySelector('#labels');
  89. function makeInstance(geometry, color, x, name) {
  90. const material = new THREE.MeshPhongMaterial({color});
  91. const cube = new THREE.Mesh(geometry, material);
  92. scene.add(cube);
  93. cube.position.x = x;
  94. const elem = document.createElement('div');
  95. elem.textContent = name;
  96. labelContainerElem.appendChild(elem);
  97. return {cube, elem};
  98. }
  99. const cubes = [
  100. makeInstance(geometry, 0x44aa88, 0, 'Aqua Colored Box'),
  101. makeInstance(geometry, 0x8844aa, -2, 'Purple Colored Box'),
  102. makeInstance(geometry, 0xaa8844, 2, 'Gold Colored Box'),
  103. ];
  104. function resizeRendererToDisplaySize(renderer) {
  105. const canvas = renderer.domElement;
  106. const width = canvas.clientWidth;
  107. const height = canvas.clientHeight;
  108. const needResize = canvas.width !== width || canvas.height !== height;
  109. if (needResize) {
  110. renderer.setSize(width, height, false);
  111. }
  112. return needResize;
  113. }
  114. const tempV = new THREE.Vector3();
  115. const raycaster = new THREE.Raycaster();
  116. function render(time) {
  117. time *= 0.001;
  118. if (resizeRendererToDisplaySize(renderer)) {
  119. const canvas = renderer.domElement;
  120. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  121. camera.updateProjectionMatrix();
  122. }
  123. cubes.forEach((cubeInfo, ndx) => {
  124. const {cube, elem} = cubeInfo;
  125. const speed = 1 + ndx * .1;
  126. const rot = time * speed;
  127. cube.rotation.x = rot;
  128. cube.rotation.y = rot;
  129. // get the position of the center of the cube
  130. cube.updateWorldMatrix(true, false);
  131. cube.getWorldPosition(tempV);
  132. // get the normalized screen coordinate of that position
  133. // x and y will be in the -1 to +1 range with x = -1 being
  134. // on the left and y = -1 being on the bottom
  135. tempV.project(camera);
  136. // ask the raycaster for all the objects that intersect
  137. // from the eye toward this object's position
  138. raycaster.setFromCamera(tempV, camera);
  139. const intersectedObjects = raycaster.intersectObjects(scene.children);
  140. // We're visible if the first intersection is this object.
  141. const show = intersectedObjects.length && cube === intersectedObjects[0].object;
  142. if (!show || Math.abs(tempV.z) > 1) {
  143. // hide the label
  144. elem.style.display = 'none';
  145. } else {
  146. // unhide the label
  147. elem.style.display = '';
  148. // convert the normalized position to CSS coordinates
  149. const x = (tempV.x * .5 + .5) * canvas.clientWidth;
  150. const y = (tempV.y * -.5 + .5) * canvas.clientHeight;
  151. // move the elem to that position
  152. elem.style.transform = `translate(-50%, -50%) translate(${x}px,${y}px)`;
  153. // set the zIndex for sorting
  154. elem.style.zIndex = (-tempV.z * .5 + .5) * 100000 | 0;
  155. }
  156. });
  157. renderer.render(scene, camera);
  158. requestAnimationFrame(render);
  159. }
  160. requestAnimationFrame(render);
  161. }
  162. main();
  163. </script>
  164. </html>