reckfellENGINE

VoxelBuffer

3D grid storing voxel data.

On this page
class VoxelBufferReckfellSharpInherits RefCounted

3D grid storing voxel data.

This contains dense voxels data storage (every single cell holds data, there is no sparse optimization of space). Works like a normal 3D grid containing a voxel value in each cell. Organized in channels of configurable bit depth. Values can be interpreted either as unsigned integers, fixed-point or floats. See VoxelBuffer.Depth for more information. Arbitrary metadata can also be stored, either for the whole buffer, or per-voxel, at higher cost. This metadata can get saved and loaded along voxels, however you must make sure the data is serializable (i.e it should not contain nodes or arbitrary objects).

Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.

Constructors1

VoxelBufferConstructor
# VoxelBuffer
C#
public VoxelBuffer();

Methods50

Clearvoid
# Clear
C#
public void Clear();

Erases all contents of the buffer and resets its size to zero. Channel depths and default values are preserved.

ClearVoxelMetadatavoid
# ClearVoxelMetadata
C#
public void ClearVoxelMetadata();

Erases all per-voxel metadata.

ClearVoxelMetadataInAreavoid
# ClearVoxelMetadataInArea
C#
public void ClearVoxelMetadataInArea(Vector3I minPos, Vector3I maxPos);

Erases per-voxel metadata within the specified area.

Parameters
NameTypeDefault
minPosVector3I—
maxPosVector3I—
CompressUniformChannelsvoid
# CompressUniformChannels
C#
public void CompressUniformChannels();

Finds channels that have the same value in all their voxels, and reduces memory usage by storing only one value instead. This is effective for example when large parts of the terrain are filled with air.

CopyChannelFromvoid
# CopyChannelFrom
C#
public void CopyChannelFrom(VoxelBuffer other, uint channel);

Copies all values from the channel of another VoxelBuffer into the same channel for the current buffer. The depth formats must match.

Parameters
NameTypeDefault
otherVoxelBuffer—
channeluint—
CopyChannelFromAreavoid
# CopyChannelFromArea
C#
public void CopyChannelFromArea(VoxelBuffer other, Vector3I srcMin, Vector3I srcMax, Vector3I dstMin, uint channel);

Copies values from a channel's sub-region of another VoxelBuffer into the same channel for the current buffer, at a specific location. The depth formats must match. If corners of the area represent a negative-size area, they will be sorted back. If coordinates are entirely or partially out of bounds, they will be clipped automatically. Copying across the same buffer to overlapping areas is not supported. You may use an intermediary buffer in this case.

Parameters
NameTypeDefault
otherVoxelBuffer—
srcMinVector3I—
srcMaxVector3I—
dstMinVector3I—
channeluint—
CopyVoxelMetadataInAreavoid
# CopyVoxelMetadataInArea
C#
public void CopyVoxelMetadataInArea(VoxelBuffer srcBuffer, Vector3I srcMinPos, Vector3I srcMaxPos, Vector3I dstMinPos);

Copies per-voxel metadata from a sub-region of another VoxelBuffer into the the current buffer, at a specific location. Values will be a shallow copy. If corners of the area represent a negative-size area, they will be sorted back. If coordinates are entirely or partially out of bounds, they will be clipped automatically. Copying across the same buffer to overlapping areas is not supported. You may use an intermediary buffer in this case.

Parameters
NameTypeDefault
srcBufferVoxelBuffer—
srcMinPosVector3I—
srcMaxPosVector3I—
dstMinPosVector3I—
Createvoid
# Create
C#
public void Create(int sx, int sy, int sz);

Clears the buffer and gives it the specified size.

Parameters
NameTypeDefault
sxint—
syint—
szint—
Create3DTextureFromSdfZxyImageTexture3D
# Create3DTextureFromSdfZxy
C#
public ImageTexture3D Create3DTextureFromSdfZxy(Image.Format outputFormat);

Creates a 3D texture from the SDF channel. If output_format is a 8-bit pixel format, the texture will contain normalized signed distances, where 0.5 is the isolevel, 0 is the furthest away under surface, and 1 is the furthest away above surface. Only 16-bit SDF channel is supported. Only Image.Format.R8 and Image.Format.L8 output formats are suported. Note: when sampling this texture in a shader, you need to swizzle 3D coordinates with .yxz. This is how voxels are internally stored, and this function does not change this convention.

Parameters
NameTypeDefault
outputFormatImage.Format—
Returns ImageTexture3D
DebugPrintSdfYSlicesReckfell.Collections.Array<Image>
# DebugPrintSdfYSlices
C#
public Reckfell.Collections.Array<Image> DebugPrintSdfYSlices(float scale = 1f);

Renders the contents of the SDF channel into images where blue gradients are negative values (below surface) and yellow gradients are positive (above surface). Each image corresponds to an XZ slice of the buffer. The scale parameter can be used to change contrast of images by scaling the SDF.

Parameters
NameTypeDefault
scalefloat1f
Returns Reckfell.Collections.Array<Image>
DecompressChannelvoid
# DecompressChannel
C#
public void DecompressChannel(int channel);

If the given channel is currently compressed, decompresses it so that all voxel values are individually stored in full. This will use more memory.

Parameters
NameTypeDefault
channelint—
DownscaleTovoid
# DownscaleTo
C#
public void DownscaleTo(VoxelBuffer dst, Vector3I srcMin, Vector3I srcMax, Vector3I dstMin);

Produces a downscaled version of this buffer, by a factor of 2, without any form of interpolation (i.e using nearest-neighbor). Metadata is not copied.

Parameters
NameTypeDefault
dstVoxelBuffer—
srcMinVector3I—
srcMaxVector3I—
dstMinVector3I—
Fillvoid
# Fill
C#
public void Fill(ulong value, int channel = 0);

Fills one channel of this buffer with a specific raw value.

Parameters
NameTypeDefault
valueulong—
channelint0
FillAreavoid
# FillArea
C#
public void FillArea(ulong value, Vector3I min, Vector3I max, uint channel = 0);

Fills an area of a channel in this buffer with a specific raw value.

Parameters
NameTypeDefault
valueulong—
minVector3I—
maxVector3I—
channeluint0
FillAreaFvoid
# FillAreaF
C#
public void FillAreaF(float value, Vector3I min, Vector3I max, uint channel);

Fills an area of a channel in this buffer with a specific SDF value.

Parameters
NameTypeDefault
valuefloat—
minVector3I—
maxVector3I—
channeluint—
FillFvoid
# FillF
C#
public void FillF(float value, int channel = 0);

Fills one channel of this buffer with a specific SDF value.

Parameters
NameTypeDefault
valuefloat—
channelint0
ForEachVoxelMetadatavoid
# ForEachVoxelMetadata
C#
public void ForEachVoxelMetadata(Callable callback);

Executes a function on every voxel in this buffer which have associated metadata. The function's arguments must be (position: Vector3i, metadata: Variant). IMPORTANT: inserting new or removing metadata from inside this function is not allowed.

Parameters
NameTypeDefault
callbackCallable—
ForEachVoxelMetadataInAreavoid
# ForEachVoxelMetadataInArea
C#
public void ForEachVoxelMetadataInArea(Callable callback, Vector3I minPos, Vector3I maxPos);

Executes a function on every voxel in this buffer which have associated metadata, within the specified area. IMPORTANT: inserting new or removing metadata from inside this function is not allowed.

Parameters
NameTypeDefault
callbackCallable—
minPosVector3I—
maxPosVector3I—
GetAllocatorVoxelBuffer.Allocator
# GetAllocator
C#
public VoxelBuffer.Allocator GetAllocator();

Gets which memory allocator is used by this buffer.

Returns VoxelBuffer.Allocator
GetBlockMetadataVariant
# GetBlockMetadata
C#
public Variant GetBlockMetadata();

Gets metadata associated to this VoxelBuffer.

Returns Variant
GetChannelAsByteArraybyte[]
# GetChannelAsByteArray
C#
public byte[] GetChannelAsByteArray(VoxelBuffer.ChannelId channelIndex);

Gets voxel data from a channel as uncompressed raw bytes. Check VoxelBuffer.Depth for information about the data format. Note: if the channel is compressed, it will be decompressed on the fly into the returned array. If you want a different behavior in this case, check VoxelBuffer.GetChannelCompression before calling this method.

Parameters
NameTypeDefault
channelIndexVoxelBuffer.ChannelId—
Returns byte[]
GetChannelCompressionVoxelBuffer.Compression
# GetChannelCompression
C#
public VoxelBuffer.Compression GetChannelCompression(int channel);

Gets which compression mode the specified channel has.

Parameters
NameTypeDefault
channelint—
Returns VoxelBuffer.Compression
GetChannelDepthVoxelBuffer.Depth
# GetChannelDepth
C#
public VoxelBuffer.Depth GetChannelDepth(uint channel);

Gets which bit depth the specified channel has.

Parameters
NameTypeDefault
channeluint—
Returns VoxelBuffer.Depth
GetSizeVector3I
# GetSize
C#
public Vector3I GetSize();

Gets the 3D size of the buffer in voxels.

Returns Vector3I
GetVoxelulong
# GetVoxel
C#
public ulong GetVoxel(int x, int y, int z, uint channel = 0);

Gets the raw value of a voxel within this buffer.

Parameters
NameTypeDefault
xint—
yint—
zint—
channeluint0
Returns ulong
GetVoxelFfloat
# GetVoxelF
C#
public float GetVoxelF(int x, int y, int z, uint channel = 0);

Gets the float value of a voxel within this buffer. You may use this function if you work with SDF volumes (smooth voxels).

Parameters
NameTypeDefault
xint—
yint—
zint—
channeluint0
Returns float
GetVoxelMetadataVariant
# GetVoxelMetadata
C#
public Variant GetVoxelMetadata(Vector3I pos);

Gets the metadata attached to a specific voxel in this buffer.

Parameters
NameTypeDefault
posVector3I—
Returns Variant
GetVoxelToolVoxelTool
# GetVoxelTool
C#
public VoxelTool GetVoxelTool();

Constructs a VoxelTool instance bound to this buffer. This provides access to some extra common functions.

Returns VoxelTool
GetVoxelVulong
# GetVoxelV
C#
public ulong GetVoxelV(Vector3I pos, uint channel = 0);

Gets the raw value of a voxel within this buffer.

Parameters
NameTypeDefault
posVector3I—
channeluint0
Returns ulong
IsUniformbool
# IsUniform
C#
public bool IsUniform(int channel);

Checks if every voxel within a channel has the same value.

Parameters
NameTypeDefault
channelint—
Returns bool
Mirrorvoid
# Mirror
C#
public void Mirror(Vector3I.Axis axis);

Mirrors voxel values along the specified axis.

Parameters
NameTypeDefault
axisVector3I.Axis—
OpAddBufferFvoid
# OpAddBufferF
C#
public void OpAddBufferF(VoxelBuffer other, VoxelBuffer.ChannelId channel);

Computes the sum of corresponding voxels between the current buffer and the other buffer, and stores the result in the current buffer. Voxels are interpreted as signed distances (usually the VoxelBuffer.ChannelId.ChannelSdf channel).

Parameters
NameTypeDefault
otherVoxelBuffer—
channelVoxelBuffer.ChannelId—
OpMaxBufferFvoid
# OpMaxBufferF
C#
public void OpMaxBufferF(VoxelBuffer other, VoxelBuffer.ChannelId channel);

Computes the maximum of corresponding voxels between the current buffer and the other buffer, and stores the result in the current buffer. Voxels are interpreted as signed distances (usually the VoxelBuffer.ChannelId.ChannelSdf channel).

Parameters
NameTypeDefault
otherVoxelBuffer—
channelVoxelBuffer.ChannelId—
OpMinBufferFvoid
# OpMinBufferF
C#
public void OpMinBufferF(VoxelBuffer other, VoxelBuffer.ChannelId channel);

Computes the minimum of corresponding voxels between the current buffer and the other buffer, and stores the result in the current buffer. Voxels are interpreted as signed distances (usually the VoxelBuffer.ChannelId.ChannelSdf channel).

Parameters
NameTypeDefault
otherVoxelBuffer—
channelVoxelBuffer.ChannelId—
OpMulBufferFvoid
# OpMulBufferF
C#
public void OpMulBufferF(VoxelBuffer other, VoxelBuffer.ChannelId channel);

Computes the multiplication of corresponding voxels between the current buffer and the other buffer, and stores the result in the current buffer. Voxels are interpreted as signed distances (usually the VoxelBuffer.ChannelId.ChannelSdf channel).

Parameters
NameTypeDefault
otherVoxelBuffer—
channelVoxelBuffer.ChannelId—
OpMulValueFvoid
# OpMulValueF
C#
public void OpMulValueF(float other, VoxelBuffer.ChannelId channel);

Multiplies every voxels in the buffer by the given value. Voxels are interpreted as signed distances (usually the VoxelBuffer.ChannelId.ChannelSdf channel).

Parameters
NameTypeDefault
otherfloat—
channelVoxelBuffer.ChannelId—
OpSelectLessSrcFDstIValuesvoid
# OpSelectLessSrcFDstIValues
C#
public void OpSelectLessSrcFDstIValues(VoxelBuffer src, VoxelBuffer.ChannelId srcChannel, float threshold, int valueIfLess, int valueIfMore, VoxelBuffer.ChannelId dstChannel);

For every voxel in the source buffer, if the value is lower than a threshold, set the corresponding voxel in the current buffer to a specific integer value, or another value otherwise. Voxels in the source buffer are interpreted as signed distances (usually the VoxelBuffer.ChannelId.ChannelSdf channel).

Parameters
NameTypeDefault
srcVoxelBuffer—
srcChannelVoxelBuffer.ChannelId—
thresholdfloat—
valueIfLessint—
valueIfMoreint—
dstChannelVoxelBuffer.ChannelId—
OpSubBufferFvoid
# OpSubBufferF
C#
public void OpSubBufferF(VoxelBuffer other, VoxelBuffer.ChannelId channel);

Computes the subtraction of corresponding voxels between the current buffer and the other buffer (current - other), and stores the result in the current buffer. Voxels are interpreted as signed distances (usually the VoxelBuffer.ChannelId.ChannelSdf channel).

Parameters
NameTypeDefault
otherVoxelBuffer—
channelVoxelBuffer.ChannelId—
RemapValues2 overloads
# RemapValues · 1/2
C#
public void RemapValues(uint channel, int[] map);

Remaps integer values in a channel using the passed map lookup table. Each index in map corresponds to an original value, and will be replaced by map[original].

Parameters
NameTypeDefault
channeluint—
mapint[]—
# RemapValues · 2/2
C#
public void RemapValues(uint channel, ReadOnlySpan<int> map);

Remaps integer values in a channel using the passed map lookup table. Each index in map corresponds to an original value, and will be replaced by map[original].

Parameters
NameTypeDefault
channeluint—
mapReadOnlySpan<int>—
Rotate90void
# Rotate90
C#
public void Rotate90(Vector3I.Axis axis, int turns);

Rotates voxel values by 90 degrees along the specified axis. 1 turn is 90 degrees, 2 turns is 180 degrees, 3 turns is 270 degrees (or -90), 4 turns does nothing. Negative turns go in the other direction. This may also rotate the size of the buffer, if it isn't cubic.

Parameters
NameTypeDefault
axisVector3I.Axis—
turnsint—
SetBlockMetadatavoid
# SetBlockMetadata
C#
public void SetBlockMetadata(Variant meta);

Sets arbitrary data on this buffer. Old data is replaced. Note, this is separate storage from per-voxel metadata. If this VoxelBuffer is saved, this metadata will also be saved along voxels, so make sure the data supports serialization (i.e you can't put nodes or arbitrary objects in it).

Parameters
NameTypeDefault
metaVariant—
SetChannelDepthvoid
# SetChannelDepth
C#
public void SetChannelDepth(uint channel, VoxelBuffer.Depth depth);

Changes the bit depth of a given channel. This controls the range of values a channel can hold. See VoxelBuffer.Depth for more information.

Parameters
NameTypeDefault
channeluint—
depthVoxelBuffer.Depth—
SetChannelFromByteArray2 overloads
# SetChannelFromByteArray · 1/2
C#
public void SetChannelFromByteArray(VoxelBuffer.ChannelId channelIndex, byte[] data);

Overwrites the contents of a channel from raw voxel data. Check VoxelBuffer.Depth for information about the expected data format.

Parameters
NameTypeDefault
channelIndexVoxelBuffer.ChannelId—
databyte[]—
# SetChannelFromByteArray · 2/2
C#
public void SetChannelFromByteArray(VoxelBuffer.ChannelId channelIndex, ReadOnlySpan<byte> data);

Overwrites the contents of a channel from raw voxel data. Check VoxelBuffer.Depth for information about the expected data format.

Parameters
NameTypeDefault
channelIndexVoxelBuffer.ChannelId—
dataReadOnlySpan<byte>—
SetVoxelvoid
# SetVoxel
C#
public void SetVoxel(ulong value, int x, int y, int z, uint channel = 0);

Sets the raw value of a voxel. If you use smooth voxels, you may prefer using VoxelBuffer.SetVoxelF.

Parameters
NameTypeDefault
valueulong—
xint—
yint—
zint—
channeluint0
SetVoxelFvoid
# SetVoxelF
C#
public void SetVoxelF(float value, int x, int y, int z, uint channel = 0);

Sets the float value of a voxel. This method should be used if you work on SDF data (smooth voxels).

Parameters
NameTypeDefault
valuefloat—
xint—
yint—
zint—
channeluint0
SetVoxelMetadatavoid
# SetVoxelMetadata
C#
public void SetVoxelMetadata(Vector3I pos, Variant value);

Attaches arbitrary data on a specific voxel. Old data is replaced. Passing null will erase metadata. If this VoxelBuffer is saved, this metadata will also be saved along voxels, so make sure the data supports serialization (i.e you can't put nodes or arbitrary objects in it).

Parameters
NameTypeDefault
posVector3I—
valueVariant—
SetVoxelVvoid
# SetVoxelV
C#
public void SetVoxelV(ulong value, Vector3I pos, uint channel = 0);
Parameters
NameTypeDefault
valueulong—
posVector3I—
channeluint0
Update3DTextureFromSdfZxyvoid
# Update3DTextureFromSdfZxy
C#
public void Update3DTextureFromSdfZxy(ImageTexture3D existingTexture);

Updates an existing 3D texture from the SDF channel. See VoxelBuffer.Create3DTextureFromSdfZxy for more information.

Parameters
NameTypeDefault
existingTextureImageTexture3D—

Fields and constants1

MaxSizeField
# MaxSize
C#
public const long MaxSize = 65535;

Maximum size a buffer can have when serialized. Buffers that contain uniform-compressed voxels can reach it, but in practice, the limit is much lower and depends on available memory.

VoxelBuffer.Allocator

ValueDescription
Count = 2
Default = 0

Uses Reckfell's default memory allocator (at time of writing, it is malloc). Preferred for occasional buffers with uncommon size, or very large size.

Pool = 1

Uses a pool allocator. Can be faster than the default allocator buffers are created very frequently with similar size. This memory will remain allocated after use, under the assumption that other buffers will need it soon after. Does not support very large buffers (greater than 2 megabytes)

VoxelBuffer.ChannelId

ValueDescription
ChannelColor = 2

Channel used to store color data. Used by VoxelMesherCubes.

ChannelData5 = 5

Free channel. Not used by the engine yet.

ChannelData6 = 6

Free channel. Not used by the engine yet.

ChannelData7 = 7

Free channel. Not used by the engine yet.

ChannelIndices = 3

Channel used to store material indices. Used with smooth voxels.

ChannelSdf = 1

Channel used to store SDF data (signed distance field). Used by VoxelMesherTransvoxel and other smooth meshers. Values should preferably be accessed as floats. Negative values are below the isosurface (inside matter), and positive values are above the surface (outside matter).

ChannelType = 0

Channel used to store voxel types. Used by VoxelMesherBlocky.

ChannelWeights = 4

Channel used to store material weights, when more than one index can be stored per voxel. Used with smooth voxels.

MaxChannels = 8

Maximum number of channels a VoxelBuffer can have.

VoxelBuffer.ChannelMask

ValueDescription
AllChannelsMask = 255

Bitmask with all channel bits set.

ChannelColorBit = 4

Bitmask with one bit set at the position corresponding to VoxelBuffer.ChannelId.ChannelColor.

ChannelData5Bit = 32

Bitmask with one bit set at the position corresponding to VoxelBuffer.ChannelId.ChannelData5.

ChannelData6Bit = 64

Bitmask with one bit set at the position corresponding to VoxelBuffer.ChannelId.ChannelData6.

ChannelData7Bit = 128

Bitmask with one bit set at the position corresponding to VoxelBuffer.ChannelId.ChannelData7.

ChannelIndicesBit = 8

Bitmask with one bit set at the position corresponding to VoxelBuffer.ChannelId.ChannelIndices.

ChannelSdfBit = 2

Bitmask with one bit set at the position corresponding to VoxelBuffer.ChannelId.ChannelSdf.

ChannelTypeBit = 1

Bitmask with one bit set at the position corresponding to VoxelBuffer.ChannelId.ChannelType.

ChannelWeightsBit = 16

Bitmask with one bit set at the position corresponding to VoxelBuffer.ChannelId.ChannelWeights.

VoxelBuffer.Compression

ValueDescription
Count = 2

How many compression modes there are.

None = 0

The channel is not compressed. Every value is stored individually inside an array in memory.

Uniform = 1

All voxels of the channel have the same value, so they are stored as one single value, to save space.

VoxelBuffer.Depth

ValueDescription
Count = 4

How many depth configuration there are.

Depth16Bit = 1

Voxels will be stored with 16 bits. Raw values will range from 0 to 65,535. Float values can take 65,535 values distributed from -500.0 to 500.0. Values outside the range will be clamped.

Depth32Bit = 2

Voxels will be stored with 32 bits. Raw values will range from 0 to 4,294,967,295, and float values will use regular IEEE 754 representation (float).

Depth64Bit = 3

Voxels will be stored with 64 bits. Raw values will range from 0 to 18,446,744,073,709,551,615, and float values will use regular IEEE 754 representation (double).

Depth8Bit = 0

Voxels will be stored with 8 bits. Raw values will range from 0 to 255. Float values can take 255 values distributed from -10.0 to 10.0. Values outside the range will be clamped.

Reckfell Engine documentationBack to top ↑
QUICK LINKSGetting startedEntity hierarchyC# & .NET 10Scenes and prefabs
Reckfell guides and full API K
reckfellENGINE
RECKFELL ACCOUNT

Sign in

Sign in to your Reckfell account.

One account for the entire Reckfell platform.