libgdx分辨率/密度缩放资产 [英] libgdx resolution/density scaling assets

查看:149
本文介绍了libgdx分辨率/密度缩放资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速提问

我正在用libgdx开发游戏,遇到了障碍.我正在尝试缩放资产,并使用 Gdx.graphics.getDensity()来获取密度,然后使用该值作为乘数来设置我的资产大小.我遇到的问题是,在分辨率为 2560x1600 的平板电脑上,其密度为2.0,而nexus 5仿真器的分辨率为 1080x1920 ,其密度为2.652...为什么平板电脑的密度比手机小?

I am working on a game in libgdx and have hit a snag. I'm trying to scale my assets and I'm using Gdx.graphics.getDensity() to get the density and then setting my asset size using that as a multiplier. The issue I'm having is that on a tablet that has a 2560x1600 resolution has a density of 2.0, yet the nexus 5 emulator that has 1080x1920 has a density of 2.652... how does the tablet have a smaller density then the smaller phone?

如果密度基于Android应用程序的屏幕尺寸不可靠,我应该使用什么来缩放倍数?

What should I be using to get my multiplier for scaling if density is not reliable based on the android app screen size?

推荐答案

要回答第一个问题,这是硬件问题.即使分辨率较小,Nexus 5的PPI(每英寸像素)比率也可能更高.

To answer your first question, it's a hardware thing. The Nexus 5 probably has a higher PPI (pixels per inch) ratio than the second, even though the resolution is smaller.

关于您的第二个问题,我建议使用 Gdx.graphics.getDensity()的替代方法:如果您要确保资产相对于屏幕的尺寸相同(即在2560x1600的显示器上显示80x80的资源将是在1280x800的显示器上显示的一半),那么我认为您想让资产保持相同的大小,然后更改摄像头的大小.

As to your second question, I would propose an alternative to Gdx.graphics.getDensity(): If you are trying to make sure that your asset is the same size relative to the screen (i.e. a 80x80 asset on a 2560x1600 display would be half the size on a 1280x800 display), then I think you want to leave your assets the same size and change the size of your camera instead.

在libGDX(或任何游戏引擎)中创建游戏时,需要记住游戏中三组尺寸/坐标之间的差异:

When you create a game in libGDX (or any game engine), you need to keep in mind the difference between three sets of sizes/coordinates in your game:

  • 视口的大小是游戏将在屏幕上绘制的空间的大小.它以像素为单位,默认为窗口大小(在全屏游戏中为屏幕大小).
  • 游戏世界的大小/坐标完全是任意的.它们以您想要的任何单位(米,英寸,香蕉,F16等)进行测量.您会知道自己在游戏世界中工作,因为您有浮标.
  • 相机的大小是您一次可以看到的世界"数量,以游戏单位为单位.您可以使相机显示1280x800的世界部分或3x5的世界部分或137.6x42的部分.棘手的是,libGDX的OrthographicCamera类默认设置您的视口大小,就像1个游戏单位== 1像素一样.
  • Viewport size is the size of the space on your screen where your game will draw. It is measured in pixels and defaults to your window size (in a fullscreen game, the size of the screen).
  • game world size/coordinates are completely arbitrary. They are measured in whatever unit you want (be it meters, inches, bananas, F16s, etc.). You'll know you're working in game world units because you have floats.
  • camera size is the amount 'world' you can see at one time measured in game units. You can make your camera show a 1280x800 portion of the world or a 3x5 portion of the world or a 137.6x42 portion. The tricky thing is that libGDX's OrthographicCamera class defaults your viewport size, acting as though 1 game unit == 1 pixel.

希望这是有道理的.让我们看看其中的含义:

Hopefully that made sense. Let's look at the implications:

假设我在游戏世界中有一个800x600 GU(游戏单元)资源,位于0,0.我的视口大小为2560x1600,我的相机也缩放为2560x1600.我的800x600 GU对象将在屏幕上渲染800x600 px的图像.

Say I have a 800x600 GU (game unit) asset at 0,0 in my game world. My viewport size is 2560x1600, and my camera is scaled to 2560x1600 as well. My 800x600 GU object will render a 800x600 px image on the screen.

现在,假设我想将游戏以相同的比例移植到其他屏幕尺寸(现在只是为了简单起见).我仍然具有2560x1600的视口大小,但是我将相机更改为8x5 GU,将游戏对象更改为2.5x1.875 GU.这些大小将显式设置-与视口大小无关.这使得1 GU = 320px.最终结果是,我的游戏对象仍将在屏幕上渲染800x600px的图像.

Now suppose I want to port my game to other screen sizes with the same ratio (just to keep things simple for now). I still have a 2560x1600 viewport size, but I change my camera to 8x5 GU and my game object to 2.5x1.875 GU. These sizes will be set explicitely- independent of the viewport size. This makes 1 GU = 320px. The net result is that my game object will still render a 800x600px image on the screen.

现在让我们看一下在1280x800px分辨率下如何工作:我的视口是1280x800 px,但是我的相机停留在8x5 GU上,我的游戏对象停留在2.5x1.875 GU.由于摄像机的尺寸,1 GU = 160px,这意味着我的游戏对象以400x300渲染,这意味着它在较小的屏幕上成比例地相同.

Now let's see how this would work on a 1280x800px resolution: My viewport is 1280x800 px, but my camera stays at 8x5 GU and my game object at 2.5x1.875 GU. Because of the size of the camera, 1 GU = 160px, which means my game object renders at 400x300, which means it is proportionally the same size on the smaller screen.

希望没有图片是有道理的.当您开始使用不同宽高比的屏幕时(即从8x5屏幕切换到3x2屏幕),您需要一些额外的逻辑来使视口的宽高比与相机相同(否则,所有内容都会拉伸).幸运的是,libGDX提供了一些视口,会为您完成.

Hopefully that made sense without pictures. When you start using screens with different aspect ratios (i.e. you go from an 8x5 screen to a 3x2 screen), you need a little extra logic to keep the aspect ratio of your viewport the same as your camera (otherwise things get all stretched). Fortunately, libGDX provides some viewports which will do this for you.

您可以在此处找到libGDX社区的外部教程,其中详细介绍了此问题

You can find an external tutorial by the libGDX community here that talks more about this issue.

这篇关于libgdx分辨率/密度缩放资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆