Iam Coder
Iam Coder

Reputation: 1003

Mouse coordinates irrelevant after zooming, is it bug?

I have a problem about getting the mouse coordinates, it behaves irrelevant after zooming.

I have a JS fiddle link of my code, it will show what the problem I face, is it bug in three.js or the way I approach to draw a line is wrong, please give your feedback.

http://jsfiddle.net/ebeit303/ceej4jxq/1/

var elem = self.renderer.domElement,
                boundingRect = elem.getBoundingClientRect(),
                x = (e.clientX - boundingRect.left) * (elem.width / boundingRect.width),
                y = (e.clientY - boundingRect.top) * (elem.height / boundingRect.height);
var vector = new THREE.Vector3((x / $("container").width()) * 2 - 1, -(y / $("container").height()) * 2 + 1, 0.5);
var pos = projector.unprojectVector(vector, camera);

var dir = pos.clone().sub(camera.position).normalize().multiplyScalar(-1);
var distance = camera.position.z / dir.z;     
var pos1 = camera.position.clone().sub(dir.multiplyScalar(distance));

Thanks in advance..

Upvotes: 0

Views: 407

Answers (1)

WestLangley
WestLangley

Reputation: 104793

Your camera near plane in your fiddle is 0.0001, and your camera far plane is 10,000,000,000.

Consequently, you are having numerical problems in your code when you call unprojectVector().

The issue is closely related to the depth buffer precision problems described here: http://www.opengl.org/wiki/Depth_Buffer_Precision.

Set your near plane to 1, or greater, and your far plane to the smallest value you can get away with, say 10000.

three.js r.68

Upvotes: 2

Related Questions