LibGDX FitViewport 中的背景图片
LibGDX Background Image in FitViewport
所以,我正在为即将推出的应用程序使用 LibGDX。
我使用 FitViewport 来确保 16:9 纵横比。
因此,具有其他宽高比 16:9 的玩家将在网站上出现黑条。
绘制屏幕填充背景图像的最佳方法是什么,该图像也覆盖黑条所在的区域?
camera = new OrthographicCamera();
viewport = new FitViewport(WIDTH, HEIGHT, camera);
viewport.apply();
camera.position.set(WIDTH / 2, HEIGHT / 2, 0);
camera.update();
这就是我目前设置 camera/viewport 的方式。
然后我用 SpriteBatch 在上面画东西。
Gdx.gl.glClearColor(1, 1, 1, 1);
这就是我目前至少将黑条的颜色更改为任何 RGB 颜色的方式。
在我看来,最好的想法是创建第二个阶段,它自己 Viewport
仅用于背景目的。第二个 Viewport
不应该是 FillViewport - 根据我的经验,它会拉伸你的图形。我认为 ExtendViewport 在这种情况下更好。
那么它应该是什么样子:
Stage stage, backStage;
FitViewport viewport;
ExtendViewport backViewport;
...
stage = new Stage(); //this is your normal stage you have now
stage.setViewport( yourFitViewport ); //here you are assingning fit viewport
backViewport = new ExtendViewport( screenWidth, screenHeight );
backStage = new Stage();
backStage.setViewport( backViewport );
...
//now add to backStage your background Image
backStage.addActor( yourBackground );
现在只需在渲染方法中处理新阶段。
backStage.act();
stage.act();
backStage.draw(); //backStage first - we want it under stage
stage.draw();
并像旧版本一样在更新或渲染中更新新的 Viewport
。就这些了。
在此处阅读有关 Viewports
的更多信息:https://github.com/libgdx/libgdx/wiki/Viewports
所以,我正在为即将推出的应用程序使用 LibGDX。
我使用 FitViewport 来确保 16:9 纵横比。 因此,具有其他宽高比 16:9 的玩家将在网站上出现黑条。
绘制屏幕填充背景图像的最佳方法是什么,该图像也覆盖黑条所在的区域?
camera = new OrthographicCamera();
viewport = new FitViewport(WIDTH, HEIGHT, camera);
viewport.apply();
camera.position.set(WIDTH / 2, HEIGHT / 2, 0);
camera.update();
这就是我目前设置 camera/viewport 的方式。
然后我用 SpriteBatch 在上面画东西。
Gdx.gl.glClearColor(1, 1, 1, 1);
这就是我目前至少将黑条的颜色更改为任何 RGB 颜色的方式。
在我看来,最好的想法是创建第二个阶段,它自己 Viewport
仅用于背景目的。第二个 Viewport
不应该是 FillViewport - 根据我的经验,它会拉伸你的图形。我认为 ExtendViewport 在这种情况下更好。
那么它应该是什么样子:
Stage stage, backStage;
FitViewport viewport;
ExtendViewport backViewport;
...
stage = new Stage(); //this is your normal stage you have now
stage.setViewport( yourFitViewport ); //here you are assingning fit viewport
backViewport = new ExtendViewport( screenWidth, screenHeight );
backStage = new Stage();
backStage.setViewport( backViewport );
...
//now add to backStage your background Image
backStage.addActor( yourBackground );
现在只需在渲染方法中处理新阶段。
backStage.act();
stage.act();
backStage.draw(); //backStage first - we want it under stage
stage.draw();
并像旧版本一样在更新或渲染中更新新的 Viewport
。就这些了。
在此处阅读有关 Viewports
的更多信息:https://github.com/libgdx/libgdx/wiki/Viewports