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

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

问题描述

快速提问,

我正在开发 libgdx 中的游戏,但遇到了问题.我正在尝试扩展我的资产,我正在使用 Gdx.graphics.getDensity() 来获取密度,然后使用它作为乘数来设置我的资产大小.我遇到的问题是,在具有 2560x1600 分辨率的平板电脑上,密度为 2.0,而具有 1080x1920 的 nexus 5 模拟器的密度为 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:

  • 视口 size 是您的游戏将在屏幕上绘制的空间大小.它以像素为单位,默认为您的窗口大小(在全屏游戏中为屏幕大小).
  • 游戏世界的大小/坐标完全是任意的.它们以您想要的任何单位进行测量(米、英寸、香蕉、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:

假设我在游戏世界中的 0,0 处有一个 800x600 GU(游戏单元)资产.我的视口大小是 2560x1600,我的相机也缩放到 2560x1600.我的 800x600 GU 对象将在屏幕上呈现 800x600 像素的图像.

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.最终结果是我的游戏对象仍将在屏幕上呈现 800x600 像素的图像.

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.

现在让我们看看这在 1280x800 像素分辨率下是如何工作的:我的视口是 1280x800 像素,但我的相机保持在 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天全站免登陆