예제 #1
0
    public static List <Mesh>[] getMeshes(List <GameObject>[] chunks, Block[,,] blockGrid)
    {
        //Array of each texture
        //List of each chunk for texture
        //List of each vertex/index/UV
        List <List <Vector3> >[] vertices = new List <List <Vector3> > [chunks.Length];
        List <List <int> >[]     indices  = new List <List <int> > [chunks.Length];
        List <List <Vector2> >[] UVs      = new List <List <Vector2> > [chunks.Length];

        for (int x = 0; x < ChunkMap.SizeX; x++)
        {
            for (int y = 0; y < ChunkMap.SizeY; y++)
            {
                for (int z = 0; z < ChunkMap.SizeZ; z++)
                {
                    Block block = blockGrid[x, y, z];

                    if (block.texture != 0)
                    {
                        int i = block.texture - 1;                         //choose the Chunk
                        if (chunks.Length == 1)
                        {
                            i = 0;
                        }

                        if (vertices[i] == null)
                        {
                            vertices[i] = new List <List <Vector3> >();
                            indices[i]  = new List <List <int> >();
                            UVs[i]      = new List <List <Vector2> >();
                        }

                        int verticeCountInStart = 0;
                        if (vertices[i].Count != 0)
                        {
                            verticeCountInStart = vertices[i].Last().Count();
                        }

                        //Create new list for chunk
                        if (vertices[i].Count == 0 || verticeCountInStart > 50000)
                        {
                            vertices[i].Add(new List <Vector3>());
                            indices[i].Add(new List <int>());
                            UVs[i].Add(new List <Vector2>());
                        }

                        Block[] adjacentBlocks = getAdjacentBlocks(blockGrid, x, y, z, block.rotation);

                        List <Vector3> verts = vertices[i].Last();
                        List <int>     inds  = indices[i].Last();
                        List <Vector2> uvs   = UVs[i].Last();

                        int rotation = block.rotation;

                        switch (block.shape)
                        {
                        case 0: Cube.makeCube(i, x, y, z, verts, inds, uvs, adjacentBlocks); break;

                        case 1: rotation = Stairs.makeStairs(i, x, y, z, verts, inds, uvs, adjacentBlocks, blockGrid); break;

                        case 2: rotation = Slope.makeSlope(i, x, y, z, verts, inds, uvs, adjacentBlocks, blockGrid, 0, 1); break;

                        case 3: Smooth.makeCube(i, x, y, z, verts, inds, uvs, adjacentBlocks, blockGrid); break;

                        case 4: Cube.makeSlab(i, x, y, z, verts, inds, uvs, adjacentBlocks); break;

                        case 5: rotation = Slope.makeSlope(i, x, y, z, verts, inds, uvs, adjacentBlocks, blockGrid, 0, 0.5f); break;
                        }

                        //ROTATE
                        for (int n = verticeCountInStart; n < verts.Count; n++)
                        {
                            Quaternion rot         = Quaternion.Euler(0, 90 * rotation, 0);
                            Vector3    middlePoint = new Vector3(x + 0.5f, y + 0.5f, z + 0.5f);

                            verts[n] = rot * (verts[n] - middlePoint) + middlePoint;
                        }
                    }
                }
            }
        }
        List <Mesh>[] meshes = new List <Mesh> [chunks.Length];
        for (int i = 0; i < chunks.Length; i++)
        {
            meshes[i] = new List <Mesh>();

            if (vertices[i] != null)
            {
                for (int j = 0; j < vertices[i].Count; j++)
                {
                    meshes[i].Add(new Mesh());
                    meshes[i][j].vertices  = vertices[i][j].ToArray();
                    meshes[i][j].triangles = indices[i][j].ToArray();
                    meshes[i][j].uv        = UVs[i][j].ToArray();
                }
            }
            else
            {
                meshes[i].Add(new Mesh());
            }
        }
        return(meshes);
    }