shared-picking.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. /* global THREE */
  3. const state = {
  4. width: 300, // canvas default
  5. height: 150, // canvas default
  6. };
  7. const pickPosition = {x: 0, y: 0};
  8. function init(data) { // eslint-disable-line no-unused-vars
  9. const {canvas} = data;
  10. const renderer = new THREE.WebGLRenderer({canvas});
  11. state.width = canvas.width;
  12. state.height = canvas.height;
  13. const fov = 75;
  14. const aspect = 2; // the canvas default
  15. const near = 0.1;
  16. const far = 100;
  17. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  18. camera.position.z = 4;
  19. const scene = new THREE.Scene();
  20. {
  21. const color = 0xFFFFFF;
  22. const intensity = 1;
  23. const light = new THREE.DirectionalLight(color, intensity);
  24. light.position.set(-1, 2, 4);
  25. scene.add(light);
  26. }
  27. const boxWidth = 1;
  28. const boxHeight = 1;
  29. const boxDepth = 1;
  30. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  31. function makeInstance(geometry, color, x) {
  32. const material = new THREE.MeshPhongMaterial({
  33. color,
  34. });
  35. const cube = new THREE.Mesh(geometry, material);
  36. scene.add(cube);
  37. cube.position.x = x;
  38. return cube;
  39. }
  40. const cubes = [
  41. makeInstance(geometry, 0x44aa88, 0),
  42. makeInstance(geometry, 0x8844aa, -2),
  43. makeInstance(geometry, 0xaa8844, 2),
  44. ];
  45. class PickHelper {
  46. constructor() {
  47. this.raycaster = new THREE.Raycaster();
  48. this.pickedObject = null;
  49. this.pickedObjectSavedColor = 0;
  50. }
  51. pick(normalizedPosition, scene, camera, time) {
  52. // restore the color if there is a picked object
  53. if (this.pickedObject) {
  54. this.pickedObject.material.emissive.setHex(this.pickedObjectSavedColor);
  55. this.pickedObject = undefined;
  56. }
  57. // cast a ray through the frustum
  58. this.raycaster.setFromCamera(normalizedPosition, camera);
  59. // get the list of objects the ray intersected
  60. const intersectedObjects = this.raycaster.intersectObjects(scene.children);
  61. if (intersectedObjects.length) {
  62. // pick the first object. It's the closest one
  63. this.pickedObject = intersectedObjects[0].object;
  64. // save its color
  65. this.pickedObjectSavedColor = this.pickedObject.material.emissive.getHex();
  66. // set its emissive color to flashing red/yellow
  67. this.pickedObject.material.emissive.setHex((time * 8) % 2 > 1 ? 0xFFFF00 : 0xFF0000);
  68. }
  69. }
  70. }
  71. const pickHelper = new PickHelper();
  72. function resizeRendererToDisplaySize(renderer) {
  73. const canvas = renderer.domElement;
  74. const width = state.width;
  75. const height = state.height;
  76. const needResize = canvas.width !== width || canvas.height !== height;
  77. if (needResize) {
  78. renderer.setSize(width, height, false);
  79. }
  80. return needResize;
  81. }
  82. function render(time) {
  83. time *= 0.001;
  84. if (resizeRendererToDisplaySize(renderer)) {
  85. camera.aspect = state.width / state.height;
  86. camera.updateProjectionMatrix();
  87. }
  88. cubes.forEach((cube, ndx) => {
  89. const speed = 1 + ndx * .1;
  90. const rot = time * speed;
  91. cube.rotation.x = rot;
  92. cube.rotation.y = rot;
  93. });
  94. pickHelper.pick(pickPosition, scene, camera, time);
  95. renderer.render(scene, camera);
  96. requestAnimationFrame(render);
  97. }
  98. requestAnimationFrame(render);
  99. }