I encountered an issue while working on a project using Three.js. In this project, users input x and y coordinates which results in a point being added to a Three.js scene. I need to calculate whether there is an object behind or in front of this point. I attempted to utilize the Raycaster feature, but struggled to find a straightforward example demonstrating how to use it with a vector defined by a point.
var projector = new THREE.Projector();
var directionVector = new THREE.Vector3();
/* How can I implement a vector specified by my initial point?
var origin = new THREE.Vector3(x, y, 0 );
Instead of using the camera vector as shown below?
*/
projector.unprojectVector(directionVector, camera);
directionVector.sub(camera.position);
directionVector.normalize();
var ray = new THREE.Raycaster();
ray.set(camera.position, directionVector)
var intersects = ray.intersectObjects(scene.children);
if (intersects.length > 0) {
console.log(intersects);
}