简体   繁体   English

webgl three.js在网格上获取鼠标位置

[英]webgl three.js get mouse position on Mesh

Honestly, i tried to find solution for question below, please help me. 老实说,我试图在下面找到问题的解决方案,请帮助我。 And sorry for my English :) 对不起,我的英语:)

I have created a map, added a camera + added camera control of all dimensions. 我创建了一个地图,添加了一个摄像头+添加了所有尺寸的摄像头控件。

And when I get the position of the cursor relative to the mesh, the position is always different from different angles. 当我得到光标相对于网格的位置时,该位置总是从不同角度变化。

View all code in jsfiddle.net: http://jsfiddle.net/BT3g3/ 查看jsfiddle.net中的所有代码: http : //jsfiddle.net/BT3g3/

I use Three.js r49. 我使用Three.js r49。

This code is just responsible for calculating the position. 该代码仅负责计算位置。

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;
    }
}

I was browsing internet and came across an article where the cursor position is caught on the map, but i fail move this method to my project :( 我正在浏览互联网,偶然发现了一篇文章 ,该文章在地图上捕获了光标位置,但是我无法将此方法移至我的项目中:(

Please, help! 请帮忙!

Have you tried with following ? 您是否尝试过追踪?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM