这个问题对我来说似乎很容易解决,但无论我尝试什么,它都不起作用。我想做的是将我的 PlayScreen 的迷你版本合并到 ScrollPane
中作为教程,您可以在其中阅读文本并立即尝试。
因为我没有找到更好的解决方案将其添加到 ScrollPane
内的 Table
中,所以我编辑了 draw()
方法PlayScreen 的相机获取 ScrollPane.getScrollPercentY()
并相应地偏移 PlayScreen 的相机。
我现在想做的是只渲染在真实游戏中通常可见的视口(viewport)的一部分。随后,我希望能够控制这个“窗口”的大小和位置。
我还希望能够调整内容大小和移动内容,同时剪掉相机不可见的边缘。这是我在 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());
}
就上图而言,这给了我什么
我需要如何更改视口(viewport)和/或相机?顺便说一句,这就是我设置两者的方式:
cam = new OrthographicCamera();
cam.setToOrtho(false, WIDTH, HEIGHT); // WIDTH = 8, HEIGHT = 16
batch.setProjectionMatrix(cam.combined);
view = new FitViewport(WIDTH, HEIGHT, cam);
最佳答案
Pixmap
类可以帮助您实现您想要的效果,因为您声明要“剪掉”绿色选择框之外的部分。
您需要将相机看到的内容渲染到 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 设置为 0,使它们不可见或“切断它们”
关于java - libGDX 仅绘制部分视口(viewport),同时切断其余部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60526912/