threejs-picking-raycaster-complex-geo.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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</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. camera.add(light);
  49. }
  50. const geometry = new THREE.SphereBufferGeometry(.6, 100, 100);
  51. function rand(min, max) {
  52. if (max === undefined) {
  53. max = min;
  54. min = 0;
  55. }
  56. return min + (max - min) * Math.random();
  57. }
  58. function randomColor() {
  59. return `hsl(${rand(360) | 0}, ${rand(50, 100) | 0}%, 50%)`;
  60. }
  61. const numObjects = 100;
  62. for (let i = 0; i < numObjects; ++i) {
  63. const material = new THREE.MeshPhongMaterial({color: randomColor()});
  64. const cube = new THREE.Mesh(geometry, material);
  65. scene.add(cube);
  66. cube.position.set(rand(-20, 20), rand(-20, 20), rand(-20, 20));
  67. cube.rotation.set(rand(Math.PI), rand(Math.PI), 0);
  68. cube.scale.set(rand(3, 6), rand(3, 6), rand(3, 6));
  69. }
  70. function resizeRendererToDisplaySize(renderer) {
  71. const canvas = renderer.domElement;
  72. const width = canvas.clientWidth;
  73. const height = canvas.clientHeight;
  74. const needResize = canvas.width !== width || canvas.height !== height;
  75. if (needResize) {
  76. renderer.setSize(width, height, false);
  77. }
  78. return needResize;
  79. }
  80. class PickHelper {
  81. constructor() {
  82. this.raycaster = new THREE.Raycaster();
  83. this.pickedObject = null;
  84. this.pickedObjectSavedColor = 0;
  85. }
  86. pick(normalizedPosition, scene, camera, time) {
  87. // restore the color if there is a picked object
  88. if (this.pickedObject) {
  89. this.pickedObject.material.emissive.setHex(this.pickedObjectSavedColor);
  90. this.pickedObject = undefined;
  91. }
  92. this.raycaster.setFromCamera(normalizedPosition, camera);
  93. const intersectedObjects = this.raycaster.intersectObjects(scene.children);
  94. if (intersectedObjects.length) {
  95. this.pickedObject = intersectedObjects[0].object;
  96. this.pickedObjectSavedColor = this.pickedObject.material.emissive.getHex();
  97. this.pickedObject.material.emissive.setHex((time * 8) % 2 > 1 ? 0xFFFF00 : 0xFF0000);
  98. }
  99. }
  100. }
  101. const pickHelper = new PickHelper();
  102. const pickPosition = {x: -1, y: -1};
  103. clearPickPosition();
  104. function render(time) {
  105. time *= 0.001; // convert to seconds;
  106. if (resizeRendererToDisplaySize(renderer)) {
  107. const canvas = renderer.domElement;
  108. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  109. camera.updateProjectionMatrix();
  110. }
  111. cameraPole.rotation.y = time * .1;
  112. pickHelper.pick(pickPosition, scene, camera, time);
  113. renderer.render(scene, camera);
  114. requestAnimationFrame(render);
  115. }
  116. requestAnimationFrame(render);
  117. function setPickPosition(event) {
  118. pickPosition.x = event.clientX;
  119. pickPosition.y = event.clientY;
  120. }
  121. function clearPickPosition() {
  122. // unlike the mouse which always has a position
  123. // if the user stops touching the screen we want
  124. // to stop picking. For now we just pick a value
  125. // unlikely to pick something
  126. pickPosition.x = -100000;
  127. pickPosition.y = -100000;
  128. }
  129. window.addEventListener('mousemove', setPickPosition);
  130. window.addEventListener('mouseout', clearPickPosition);
  131. window.addEventListener('mouseleave', clearPickPosition);
  132. window.addEventListener('touchstart', (event) => {
  133. // prevent the window from scrolling
  134. event.preventDefault();
  135. setPickPosition(event.touches[0]);
  136. }, {passive: false});
  137. window.addEventListener('touchmove', (event) => {
  138. setPickPosition(event.touches[0]);
  139. });
  140. window.addEventListener('touchend', clearPickPosition);
  141. }
  142. main();
  143. </script>
  144. </html>