threejs-align-html-to-3d-w-hiding.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. left: 0; /* make our position the top left of the container */
  26. top: 0;
  27. color: white;
  28. }
  29. #labels>div {
  30. position: absolute; /* let us position them inside the container */
  31. left: 0; /* make their default position the top left of the container */
  32. top: 0;
  33. cursor: pointer; /* change the cursor to a hand when over us */
  34. font-size: large;
  35. user-select: none; /* don't let the text get selected */
  36. text-shadow: /* create a black outline */
  37. -1px -1px 0 #000,
  38. 0 -1px 0 #000,
  39. 1px -1px 0 #000,
  40. 1px 0 0 #000,
  41. 1px 1px 0 #000,
  42. 0 1px 0 #000,
  43. -1px 1px 0 #000,
  44. -1px 0 0 #000;
  45. }
  46. #labels>div:hover {
  47. color: red;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div id="container">
  53. <canvas id="c"></canvas>
  54. <div id="labels"></div>
  55. </div>
  56. </body>
  57. <script src="resources/threejs/r102/three.min.js"></script>
  58. <script src="resources/threejs/r102/js/controls/OrbitControls.js"></script>
  59. <script>
  60. 'use strict';
  61. /* global THREE */
  62. function main() {
  63. const canvas = document.querySelector('#c');
  64. const renderer = new THREE.WebGLRenderer({canvas: 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 THREE.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'),
  99. makeInstance(geometry, 0x8844aa, -2, 'Purple'),
  100. makeInstance(geometry, 0xaa8844, 2, 'Gold'),
  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. }
  152. });
  153. renderer.render(scene, camera);
  154. requestAnimationFrame(render);
  155. }
  156. requestAnimationFrame(render);
  157. }
  158. main();
  159. </script>
  160. </html>