threejs-picking-raycaster-transparency.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 - Picking - RayCaster w/Transparency</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. position: fixed;
  12. }
  13. #c {
  14. width: 100vw;
  15. height: 100vh;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script src="resources/threejs/r98/three.min.js"></script>
  24. <script>
  25. 'use strict';
  26. /* global THREE */
  27. function main() {
  28. const canvas = document.querySelector('#c');
  29. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  30. const fov = 60;
  31. const aspect = 2; // the canvas default
  32. const near = 0.1;
  33. const far = 200;
  34. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  35. camera.position.z = 30;
  36. const scene = new THREE.Scene();
  37. scene.background = new THREE.Color('white');
  38. // put the camera on a pole (parent it to an object)
  39. // so we can spin the pole to move the camera around the scene
  40. const cameraPole = new THREE.Object3D();
  41. scene.add(cameraPole);
  42. cameraPole.add(camera);
  43. {
  44. const color = 0xFFFFFF;
  45. const intensity = 1;
  46. const light = new THREE.DirectionalLight(color, intensity);
  47. light.position.set(-1, 2, 4);
  48. scene.add(light);
  49. }
  50. const boxWidth = 1;
  51. const boxHeight = 1;
  52. const boxDepth = 1;
  53. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  54. function rand(min, max) {
  55. if (max === undefined) {
  56. max = min;
  57. min = 0;
  58. }
  59. return min + (max - min) * Math.random();
  60. }
  61. function randomColor() {
  62. return `hsl(${rand(360) | 0}, ${rand(50, 100) | 0}%, 50%)`;
  63. }
  64. const loader = new THREE.TextureLoader();
  65. const texture = loader.load('resources/images/frame.png');
  66. const numObjects = 100;
  67. for (let i = 0; i < numObjects; ++i) {
  68. const material = new THREE.MeshPhongMaterial({
  69. color: randomColor(),
  70. map: texture,
  71. transparent: true,
  72. side: THREE.DoubleSide,
  73. alphaTest: 0.1,
  74. });
  75. const cube = new THREE.Mesh(geometry, material);
  76. scene.add(cube);
  77. cube.position.set(rand(-20, 20), rand(-20, 20), rand(-20, 20));
  78. cube.rotation.set(rand(Math.PI), rand(Math.PI), 0);
  79. cube.scale.set(rand(3, 6), rand(3, 6), rand(3, 6));
  80. }
  81. function resizeRendererToDisplaySize(renderer) {
  82. const canvas = renderer.domElement;
  83. const width = canvas.clientWidth;
  84. const height = canvas.clientHeight;
  85. const needResize = canvas.width !== width || canvas.height !== height;
  86. if (needResize) {
  87. renderer.setSize(width, height, false);
  88. }
  89. return needResize;
  90. }
  91. class PickHelper {
  92. constructor() {
  93. this.raycaster = new THREE.Raycaster();
  94. this.pickedObject = null;
  95. this.pickedObjectSavedColor = 0;
  96. }
  97. pick(normalizedPosition, scene, camera, time) {
  98. // restore the color if there is a picked object
  99. if (this.pickedObject) {
  100. this.pickedObject.material.emissive.setHex(this.pickedObjectSavedColor);
  101. this.pickedObject = undefined;
  102. }
  103. // cast a ray through the frustum
  104. this.raycaster.setFromCamera(normalizedPosition, camera);
  105. // get the list of objects the ray intersected
  106. const intersectedObjects = this.raycaster.intersectObjects(scene.children);
  107. if (intersectedObjects.length) {
  108. // pick the first object. It's the closest one
  109. this.pickedObject = intersectedObjects[0].object;
  110. // save its color
  111. this.pickedObjectSavedColor = this.pickedObject.material.emissive.getHex();
  112. // set its emissive color to flashing red/yellow
  113. this.pickedObject.material.emissive.setHex((time * 8) % 2 > 1 ? 0xFFFF00 : 0xFF0000);
  114. }
  115. }
  116. }
  117. const pickPosition = {x: 0, y: 0};
  118. const pickHelper = new PickHelper();
  119. clearPickPosition();
  120. function render(time) {
  121. time *= 0.001; // convert to seconds;
  122. if (resizeRendererToDisplaySize(renderer)) {
  123. const canvas = renderer.domElement;
  124. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  125. camera.updateProjectionMatrix();
  126. }
  127. cameraPole.rotation.y = time * .1;
  128. pickHelper.pick(pickPosition, scene, camera, time);
  129. renderer.render(scene, camera);
  130. requestAnimationFrame(render);
  131. }
  132. requestAnimationFrame(render);
  133. function setPickPosition(event) {
  134. pickPosition.x = (event.clientX / canvas.clientWidth ) * 2 - 1;
  135. pickPosition.y = (event.clientY / canvas.clientHeight) * -2 + 1; // note we flip Y
  136. }
  137. function clearPickPosition() {
  138. // unlike the mouse which always has a position
  139. // if the user stops touching the screen we want
  140. // to stop picking. For now we just pick a value
  141. // unlikely to pick something
  142. pickPosition.x = -100000;
  143. pickPosition.y = -100000;
  144. }
  145. window.addEventListener('mousemove', setPickPosition);
  146. window.addEventListener('mouseout', clearPickPosition);
  147. window.addEventListener('mouseleave', clearPickPosition);
  148. window.addEventListener('touchstart', (event) => {
  149. // prevent the window from scrolling
  150. event.preventDefault();
  151. setPickPosition(event.touches[0]);
  152. }, {passive: false});
  153. window.addEventListener('touchmove', (event) => {
  154. setPickPosition(event.touches[0]);
  155. });
  156. window.addEventListener('touchend', clearPickPosition);
  157. }
  158. main();
  159. </script>
  160. </html>