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(); }