LibGDX如何使用多个视窗/摄影机?

a8jjtwal  于 2022-09-26  发布在  其他
关注(0)|答案(2)|浏览(164)

在我目前正在开发的LibGDX游戏中,我遇到了一个关于视窗的问题。我正在尝试创建两个视区-一个容纳整个屏幕,包括控制,另一个容纳第一个中间的游戏视觉效果。This is what I mean.

然而,我在使用这些视窗时遇到了一些问题。首先,在FitViewportapply内部渲染的平铺Map和播放器是看不到的(要么没有渲染,要么在可见的屏幕空间之外),但我的光线处理器的光是可以看到的(并且传播的比它分配的1:1更多)。Proof

private OrthographicCamera camera;    
Viewport v1;
Viewport v2;
//...
RayHandler devRay;
//...
TiledMap map;
OrthogonalTiledMapRenderer tmr;
Player player;

@Override
public void show() {    
    camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
    camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    //...
    v1 = new ScreenViewport();
    v2 = new FitViewport(160, 160, camera);
    //...
    tmr = new OrthogonalTiledMapRenderer(map);
    tmr.setView(camera);
    RayHandler.useDiffuseLight(true);
    devRay = new RayHandler(world);
}
public void render(float delta) {

    Gdx.gl.glClearColor(1,1,1,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    v1.apply();
    //Draw Controls

    v2.apply();
    //Draw Visuals  
    tmr.render(); //Tiled Map Renderer
    player.render();
    devRay.updateAndRender();
    //...
}

我猜我完全忽略了视窗的要点。我想知道在我的项目中是否有办法将可玩区域和控制区域分开,这样,例如,我可以移动FitViewport的摄像头,而游戏画面不会占据超过1:1的比例。谢谢你的帮助!

9nvpjoqh

9nvpjoqh1#

我通常做的是用一台相机来渲染世界,然后通过绘制最后一个stage.draw()的舞台来覆盖HUD。舞台就是最好的舞台。显然,这模糊了一些通常在没有HUD的情况下可以看到的游戏世界,所以如果你真的想要多个“视窗”,那么你必须使用Viewports

视区易于实现,并且可以为您处理所有内容if you pick the correct one。设置摄影机后,您可以创建如下所示的视区:

camera = new OrthographicCamera(1920 / 2, 1080);
    //1920, 1080 for sake of example. This could be anything and it is often
    // better to not think in pixels but in world units.

    viewport = new FitViewport(camera.viewportWidth, camera.viewportHeight, camera);
    viewport.setScreenBounds(0, 0, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight());

这将使用setScreenBounds(...)在屏幕的左半部创建一个视区。因为我们在相机上绘制的东西只有一半的屏幕,所以我们必须匹配宽高比,否则它会看起来被拉伸。

要求澄清,因为这有时会有点令人困惑:

float worldWidth, worldHeight; // How much of the world to display
viewport = new FitViewport(worldWidth, worldHeight, camera);

int screenPosX, screenPosY, screenWidth, screenHeight; 
// The position of the viewport where 0, 0 is bottom left and 
// Gdx.graphcis.getWidth, Gdx.graphics.getHeight is top right
viewport.setScreenBounds(screenPosX, screenPosY, screenWidth, screenHeight);

你实际上不需要指定相机的宽度和高度,因为一旦你用连接到副总裁的相机点击vp.apply(),它就会被覆盖。

update()方法中,在该特定视区上绘制任何内容之前,必须先应用视区viewport.apply()

7uzetpgm

7uzetpgm2#

我也面临着同样的问题。Official Documentation中有答案。
多个视区使用具有不同屏幕大小的多个视区(或使用设置glViewport的其他代码)时,需要在绘制之前应用视区,以便为该视区设置glViewport。

viewport1.apply();
// draw
viewport2.apply();
// draw

当使用多个阶段时:

stage1.getViewport().apply();
stage1.draw();
stage2.getViewport().apply();
stage2.draw();

相关问题