我一直致力于Transvoxel的实施。它现在有效,它是多边形的,但它有点块状。如下图所示:
这是多边形化的代码:
private static Mesh PolygonizeRegularCell (Vector3i pos, Mesh mesh, int lodIndex, Chunk samples)
{
int lod = 1 << lodIndex;
for (int i = 0; i < density.Length; i++) {
density [i] = samples [pos + Transvoxel.cornerIndex [i] * lod].Density;
}
byte caseCode = getCaseCode(density);
if ((caseCode ^ ((density [7] >> 7) & 0xFF)) != 0) {
byte cellClass = Transvoxel.regularCellClass [caseCode];
vertexLocations = Transvoxel.regularVertexData [caseCode];
Transvoxel.RegularCell c = Transvoxel.regularCellData [cellClass];
vertexCount = (long)c.GetVertexCount ();
triangleCount = (long)c.GetTriangleCount ();
indexOffset = c.Indizes ();
localVertexMapping = new int[indexOffset.Length];
for (int i = 0; i < vertexCount; i++) {
ushort edgeCode = (ushort)(vertexLocations [i] & 0xFF);
byte v0 = (byte)((edgeCode >> 4) & 0x0F);
byte v1 = (byte)(edgeCode & 0x0F);
Vector3 p0 = (pos + Transvoxel.cornerIndex [v0] * lod).ToVector3 ();
Vector3 p1 = (pos + Transvoxel.cornerIndex [v1] * lod).ToVector3 ();
long t = (density [v1] << 8) / (density [v1] - density [v0]);
long u = 0x0100 - t;
localVertexMapping [i] = vertices.Count;
Vector3 Q = p0 * t + p1 * u;
Q /= 256f;
AddVertex (Q, samples [pos.x, pos.y, pos.z].block);
}
for (int t = 0; t < triangleCount; t++) {
for (int i = 0; i < 3; i++) {
triangles.Add (localVertexMapping [c.Indizes () [t * 3 + i]]);
}
}
}
return mesh;
}
这是另一个用于生成高度图和密度值的文件:
public void GenerateNewDensityField (int x, int z)
{
densityColumn = new sbyte[128];
noise = (float)(Noise.noise (x * 0.01f, z * 0.01f) * 0.25f);
fHeight = ((noise + 1f) * 54f) + worldSize;
height = Mathf.FloorToInt (fHeight);
for (int i = 0; i < densityColumn.Length; i++) {
if (i < height - 1)
densityColumn [i] = -128;
else if (i == height - 1)
densityColumn [height - 1] = (sbyte)Mathf.Clamp (height - 128, -127, 128);
else if (i == height)
densityColumn [height] = (sbyte)Mathf.Clamp (-height, -128, 127);
else if (i == height + 1)
densityColumn [height + 1] = (sbyte)Mathf.Clamp (127 - height, -128, 127);
else
densityColumn [i] = 127;
}
densityCache [x, z] = new DensityColumn (densityColumn, height);
}
如果您之前使用过Transvoxel或者您对此有任何疑问,请发表评论或回答。我真的坚持这个。
编辑:密度生成代码的变化,不产生过多的零等等,但它仍然是块状的。
答案 0 :(得分:1)
我没有详细查看过您的代码,但屏幕截图中的块状图案让人联想到不精确的体素密度值。例如,如果所有体素数据值都是-1.0或+1.0(或-127和+127作为整数数据),则会看到此模式,两者之间没有任何内容。体素应以单个体素宽度为单位表示到表面的距离,+ 127表示数据点是表面外的一个体素,-127表示数据点是表面内的一个体素。您可能只需要相应地重新调整数据,然后您就会看到平滑的结果。