Libgdx中的视口和相机之间的区别? [英] difference between Viewport and camera in Libgdx?

查看:103
本文介绍了Libgdx中的视口和相机之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是LibGdx框架的新手,无法使用视口和相机.任何人都可以在两者之间以及在两者的使用之间做出简单的区别吗?

I am new to LibGdx framework and having trouble working with viewport and camera. Can anyone give a simple difference between each and use of both?

推荐答案

摄像机定义了有利位置以及在屏幕上可以看到的世界数量.通过定义世界视野的摄像机可以看到游戏中的所有内容.对于2D游戏,您可以使用OrthographicCamera来定义当前可见的世界的矩形. OpenGL使用矩阵来计算可见的图像,因此要使用相机,请对其进行更新,并使用其combined矩阵传递到SpriteBatch中.

The camera defines the vantage point and how much of the world is seen on screen. Everything in your game is seen through a camera that defines a view of the world. In the case of 2D games, you use an OrthographicCamera that defines a rectangle of the world that is currently visible. OpenGL works with matrices to calculate what is seen, so to use the camera, you update it and take its combined matrix to pass into the SpriteBatch.

视口在LibGDX中具有三种不同的含义:

Viewport has three different meanings in LibGDX:

  1. OpenGL视口,即OpenGL屏幕的矩形区域,当前设置为以像素尺寸绘制.无论OrthographicCamera具有什么尺寸,它定义的世界的可见矩形都会拉伸以适合OpenGL视口,这意味着它看起来会变形/压扁.

  1. The OpenGL viewport, the rectangular area of the screen OpenGL is currently set to draw to in pixel dimensions. No matter what dimensions an OrthographicCamera has, the viewable rectangle of the world that it defines is stretched to fit the OpenGL viewport, which means it can look distorted/squashed.

OrthographicCamera的viewportWidthviewportHeight.这些术语是LibGDX婴儿期的不幸记录,假设您一直使用的摄像头尺寸等于OpenGL视口的尺寸.这些术语实际上描述了您可以看到的世界的矩形的大小,但是这个可见的矩形可以具有任意尺寸,并且最终图像被拉伸以适合OpenGL视口.

The viewportWidth and viewportHeight of an OrthographicCamera . These terms are an unfortunate relic of LibGDX's infancy, when it was assumed you'd always be using a camera sized to equal the size of the OpenGL viewport. These terms actually describe the size of the rectangle of the world you can see, but this visible rectangle can have arbitrary dimensions and the final image is stretched to fit the OpenGL viewport.

Viewport类,该类同时管理OrthographicCamera和OpenGL Viewport,以实现某种策略来处理屏幕密度和纵横比的无限可能.

The Viewport class, which is a class that simultaneously manages an OrthographicCamera and the OpenGL Viewport to achieve a certain strategy for dealing with the infinite possibilities of screen densities and aspect ratios.

LibGDX视口类是通过提供世界宽度和世界高度来设置的,世界宽度和世界高度定义了您希望在屏幕上可见的游戏世界的矩形.根据您使用的视口类型,此矩形将以某种方式用于定位多种屏幕尺寸.

LibGDX Viewport classes are set up by providing a world width and world height, which define a rectangle of your game world that you want to be visible on screen. Depending on which type of Viewport you use, this rectangle will be used in some manner to target multiple sizes of screen.

LibGDX包含的视口的一些示例:

Some examples of LibGDX's included viewports:

StretchViewport::即使这会引起变形,它也会拉伸您的世界宽度/高度以适合屏幕.永远不要将其用于发布的游戏,因为玩家会讨厌它.仅适用于非常肮脏的原型.

StretchViewport: It stretches your world width/height to fit the screen, even if this causes distortion. This should never be used for a released game because players will hate it. Only useful for very quick-and-dirty prototyping.

FitViewport::它将扩大您的世界宽度/高度以适合屏幕,并在OpenGL视口中添加字母框,以使图像不失真.就像在电视上观看电影时看到的黑色条形.这是设计游戏的最快/最脏的方法,因为您知道世界的可见矩形在所有设备上都是完全相同的.但是您的播放器可能不喜欢浪费屏幕空间.如果您将其用于iOS游戏,Apple将拒绝它.

FitViewport: It enlarges your world width/height to fit the screen, and letterboxes the OpenGL viewport such that the image is not distorted. This is like the black bars you see when watching a movie on a TV. This is the quickest/dirtiest way to design your game since you know the viewable rectangle of your world is exactly the same on all devices. But your players might not like wasted screen space. If you use this for an iOS game, Apple will reject it.

ExtendViewport::它会放大您的世界宽度/高度以适合屏幕,然后放大您的世界宽度或高度以填充屏幕上的所有剩余空间,因此图像不会失真.如果您不希望装箱,这是最好的选择,但是这意味着您必须在设计游戏时牢记,您必须画足够多的东西以覆盖计划的世界宽度/高度之外的可能额外区域,并尝试保持游戏玩法相当公平.

ExtendViewport: It enlarges your world width/height to fit the screen, and then enlarges either your world width or height to fill any remaining space on the screen so the image won't be distorted. This is the best option if you don't want letterboxing, but it means you must design your game keeping in mind that you have to draw enough to cover the possible extra area outside your planned world width/height, and try to keep the gameplay reasonably fair.

ScreenViewport::这是唯一的,因为它没有世界宽度/高度的输入.它只是使世界的宽度和高度与屏幕尺寸匹配(以像素为单位).切勿将其用于游戏,因为在大型XHD设备上看到的矩形可能是小型MD设备的矩形的五倍或更多倍.它具有用于UI的用途.您可以为各种屏幕密度设计多个UI资产,并使用ScreenViewport,以便文本和按钮在所有设备上看起来都非常清晰.

ScreenViewport: This one is unique because it does not have an input for world width/height. It simply makes the world width and height match the screen size in pixels. This should never be used for game play because you could be seeing a rectangle five or more times as big on a large XHD device as compared to a small MD device. It has its uses for UI. You can design multiple UI assets for various screen densities and use ScreenViewport so text and buttons look very crisp on all devices.

我个人始终使用ExtendViewport进行游戏,因此不会浪费屏幕空间,并且可以在iOS上发布游戏.

I personally always use ExtendViewport for gameplay so screen space is not wasted and the game can be released on iOS.

对于UI,我将ExtendViewport用于游戏性HUD(将小部件推向屏幕边缘),并使用FitViewport(如果有一个暂停屏幕覆盖了游戏性).对于此用途,FitViewport很好,因为您实际上不会看到黑条.如果您知道适合的矩形总是相同,则管理复杂的暂停屏幕UI会更容易.

For UI, I use ExtendViewport for the gameplay HUD (to push the widgets against the edges of the screen), and FitViewport if there is a pause screen that overlays the gameplay. For this use, FitViewport is fine because you won't actually see black bars. It's easier to manage a complicated pause screen UI if you know the rectangle it fits in will always be the same.

为了以最小的努力使UI看起来相当清晰,我在大约200-250dpi的电话屏幕上以大约像素到像素的大小生成了它,并对其进行了三线性过滤.

To keep UI looking reasonably crisp with minimal effort I generate it at a size that would be approximately pixel-to-pixel on a ~200-250dpi phone screen, and give it trilinear filtering.

这篇关于Libgdx中的视口和相机之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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