How to retrieve the mouse coordinates on a Three.js Mesh using WebGL

Struggling to find a solution for the issue below, I could really use some assistance. Apologies for any mistakes in my English.

I have set up a map, added a camera, and implemented camera controls for all dimensions.

However, every time I try to determine the cursor's position relative to the mesh, the results vary depending on the viewing angle.

You can view the full code on jsfiddle.net: http://jsfiddle.net/BT3g3/

My code is designed to solely calculate the position as follows:

function onDocumentMouseMove(event) {
    var vector = new THREE.Vector3(  ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 1);
    projector.unprojectVector( vector, camera );

    var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
    var intersect = ray.intersectObject( island );  

    if ( intersect.length > 0) { // !!
        document.getElementById('z').value = intersect[0].point.z;
        document.getElementById('x').value = intersect[0].point.x;
    }
}

While searching online, I found a helpful article on capturing the cursor position on a map. However, I am struggling to integrate this method into my project.

Your assistance would be greatly appreciated. Please, help!

Answer №1

Did you experiment with the following?

mouseX = ( ( event.clientX - canvas.offsetLeft ) / canvas.clientWidth ) * 2 - 1;
mouseY = - ( ( event.clientY - canvas.offsetTop ) / canvas.clientHeight ) * 2 + 1;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"When using the Fetch API to make a POST request, the req.body is found to

When attempting to call the POST API using the fetch method and passing parameters in the body, I encountered an issue where req.body was undefined on the Node.js backend side. However, when I called the same POST API through Postman with parameters in the ...

Error in TypeScript: The type 'Ticket[] | undefined' is not recognized as an array type

How can I add a new item to an array of objects in React state using TypeScript? Here is the code snippet in question: export type AppState = { tickets?: Ticket[], } export type Ticket = { id: string, title: string; } export type ApiC = { ...

The state of my React components keeps disappearing

Within my code, I have implemented a click event on the InterestBox to trigger updates in its appearance and alter the state of its parent container. However, despite inspecting the element using React Developer Tools and attempting API requests, the stat ...

Step-by-step guide on how to make a POST request with session data to a specific endpoint in a Node.js server

Currently, I am utilizing express and have a task to execute a POST request to an internal endpoint in the server. Below is the code snippet I am using: request({ url : 'http://localhost:3000/api/oauth2/authorize', qs:{ transaction_id:re ...

Issue with "Access-Control-Allow-Origin" header missing in express and react application

I've decided to work on a Node/Express project to improve my JavaScript skills, but I'm facing an issue with 'Access-Control-Allow-Origin'. For some reason, I can't do a get request from the front end and despite looking at other p ...

Easily resolving conflicts by removing `package-lock.json`

Whenever I work in a team environment, I often encounter merge conflicts with the package-lock.json file. My usual solution is to simply delete the file and generate it again using npm install. So far, I haven't noticed any negative effects from this ...

Simple method to retrieve the ID of an input field within a form using jQuery selectors

I have a form with each input field having a unique id, and I have attached a jQuery 'input' event to the form. I want to retrieve the id of the field on which the user changes some value using a jQuery function. There seems to be something missi ...

Adding fields to all objects in an array within MongoDB

I'm trying to update all objects in an array by adding a new field only if it doesn't already exist. I attempted to use the updateMany method but it doesn't seem to be working. Can someone please assist me? const updateResult = await Form.up ...

What is the best way to retrieve the source code generated by Ajax

I designed a PHP script that generates text dynamically and returns it with a unique div id. However, when I use Ajax to retrieve this text as responseText, I am unable to access the properties of the div using JavaScript because the new text is not added ...

What is the best way to showcase 3 items on each slide while retrieving the data from the database instead of using static html?

While reviewing the documentation for this slider plugin, I encountered a problem. http://www.slidesjs.com/#docs I couldn't find instructions on how to display 3 items on a single slide. For example, if I have a $data object and use a PHP foreach lo ...

"The utilization of either Threejs ArrowHelper or Line with an outlined design

Is there a way to outline an arrow or line using the ArrowHelper or Line geometries within the Three.js framework? While trying to achieve this, I encountered the issue that outlining a complex object or a line is not as straightforward as shown in this e ...

The tooltip chart is not displaying all of its data

I create a dynamic tooltip with a custom chart inside of it. tooltip: { borderRadius: 15, borderWidth: 0, shadow: false, enabled: true, backgroundColor: 'none', useHTML: true, shared: true, formatter: function() { ...

Angular's watch feature fails to trigger when the value is modified

My setup includes two sliders, one for brightness and the other for contrast. They are linked to a "brightness" model and a "contrast" model. These sliders are located within a tab interface, and I noticed that every time the tab was changed, the values we ...

organizing various kinds of data values within an array

Within this array, I have two types of variables - an integer and a string. My goal is to sort the array either alphabetically or by the length of the string. However, it seems that the sorting algorithm is prioritizing the integer over the string. ...

Steps for creating a dynamic validation using a new form control

I have an item that needs to generate a form const textBox = { fontColor: 'blue', fontSize: '18', placeholder: 'email', name: 'input email', label: 'john', validation: { required: false } ...

Problem with Masonry.js only occurring on live WordPress website, not on local development site

I have been working on the following website: This website is built using a Wordpress theme with Masonry.js integration. After downloading and cloning the site's database, I noticed that it displays perfectly on my local MAMP Pro environment. Howev ...

Is it possible for me to utilize a validation function to display error messages within a specific span id tag?

document.getElementById("button1").addEventListener("click", mouseOver1); function mouseOver1(){ document.getElementById("button1").style.color = "red"; } document.getElementById("button2").addEventListener("click", mouseOver); function mous ...

Creating an array of objects by utilizing another array - the process explained

I am looking to create a new array by pushing objects after checking the values in an existing array. Here are the conditions: var ar=['aa','cc','po'] var arr =[{name:"po"},{name:'aa'},{name:'cc'}]; Desi ...

An unfamiliar data type is provided as a number but is treated as a string that behaves like a number

Here is the code snippet in question: let myVar = unknown; myVar = 5; console.log((myVar as string) + 5); Upon running this code, it surprisingly outputs 10 instead of what I expected to be 55. Can someone help me understand why? ...

Is it possible to leverage both functions and variables within the ng-options expression in Angularjs?

I am faced with a situation where I have 2 select boxes. The first one is used to choose the user type (such as groups or individual), and the second one displays the options based on the selection made in the first box. I was wondering if it is possible t ...