libGDX 只绘制部分视口,同时切断其余部分

libGDX Draw viewport only partly while cutting off the rest

这个问题对我来说似乎很容易解决,但无论我尝试什么,它都不起作用。我想做的是将我的 PlayScreen 的 mini-version 合并到 ScrollPane 中作为教程,您可以在其中阅读文本并立即试用。

因为我没有找到任何更好的解决方案将其添加到 ScrollPane 内的 Table,我编辑了 PlayScreen 的 draw() 方法以获取 ScrollPane.getScrollPercentY() 并相应地偏移 PlayScreen 的相机。

我现在想做的是只渲染真实游戏中通常可见的部分视口。随后,我希望能够控制这个"window"的大小和位置。

我还希望能够调整内容的大小和移动内容,同时切掉相机不可见的边缘。这是我在 PlayScreenDraw 中尝试的:

public void draw(final float yOffset,
                 final int xTiles,
                 final int yTiles) {
    view.getCamera().position.y = yTiles / 2f - yOffset * yTiles / HEIGHT; // HEIGHT = 800
    view.getCamera().position.x = xTiles / 2f;
    view.setWorldSize(xTiles, yTiles); //Do i even need to change the world size?
    b.setProjectionMatrix(view.getCamera().combined);

    b.begin();
    ...
    b.end();

    view.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}

根据上图,这给我的是

我需要如何更改视口 and/or 相机?顺便说一句,这就是我设置两者的方式:

cam = new OrthographicCamera();
cam.setToOrtho(false, WIDTH, HEIGHT); // WIDTH = 8, HEIGHT = 16
batch.setProjectionMatrix(cam.combined);
view = new FitViewport(WIDTH, HEIGHT, cam);

Pixmap class 可以帮助您实现您想要的,因为您表示要“切断”绿色选择框之外的部分。

您需要将相机看到的内容渲染到 FBO,然后从 FBO 本身获取像素图。

Framebuffer Objects are OpenGL Objects, which allow for the creation of user-defined Framebuffers. With them, one can render to non-Default Framebuffer locations, and thus render without disturbing the main screen.

-- OpenGL wiki

// Construct an FBO and keep a reference to it. Remember to dispose of it.
FrameBuffer fbo = new FrameBuffer(Format.RGBA8888, width, height, false);

public void render() {
//Start rendering to the fbo.
fbo.begin();
//From the camera's perspective.
batch.setProjectionMatrix(camera.combined);
batch.begin();

//Draw whatever you want to draw with the camera.

batch.end();
// Finished drawing, get pixmap.
Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, width, height);
//Stop drawing to your fbo.
fbo.end();
}

获得像素图后,您可以遍历像素并将绿色选区之外的像素的 alpha window 设置为 0,使它们不可见或“将它们切断”