//Сделать блоки и сгенерить меши
        public IEnumerable <Progress> MakeBlocks()
        {
            blocks = new List <BlockInfo>();

            var qmeshstructures = new List <MyQuadMesh>();

            //Создадим сразу все блоки
            int blocksSizeX     = (int)Math.Ceiling(loc.sizeX / (float)sizeRazer);
            int blocksSizeZ     = (int)Math.Ceiling(loc.sizeZ / (float)sizeRazer);
            var landscapeBlocks = new BlockInfo[blocksSizeX, blocksSizeZ];

            for (int i = 0; i < blocksSizeX; i++)
            {
                for (int j = 0; j < blocksSizeZ; j++)
                {
                    landscapeBlocks[i, j] = new BlockInfo()
                    {
                        name   = "block_" + i + "_" + j,
                        offset = new MyVector(i * sizeRazer, 0, j * sizeRazer),
                    }
                }
            }
            ;

            //Генерация основного ландшафта
            float prog            = 0;
            float maxprog         = (sizeX / sizeRazer + 1) * (sizeZ / sizeRazer + 1);
            var   landscapeMeshes = new MyQuadMesh[blocksSizeX, blocksSizeZ];
            var   offsetmesh      = new MyVector(0, 0, 0);

            foreach (var qmesh in landscapeBlock.MakeGroundMesh(this, offsetmesh, landscapeBlock.H - 1).Razer(sizeRazer))
            {
                prog++;
                yield return(new Progress(prog / maxprog));

                landscapeMeshes[qmesh.RazerI, qmesh.RazerJ] = qmesh;
            }

            //Прибавление дополнительных блоков и финальная генерация ландшафта
            prog    = 0;
            maxprog = blocksSizeX * blocksSizeZ;
            for (int i = 0; i < blocksSizeX; i++)
            {
                for (int j = 0; j < blocksSizeZ; j++)
                {
                    prog++;
                    yield return(new Progress(prog / maxprog));

                    var landscapeMeshe = landscapeMeshes[i, j] ?? new MyQuadMesh {
                        RazerI = i, RazerJ = j
                    };
                    landscapeMeshe.AddStructure(qmeshstructures, i, j, sizeRazer);
                    landscapeBlocks[i, j].mesh = landscapeMeshe.Build(this, sizeQuad, sizeQuadVertical);
                }
            }

            for (int i = 0; i < blocksSizeX; i++)
            {
                for (int j = 0; j < blocksSizeZ; j++)
                {
                    var block = landscapeBlocks[i, j];
                    blocks.Add(block);
                }
            }

            blocks.RemoveAll(b => b.mesh == null);
        }
Beispiel #2
0
        //Сгенерировать меш
        public MyQuadMesh MakeGroundMesh(LocationCreator lc, MyVector offset, int h)
        {
            var quadMesh = new MyQuadMesh();

            //Проходим по всем клеткам блока
            for (int x = 0; x < SizeX; x++)
            {
                for (int z = 0; z < SizeZ; z++)
                {
                    int totalX   = OffsetX + x;
                    int totalZ   = OffsetZ + z;
                    int currentH = h;

                    //Проверяем цвет
                    var color = Colors[x, z];
                    if (color == null)
                    {
                        continue;
                    }

                    var y = Land[x, z];

                    //Строим верхнюю грань
                    {
                        if (lc.Land[x, z] - 1 != y)
                        {
                            var colorUp = color;

                            int v      = y;
                            var color1 = lc.GetColor(colorUp, x, z, v + 1);
                            var color2 = lc.GetColor(colorUp, x + 1, z, v + 1);
                            var color3 = lc.GetColor(colorUp, x, z + 1, v + 1);
                            var color4 = lc.GetColor(colorUp, x + 1, z + 1, v + 1);
                            quadMesh.CreateY1(x + offset.X, v + offset.Y, z + offset.Z, new[] { color1, color2, color3, color4 });
                        }
                    }

                    //Строим боковые грани X0
                    if (totalX > 0)
                    {
                        var v0 = lc.Land[totalX - 1, totalZ];

                        for (int v = Math.Max(v0 + 1, y - currentH); v <= y; v++)
                        {
                            var color1 = lc.GetColor(color, x, z, v);
                            var color2 = lc.GetColor(color, x, z, v + 1);
                            var color3 = lc.GetColor(color, x, z + 1, v);
                            var color4 = lc.GetColor(color, x, z + 1, v + 1);
                            quadMesh.CreateX0(x + offset.X, v + offset.Y, z + offset.Z, new[] { color1, color2, color3, color4 });
                        }
                    }

                    //Строим боковые грани X1
                    if (lc.GenerateVoxelSideX1 && totalX < SizeX - 1)
                    {
                        var v0 = lc.Land[totalX + 1, totalZ];

                        for (int v = Math.Max(v0 + 1, y - currentH); v <= y; v++)
                        {
                            var color1 = lc.GetColor(color, x + 1, z, v);
                            var color2 = lc.GetColor(color, x + 1, z + 1, v);
                            var color3 = lc.GetColor(color, x + 1, z, v + 1);
                            var color4 = lc.GetColor(color, x + 1, z + 1, v + 1);
                            quadMesh.CreateX1(x + offset.X, v + offset.Y, z + offset.Z, new[] { color1, color2, color3, color4 });
                        }
                    }

                    //Строим боковые грани Z0
                    if (lc.GenerateVoxelSideZ0 && totalZ > 0)
                    {
                        var v0 = lc.Land[totalX, totalZ - 1];

                        for (int v = Math.Max(v0 + 1, y - currentH); v <= y; v++)
                        {
                            var color1 = lc.GetColor(color, x, z, v);
                            var color2 = lc.GetColor(color, x + 1, z, v);
                            var color3 = lc.GetColor(color, x, z, v + 1);
                            var color4 = lc.GetColor(color, x + 1, z, v + 1);
                            quadMesh.CreateZ0(x + offset.X, v + offset.Y, z + offset.Z, new[] { color1, color2, color3, color4 });
                        }
                    }

                    //Строим боковые грани Z1
                    if (totalZ < SizeZ - 1)
                    {
                        var v0 = lc.Land[totalX, totalZ + 1];

                        for (int v = Math.Max(v0 + 1, y - currentH); v <= y; v++)
                        {
                            var color1 = lc.GetColor(color, x, z + 1, v);
                            var color2 = lc.GetColor(color, x, z + 1, v + 1);
                            var color3 = lc.GetColor(color, x + 1, z + 1, v);
                            var color4 = lc.GetColor(color, x + 1, z + 1, v + 1);
                            quadMesh.CreateZ1(x + offset.X, v + offset.Y, z + offset.Z, new[] { color1, color2, color3, color4 });
                        }
                    }
                }
            }

            return(quadMesh);
        }