【发布时间】:2021-10-18 03:20:06
【问题描述】:
为了提供一些内容,我开发了一个块生成系统,它可以随机生成块到 1 个网格中以进行优化。 (类似于 Minecraft。)所有的顶点、三角形、UV 等都是从头开始制作的。根据您的外观,面部会相互夹杂,如 GIF 所示。这里发生的可能的问题是什么?生成如何工作的基本概念:
for (int x = 0; x < 10; x++) { //For each block in chunk being loaded on x axis,
for (int y = 0; y < 10; y++) { //For each block in chunk being loaded on y axis,
for (int z = 0; z < 10; z++) { //For each block in chunk being loaded on z axis,
if (exists[x, y, z] == true) {
Vector2[] normalUVs = {
new Vector2(0, 0), new Vector2(.1f, 0), new Vector2(.1f, .1f), new Vector2(0, .1f)
};
normalUVs[0] = new Vector2(normalUVs[0].x + (texPosX[x, y, z] ), normalUVs[0].y + (texPosY[x, y, z] ));
normalUVs[1] = new Vector2(normalUVs[1].x + (texPosX[x, y, z] ), normalUVs[1].y + (texPosY[x, y, z] ));
normalUVs[2] = new Vector2(normalUVs[2].x + (texPosX[x, y, z] ), normalUVs[2].y + (texPosY[x, y, z] ));
normalUVs[3] = new Vector2(normalUVs[3].x + (texPosX[x, y, z] ), normalUVs[3].y + (texPosY[x, y, z] ));
Vector2[] flippedUVs = {
new Vector2(.1f, .1f), new Vector2(0, .1f), new Vector2(0, 0), new Vector2(.1f, 0)
};
flippedUVs[0] = new Vector2(flippedUVs[0].x + (texPosX[x, y, z] ), flippedUVs[0].y + (texPosY[x, y, z] ));
flippedUVs[1] = new Vector2(flippedUVs[1].x + (texPosX[x, y, z] ), flippedUVs[1].y + (texPosY[x, y, z] ));
flippedUVs[2] = new Vector2(flippedUVs[2].x + (texPosX[x, y, z] ), flippedUVs[2].y + (texPosY[x, y, z] ));
flippedUVs[3] = new Vector2(flippedUVs[3].x + (texPosX[x, y, z] ), flippedUVs[3].y + (texPosY[x, y, z] ));
Vector2[] heightUVs = {
new Vector2(0, 0), new Vector2(0, .1f), new Vector2(.1f, .1f), new Vector2(.1f, 0)
};
heightUVs[0] = new Vector2(heightUVs[0].x + (texPosX[x, y, z] ), heightUVs[0].y + (texPosY[x, y, z] ));
heightUVs[1] = new Vector2(heightUVs[1].x + (texPosX[x, y, z] ), heightUVs[1].y + (texPosY[x, y, z] ));
heightUVs[2] = new Vector2(heightUVs[2].x + (texPosX[x, y, z] ), heightUVs[2].y + (texPosY[x, y, z] ));
heightUVs[3] = new Vector2(heightUVs[3].x + (texPosX[x, y, z] ), heightUVs[3].y + (texPosY[x, y, z] ));
List<Vector3> visibleVerts = new List<Vector3>(); //Used as a temp array
List<int> visibleTris = new List<int>(); //Used as a temp array
List<Vector2> visibleUVs = new List<Vector2>(); //Used as a temp array
如何将正面添加到顶点、UV 和三角形数组的示例:
if ((z == 0 && exists[x, y, z] == true) || (z > 0 && exists[x, y, z - 1] == false)) { //If there's not a block in front of this one or if it's on the edge of the chunk,
faces = new Vector3[] {
new Vector3 (0 + x, 0 + y, 0 + z),
new Vector3 (0 + x, 1 + y, 0 + z),
new Vector3 (1 + x, 1 + y, 0 + z),
new Vector3 (1 + x, 0 + y, 0 + z)
};
tris = new int[] {
0 + (faceCount * 4), //0
1 + (faceCount * 4), //1
2 + (faceCount * 4), //2
0 + (faceCount * 4), //0
2 + (faceCount * 4), //2
3 + (faceCount * 4) //3
};
visibleVerts.AddRange(faces);
visibleTris.AddRange(tris);
visibleUVs.AddRange(normalUVs);
faceCount++;
}
【问题讨论】:
-
我会尝试缩小问题范围,似乎是渲染问题,与网格生成或元素位置无关。在这种情况下,所有发布的代码都不相关。我会检查问题是否重现了根据 cemera 的 z 渲染的材料,例如统一的默认材料具有不同的颜色,以便您可以区分立方体。如果在这种情况下没有出现问题,您需要检查您的材质/着色器及其与混合有关的行为
标签: unity3d graphics 3d rendering