I’ve done fixed aspect ratio render scripts (with black bars) multiple times, but apparently I never did much GUI work with them. Now I’m having issues getting the GUI to render inside my desired viewport and without distortion.
This is what my test GUI looks like, just two purple boxes, pivoted and anchored to the bottom left and top right, respectively. All nodes are set to ‘Fit’ adjust mode.
If I set the viewport and the orthographic projection to the cropped size and offset, there’s no distortion, but the GUI is fit to the window edges, and therefore cropped.
If I set the viewport to the cropped size and offset, but the projection to the full window size then the placement looks right but everything is stretched
If I set both the viewport and the projection to the full window size then it looks as I expect, no distortion or issues, but of course the placement is based on the full window size, which is not what I want.
If I set the viewport to the full window but the projection to the cropped area then I get the worst of both worlds: the GUI is stretched and cropped outside the window (I won’t bother showing this one).
Is there some way to make this work properly?
[Edit] Here’s the relevant chunk of my render script, nothing fancy.
-- GUI Rendering
-- `vp` is a table with the data for my desired fixed-aspect-ratio viewport
-- x, y offset, and width and height
render.set_viewport(vp.x, vp.y, vp.width, vp.height)
-- (x, y, width, height)
self.gui_proj = vmath.matrix4_orthographic(vp.x, vp.x + vp.width, vp.y, vp.y + vp.height, -1, 1)
-- (left, right, bottom, top, near, far)
--render.set_viewport(0, 0, render.get_window_width(), render.get_window_height())
--self.gui_proj = vmath.matrix4_orthographic(0, render.get_window_width(), 0, render.get_window_height(), -1, 1)
render.set_view(IDENTITY_MATRIX)
render.set_projection(self.gui_proj)
-- then actually draw gui stuff . . .