为什么我不应该使用 LibGDX 的 createBox 或 createRect? [英] Why excatly shouldn't I use LibGDX's createBox or createRect?

查看:25
本文介绍了为什么我不应该使用 LibGDX 的 createBox 或 createRect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个体素引擎,以熟悉所涉及的许多概念,这对我来说也是一种优化练习.在四处搜索时,我在多个网站上看到了多个来源,这表明我不使用 ModelBuilder().createBox() 或 createRect() 来执行除故障排除之外的任何其他操作.

I'm currently in the process of creating a voxel engine in order to familiarize myself with many of the concepts involved, It also happens to be an exercise in optimization for me. When searching around I've seen multiple sources across several websites suggust that I not use ModelBuilder().createBox() or createRect() to do anything but troubleshooting.

相反,他们建议在诸如 Blender 之类的东西中创建您自己的立方体模型,而不是使用它们来代替 createBox.我刚刚完成了优化剔除的实现,重写我的代码以切换到自定义模型需要一些时间.在做出类似的事情之前,我想了解我在做什么以及我为什么要这样做.

Instead they recommend creating your own cube models in something like blender, than using those in place of createBox. I've just finished Implementing culling for optimization, and rewriting my code to change over to custom models would take some time. I want to understand what I'm doing and why I'm doing it before committing to something like that.

为什么不推荐使用 createBox?在这种情况下,创建单独的模型真的是最好的主意吗?

Why exactly is createBox not recommended? is creating individual models really the best idea in this situation?

推荐答案

ModelBuilder#createBox 方法是一种使用单个 创建 Model 的便捷方法Node 包含单个 NodePart ,其中包含单个框形状.和做的完全一样:

The ModelBuilder#createBox method is a convenience method to create a Model with a single Node that contains a single NodePart which contains a single box shape. It is exactly the same as doing:

ModelBuilder builder = new ModelBuilder();
builder.begin();
builder.node();
MeshPartBuilder mpb = builder.part("box", primitiveType, attributes, material);
BoxShapeBuilder.build(mpb, width, height, depth);
model = builder.end();

它创建一个 Mesh 对象,它是一个 OpenGL 资源,它包含(取决于属性)仅 8 或 24 个顶点和 36 个索引.除非它是你唯一想要渲染的东西,否则有这么小的 Mesh 对性能来说是非常糟糕的.

It creates a Mesh object, which is an OpenGL resource, which contains (depending on the attributes) only 8 or 24 vertices and 36 indices. Unless it is the only thing you want to render, it is very bad for performance to have such small Mesh.

这是因为每个Mesh(或Node的(部分))都暗示了一个渲染调用.这意味着它必须等待前一个渲染调用完成并且 CPU 和 GPU 同步.将尽可能多的形状组合成一个 Mesh 并立即渲染它会更高效.毕竟,GPU 旨在同时在多个顶点上并行执行.

This is because every Mesh (or (part) of a Node) implies a render call. This means that it has to wait for the previous render call to be finished and the CPU and GPU to synchronize. It would be far more performant to combine as many shapes as possible into one Mesh and render that at once. After all, the GPU is intended to be executed on many vertices in parallel at once.

这与为什么的原因相同,例如SpriteBatch 在渲染之前组合尽可能多的精灵.这也是为什么使用单个 TextureAtlas 比使用单独的纹理在性能上更好的原因.

This is the same reason as why e.g. SpriteBatch combines as many sprites as possible before rendering it. And it is also the reason why using a single TextureAtlas is better for performance than using separate textures.

因此,如果您使用仅包含单个框的模型创建体素引擎,那么您最终会遇到数千次渲染调用.这会很慢,以至于您的游戏几乎无法玩.

So, if you would create a voxel engine using models that contain only a single box then you would end up with thousands of render calls. That would be so slow that it would make your game practically unplayable.

当然,解决这个问题很简单,只需在零件中添加多个框即可:

Ofcourse, the solution to this is easy, just add multiple boxes to the part:

ModelBuilder builder = new ModelBuilder();
builder.begin();
builder.node();
MeshPartBuilder mpb = builder.part("box", primitiveType, attributes, material);
BoxShapeBuilder.build(mpb, x1, y1, z1, width1, height1, depth1);
BoxShapeBuilder.build(mpb, x2, y2, z2, width2, height2, depth2);
//...
BoxShapeBuilder.build(mpb, xn, yn, zn, widthn, heightn, depthn);
model = builder.end();

ModelBuilder#createBox(和其他创建方法)隐藏了这个逻辑,这使得查看幕后发生的事情以及优化的难易程度变得不那么明显.因此,我可能有一天会删除这些方法.不幸的是,createXXX 方法对于学习 3D API 的新手非常有帮助(例如我在 教程 在我的博客上),谁想要快速入门.这就是我还没有删除它们的原因.

The ModelBuilder#createBox (and other create methods) hide this logic, which makes it less obvious to see what is going on behind the scenes and how easily it can be optimized. Therefor I will probably remove those methods some day. Unfortunately the createXXX methods are quite helpful for newcomers learning the 3D API (e.g. I use it in the tutorials on my blog), who want a quick start. Which is the reason I still haven't removed them.

顺便说一句,将 ModelBuilder 用于体素引擎是一种矫枉过正.你可能想看看本教程,我在那里展示一种在运行时组合简单形状的简单方法.最新版本的 libGDX 甚至包含 ShapeCache,它实际上可以作为块直接使用体素.

Btw, using ModelBuilder for a voxel engine is an overkill. You might want to have a look at this tutorial instead, where I show a simple way to combine simple shapes at runtime. The latest release of libGDX even contains ShapeCache, which does practically what you can use directly as chunk of voxels.

我不知道为什么您会认为在建模应用程序中创建一个盒子与通过代码创建一个盒子有什么不同.也许您可以链接到您找到的参考资料.

I'm not sure though why you'd think that creating a box in a modelling application is any different than creating it by code. Perhaps you can link to the reference where you found that.

这篇关于为什么我不应该使用 LibGDX 的 createBox 或 createRect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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