Example #1
0
        private void CreateChunk(int i, int x, int y)
        {
            VoxelGrid chunk    = Instantiate(voxelGridPrefab) as VoxelGrid;
            var       chunkPos = new Vector2Int(x, y);

            chunk.Initialize(this, voxelResolution, chunkResolution, chunkPos, chunkSize * transform.lossyScale.x, maxFeatureAngle);
            chunk.transform.parent        = transform;
            chunk.transform.localPosition =
                new Vector3(x * chunkSize - halfSize, y * chunkSize - halfSize);
            chunks[i] = chunk;
            if (x > 0)
            {
                chunks[i - 1].xNeighbor = chunk;
            }
            if (y > 0)
            {
                chunks[i - chunkResolution].yNeighbor = chunk;
                if (x > 0)
                {
                    chunks[i - chunkResolution - 1].xyNeighbor = chunk;
                }
            }
        }
Example #2
0
        private void ApplyChunkStencil(VoxelGrid grid, VoxelStencil stencil)
        {
            int xStart = stencil.XStart;

            if (xStart < 0)
            {
                xStart = 0;
            }
            int xEnd = stencil.XEnd;

            if (xEnd >= grid.resolution)
            {
                xEnd = grid.resolution - 1;
            }
            int yStart = stencil.YStart;

            if (yStart < 0)
            {
                yStart = 0;
            }
            int yEnd = stencil.YEnd;

            if (yEnd >= grid.resolution)
            {
                yEnd = grid.resolution - 1;
            }

            for (int y = yStart; y <= yEnd; y++)
            {
                int i = y * grid.resolution + xStart;
                for (int x = xStart; x <= xEnd; x++, i++)
                {
                    grid.voxels[i].state = stencil.Apply(x, y, grid.voxels[i].state);
                }
            }
            grid.UpdateGrid();
        }