Hello,
I have an orthographic camera defined like this:
new three.OrthographicCamera(20 * aspectRatio / -2, 20 * aspectRatio / 2, 20 / 2, 20 / -2, -1000, 1000)
The canvas takes up the whole space. The camera position is (player.x - 3, 1 / 8 + 15, player.z + 15)
and rotation ((-90 / 360) * Math.PI, (-15 / 360) * Math.PI, (-10 / 360) * Math.PI)
I want to interact with objects in the scene with the mouse, so I want to map 2d coordinates to (x, 1 / 8, z)
. 1 / 8
is top of the floor. All my Object3D
have a parent which simulates a pivot in min point of the geometry, so by doing Math.floor(z)
I will get the z-position of the clicked object
Using ray casting here doesn’t make sense, and is also expensive, since I don’t care about y-axis (which is facing up). I want to get (x, z)
for the plane y = 1 / 8
I tried various answers and the latest code I have wrote down is:
let vec = new three.Vector3((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, (camera.near + camera.far) / (camera.near - camera.far));
console.log(vec.unproject(camera));
With the help of GridHelper
I click near the (0, 0, 0)
, but what I get in the console is no where near what it should be, I get x: -2.8929188820998046, y: 15.546389670485576, z: 15.493743978055074
I also tried an answer which said that by unprojecting I’ll get a ray which I have to extend until it hits y = 1 / 8
, but the result was also way off (this time y
was always 1 / 8
, so I was extending fine)