Exemplo n.º 1
0
    public static MeshData GenerateTerrainMesh(DisplayReadySlice mapData, float heightMultiplier = 0f, int levelOfDetail = 0, float minHeight = 0f)
    {
        int width  = mapData.GetWidth();
        int height = mapData.GetHeight();
        //Debug.Log("w: " + width + "  h:" + height);
        Vector2 topLeft  = mapData.GetTopLeft();
        float   topLeftX = topLeft.x;
        float   topLeftZ = topLeft.y;

        int actualLOD         = mapData.GetActualLOD();
        int verticesPerLine   = GetVerticesPerDimension(width, actualLOD);
        int verticesPerColumn = GetVerticesPerDimension(height, actualLOD);

        MeshData meshData    = new MeshData(verticesPerLine, verticesPerColumn);
        int      vertexIndex = 0;

        for (int y = 0; y < height; y += mapData.SimplificationIncrementForY(y))
        {
            for (int x = 0; x < width; x += mapData.SimplificationIncrementForX(x))
            {
                meshData.vertices [vertexIndex] = new Vector3(topLeftX + x, mapData.GetNormalized(x, y), topLeftZ - y);
                meshData.uvs [vertexIndex]      = new Vector2(x / (float)width, y / (float)height);

                if (x < width - 1 && y < height - 1)
                {
                    meshData.AddTriangle(vertexIndex, vertexIndex + verticesPerLine + 1, vertexIndex + verticesPerLine);
                    meshData.AddTriangle(vertexIndex + verticesPerLine + 1, vertexIndex, vertexIndex + 1);
                }
                vertexIndex++;
            }
        }
        return(meshData);
    }
Exemplo n.º 2
0
    public Mesh FixNormals(Mesh mesh)
    {
        Vector3[] normals = mesh.normals;
        int       width   = MeshGenerator.GetVerticesPerDimension(mapData.GetWidth(), GetActualLOD());
        int       height  = MeshGenerator.GetVerticesPerDimension(mapData.GetHeight(), GetActualLOD());

        foreach (DisplayNeighborRelation relation in mapData.GetDisplayNeighbors())
        {
            MapDisplay other = relation.GetOtherDisplay(mapData);
            if (other == null || other.GetStatus() == MapDisplayStatus.HIDDEN)
            {
                continue;
            }
            Mesh otherMesh = relation.GetOtherMesh(mapData);
            DisplayReadySlice otherSlice   = relation.GetOtherDRSlice(mapData);
            Vector3[]         otherNormals = otherMesh.normals;
            int otherWidth  = MeshGenerator.GetVerticesPerDimension(otherSlice.GetWidth(), other.GetActualLOD());
            int otherHeight = MeshGenerator.GetVerticesPerDimension(otherSlice.GetHeight(), other.GetActualLOD());
            if (relation.IsFirstMember(mapData))
            {
                FixNormals(normals, width, height,
                           otherNormals, otherWidth, otherHeight,
                           relation.neighborType);
            }
            else
            {
                FixNormals(otherNormals, otherWidth, otherHeight,
                           normals, width, height,
                           relation.neighborType);
            }
            otherMesh.normals = otherNormals;
        }
        mesh.normals = normals;
        return(mesh);
    }