Example #1
0
        public void Process(CoordDir pos, Matrix3 <byte> matrix, Voxeland.EditMode mode, Form formOverride, byte type)         //overriding brush form to place objects and grass
        {
            //blurring matrix in case of blur mode
            Matrix3 <float> blurredExist = null;

            if (mode == Voxeland.EditMode.smooth)
            {
                blurredExist = new Matrix3 <float>(matrix.cube);
                for (int i = 0; i < blurredExist.array.Length; i++)
                {
                    if (matrix.array[i] != Data.emptyByte)
                    {
                        blurredExist.array[i] = 1;
                    }
                    else
                    {
                        blurredExist.array[i] = 0;
                    }
                }
                BlurExistMatrix(blurredExist);
            }

            //single brush
            if (formOverride == Form.single)
            {
                switch (mode)
                {
                case Voxeland.EditMode.add:
                case Voxeland.EditMode.replace: matrix[pos.x, pos.y, pos.z] = type; break;                                                     //already switched add to opposite, so add==replace

                case Voxeland.EditMode.dig: matrix[pos.x, pos.y, pos.z] = Data.emptyByte; break;
                }
            }

            //blob brush
            if (formOverride == Form.blob)
            {
                CoordDir[] neigCoords = ChunkMesh.NeighbourCoordinates(pos, matrix, extent, round: round);

                for (int i = 0; i < neigCoords.Length; i++)
                {
                    int      x = neigCoords[i].x; int y = neigCoords[i].y; int z = neigCoords[i].z;
                    CoordDir neigOpposite = neigCoords[i].opposite;
                    int      ox = neigOpposite.x; int oy = neigOpposite.y; int oz = neigOpposite.z;

                    switch (mode)
                    {
                    case Voxeland.EditMode.add: matrix[ox, oy, oz] = type; break;

                    case Voxeland.EditMode.replace: matrix[x, y, z] = type; break;

                    case Voxeland.EditMode.dig: matrix[x, y, z] = Data.emptyByte; break;

                    case Voxeland.EditMode.smooth:
                        if (blurredExist[ox, oy, oz] > 0.5f && matrix[ox, oy, oz] == Data.emptyByte)
                        {
                            matrix[ox, oy, oz] = ClosestExistingType(matrix, ox, oy, oz);                                                                                               //TODO: exist check
                        }
                        if (blurredExist[x, y, z] < 0.5f && matrix[x, y, z] != Data.emptyByte)
                        {
                            matrix[x, y, z] = Data.emptyByte;
                        }
                        break;
                    }
                }
            }

            //volume brush
            else if (formOverride == Form.volume)
            {
                CoordDir min = matrix.cube.Min; CoordDir max = matrix.cube.Max;

                for (int x = min.x; x < max.x; x++)
                {
                    for (int y = min.y; y < max.y; y++)
                    {
                        for (int z = min.z; z < max.z; z++)
                        {
                            //ignoring out-of-sphere
                            if (round)
                            {
                                int dx = x - pos.x; int dy = y - pos.y; int dz = z - pos.z;
                                if (dx * dx + dy * dy + dz * dz > (extent + 0.5f) * (extent + 0.5f))
                                {
                                    continue;
                                }
                            }

                            //setting block
                            int i = matrix.cube.GetPos(x, y, z);
                            switch (mode)
                            {
                            case Voxeland.EditMode.add: matrix.array[i] = type; break;

                            case Voxeland.EditMode.dig: matrix.array[i] = Data.emptyByte; break;

                            case Voxeland.EditMode.replace: if (matrix.array[i] != Data.emptyByte)
                                {
                                    matrix.array[i] = type;
                                }
                                break;                                                                                                                //TODO: exists check

                            case Voxeland.EditMode.smooth:
                                if (blurredExist.array[i] > 0.5f && (matrix.array[i] == Data.emptyByte || matrix.array[i] >= Data.constructorByte))
                                {
                                    matrix.array[i] = ClosestExistingType(matrix, x, y, z);                                                                                                                                 //if blured exists but matrix empty adding closest //TODO: exist check
                                }
                                if (blurredExist.array[i] < 0.5f && (matrix.array[i] != Data.emptyByte && matrix.array[i] < Data.constructorByte))
                                {
                                    matrix.array[i] = Data.emptyByte;                                                                                                                                   //if blured empty but matrix exists
                                }
                                break;
                            }
                        }
                    }
                }
            }

            else if (formOverride == Form.stamp)
            {
                if (getStamp)
                {
                    stamp = new Matrix3 <bool>(getStampMin, matrix.cube.size);

                    for (int x = 0; x < matrix.cube.size.x; x++)
                    {
                        for (int y = 0; y < matrix.cube.size.y; y++)
                        {
                            for (int z = 0; z < matrix.cube.size.z; z++)
                            {
                                if (matrix[x + matrix.cube.offset.x, y + matrix.cube.offset.y, z + matrix.cube.offset.z] != Data.emptyByte)
                                {
                                    stamp[x + stamp.cube.offset.x, y + stamp.cube.offset.y, z + stamp.cube.offset.z] = true;                                                                                                             //TODO: exists check
                                }
                            }
                        }
                    }
                }
                else if (stamp != null)
                {
                    for (int x = 0; x < stamp.cube.size.x; x++)
                    {
                        for (int y = 0; y < stamp.cube.size.y; y++)
                        {
                            for (int z = 0; z < stamp.cube.size.z; z++)
                            {
                                CoordDir s = new CoordDir(x, y, z) + stamp.cube.offset;
                                CoordDir m = new CoordDir(x, y, z) + matrix.cube.offset;

                                switch (mode)
                                {
                                case Voxeland.EditMode.add: if (stamp[s])
                                    {
                                        matrix[m] = type;
                                    }
                                    break;

                                case Voxeland.EditMode.dig: if (stamp[s])
                                    {
                                        matrix[m] = Data.emptyByte;
                                    }
                                    break;

                                case Voxeland.EditMode.replace:  if (matrix[m] != 0 && stamp[s])
                                    {
                                        matrix[m] = type;
                                    }
                                    break;                                                                                                      //TODO: exists check

                                case Voxeland.EditMode.smooth:
                                    if (stamp[s])
                                    {
                                        if (blurredExist[m] > 0.5f && (matrix[m] == Data.emptyByte || matrix[m] >= Data.constructorByte))
                                        {
                                            matrix[m] = ClosestExistingType(matrix, x, y, z);                                                                                                                       //if blured exists but matrix empty adding closest //TODO: exist check
                                        }
                                        if (blurredExist[m] < 0.5f && (matrix[m] != Data.emptyByte && matrix[m] < Data.constructorByte))
                                        {
                                            matrix[m] = Data.emptyByte;                                                                                                                         //if blured empty but matrix exists
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }