问题
I was wondering how I would go about mapping 2D screen coordinates to a 3D world (specifically the xz plane) knowing:
-position of the camera
-equation of the screen plane
-equation of the xz plane
All I want to do is have the land on the xz plane light up when I hover the mouse over it.
Any help is greatly appreciated!
Thanks!
回答1:
If your world is rotated and shifted such that the camera is at x=y=z=0
(world coordinates), the world z
coordinate increases away from the viewer (into the screen), and the projection plane is parallel to the screen and is at z=d
(world coordinate; d > 0
), then you determine screen coordinates from world coordinates this way:
xs = d * xw / zw
ys = d * yw / zw
And that's pretty intuitive: the farther the object from the viewer/projection plane, the bigger its zw
and the smaller xs
and ys
, closer to the vanishing point of xw=yw=0
and zw=+infinity
, which projects onto the center of the projection plane xs=ys=0
.
By rearranging each of the above you get xw
and zw
back:
xw = xs * zw / d
zw = d * yw / ys
Now, if your object (the land) is a plane at a certain yw
, then, well, that yw
is known, so you can substitute it and get zw
:
zw = d * yw / ys
Having found zw
, you can now get xw
by, again, substitution:
xw = xs * zw / d = xs * (d * yw / ys) / d = xs * yw / ys
So, there, given the setup described in the beginning and screen coordinates xs
and ys
of the mouse pointer (0,0 being the screen/window center), the distance between the camera and the projection plane d
, and the land plane's yw
you get the location of the land spot the mouse points at:
xw = xs * yw / ys
zw = d * yw / ys
Of course, these xw
and zw
are in the rotated and shifted world coordinates and if you want the original absolute coordinates in the "map" of the land, you un-rotate and un-shift them.
That's the gist of it.
来源:https://stackoverflow.com/questions/9971701/unprojecting-2d-screen-coordinates-to-3d-coordinates