我检查了一个LibGDX高度图加载器and found one,但我不太确定buildIndices()代码是如何工作的。我也不确定为什么它只生成一个128 * x * 128的高度图块,任何大于128(例如1024)的东西都会让加载器陷入困境:
1024 chunk
正如您所看到的,山脉有洞,大块的形状呈现为128 * 1024网格
128 chunk
完美无缺的128 * 128块
这是我的代码:
package com.amzoft.game.utils;
import java.util.Arrays;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
public class HeightmapConverter {
public int mapWidth, mapHeight;
private float[] heightMap;
public float[] vertices;
public short[] indices;
private int strength;
private String heightmapFile;
private float textureWidth;
public HeightmapConverter(int mapWidth, int mapHeight, int strength, String heightmapFile)
{
this.heightMap = new float[(mapWidth+1) * (mapHeight+1)];
this.mapWidth = mapWidth;
this.mapHeight = mapHeight;
this.vertices = new float[heightMap.length*5];
this.indices = new short[mapWidth * mapHeight * 6];
this.strength = strength;
this.heightmapFile = heightmapFile;
loadHeightmap();
createIndices();
createVertices();
}
public void loadHeightmap()
{
try{
FileHandle handle = Gdx.files.internal(heightmapFile);
Pixmap heightmapImage = new Pixmap(handle);
textureWidth = (float)heightmapImage.getWidth();
Color color = new Color();
int indexToIterate = 0;
for(int y = 0; y < mapHeight + 1; y++)
{
for(int x = 0; x < mapWidth + 1; x++)
{
Color.rgba8888ToColor(color, heightmapImage.getPixel(x, y));
heightMap[indexToIterate++] = color.r;
}
}
handle = null;
heightmapImage.dispose();
heightmapImage = null;
color = null;
indexToIterate = 0;
}catch(Exception e){
e.printStackTrace();
}
}
public void createVertices()
{
int heightPitch = mapHeight + 1;
int widthPitch = mapWidth + 1;
int idx = 0;
int hIdx = 0;
for(int z = 0; z < heightPitch; z++)
{
for(int x = 0; x < widthPitch; x++)
{
vertices[idx+0] = x;
vertices[idx+1] = heightMap[hIdx++] * strength;
vertices[idx+2] = z;
vertices[idx+3] = x/textureWidth;
vertices[idx+4] = z/textureWidth;
idx += 5;
}
}
}
public void createIndices()
{
int idx = 0;
short pitch = (short)(mapWidth + 1);
short i1 = 0;
short i2 = 1;
short i3 = (short)(1 + pitch);
short i4 = pitch;
short row = 0;
for(int z = 0; z < mapHeight; z++)
{
for(int x = 0; x < mapWidth; x++)
{
indices[idx++] = (short)(i1);
indices[idx++] = (short)(i2);
indices[idx++] = (short)(i3);
indices[idx++] = (short)(i3);
indices[idx++] = (short)(i4);
indices[idx++] = (short)(i1);
i1++;
i2++;
i3++;
i4++;
}
row += pitch;
i1 = row;
i2 = (short)(row + 1);
i3 = (short)(i2 + pitch);
i4 = (short)(row + pitch);
}
}
public String getHeightmapFile()
{
return heightmapFile;
}
}
为什么没有更大的块工作? createIndices()(在LibGDX代码中称为buildIndices())如何工作?
答案 0 :(得分:1)
解决方案在这里:http://badlogicgames.com/forum/viewtopic.php?f=11&t=4918#p23683
Java short只能保存值[-32768 .. 32767]那个地形网格 使用更多的指数。 256 * 256太多(大约65k)所以你是 只能看到一小部分高度图。
感谢kalle_h的解决方案。