I wanted to share a more detailed account of my experiences in implementing this process, as I believe the previous answers lack in providing a comprehensive explanation.
In my scenario, I utilized STL_Viewer which can be found at this link (). This tool assists in creating a threeJS scene and provides the necessary camera and scene components required for this task.
function mouse_click_normalize(clientX,clientY,offset_left,offset_top){
var mouse = new THREE.Vector2(); // initialize once
var c = document.getElementById("stl_contEye").childNodes[0];//retrieve canvas element created here
mouse.x = ( (event.clientX-offset_left) / (c.clientWidth) * 2) - 1;
mouse.y = - ( (event.clientY-offset_top) / (c.clientHeight) ) * 2 + 1;
console.log(`clientX=${clientX},clientY=${clientY},mouse.x=${mouse.x},mouse.y=${mouse.y}\nwidth=${c.clientWidth},height=${c.clientHeight},offset_left=${offset_left},offset_top=${offset_top}`);
return mouse;
}
The above function automatically normalizes the x,y coordinates to the clicked space (between -1 and 1), as required by the raycaster for its setFromCamera function.
To account for potential offsets in the screen section being clicked, I designed it this way to ensure it functions correctly regardless of its position in the DOM. Simply replace "stl_contEye" with the name of the div containing the rendered ThreeJS or STLViewer in your case.
function model_clicked(model_id, e, distance, click_type){
console.log(e);
var pos = new THREE.Vector3(); // initialize once and reuse
// var vec = new THREE.Vector3(); // initialize once and reuse
var camera = stl_viewer1.camera;
var scene = stl_viewer1.scene;
// vec.unproject( camera );
// vec.sub( camera.position ).normalize();
// //var distance2 = (distance - camera.position.z) / vec.z;
// pos.copy( camera.position ).add( vec.multiplyScalar( distance ) );
var mouse_coords_normalized = mouse_click_normalize(e.clientX,e.clientY,e.srcElement.offsetLeft,e.srcElement.offsetTop);
var raycaster = new THREE.Raycaster(); // initialize once
raycaster.setFromCamera( mouse_coords_normalized, camera );
//console.log(raycaster);
//console.log(scene);
var intersects = raycaster.intersectObjects( scene.children, true );
if(intersects.length > 0){
console.log(intersects);
console.log(intersects[0].point);
pos = intersects[0].point
}
}
This method allows you to pinpoint the exact location in 3D space where you clicked. The model_clicked function simply returns an event containing the clientX or clientY, which you must obtain yourself if not using STLViewer's event for click detection. Various examples are provided in the previous answers for this.
I hope this guide proves helpful to those navigating this process, whether with or without stl_viewer.