reckfellENGINE

VoxelMeshSdf

Signed distance field of a mesh as voxels.

On this page
class VoxelMeshSdfReckfellSharpInherits Resource

Signed distance field of a mesh as voxels.

This resource can be used to bake a mesh into a signed distance field (SDF). The data is stored as an internal voxel buffer. This can then be used as a shape or brush in voxel-based operations.

Note: if you can, prefer using procedural shapes (like sphere or box) for better quality and performance. While versatile, VoxelMeshSdf it is more expensive to use and currently has lower quality compared to procedural equivalents.

Note 2: not all meshes can be baked. Best meshes should be manifold and represent a closed shape, with clear inside and outside.

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

Constructors1

VoxelMeshSdfConstructor
# VoxelMeshSdf
C#
public VoxelMeshSdf();

Properties7

BakeModeProperty
# BakeMode
C#
public VoxelMeshSdf.BakeModeEnum BakeMode { get; set; }

Selects the algorithm that will be used to compute SDF from the mesh.

BoundarySignFixEnabledProperty
# BoundarySignFixEnabled
C#
public bool BoundarySignFixEnabled { get; set; }

Some algorithms might fail to properly compute the sign of distances on their own. Usually outside the shape has positive sign, and inside the shape has negative sign. Errors can cause the wrong sign to "leak" and propagate outside the shape. This option attempts to fix this when it happens by assuming that the border of the baked region is always outside, and propagates positive signs inwards to ensure no negative signs leak.

CellCountProperty
# CellCount
C#
public int CellCount { get; set; }

Controls the resolution of the baked SDF, proportionally to one side of the 3D voxel buffer. Higher increases quality, but is slower to bake and uses more memory.

MarginRatioProperty
# MarginRatio
C#
public float MarginRatio { get; set; }

Controls the addition of extra space around the mesh's baking area. Proper SDF buffers should have some margin in order to capture outside gradients. Without margin, the outside could appear cutoff or blocky when remeshed. This property adds margin based on a ratio of the size of the mesh. For example, if the mesh is 100 units wide, a margin of 0.1 will add 10 extra units of space around it.

MeshProperty
# Mesh
C#
public Mesh Mesh { get; set; }

Mesh that will be baked when calling the VoxelMeshSdf.Bake function, or when pressing the Bake button in the editor. Setting this property back to null will not erase the baked result, so you don't need the original mesh to be loaded in order to use the SDF.

PartitionSubdivProperty
# PartitionSubdiv
C#
public int PartitionSubdiv { get; set; }

Controls how many subdivisions to use across the baking area when using the VoxelMeshSdf.BakeModeEnum.AccuratePartitioned and VoxelMeshSdf.BakeModeEnum.ApproxFloodfill modes. When using VoxelMeshSdf.BakeModeEnum.AccuratePartitioned, that value may be proportionally adjusted based on the amount of triangles the mesh has, due to an imperfection of the algorithm. If the mesh has few triangles, lower values will perform better. If it has a lot of small triangles, higher values will perform better. However, if triangles are large or few and the value is big, this will also potentially create artifacts.

_DataProperty
# _Data
C#
public Reckfell.Collections.Dictionary _Data { get; set; }

Methods19

Bakevoid
# Bake
C#
public void Bake();

Bakes the SDF on the calling thread, using the currently assigned VoxelMeshSdf.Mesh. Might cause the game to stall if done on the main thread.

BakeAsyncvoid
# BakeAsync
C#
public void BakeAsync(EntityTree sceneTree);

Bakes the SDF on a separate thread, using the currently assigned VoxelMeshSdf.Mesh. See also VoxelMeshSdf.IsBaking, VoxelMeshSdf.IsBaked and VoxelMeshSdf.Baked.

Parameters
NameTypeDefault
sceneTreeEntityTree—
DebugCheckSdfReckfell.Collections.Array
# DebugCheckSdf
C#
public Reckfell.Collections.Array DebugCheckSdf(Mesh mesh);

Experimental. Runs some checks to verify if the baked SDF contains errors. The mesh to pass should be the mesh that was used for baking. If there are errors, the returned array will contain information about up to 2 cells of the buffer containing wrong values, in the following format:

Text
[
    cell0_position: Vector3,
    cell0_triangle_v0: Vector3,
    cell0_triangle_v1: Vector3,
    cell0_triangle_v2: Vector3,
    cell1_position: Vector3,
    cell1_triangle_v0: Vector3,
    cell1_triangle_v1: Vector3,
    cell1_triangle_v2: Vector3,
]

If there are no errors, the returned array will be empty. This method is a leftover from when this resource was initially implemented, as an attempt to automate checks when debugging. Errors might not always indicate an unusable SDF, depending on the use case. It might be removed or changed in the future.

Parameters
NameTypeDefault
meshMesh—
Returns Reckfell.Collections.Array
GetAabbAabb
# GetAabb
C#
public Aabb GetAabb();

Get the reference bounding box of the baked shape. This may be a bit larger than the original mesh's AABB because of the VoxelMeshSdf.MarginRatio property.

Returns Aabb
GetBakeModeVoxelMeshSdf.BakeModeEnum
# GetBakeMode
C#
public VoxelMeshSdf.BakeModeEnum GetBakeMode();
Returns VoxelMeshSdf.BakeModeEnum
GetCellCountint
# GetCellCount
C#
public int GetCellCount();
Returns int
GetMarginRatiofloat
# GetMarginRatio
C#
public float GetMarginRatio();
Returns float
GetMeshMesh
# GetMesh
C#
public Mesh GetMesh();
Returns Mesh
GetPartitionSubdivint
# GetPartitionSubdiv
C#
public int GetPartitionSubdiv();
Returns int
GetVoxelBufferVoxelBuffer
# GetVoxelBuffer
C#
public VoxelBuffer GetVoxelBuffer();

Get the VoxelBuffer containing the baked distance field. Results will be stored in the SDF channel. This buffer should not be modified.

Returns VoxelBuffer
IsBakedbool
# IsBaked
C#
public bool IsBaked();

Gets whether the resource contains baked SDF data.

Returns bool
IsBakingbool
# IsBaking
C#
public bool IsBaking();

Gets whether a asynchronous baking operation is pending.

Returns bool
IsBoundarySignFixEnabledbool
# IsBoundarySignFixEnabled
C#
public bool IsBoundarySignFixEnabled();
Returns bool
SetBakeModevoid
# SetBakeMode
C#
public void SetBakeMode(VoxelMeshSdf.BakeModeEnum mode);
Parameters
NameTypeDefault
modeVoxelMeshSdf.BakeModeEnum—
SetBoundarySignFixEnabledvoid
# SetBoundarySignFixEnabled
C#
public void SetBoundarySignFixEnabled(bool enable);
Parameters
NameTypeDefault
enablebool—
SetCellCountvoid
# SetCellCount
C#
public void SetCellCount(int cellCount);
Parameters
NameTypeDefault
cellCountint—
SetMarginRatiovoid
# SetMarginRatio
C#
public void SetMarginRatio(float ratio);
Parameters
NameTypeDefault
ratiofloat—
SetMeshvoid
# SetMesh
C#
public void SetMesh(Mesh mesh);
Parameters
NameTypeDefault
meshMesh—
SetPartitionSubdivvoid
# SetPartitionSubdiv
C#
public void SetPartitionSubdiv(int subdiv);
Parameters
NameTypeDefault
subdivint—

Signals and events1

BakedEvent
# Baked
C#
public event Action Baked;

Emitted when asynchronous baking is complete.

VoxelMeshSdf.BakeModeEnum

ValueDescription
AccurateNaive = 0

Checks every triangle from every cell to find the closest distances. It tries to be accurate, but is much slower than other techniques. The mesh must be closed, otherwise the SDF will contain errors. Signs are calculated by getting the closest triangle and checking which side of the triangle the center of the cell is.

AccuratePartitioned = 1

Similar to the naive method, but subdivides space into partitions in order to more easily skip triangles that don't need to be checked. Faster than the naive method, but still relatively slow.

ApproxFloodfill = 3

Approximates the SDF by calculating a thin "hull" of accurate values near triangles, then propagates those values with a 26-way floodfill. Signs are calculated only on the initial hull by doing several raycasts from the center of each cell: if the ray hits a backface, the cell is assumed to be inside. Otherwise, it is assumed to be outside. Signs are propagated as part of the floodfill. While technically not accurate, it is currently the fastest method and results are often good enough.

ApproxInterp = 2

Experimental method subdividing space in 4x4x4 cells, and skipping SDF calculations by interpolating 8 corners instead if no triangles are present in those cells. Faster than the naive method, but not particularly interesting. Might be removed.

Count = 4

How many baking modes there are.

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.