将 2D 屏幕坐标取消投影到 3D 坐标
我想知道如何将 2D 屏幕坐标映射到 3D 世界(特别是 xz 平面):
I was wondering how I would go about mapping 2D screen coordinates to a 3D world (specifically the xz plane) knowing:
-相机位置
-屏幕平面方程
-xz 平面方程
当我将鼠标悬停在 xz 平面上时,我想要做的就是点亮它.
All I want to do is have the land on the xz plane light up when I hover the mouse over it.
非常感谢任何帮助!
谢谢!
如果你的世界被旋转和移动,使得相机位于 x=y=z=0
(世界坐标),world z
坐标远离观察者(进入屏幕)增加,投影平面平行于屏幕,位于z=d
(世界坐标;d > 0
),然后您可以通过以下方式根据世界坐标确定屏幕坐标:
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
这很直观:物体离观察者/投影平面越远,它的 zw
越大,xs
和 ys
越小,更接近 xw=yw=0
和 zw=+infinity
的消失点,投影到投影平面的中心 xs=ys=0代码>.
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
.
通过重新排列上面的每一个你得到 xw
和 zw
:
By rearranging each of the above you get xw
and zw
back:
xw = xs * zw/d
zw = d * yw/ys
现在,如果你的物体(土地)是在某个 yw
处的一个平面,那么,那个 yw
是已知的,所以你可以替换它并得到zw
:
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
找到zw
,你现在可以再次通过替换得到xw
:
Having found zw
, you can now get xw
by, again, substitution:
xw = xs * zw/d = xs * (d * yw/ys)/d = xs * yw/ys
因此,鉴于鼠标指针的开始和屏幕坐标 xs
和 ys
中描述的设置(0,0 是屏幕/窗口中心),相机和投影平面之间的距离 d
和陆地平面的 yw
你得到鼠标指向的陆地点的位置:
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
当然,这些 xw
和 zw
是在旋转和移动的世界坐标中,如果你想要土地地图"中的原始绝对坐标,你不旋转和不移动它们.
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.
这就是它的要点.