Move camera to fit 3D scene

后端 未结 6 992
别跟我提以往
别跟我提以往 2020-12-01 00:37

I\'m looking for an algorithm to fit a bounding box inside a viewport (in my case a DirectX scene). I know about algorithms for centering a bounding sphere in a orthographic

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 01:01

    Since you have a bounding box, you should have a basis describing it's orientation. It seems that you want to position the camera on the line coincident with the basis vector describing the smallest dimension of the box, then roll the camera so that the largest dimension is horizontal (assuming you have OBB and not AABB). This assumes that the aspect ratio is greater than 1.0; if not you'll want to use the vertical dimension.

    What I would attempt:

    1. Find the smallest box dimension.
    2. Find the associated basis vector.
    3. Scale the basis vector by the distance from the center of the box the camera should be. This distance is just boxWidth / (2 * tan(horizontalFov / 2)). Note that boxWidth is the width of the largest dimension of the box.
    4. Place the camera at boxCenter + scaledBasis looking at the boxCenter.
    5. Roll the camera if necessary to align the camera's up vector with the appropriate box basis vector.

    Edit:

    So I think what you're getting at is that you have the camera at an arbitrary position looking somewhere, and you have an AABB at another position. Without moving the camera to face a side of the box, you want to:

    • Look at the center of the box
    • Translate the camera along it's look vector so that the box takes the maximum amount of screen space

    If this is the case you'll have a bit more work; here's what I suggest:

    1. Rotate the camera to look at the center of the bounding box.
    2. Project all the points of the box into screen space and find the min/max bounding box in screen space (you already have this).
    3. Now Unproject two opposing corners of the screen space bounding box into world space. For a Z value use the closest world space points of your AABB to the camera.
    4. This should get you a world space plane facing the camera positioned at the point on the AABB that is closest to the camera.
    5. Now use our existing side-facing method to move the camera to the appropriate spot, treating this plane as the side of your box.

提交回复
热议问题