JavaScript Three.js ray casting for particular Points

I'm working on a network visualization using Three.js and am having trouble determining why my ray casting implementation is not identifying the correct points (Full example and source).

More specifically, I'm trying to use ray casting with a Points cloud, and am attempting to change the color of a point to white once users hover on the point. Right now, hovering points does change the color of points, but the event seems to be triggered on points near the cursor rather than immediately below the cursor.

Here is the code I'm using to initialize the ray caster:

// configure the raycaster
raycaster = new THREE.Raycaster();
raycaster.params.Points.threshold = 20;

Here is the render function:

function render() {
  renderer.render(scene, camera);
  controls.update();
  raycast();
}

// Color hovered points
function raycast() {
  raycaster.setFromCamera(mouse, camera); 
  var intersects = raycaster.intersectObject(particles);
  if (intersects.length) {

    var newColor = new THREE.Color();
    newColor.setRGB( 1, 1, 1 );

    var index = intersects[0].index;
    particles.geometry.colors[index] = newColor;
    particles.geometry.colorsNeedUpdate=true;
  }
}

And finally, the callback to the mousemove event triggered on the body:

/**
* Mouse coordinates go from 0 to container width {0:1} 
* and 0 to container height {0:1}. Multiply by 2
* and subtract 1 to center the mouse coords {-1:1}.
* Furthermore, negate the y axis coords, as in the DOM
* the origin is in the top left corner, while in WebGL
* the origin is in the bottom left corner.
**/

function onDocumentMousemove(e) {
  mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}

Does anyone know why this code doesn't highlight the right points when users mouse around? Any insights would be very helpful!

Answer:1

Sounds easy probably, but not for a beginner programmer :) I have a simple 3 field form with a submit button and a clear button. This is for a homework assignment, and we have been tasked to get the ...

Sounds easy probably, but not for a beginner programmer :) I have a simple 3 field form with a submit button and a clear button. This is for a homework assignment, and we have been tasked to get the ...

I'm new to Angular JS. Sorry for this simple question. I have learnt something from this tutorial: http://courseware.codeschool.com/shaping-up-with-angular-js/Slides/level01-05.pdf And I have tried ...

I'm new to Angular JS. Sorry for this simple question. I have learnt something from this tutorial: http://courseware.codeschool.com/shaping-up-with-angular-js/Slides/level01-05.pdf And I have tried ...

In this code, I want to display show more link, after 3 buttons: I know, how to display show more button after x words, but how to display show more link after buttons? This is my code to display ...

In this code, I want to display show more link, after 3 buttons: I know, how to display show more button after x words, but how to display show more link after buttons? This is my code to display ...

  1. show read more in jquery
  2. show read more in php
  3. show read more in wordpress
  4. today show read more today
  5. show hide read more javascript
  6. wordpress show read more link
  7. how to show read more in html
  8. javascript hide/show read more for long text
  9. how to show read more link in wordpress post
  10. how to show read more in textview in android
  11. read more show less
  12. read more show hide text
  13. read more show less jquery
  14. read more show less javascript
  15. read more show hide jquery
  16. read more show less css
  17. read more show less bootstrap
  18. read more show hide div
  19. wp-show-posts-read-more
  20. read more. show less. reply

I'm using Rails 4.1.1 and Ruby 2.1.2p95. I am using Slim as my HTML templating language (similar to HAML). I'm using Thin as my Rack server. I am trying to make a fully-AJAX application (one of my ...

I'm using Rails 4.1.1 and Ruby 2.1.2p95. I am using Slim as my HTML templating language (similar to HAML). I'm using Thin as my Rack server. I am trying to make a fully-AJAX application (one of my ...

  1. rails default text
  2. rails textarea default value
  3. rails text default value
  4. rails select default text
  5. rails default value text_field