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]); } } }
public void SetVoxelColumn(int2 position, RLEColumn column) { int index = GetIndexKnownInBounds(position); RLEColumn *pointer = Storage.GetColumnPointer(index); if (pointer->RunCount > 0) { throw new InvalidOperationException(); } *pointer = column; }
#pragma warning disable CS0162 // Unreachable code detected public int GetVoxelColumn(int2 position, ref RLEColumn column) { if (REPEAT_WORLD) { position &= dimensionMaskXZ; } else { int2 inBoundsPosition = position & dimensionMaskXZ; if (any(inBoundsPosition != position)) { return(-1); } } column = *Storage.GetColumnPointer(GetIndexKnownInBounds(position)); return(column.RunCount); }