何时在 Libgdx 中使用 ShapeRenderer、Mesh + SpriteBatch、

When to use ShapeRenderer, Mesh + SpriteBatch, Box2D and Scene2D in Libgdx?(何时在 Libgdx 中使用 ShapeRenderer、Mesh + SpriteBatch、Box2D 和 Scene2D?)

本文介绍了何时在 Libgdx 中使用 ShapeRenderer、Mesh + SpriteBatch、Box2D 和 Scene2D?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 游戏开发的新手,在我开始使用 libgdx ShapeRenderer 并进行了更多搜索后,如果我从右脚开始,我会感到困惑.

I'm new in Android Game Development and after I started with libgdx ShapeRenderer and did a little more search, I became confused if I started with the right foot.

所以,我真正想知道的是何时应该使用 ShapeRenderer、Mesh + SpriteBatch、Box2D 和 Scene2D.

So, what I really would like to know is when should I use ShapeRenderer, Mesh + SpriteBatch, Box2D and Scene2D.

推荐答案

LibGDX 有很多(大部分是正交的)用于渲染的 API.我仍在学习其中的许多方法,但我可以概述不同的部分.

LibGDX has quite a lot of (mostly orthogonal) APIs for rendering. I'm still learning my way around many of them, but I can give an overview of the different parts.

ShapeRenderer 让您可以快速轻松地将基本的纯色多边形和线条放在屏幕上.它不是特别有效(它在每次渲染时上传大量顶点数据).但它可以很快开始.它强烈地面向屏幕(默认为全屏正交项目,其单位通常为像素).

ShapeRenderer lets you quickly and easily put basic flat-colored polygons and lines on the screen. Its not particularly efficient (it uploads a lot of vertex data on each render). But it can be very quick to get going. Its oriented strongly to the screen (it defaults to an orthographic project on the full screen and its units are generally pixels).

Mesh 类让您可以更直接地控制几何图形,但您会接触到更多细节(例如将颜色编码为浮点数以及将顶点数据、颜色数据和纹理数据都存储在同一个浮点数组中,以及索引数组的概念).

The Mesh class gives you more direct control over geometry, but you're exposed to a lot more of the details (like encoding colors as floats and storing vertex data, color data, and texture data all in the same float array, and the concept of index arrays).

SpriteBatch 围绕精灵"(通常是其中带有纹理的矩形区域).这隐藏了网格纹理的一些复杂性(和灵活性).

The SpriteBatch is oriented around "sprites" (generally rectangular areas with a texture in them). This hides some of the complexity (and flexibility) of texturing with meshes.

Box2D 用于物理建模,与您提到的任何其他 API 部分不同(主要与渲染有关).

Box2D is for physics modeling, and is not like any of the other API parts you mentioned (which are mostly about rendering).

最后,Scene2D 主要是关于创建动画用户界面元素,如按钮、列表和滑块.但它足够强大(并且对动画和交互事件有足够简洁和灵活的支持),您也可以在其中实现相当多的游戏".

Finally, Scene2D is mostly about creating animated user-interface elements like buttons, lists, and sliders. But its powerful enough (and has enough neat and flexible support for animations and interaction events) that you can implement quite a lot of "game" in it too.

您还可以使用原始的OpenGL"命令(请参阅 GL20),也是.

You can also use raw "OpenGL" commands (see GL20), too.

您可以完全混合和匹配这些不同的基元(需要注意的是,您通常必须刷新"和结束"任何 ShapeRenderer/SpriteBatch/Mesh 在开始不同风格之前渲染).因此,例如,您可以将 Scene2D 用于您的主菜单"和游戏中的一些元操作,使用 SpriteBatch 来渲染游戏的核心,并使用 ShapeRenderer 生成调试覆盖(考虑可视化边界框等).

You can totally mix-and-match these different primitives (one caveat is that you generally must "flush" and "end" whatever ShapeRenderer/SpriteBatch/Mesh render before starting a different flavor). So, for example, you might use Scene2D for your "main menu" and some meta actions in your game, a SpriteBatch to render the core of your game, and use a ShapeRenderer to generate debug overlays (think visualizing bounding boxes, etc).

还请注意,大多数 libGDX 都是面向 2D 的.原始 OpenGLMesh API 也可用于构建 3D 对象.

Also note that most of libGDX is geared towards 2D. Raw OpenGL and the Mesh APIs can also be used to build 3D objects.

查看 gdx-invaders 示例 了解它如何使用 Mesh、OpenGL 和 SpriteBatch.(我认为这个例子早于 Scene2d,所以没有那个......).查看这些类的实现以了解它们在幕后所做的工作也是值得的.

Check the gdx-invaders example to see how it uses Mesh, OpenGL, and SpriteBatch. (I think this example pre-dates Scene2d, so there is none of that...). Its also worthwhile to look over the implementation of these classes to see what they're doing underneath the covers.

这篇关于何时在 Libgdx 中使用 ShapeRenderer、Mesh + SpriteBatch、Box2D 和 Scene2D?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:何时在 Libgdx 中使用 ShapeRenderer、Mesh + SpriteBatch、

基础教程推荐