LibGDX: 如何在FitViewport 的黑条区域绘制?

LibGDX: How to draw in the area of the black bars of a FitViewport?

我得到了一个具有虚拟宽度和虚拟高度的 FitViewport。当屏幕的宽高比不同于我的虚拟分辨率时,会添加黑条。我想在这些信箱里画点东西。

我试着那样做,但只有对象 "inside" 我的虚拟分辨率被绘制,对象 "outside" 只是不可见:

viewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, cam);
viewport.apply();

batch.setProjectionMatrix(cam.combined);

batch.begin();
batch.draw(texture, viewport.getRightGutterX(), 0);
batch.end();

如何在信箱内绘制?

您需要第二个视口,可能是与 FitViewport 具有相同虚拟尺寸的 ExtendViewport。

//create:
fitViewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
extendViewport = new ExtendViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);

//resize:
fitViewport.update(width, height);
extendViewport.update(width, height);

//render:

fitViewport.apply();
batch.setProjectionMatrix(fitViewport.getCamera().combined);
batch.begin();
//draw game
batch.end();

extendViewport.apply();
batch.setProjectionMatrix(extendViewport.getCamera().combined);
batch.begin();
//draw stuff in border
batch.end();

如果您想确保边框内容不会与您的游戏重叠,您可以交换上面的绘制顺序。

或者您可以只使用 ExtendViewport 开始所有的事情。