Exemple #1
0
        // TODO: let consumers of IChunkMesh set its effect, so consumers can let it be transparent.
        public override Buffer <VertexPositionColorTexture> Meshalyze(IEditorGame game, BlockSelection volume)
        {
            var chunkVerticesList = new List <VertexPositionColorTexture>(13000);

            volume.GetChunks(AccessMode.Read, portions =>
            {
                foreach (var partition in portions)
                {
                    var chunk  = partition.Chunk;
                    var blocks = chunk.Blocks;

                    for (var y = partition.YMin; y < partition.YMax; y++)
                    {
                        for (var x = partition.XMin; x < partition.XMax; x++)
                        {
                            for (var z = partition.ZMin; z < partition.ZMax; z++)
                            {
                                var block = blocks.GetBlock(x, y, z);
                                if (block.Info.State == BlockState.NONSOLID || block.Info.State == BlockState.FLUID)
                                {
                                    continue;
                                }

                                var blockVector = new Vector3(x, y, z);
                                // block isn't air, assume its solid.
                                // at this point, texture coordinates should be calculated
                                // block specific shapes should happen (eg water)
                                if (x + 1 < blocks.XDim)
                                {
                                    // face with normal x+
                                    var xPlusBlock = blocks.GetBlock(x + 1, y, z);
                                    if (xPlusBlock.Info.State == BlockState.NONSOLID || block.Info.State == BlockState.FLUID)
                                    {
                                        AddTriangleQuad(blockVector, GeometricPrimitives.RightQuad, chunkVerticesList,
                                                        GetVertexColor(chunk, new LocalBlockPosition(x, y, z),
                                                                       new LocalBlockPosition(x + 1, y, z)));
                                    }
                                }

                                if (y + 1 < blocks.YDim)
                                {
                                    var yPlusBlock = blocks.GetBlock(x, y + 1, z);
                                    if (yPlusBlock.Info.State == BlockState.NONSOLID || block.Info.State == BlockState.FLUID)
                                    {
                                        AddTriangleQuad(blockVector, GeometricPrimitives.TopQuad, chunkVerticesList,
                                                        GetVertexColor(chunk, new LocalBlockPosition(x, y, z),
                                                                       new LocalBlockPosition(x, y + 1, z)));
                                    }
                                }

                                if (z + 1 < blocks.ZDim)
                                {
                                    var zPlusBlock = blocks.GetBlock(x, y, z + 1);
                                    if (zPlusBlock.Info.State == BlockState.NONSOLID || block.Info.State == BlockState.FLUID)
                                    {
                                        AddTriangleQuad(blockVector, GeometricPrimitives.BackwardQuad,
                                                        chunkVerticesList,
                                                        GetVertexColor(chunk, new LocalBlockPosition(x, y, z),
                                                                       new LocalBlockPosition(x, y, z + 1)));
                                    }
                                }

                                if (x - 1 >= 0)
                                {
                                    var xMinusBlock = blocks.GetBlock(x - 1, y, z);
                                    if (xMinusBlock.Info.State == BlockState.NONSOLID || block.Info.State == BlockState.FLUID)
                                    {
                                        AddTriangleQuad(blockVector, GeometricPrimitives.LeftQuad, chunkVerticesList,
                                                        GetVertexColor(chunk, new LocalBlockPosition(x, y, z),
                                                                       new LocalBlockPosition(x - 1, y, z)));
                                    }
                                }

                                if (y - 1 >= 0)
                                {
                                    var yMinusBlock = blocks.GetBlock(x, y - 1, z);
                                    if (yMinusBlock.Info.State == BlockState.NONSOLID || block.Info.State == BlockState.FLUID)
                                    {
                                        AddTriangleQuad(blockVector, GeometricPrimitives.BottomQuad, chunkVerticesList,
                                                        GetVertexColor(chunk, new LocalBlockPosition(x, y, z),
                                                                       new LocalBlockPosition(x, y - 1, z)));
                                    }
                                }

                                if (z - 1 >= 0)
                                {
                                    var zMinusBlock = blocks.GetBlock(x, y, z - 1);
                                    if (zMinusBlock.Info.State == BlockState.NONSOLID || block.Info.State == BlockState.FLUID)
                                    {
                                        AddTriangleQuad(blockVector, GeometricPrimitives.ForwardQuad, chunkVerticesList,
                                                        GetVertexColor(chunk, new LocalBlockPosition(x, y, z),
                                                                       new LocalBlockPosition(x, y, z - 1)));
                                    }
                                }
                            }
                        }
                    }
                }
            });

            return(chunkVerticesList.Count == 0
                ? null
                : Buffer.Vertex.New(game.GraphicsDevice, chunkVerticesList.ToArray()));
        }