Unity3d 垂直壁上正x轴和负x轴上的统一网格UV问题

Unity3d 垂直壁上正x轴和负x轴上的统一网格UV问题,unity3d,3d,mesh,marching-cubes,Unity3d,3d,Mesh,Marching Cubes,我正在尝试制作一个3D体素游戏,它使用marching cubes算法按程序生成一个游戏世界。到目前为止,这一切都很好,除了在给定垂直世界/块网格块的正x边和负x边上的完全垂直边上,uv坐标看起来不太正确,因为它只显示纯色而不是纹理。[![调试块和错误方面的视图][1][1] 在这里,你可以看到砖墙的样子,你可以看到奇怪的虫子。这只发生在完全垂直的x边三角形上!图像中的这些网格是用于显示问题的调试块。 所有的噪音到地形的转换都很好,我没有发现任何错误。只有紫外线会引起问题。 我正在使用以下代码填

我正在尝试制作一个3D体素游戏,它使用marching cubes算法按程序生成一个游戏世界。到目前为止,这一切都很好,除了在给定垂直世界/块网格块的正x边和负x边上的完全垂直边上,uv坐标看起来不太正确,因为它只显示纯色而不是纹理。[![调试块和错误方面的视图][1][1]

在这里,你可以看到砖墙的样子,你可以看到奇怪的虫子。这只发生在完全垂直的x边三角形上!图像中的这些网格是用于显示问题的调试块。 所有的噪音到地形的转换都很好,我没有发现任何错误。只有紫外线会引起问题。 我正在使用以下代码填充Mesh.vertices、Mesh.triangles和Mesh.uv:

void MakeChunkMeshData()
{
    for (int x = 0; x < Variables.chunkSize.x - 1; x++)
    {
        for (int y = 0; y < Variables.chunkSize.y - 1; y++)
        {
            for (int z = 0; z < Variables.chunkSize.z - 1; z++)
            {
                byte[] cubeCornerSolidityValues = new byte[8]{
                    chunkMapSolidity[x, y, z],
                    chunkMapSolidity[x + 1, y, z],
                    chunkMapSolidity[x + 1, y + 1, z],
                    chunkMapSolidity[x, y + 1, z],

                    chunkMapSolidity[x, y, z + 1],
                    chunkMapSolidity[x + 1, y, z + 1],
                    chunkMapSolidity[x + 1, y + 1, z + 1],
                    chunkMapSolidity[x, y + 1, z + 1]
                };

                MarchOne(new Vector3Int(x, y, z), cubeCornerSolidityValues);
            }
        }
    }
}

void DrawChunk()
{
    chunkMesh.vertices = vertices.ToArray();
    chunkMesh.triangles = triangles.ToArray();
    chunkMesh.SetUVs(0, uvs.ToArray());
    chunkMesh.RecalculateNormals();
}

void ClearMesh()
{
    chunkMesh.Clear();
}

void ClearChunkMeshAndData()
{
    chunkMesh.Clear();
    uvs = new List<Vector2>();
    vertices = new List<Vector3>();
    triangles = new List<int>();
}

/// <summary>
/// cube contains bytes for each corner of the cube
/// </summary>
/// <param name="cube"></param>
/// <returns></returns>
int GetCubeConfiguration(byte[] cube)
{
    int u = 0;
    int result = 0;
    for(int corner = 0; corner < 8; corner++)
    {
        if (cube[corner] < Variables.solidityThreshold)
        {
            u++;
            result |= 1 << corner;
        }
    }
    return result;
}

Vector2[] getUvsPerTriangle(byte[] voxelTypes)
{
    int resId = voxelTypes[0];
    if (voxelTypes[1] == voxelTypes[2])
        resId = voxelTypes[1];
    resId = 1;

    Vector2 normalized = getUvCoordFromTextureIndex(resId) / Constants.TextureAtlasSizeTextures;
    float textureLength = 1f/Constants.TextureAtlasSizeTextures;
    
    Vector2[] result = new Vector2[3] {
        normalized + new Vector2(1,0) * textureLength,
        normalized + new Vector2(0,1) * textureLength,
        normalized + new Vector2(1,1) * textureLength
    };
    //Debug.Log(result);
    return result;
}

/// <summary>
/// returns the absolute x and y coordinates of the given texture in the atlas (example: [4, 1])
/// </summary>
/// <param name="textureIndex"></param>
/// <returns></returns>
Vector2 getUvCoordFromTextureIndex(int textureIndex)
{
    int x = textureIndex % Constants.TextureAtlasSizeTextures;
    int y = (textureIndex - x) / Constants.TextureAtlasSizeTextures;
    return new Vector2(x, y);
}

/// <summary>
/// takes the chunk-wide mesh data and adds its results after marching one cube to it.
/// </summary>
/// <returns></returns>
void MarchOne(Vector3Int offset, byte[] cube)
{
    int configuration = GetCubeConfiguration(cube);

    byte[] voxelTypes = new byte[3];

    int edge = 0;
    for (int i = 0; i < 5; i++) //loop at max 5 times (max number of triangles in one cube config)
    {
        for(int v = 0; v < 3; v++)  // loop 3 times through shit(count of vertices in a TRIangle, who would have thought...)
        {
            int cornerIndex = VoxelData.TriangleTable[configuration, edge];

            if (cornerIndex == -1)  // indicates the end of the list of vertices/triangles
                return;

            Vector3 vertex1 = lwTo.Vec3(VoxelData.EdgeTable[cornerIndex, 0]) + offset;
            Vector3 vertex2 = lwTo.Vec3(VoxelData.EdgeTable[cornerIndex, 1]) + offset;

            Vector3Int vertexIndex1 = lwTo.Vec3Int(VoxelData.EdgeTable[cornerIndex, 0]) + offset;
            Vector3Int vertexIndex2 = lwTo.Vec3Int(VoxelData.EdgeTable[cornerIndex, 1]) + offset;

            Vector3 vertexPosition;
            if (Variables.badGraphics)
            {
                vertexPosition = (vertex1 + vertex2) / 2f;
            }
            else
            {
                // currently using this "profile" 
                // this code determines the position of the vertices per triangle based on the value in chunkSolidityMap[,,]
                float vert1Solidity = chunkMapSolidity[vertexIndex1.x, vertexIndex1.y, vertexIndex1.z];
                float vert2Solidity = chunkMapSolidity[vertexIndex2.x, vertexIndex2.y, vertexIndex2.z];

                float difference = vert2Solidity - vert1Solidity;
                difference = (Variables.solidityThreshold - vert1Solidity) / difference;

                vertexPosition = vertex1 + ((vertex2 - vertex1) * difference);
            }
            vertices.Add(vertexPosition);
            triangles.Add(vertices.Count - 1);

            voxelTypes[v] = chunkMapVoxelTypes[vertexIndex1.x, vertexIndex1.y, vertexIndex1.z];

            edge++;
        }
        uvs.AddRange(getUvsPerTriangle(voxelTypes));
    }
}
void MakeChunkMeshData()
{
for(intx=0;x结果|=1好吧,这可能是unity的一些奇怪行为。当在网格填充过程后显式地将网格设置为meshFilters的网格时,它工作得很好。此外,我还必须调用mesh.RecreactAngents()使其工作