Position mesh to fill viewport width


I have a simple mesh containing a row of cubes at the origin, and a simple camera at (0, 0, 4), and I want to calculate where that mesh should be in world space, so that it fills the visible viewport width.

I’ve been able to achieve this result by scaling the object in question, but I don’t want to do this - I would rather reposition the object in world space. I also make use of a method that determines the width of the viewport as a world-space distance, and I’ve tried to rearrange that calculation to solve for depth, because I know the world-space width of my cubes container, but I wasn’t able to get this to work.

I wondered if this utility class would be right for me, but I can’t find it in the forum to use in earnest:

Can anyone help with this? I’d prefer to solve this without using the above CameraViewBox, but I thought I’d mention that I’d seen it.


This thread is somewhat related. It solves the opposite problem, though:

For your case, it might be important to know the possible situations, because in some cases you might need not only to reposition your cubes, but also to rotate them as a group. See an illustration:


I found this explanation and solution by Michael Jirků, which is what I was looking for:

1 Like