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

查看:99
本文介绍了为什么我不应该使用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.

相反,他们建议您使用混合器之类的方法来创建自己的多维数据集模型,而不是使用这些模型代替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方法是使用单个Node创建一个Model的便捷方法,该Node包含一个NodePart,该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天全站免登陆