问题已经解决!我成功地在一台非常低端的移动设备上实现了60帧每秒的游戏运行。游戏运行流畅。我找到了如何将多个网格合并为一个网格,以便可以使用VBO机制。在libGDX中存在一个错误导致多个网格无法使用网格复制方法。更改后,地图被分成小的区块。每个区块由相同z轴值的网格组成,如下图所示:
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;
{
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) {
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游戏的人非常有用。如果没有这个机制,要编写比几个模型更复杂的任何东西几乎是不可能的。