开发者

How can I determine the width of the visible side of a 3d box from a centered fixed distance camera?

开发者 https://www.devze.com 2023-02-02 18:54 出处:网络
I have a 2.5d viewport wherein I am trying to express a visualization of the depth of a box with 45 degree 2D lines inwards as if it had no front and you were staring inside from a central point, like

I have a 2.5d viewport wherein I am trying to express a visualization of the depth of a box with 45 degree 2D lines inwards as if it had no front and you were staring inside from a central point, like this:

-------------
| \_______/ |
| |       | |
| |_______| |
| /       \ |
-------------

I want to draw the metaphor with real 3D geometry, in that the distance of the inner rect from the outer rect is correct, given a camera distance (say fixed at 2 feet for an average eye distance from the user's monitor)

How can I deriv开发者_C百科e the "common inner side width" from: the centered camera distance, the width and height of the box, and the depth I'm trying to represent?

Thanks!


What's you're looking for is a perspective projection. In addition to the position of the camera and dimensions of the box, you also need a "screen" somewhere between the camera and the box onto which the box will be projected. The distance from the camera to this screen will scale the size of the projected box.

Let's say the camera is at the origin and z units away from the box, the screen is one unit away from the camera, and the box was depth d, width 2w, and height 2h.

In 3D ambient space, the top-left corner of the box is at position (-w, h, z). The ray between the camera and this point intersects the projection screen at (-w/z, h/z, 1).

The top-left corner of the floor of the box is at position (-w, h, z+d) which is projected to (-w/(z+d), h/(z+d), 1).

Similarly, you can find the positions of the other projected points. Therefore the width of the outer box on the screen is 2w/z, the width of the inner box 2w/(z+d), and analogously for the heights. The inner box is thus just a uniformly scaled version of the outer box, with the inner box dimensions z/(z+d) times the outer box ones.

Note that the diagonals connecting the two boxes are not oriented at 45 degrees unless the box happens to be square.

0

精彩评论

暂无评论...
验证码 换一张
取 消