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.
Asked
Active
Viewed 5,202 times
1
-
Is "picking" the word you are looking for? like http://threejs.org/examples/webgl_interactive_cubes_gpu.html – WacławJasper Aug 19 '15 at 05:17
-
Or http://alexan0308.github.io/threejs/examples/controls_events_piano.html – gevaraweb Aug 19 '15 at 10:00
-
Yeah, it is exactly like that. – Aldmeri Aug 19 '15 at 12:48
1 Answers
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.
-
-
2Do 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