public unsafe World DownSample(int extraLods, out int voxelCount) { World subWorld = new World(dimensions, lod + extraLods); World thisWorld = this; int step = 1 << subWorld.lod; int totalVoxels = 0; // parallelize downsampling on the X-axis System.Threading.Tasks.Parallel.For(0, dimensions.x / step, (int i) => { int yVoxels = subWorld.dimensions.y >> subWorld.lod; RLEElement[] elementBuffer = new RLEElement[yVoxels]; WorldBuilder.RLEColumnBuilder builder = new WorldBuilder.RLEColumnBuilder(); int x = i * step; for (int z = 0; z < subWorld.dimensions.z; z += step) { // downsample a {step, step} grid of columns into one RLEColumn downSampled = thisWorld.DownSampleColumn(x, z, elementBuffer, extraLods, ref builder, ref totalVoxels, ref subWorld.Storage); *subWorld.Storage.GetColumnPointer(subWorld.GetIndexKnownInBounds(int2(x, z))) = downSampled; } }); voxelCount = totalVoxels; return(subWorld); }
/// <summary> /// Output a column of data into the columnbuilder; after doing this with all columns the builder will be resolved to a new, merged column /// </summary> unsafe void DownSamplePartial(int x, int z, int extraLods, ref WorldBuilder.RLEColumnBuilder columnBuilder) { RLEColumn column = *Storage.GetColumnPointer(GetIndexKnownInBounds(int2(x, z))); if (column.RunCount <= 0) { return; } int2 elementBounds = dimensions.y >> lod; int nextLod = lod + extraLods; ColorARGB32 *colorPointer = column.ColorPointer(ref Storage); for (int run = 0; run < column.RunCount; run++) { RLEElement element = column.GetIndex(ref Storage, run); elementBounds = int2(elementBounds.x - element.Length, elementBounds.x); if (element.IsAir) { continue; } for (int i = 0; i < element.Length; i++) { int Y = elementBounds.x + i; int colorIdx = element.ColorsIndex + element.Length - i - 1; columnBuilder.SetVoxel(Y >> nextLod, colorPointer[colorIdx]); } } }
// downsample a grid of columns into one column public RLEColumn DownSampleColumn(int xStart, int zStart, RLEElement[] buffer, int extraLods, ref WorldBuilder.RLEColumnBuilder columnBuilder, ref int totalVoxels, ref WorldAllocator newStorage) { // lod 0 = 0, 1 // lod 1 = 0, 2 int stepSize = 1 << lod; int steps = 1 << extraLods; int nextVoxelCountY = (dimensions.y >> (lod + extraLods)) - 1; columnBuilder.Clear(); for (int ix = 0; ix < steps; ix++) { int x = xStart + ix * stepSize; for (int iz = 0; iz < steps; iz++) { int z = zStart + iz * stepSize; DownSamplePartial(x, z, extraLods, ref columnBuilder); } } return(columnBuilder.ToFinalColumn(1 << (lod + extraLods), (short)(nextVoxelCountY), buffer, ref totalVoxels, ref newStorage)); }