将屏幕上的点取消投影回等距投影的世界
我正在幕后进行 3D 模拟,同时在 2D 等距引擎中渲染世界。 我以前从未做过等距引擎,而且我的矩阵数学总体上很生疏,所以我遇到了问题。
我有一个投影矩阵,其最简单的形式是这样的:
0.7 0.35 0
0 -0.87 0
-0.71 0.35 1
几个符号被翻转,因为我的引擎坐标系是左上角的 0,0,+X 位于右/东,+Z 位于南。
现在,它的反面是:
1.4080 0.5670 0.0000
0.0000 -1.1490 0.0000
1.0000 0.8050 1.0000
现在,这些矩阵大部分都有效。
例如
WC: 500,0,500 = 屏幕: -1.44, 350, 500(X 和 Y 正确)
WC: 0,0,500 = 屏幕: -355, 175, 500(X 和 Y 再次正确)
但是,现在如果您需要走另一条路,您不再有方便的 Z 值,因此
屏幕: -1.44, 350, 0 = WC: -2, -402.97, 0 (所以,垃圾。)
还有更多 - 一旦我不再有那个 Z 值,我就可以不从屏幕坐标中检索世界坐标。
这里有什么解决方法吗?
编辑
我应该指出,取消投影的目的是获得用于鼠标拾取的光线。
似乎这只是我对自己正在做的事情的误解,才把我搞砸了。
I am doing a behind the curtains 3d simulation while rendering the world in my 2d isometric engine. I've never done an isometric engine before, and my matrix math is rusty in general so I am having problems.
I have a projection matrix, which in its simplest form is this:
0.7 0.35 0
0 -0.87 0
-0.71 0.35 1
A couple of signs are flipped because my engines coordinate system is 0,0 in the top left, with +X to the right/east and +Z to the south.
Now, the inverse of that is:
1.4080 0.5670 0.0000
0.0000 -1.1490 0.0000
1.0000 0.8050 1.0000
Now, these matrices mostly work.
For instance
WC: 500,0,500 = Screen: -1.44, 350, 500 (X and Y are correct)
WC: 0,0,500 = Screen: -355, 175, 500 (X and Y are correct again)
But, now if you need to go the other way, you no longer have that handy Z value, so
Screen: -1.44, 350, 0 = WC: -2, -402.97, 0 (So, garbage.)
And lots more - as soon as I no longer have that Z value, I can't retrieve the world coords from the screen coords.
What's the workaround here?
EDIT
I should point out that the point of the unproject is to get a ray for mouse picking..
It seems like it's just my misperception of what I was doing that was screwing me up here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您所发现的,转换回 3D 空间需要某种 Z 坐标才能有意义。
我建议您进行反向转换两次。 一次 Z 坐标靠近屏幕(最靠近观察者),一次 Z 坐标位于 3D 场景的后面。 这两个 3D 点将为您提供一条 3D 线,该线将占据该 2D 点“后面”的所有位置。
As you discovered, your conversion back into 3D space requires some kind of Z coordinate to make any sense at all.
I would suggest that you do the reverse transformation twice. Once with a Z coordinate near the screen (closest to the observer), and once with a Z coordinate at the back of your 3D scene. These two 3D points would give you a 3D line, which would occupy all of the positions "behind" that 2D point.
你不能。 您正在投影到丢失信息的屏幕上。
如果您考虑一下,多个 3D 坐标会投影到屏幕上的同一点,并且仅知道屏幕坐标不足以检索原始坐标。
[编辑]
查看屏幕坐标,将它们的 z 值全部设为 0。这意味着投影矩阵的最后一列应全部为零,从而使该矩阵不可逆。
you can't. you are projecting onto the screen which loses information.
If you think about it, several 3d coordinates get projected onto the same point on the screen, and just knowing that screen coordinate isn't enough to retrieve the original coordinate.
[edit]
looking at your screen coordinates, you give them all z-value 0. which means the last columns of your projection matrix should have all zeros, making that matrix non-invertible.
屏幕上的每个像素代表从观看者的眼睛到屏幕后面想象的 3D 世界的一条线。 您必须使这条线与该世界中可能潜伏的任何物体相交才能获得 3D 坐标。
Every pixel on the screen represents a line from the eye of the beholder into the imaginary 3D world behind the screen. You have to intersect this line with whatever objects may lurk in that world in order to get 3D coordinates.