I am currently utilizing version 68 of three.js.
My objective is to gather the X, Y, and Z coordinates upon clicking somewhere on the canvas. Despite following the steps outlined in this guide, I am encountering a consistent Z value of 0: Mouse / Canvas X, Y to Three.js World X, Y, Z
The main idea is that when clicking within the scene where a mesh exists, the intention is to calculate the position values resembling that of the mesh itself. Please note that this scenario serves solely as an example. While I acknowledge the option of using raycasting by checking for collisions with meshes and examining their positions, my preference is for a method that functions seamlessly even without direct interaction with a mesh.
Is such functionality feasible? Referencing the provided jsfiddle: http://jsfiddle.net/j9ydgyL3/
In the aforementioned jsfiddle, targeting the center of the square should ideally yield X, Y, and Z values of 10, 10, and 10 respectively, mirroring the coordinates of the square's position. Delving into the following key functions:
function getMousePosition(clientX, clientY) {
var mouse2D = new THREE.Vector3();
var mouse3D = new THREE.Vector3();
mouse2D.x = (clientX / window.innerWidth) * 2 - 1;
mouse2D.y = -(clientY / window.innerHeight) * 2 + 1;
mouse2D.z = 0.5;
mouse3D = projector.unprojectVector(mouse2D.clone(), camera);
return mouse3D;
}
function onDocumentMouseUp(event) {
event.preventDefault();
var mouse3D = getMousePosition(event.clientX, event.clientY);
console.log(mouse3D.x + ' ' + mouse3D.y + ' ' + mouse3D.z);
}
A portion of alternative code attempts remains commented out. It's worth noting that these commented segments didn't achieve the desired outcome in the jsfiddle environment, potentially due to the utilization of version 54 of three.js. Conversely, everything operates smoothly on my end with the upgraded version 68.
Edit: To emphasize, my goal is to obtain the coordinates regardless of the cursor's location. While a mesh was employed in this demonstration for simplicity, allowing verification by aligning calculated coordinates with those of the mesh, the ultimate aim is to function sans reliance on raycasting toward a mesh. For instance, having real-time display of calculated coordinates on the console irrespective of the scene's elements would be ideal.