Пример #1
0
    // Update is called once per frame
    public void BuildMesh()
    {
        instance     = this;
        MapCreated   = false;
        MapWorldSize = new Vector2(size_x * tileSize, size_y * tileSize);
        if (grid != null)
        {
            grid.node_radius   = tileSize / 2;
            grid.GridWorldSize = MapWorldSize;
        }

        CalcTex();

        Global_TDMap = new TDMap(size_x, size_y, MapTerrainType, TileChance, MaxHeightForPlants, MaxHeightLeap);

        int numTiles = size_x * size_y;
        int numTris  = numTiles * 2;
        int vsize_x  = size_x * 2;
        int vsize_y  = size_y * 2;
        int numVerts = vsize_x * vsize_y;

        if (vsize_x * vsize_y > 65000)
        {
            size_x = 125;
            size_y = 125;
            return;
        }

        Vector3[] vertices = new Vector3[numVerts];
        Vector3[] normals  = new Vector3[numVerts];
        Vector2[] uv       = new Vector2[numVerts];

        int[] triangles = new int[numTris * 3];

        int x, y;

        Vector3 sp = Vector3.zero;

        sp.x -= tileSize * size_x / 2;
        sp.y -= tileSize * size_x / 2;
        float r = Random.Range(-2f, 2f);

        for (y = 0; y < vsize_y; y++)
        {
            for (x = 0; x < vsize_x; x++)
            {
                vertices[y * vsize_x + x] = new Vector3((x + 1) / 2 * tileSize, (y + 1) / 2 * tileSize, 0) + sp;
                normals[y * vsize_x + x]  = new Vector3(0, 0, -1);
                uv[y * vsize_x + x]       = GetTexture(Global_TDMap.GetTile(x / 2, y / 2).type, x, y);
            }
        }


        if (Heights)
        {
            for (y = 1; y < vsize_y - 1; y += 2)
            {
                for (x = 1; x < vsize_x - 1; x += 2)
                {
                    r = Random.Range(0, 0);

                    if (Global_TDMap.GetIfWater(x / 2, y / 2))
                    {
                        r = -Random.Range(-8f, -10f);
                    }
                    else
                    {
                        r = Global_TDMap.GetIfMountine((x + 0) / 2, (y + 0) / 2);
                    }
                    vertices[y * vsize_x + x].z           = r;
                    vertices[y * vsize_x + x + 1].z       = r;
                    vertices[(y + 1) * vsize_x + x].z     = r;
                    vertices[(y + 1) * vsize_x + x + 1].z = r;
                }
            }

            for (y = 0; y < vsize_y; y++)
            {
                vertices[y * vsize_x].z = vertices[y * vsize_x + 1].z;
                vertices[y * vsize_x + vsize_x - 1].z = vertices[y * vsize_x + vsize_x - 2].z;
            }

            for (x = 0; x < vsize_x; x++)
            {
                vertices[x].z = vertices[vsize_x + x].z;
                vertices[(vsize_y - 1) * vsize_x + x].z = vertices[(vsize_y - 2) * vsize_x + x].z;
            }

            print("End OF Heights!");
        }
        for (y = 0; y < size_y; y++)
        {
            for (x = 0; x < size_x; x++)
            {
                //x = 1
                int squareIndex = y * size_x + x;
                int triOffset   = squareIndex * 6;
                triangles[triOffset + 0] = (y * 2) * vsize_x + (x * 2) + 0;
                triangles[triOffset + 1] = (y * 2) * vsize_x + (x * 2) + vsize_x + 0;
                triangles[triOffset + 2] = (y * 2) * vsize_x + (x * 2) + vsize_x + 1;

                triangles[triOffset + 3] = (y * 2) * vsize_x + (x * 2) + 0;
                triangles[triOffset + 4] = (y * 2) * vsize_x + (x * 2) + vsize_x + 1;
                triangles[triOffset + 5] = (y * 2) * vsize_x + (x * 2) + 1;
            }
        }

        Mesh mesh = new Mesh();

        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.uv        = uv;
        mesh.triangles = triangles;

        MeshFilter   mesh_filter   = GetComponent <MeshFilter>();
        MeshRenderer mesh_renderer = GetComponent <MeshRenderer>();
        MeshCollider mesh_collider = GetComponent <MeshCollider>();

        mesh_filter.mesh         = mesh;
        mesh_collider.sharedMesh = mesh;
        Debug.Log("Done Mesh!");
        //BuildTexture();

        OnMapGenerator GenerateTrees = new OnMapGenerator(Global_TDMap, size_x, size_y, tileSize, MapHolder);

        StartCoroutine(GenerateTreesEI(GenerateTrees));
        //MapCreated = true;
    }