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

查看:15
本文介绍了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.

Viewport 在 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:这是独一无二的,因为它没有世界宽度/高度的输入.它只是让世界的宽度和高度与屏幕大小(以像素为单位)相匹配.这绝不应该用于玩游戏,因为与小型 MD 设备相比,您在大型 XHD​​ 设备上看到的矩形可能是 5 倍或更多倍.它对 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天全站免登陆