libGDX中模型批次渲染变慢的问题

16

我有一个问题,想知道在libGDX中是否有一种特殊的模式来分组模型。我创建了一个简单的类,实现了ApplicationListener接口,用于展示我的问题。我正在使用libGDX的夜间构建版本。

我读取了两个不同的模型,它们使用相同的纹理。应用程序分别渲染每种类型的250个模型。以下是渲染代码的一部分:

  mModelBatch.begin(camera);
  for(int y=0; y<50; y++)
  {
     for(int x=-5; x<5; x++)
     {
        ModelInstance instance;
        if(x%2 == 0) instance = modelInstance1;
        else instance = modelInstance2;

        instance.transform.setToTranslation(x, 0, -y);
        mModelBatch.render(instance);
     }

  }

  mModelBatch.end(); 

我在我的手机(Sony Xperia小型专业版)上只有大约12帧每秒的速度。

我正在寻找一个好的解决方案,所以我编写了另一个测试代码:

public void getRenderables(Array<Renderable> renderables, Pool<Renderable> pool)
{
  for(int y=0; y<50; y++)
  {
     for(int x=-5; x<5; x++)
     {
        ModelInstance instance;
        if(x%2 == 0) instance = modelInstance1;
        else instance = modelInstance2;

        instance.transform.setToTranslation(x, y%3, -y);

        Renderable renderable = pool.obtain();
        renderable = instance.getRenderable(renderable);
        renderables.add(renderable);
     }
  }

我按照下面所示使用它:

mModelBatch.begin(camera);      
mModelBatch.render(testRenderProvider);
mModelBatch.end();

然而,它仍然给了我13帧每秒的结果。同时,为了进行另一个测试,我在blender中创建了与之前程序中相同的地图。接下来,我把所有内容都分组到了一个大对象中(没有任何其他编辑)。这样我就创建了一个近1MB大小的巨大对象,在blender的截图上可以看到。

enter image description here

我改变了测试程序的方式,只绘制这个巨大的对象:

mModelBatch.begin(camera);
      modelInstance1.transform.setToTranslation(0, 0, 0);
      mModelBatch.render(modelInstance1);
mModelBatch.end();
下一步我做的是在我的手机(Sony XPeria Mini Pro - 与之前相同)和iPod 5g上启动程序,结果得到了... 60 FPS! enter image description here 是否可能仅使用一个绘制调用来渲染所有内容?

我还没有使用LibGDX进行3D开发,上一次使用OpenGL也已经过去了几年。但是我还记得,如果你想多次渲染同一个对象,应该使用顶点缓冲对象(http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/VertexBufferObject.html)。我认为问题不在于纹理绑定,因为我认为modelBatch会以一种聪明的方式自行处理。 - noone
1
尝试对代码进行分析以了解哪些部分占用了大量时间。Mario在这里有一些关于这个主题的幻灯片:http://www.badlogicgames.com/wordpress/?p=3224 - R Hyde
我知道所有的东西都应该在一个绘制调用中完成。它应该只使用一个纹理,所有的模型都应该一次性渲染出来。我不知道如何在libgdx中启用适当的绘图模式(如果存在的话)。或者也许我必须编写自己的渲染器部分来处理这个问题。有人遇到过类似的问题并能与我分享经验吗? - Paweł Jastrzębski
1个回答

13

问题已经解决!我成功地在一台非常低端的移动设备上实现了60帧每秒的游戏运行。游戏运行流畅。我找到了如何将多个网格合并为一个网格,以便可以使用VBO机制。在libGDX中存在一个错误导致多个网格无法使用网格复制方法。更改后,地图被分成小的区块。每个区块由相同z轴值的网格组成,如下图所示: enter image description here

VBO机制非常有限,因此不能同时绘制太多的顶点,这就是为什么区块必须相对较小的原因。必须编写新的渲染器来正确处理渲染。渲染器的部分正在动态合并网格(无需任何单独的工具,例如blender)。

public static Mesh mergeMeshes(AbstractList<Mesh> meshes, AbstractList<Matrix4> transformations)
{
    if(meshes.size() == 0) return null;

    int vertexArrayTotalSize = 0;
    int indexArrayTotalSize = 0;

    VertexAttributes va = meshes.get(0).getVertexAttributes();
    int vaA[] = new int [va.size()];
    for(int i=0; i<va.size(); i++)
    {
        vaA[i] = va.get(i).usage;
    }

    for(int i=0; i<meshes.size(); i++)
    {
        Mesh mesh = meshes.get(i);
        if(mesh.getVertexAttributes().size() != va.size()) 
        {
            meshes.set(i, copyMesh(mesh, true, false, vaA));
        }

        vertexArrayTotalSize += mesh.getNumVertices() * mesh.getVertexSize() / 4;
        indexArrayTotalSize += mesh.getNumIndices();
    }

    final float vertices[] = new float[vertexArrayTotalSize];
    final short indices[] = new short[indexArrayTotalSize];

    int indexOffset = 0;
    int vertexOffset = 0;
    int vertexSizeOffset = 0;
    int vertexSize = 0;

    for(int i=0; i<meshes.size(); i++)
    {
        Mesh mesh = meshes.get(i);

        int numIndices = mesh.getNumIndices();
        int numVertices = mesh.getNumVertices();
        vertexSize = mesh.getVertexSize() / 4;
        int baseSize = numVertices * vertexSize;
        VertexAttribute posAttr = mesh.getVertexAttribute(Usage.Position);
        int offset = posAttr.offset / 4;
        int numComponents = posAttr.numComponents;

        { //uzupelnianie tablicy indeksow
            mesh.getIndices(indices, indexOffset);
            for(int c = indexOffset; c < (indexOffset + numIndices); c++)
            {
                indices[c] += vertexOffset;
            }
            indexOffset += numIndices;
        }

        mesh.getVertices(0, baseSize, vertices, vertexSizeOffset);
        Mesh.transform(transformations.get(i), vertices, vertexSize, offset, numComponents, vertexOffset, numVertices);
        vertexOffset += numVertices;
        vertexSizeOffset += baseSize;
    }

    Mesh result = new Mesh(true, vertexOffset, indices.length, meshes.get(0).getVertexAttributes());
    result.setVertices(vertices);
    result.setIndices(indices);
    return result;
} 

    public static Mesh copyMesh(Mesh meshToCopy, boolean isStatic, boolean removeDuplicates, final int[] usage) {
    // TODO move this to a copy constructor?
    // TODO duplicate the buffers without double copying the data if possible.
    // TODO perhaps move this code to JNI if it turns out being too slow.
    final int vertexSize = meshToCopy.getVertexSize() / 4;
    int numVertices = meshToCopy.getNumVertices();
    float[] vertices = new float[numVertices * vertexSize];
    meshToCopy.getVertices(0, vertices.length, vertices);
    short[] checks = null;
    VertexAttribute[] attrs = null;
    int newVertexSize = 0;
    if (usage != null) {
        int size = 0;
        int as = 0;
        for (int i = 0; i < usage.length; i++)
            if (meshToCopy.getVertexAttribute(usage[i]) != null) {
                size += meshToCopy.getVertexAttribute(usage[i]).numComponents;
                as++;
            }
        if (size > 0) {
            attrs = new VertexAttribute[as];
            checks = new short[size];
            int idx = -1;
            int ai = -1;
            for (int i = 0; i < usage.length; i++) {
                VertexAttribute a = meshToCopy.getVertexAttribute(usage[i]);
                if (a == null)
                    continue;
                for (int j = 0; j < a.numComponents; j++)
                    checks[++idx] = (short)(a.offset/4 + j);
                attrs[++ai] = new VertexAttribute(a.usage, a.numComponents, a.alias);
                newVertexSize += a.numComponents;
            }
        }
    }
    if (checks == null) {
        checks = new short[vertexSize];
        for (short i = 0; i < vertexSize; i++)
            checks[i] = i;
        newVertexSize = vertexSize;
    }

    int numIndices = meshToCopy.getNumIndices();
    short[] indices = null; 
    if (numIndices > 0) {
        indices = new short[numIndices];
        meshToCopy.getIndices(indices);
        if (removeDuplicates || newVertexSize != vertexSize) {
            float[] tmp = new float[vertices.length];
            int size = 0;
            for (int i = 0; i < numIndices; i++) {
                final int idx1 = indices[i] * vertexSize;
                short newIndex = -1;
                if (removeDuplicates) {
                    for (short j = 0; j < size && newIndex < 0; j++) {
                        final int idx2 = j*newVertexSize;
                        boolean found = true;
                        for (int k = 0; k < checks.length && found; k++) {
                            if (tmp[idx2+k] != vertices[idx1+checks[k]])
                                found = false;
                        }
                        if (found)
                            newIndex = j;
                    }
                }
                if (newIndex > 0)
                    indices[i] = newIndex;
                else {
                    final int idx = size * newVertexSize;
                    for (int j = 0; j < checks.length; j++)
                        tmp[idx+j] = vertices[idx1+checks[j]];
                    indices[i] = (short)size;
                    size++;
                }
            }
            vertices = tmp;
            numVertices = size;
        }
    }

    Mesh result;
    if (attrs == null)
        result = new Mesh(isStatic, numVertices, indices == null ? 0 : indices.length, meshToCopy.getVertexAttributes());
    else
        result = new Mesh(isStatic, numVertices, indices == null ? 0 : indices.length, attrs);
    result.setVertices(vertices, 0, numVertices * newVertexSize);
    result.setIndices(indices);
    return result;
}

这对于试图在libGDX中编写自己的3D游戏的人非常有用。如果没有这个机制,要编写比几个模型更复杂的任何东西几乎是不可能的。


2
很好的答案,不要犹豫接受它。接受自己的答案也是可以的 :) - Daahrien
2
嗨,Paweł。60 fps 真的很棒!我想知道你是如何使用这些方法的?我是一个libgdx的初学者,我的游戏fps经常降到8。我看了一下libgdx的代码,发现SpriteBatch创建Mesh时使用的是VertexArray而不是VertexBufferObject。我读到VBO是一种提高性能的方法。我在Mesh中将forceVBO设置为true,但没有看到明显的差异。如果你能帮助我,我会继续提供更多细节。非常感谢任何帮助。 - Ruzanna
当然我会帮助你,但这并不像你想象的那么简单,因为你需要编写自己的libGDX部分。这是因为libGDX代码存在缺陷,无法直接使用。诀窍是将模型(假设您正在使用3D)分组成小块,然后使用VBO(强制VBO适用于一个单独的模型)。所以诀窍是将每个网格连接起来创建一个更大的网格(但不要太大),然后使用VBO进行渲染。如果您需要更复杂的答案(包括代码片段),请告诉我,我非常乐意帮助您。 - Paweł Jastrzębski
1
我相信我有同样的问题,但我不确定如何使用mergeMeshes()方法。您能否给出一些使用它的代码示例?基本上,我困惑的是在哪里获取AbstractList<Mesh> meshes值以作为参数传递。 - twiz
你有没有把你的代码放到Github上,这会很有用。 - Burf2000
你好@PawełJastrzębski,我一直在寻找这段代码,请帮我解决一些问题。我正在从纹理区域创建2D矩形模型,但不知道如何将它们组合起来,任何帮助或代码都将是极大的帮助,请提供有关您上面的代码和使用的绘制方法的说明。 - Diljeet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接