1

For instance, if I point with the mouse at a circle in the scene, being able to detect that. I have looked everywhere and can't seem to find anything. Three.js documentation doesn't really talk about it either.

LJᛃ
  • 7,655
  • 2
  • 24
  • 35
Aldmeri
  • 187
  • 1
  • 2
  • 9

1 Answers1

1

Firstly you should add Event Listeners like 'mousemove', 'mousedown'.

document.addEventListener('mousedown', onDocumentMouseDown, false);
document.addEventListener('mousemove', onDocumentMouseMove, false); 

So you want an event while moving your mouse over the circle. So lets make an alert that on mouse move on that circle you will get an alert.

function onDocumentMouseDown(event) {

    event.preventDefault();

    mouseYOnMouseDown = event.clientY - windowHalfY;
    mouseXOnMouseDown = event.clientX - windowHalfX;

    var vector = new THREE.Vector3((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, 0.5);
    vector = vector.unproject(camera);

    var raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());
    var intersects = raycaster.intersectObjects(circleObj, true); // Circle element which you want to identify

    if (intersects.length > 0) {
        alert("Mouse on Circle");
    }

}

In three.js we use raycaster to point any object. Go through the raycaster in three.js documentation.

halfer
  • 19,824
  • 17
  • 99
  • 186
disciple
  • 252
  • 1
  • 3
  • 13
  • This is a really great answer. Thank you so much! – Aldmeri Aug 19 '15 at 12:47
  • 2
    Do not instantiate a new raycaster each time. Create one and reuse it. See [this post](http://stackoverflow.com/questions/26652888/three-js-orthographic-camera-object-picking/26656345#26656345) for the new pattern to use. – WestLangley Aug 19 '15 at 13:18