VoxelTool
Helper class to easily access and modify voxels
Helper class to easily access and modify voxels
Abstract interface to access and edit voxels. It allows accessing individual voxels, or doing bulk operations such as carving large chunks or copy/paste boxes. It's not a class to instantiate alone, you may get it from the voxel objects you want to work with, because it has multiple derived implementations. By default, if an operation overlaps a non-editable area (if not loaded yet for example), the operation will be cancelled. This behavior may vary in derived classes.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Properties9
ChannelProperty
public VoxelBuffer.ChannelId Channel { get; set; }Set which channel will be edited. When used on a terrain node, it will default to the first available channel, based on the stream and generator.
EraserValueProperty
public ulong EraserValue { get; set; }Sets which value will be used to erase voxels when editing the VoxelBuffer.ChannelId.ChannelType channel in VoxelTool.ModeEnum.Remove mode. Only relevant for blocky voxels.
ModeProperty
public VoxelTool.ModeEnum Mode { get; set; }Sets how do_* functions will behave. This may vary depending on the channel.
SdfScaleProperty
public float SdfScale { get; set; }When working with smooth voxels, applies a scale to the signed distance field. A high scale (1 or higher) will tend to produce blocky results, and a low scale (below 1, but not too close to zero) will tend to be smoother.
This is related to the VoxelBuffer.Depth configuration on voxels. For 8-bit and 16-bit, there is a limited range of values the Signed Distance Field can take, and by default it is clamped to -1..1, so the gradient can only range across 2 voxels. But when LOD is used, it is better to stretch that range over a longer distance, and this is achieved by scaling SDF values.
SdfStrengthProperty
public float SdfStrength { get; set; }When editing VoxelBuffer.ChannelId.ChannelSdf of smooth Terrains in VoxelTool.ModeEnum.Add or VoxelTool.ModeEnum.Remove, determines the interpolation phase between current values and values set by the tool. Can be interpreted as the amount of "matter" added or subtracted.
TextureFalloffProperty
public float TextureFalloff { get; set; }Range VoxelTool.ModeEnum.TexturePaint. Lower values produce sharper transitions. Can be compared to brush softness in an image editing program. This is only relevant with smooth voxels and texturing modes that support long gradients.
TextureIndexProperty
public int TextureIndex { get; set; }Index of the texture used in smooth voxel texture painting mode. The choice of this index depends on the way you setup rendering of textured voxel meshes (for example, layer index in a texture array).
TextureOpacityProperty
public float TextureOpacity { get; set; }Range VoxelTool.TextureIndex when tool is set to VoxelTool.ModeEnum.TexturePaint. Can be compared to brush opacity in an image editing program.
ValueProperty
public ulong Value { get; set; }Sets which voxel value will be used. This is not relevant when editing VoxelBuffer.ChannelId.ChannelSdf.
Methods47
ColorToU16int
public static int ColorToU16(Color color);Encodes normalized 4-float color into 16-bit integer data. It is used with the COLOR channel, in cases where the channel represents direct colors (without using a palette).
| Name | Type | Default |
|---|---|---|
color | Color | — |
intColorToU16Weightsint
public static int ColorToU16Weights(Color _UnnamedArg0);Encodes normalized 4-float color into 16-bit integer data, for use with the WEIGHTS channel.
| Name | Type | Default |
|---|---|---|
_UnnamedArg0 | Color | — |
intColorToU32uint
public static uint ColorToU32(Color color);Encodes normalized 4-float color into 32-bit integer data, for use with the COLOR channel.
| Name | Type | Default |
|---|---|---|
color | Color | — |
uintCopyvoid
public void Copy(Vector3I srcPos, VoxelBuffer dstBuffer, int channelsMask = 255, bool withMetadata = true);Copies voxels in a box and stores them in the passed buffer. The source format will overwrite the destination.
src_pos is the lowest corner of the box, and its size is determined by the size of dst_buffer.
channels_mask is a bitmask where each bit tells which channels will be copied. Example: 1 << VoxelBuffer.CHANNEL_SDF to get only SDF data. Use VoxelBuffer.ChannelMask.AllChannelsMask if you want them all.
| Name | Type | Default |
|---|---|---|
srcPos | Vector3I | — |
dstBuffer | VoxelBuffer | — |
channelsMask | int | 255 |
withMetadata | bool | true |
DoBoxvoid
public void DoBox(Vector3I begin, Vector3I end);Operate on a rectangular cuboid section of the terrain.
With blocky voxels, begin and end are inclusive.
With smooth voxels, end is exclusive.
You may choose which operation to do before calling this function, by setting VoxelTool.Mode. With blocky voxels, you may also set VoxelTool.Value to choose which voxel ID to use.
| Name | Type | Default |
|---|---|---|
begin | Vector3I | — |
end | Vector3I | — |
DoMeshvoid
public void DoMesh(VoxelMeshSdf meshSdf, Transform3D transform, float isolevel = 0f);Applies the given mesh shape to the terrain. The model must be baked into a signed distance field with VoxelMeshSdf. transform may be used to position, rotate and scale the model. Non-uniform scale might introduce artifacts. isolevel is a distance that inflates or contracts the model if increased or decreased respectively. Quality depends on resolution of the model's SDF, and performance will be lower than primitive shapes such as VoxelTool.DoSphere.
| Name | Type | Default |
|---|---|---|
meshSdf | VoxelMeshSdf | — |
transform | Transform3D | — |
isolevel | float | 0f |
DoPath2 overloads
public void DoPath(Vector3[] points, float[] radii);Traces a "tube" defined by a list of points, each having a corresponding radius to control the width of the tube at each point. The begin and end of the path is rounded. This is equivalent to placing/carving multiple connected capsules with varying top/bottom radius. The path is not using bezier or splines, each point is connected linearly to the next. If you need more smoothness, you may add points to areas that need them. The more points, the slower it is.
| Name | Type | Default |
|---|---|---|
points | Vector3[] | — |
radii | float[] | — |
public void DoPath(ReadOnlySpan<Vector3> points, ReadOnlySpan<float> radii);Traces a "tube" defined by a list of points, each having a corresponding radius to control the width of the tube at each point. The begin and end of the path is rounded. This is equivalent to placing/carving multiple connected capsules with varying top/bottom radius. The path is not using bezier or splines, each point is connected linearly to the next. If you need more smoothness, you may add points to areas that need them. The more points, the slower it is.
| Name | Type | Default |
|---|---|---|
points | ReadOnlySpan<Vector3> | — |
radii | ReadOnlySpan<float> | — |
DoPointvoid
public void DoPoint(Vector3I pos);Operates on a single voxel.
You may choose which operation to do before calling this function, by setting VoxelTool.Mode. With blocky voxels, you may also set VoxelTool.Value to choose which voxel ID to use.
This function is not well adapted to smooth voxels, and can introduce blockyness.
| Name | Type | Default |
|---|---|---|
pos | Vector3I | — |
DoSpherevoid
public void DoSphere(Vector3 center, float radius);Operate on voxels within a sphere.
You may choose which operation to do before calling this function, by setting VoxelTool.Mode. With blocky voxels, you may also set VoxelTool.Value to choose which voxel ID to use.
| Name | Type | Default |
|---|---|---|
center | Vector3 | — |
radius | float | — |
GetChannelVoxelBuffer.ChannelId
public VoxelBuffer.ChannelId GetChannel();VoxelBuffer.ChannelIdGetEraserValueulong
public ulong GetEraserValue();ulongGetModeVoxelTool.ModeEnum
public VoxelTool.ModeEnum GetMode();VoxelTool.ModeEnumGetSdfScalefloat
public float GetSdfScale();floatGetSdfStrengthfloat
public float GetSdfStrength();floatGetTextureFallofffloat
public float GetTextureFalloff();floatGetTextureIndexint
public int GetTextureIndex();intGetTextureOpacityfloat
public float GetTextureOpacity();floatGetValueulong
public ulong GetValue();ulongGetVoxelulong
public ulong GetVoxel(Vector3I pos);Gets data from voxel at pos coordinates. The returned value will be an unsigned integer. The meaning of the value depends on VoxelTool.Channel the tool is set to.
When using VoxelBuffer.ChannelId.ChannelSdf for smooth voxels, the returned value will be an encoded value, so you may use VoxelTool.GetVoxelF to get a float value instead.
| Name | Type | Default |
|---|---|---|
pos | Vector3I | — |
ulongGetVoxelFfloat
public float GetVoxelF(Vector3I pos);Gets data from voxel at pos coordinates, interpreting it as a floating-point SDF value. This is recommended to query the VoxelBuffer.ChannelId.ChannelSdf channel for smooth voxels.
| Name | Type | Default |
|---|---|---|
pos | Vector3I | — |
floatGetVoxelMetadataVariant
public Variant GetVoxelMetadata(Vector3I pos);Gets arbitrary data attached to a specific voxel.
| Name | Type | Default |
|---|---|---|
pos | Vector3I | — |
VariantGrowSpherevoid
public void GrowSphere(Vector3 sphereCenter, float sphereRadius, float strength);Adds/removes (depending on VoxelTool.Mode) a value to/from all voxels within a sphere. Added/removed value will be equal to strength at the center of the sphere, and decreases linearly to zero at the surface of the sphere. Voxels outside the sphere will not be affected.
sphere_center is position in the terrain that will be smoothed out.
sphere_radius radius of a sphere from the center where voxel values will be affected. Should be greater than zero.
strength value that controls maximal value that will be added/removed to/from voxels. Suggested range is [0, 10].
Note 1: This is currently implemented only for terrain that uses SDF data (smooth voxels).
Note 2: This is meant to be analogous to Surface tool from Unreal Engine Voxel Plugin.
Note 3: This method assumes terrain SDF is coherent. If it isn't, you may notice differences in speeds at which terrain erodes or grows. For example, some generators fallback to a constant SDF when far away from the surface in order to speed up calculations (see VoxelGeneratorGraph.SdfClipThreshold).
Note 4: If you want to use this method to dig terrain "smoothly" by calling it every frame, an alternative is to use VoxelTool.DoSphere but instead of the center being on the surface, move it back by about 0.95% of the radius so only a small fraction of the sphere will penetrate, digging a hole progressively.
| Name | Type | Default |
|---|---|---|
sphereCenter | Vector3 | — |
sphereRadius | float | — |
strength | float | — |
IsAreaEditablebool
public bool IsAreaEditable(Aabb box);Returns true if the specified voxel area can be edited. This can also be interpreted as the area being "loaded". Note: when using LOD, only the nearest LOD (0) is editable. Other factors can influence whether an area is editable or not, such as streaming mode or terrain bounds.
| Name | Type | Default |
|---|---|---|
box | Aabb | — |
boolNormalizeColorColor
public static Color NormalizeColor(Color _UnnamedArg0);A helper method to set the sum of channels of the Color to 1.
| Name | Type | Default |
|---|---|---|
_UnnamedArg0 | Color | — |
ColorPastevoid
public void Paste(Vector3I dstPos, VoxelBuffer srcBuffer, int channelsMask = 255);Paste voxels in a box from the given buffer at a specific location.
dst_pos is the lowest corner of the box, and its size is determined by the size of src_buffer.
channels_mask is a bitmask where each bit tells which channels will be modified. Example: 1 << VoxelBuffer.CHANNEL_SDF only write SDF data. Use VoxelBuffer.ChannelMask.AllChannelsMask if you want them all.
| Name | Type | Default |
|---|---|---|
dstPos | Vector3I | — |
srcBuffer | VoxelBuffer | — |
channelsMask | int | 255 |
PasteMaskedvoid
public void PasteMasked(Vector3I dstPos, VoxelBuffer srcBuffer, int channelsMask, int maskChannel, long maskValue);Paste voxels in a box from the given buffer at a specific location. Voxels having a specific value in a mask channel will not be pasted.
dst_pos is the lowest corner of the box, and its size is determined by the size of src_buffer.
channels_mask is a bitmask where each bit tells which channels will be modified. Example: 1 << VoxelBuffer.CHANNEL_SDF only write SDF data. Use VoxelBuffer.ChannelMask.AllChannelsMask if you want them all.
src_mask_channel channel from the source buffer that will be used to lookup mask values.
src_mask_value if voxels of the source buffer have this value in the channel specified for masking, then they won't be pasted.
| Name | Type | Default |
|---|---|---|
dstPos | Vector3I | — |
srcBuffer | VoxelBuffer | — |
channelsMask | int | — |
maskChannel | int | — |
maskValue | long | — |
PasteMaskedWritableList2 overloads
public void PasteMaskedWritableList(Vector3I position, VoxelBuffer voxels, byte channelsMask, byte srcMaskChannel, ulong srcMaskValue, byte dstMaskChannel, int[] dstWritableList);Paste voxels in a box from the given buffer at a specific location. Voxels having a specific value in a mask channel of the source buffer will not be pasted, and existing voxels at the destination will only be modified if they have specific values.
dst_pos is the lowest corner of the box, and its size is determined by the size of src_buffer.
channels_mask is a bitmask where each bit tells which channels will be modified. Example: 1 << VoxelBuffer.CHANNEL_SDF only write SDF data. Use VoxelBuffer.ChannelMask.AllChannelsMask if you want them all.
src_mask_channel channel from the source buffer that will be used to lookup mask values.
src_mask_value if voxels of the source buffer have this value in the channel specified for masking, then they won't be pasted.
dst_mask_channel channel from the destination that will be used to select writable voxels.
dst_writable_list List of values the destination voxels must have in order to be written to. Values in that list must be between 0 and 65535. A very large amount of values can also affect performance.
| Name | Type | Default |
|---|---|---|
position | Vector3I | — |
voxels | VoxelBuffer | — |
channelsMask | byte | — |
srcMaskChannel | byte | — |
srcMaskValue | ulong | — |
dstMaskChannel | byte | — |
dstWritableList | int[] | — |
public void PasteMaskedWritableList(Vector3I position, VoxelBuffer voxels, byte channelsMask, byte srcMaskChannel, ulong srcMaskValue, byte dstMaskChannel, ReadOnlySpan<int> dstWritableList);Paste voxels in a box from the given buffer at a specific location. Voxels having a specific value in a mask channel of the source buffer will not be pasted, and existing voxels at the destination will only be modified if they have specific values.
dst_pos is the lowest corner of the box, and its size is determined by the size of src_buffer.
channels_mask is a bitmask where each bit tells which channels will be modified. Example: 1 << VoxelBuffer.CHANNEL_SDF only write SDF data. Use VoxelBuffer.ChannelMask.AllChannelsMask if you want them all.
src_mask_channel channel from the source buffer that will be used to lookup mask values.
src_mask_value if voxels of the source buffer have this value in the channel specified for masking, then they won't be pasted.
dst_mask_channel channel from the destination that will be used to select writable voxels.
dst_writable_list List of values the destination voxels must have in order to be written to. Values in that list must be between 0 and 65535. A very large amount of values can also affect performance.
| Name | Type | Default |
|---|---|---|
position | Vector3I | — |
voxels | VoxelBuffer | — |
channelsMask | byte | — |
srcMaskChannel | byte | — |
srcMaskValue | ulong | — |
dstMaskChannel | byte | — |
dstWritableList | ReadOnlySpan<int> | — |
RaycastVoxelRaycastResult
public VoxelRaycastResult Raycast(Vector3 origin, Vector3 direction, float maxDistance = 10f, uint collisionMask = 4294967295);Runs a voxel-based raycast to find the first hit from an origin and a direction. Coordinates are in world space.
Returns a result object if a voxel got hit, otherwise returns null.
This is useful when colliders cannot be relied upon. It might also be faster (at least at short range), and is more precise to find which voxel is hit. It internally uses the DDA algorithm.
collision_mask is currently only used with blocky voxels. It is combined with VoxelBlockyModel.CollisionMask to decide which voxel types the ray can collide with.
| Name | Type | Default |
|---|---|---|
origin | Vector3 | — |
direction | Vector3 | — |
maxDistance | float | 10f |
collisionMask | uint | 4294967295 |
VoxelRaycastResultSetChannelvoid
public void SetChannel(VoxelBuffer.ChannelId v);| Name | Type | Default |
|---|---|---|
v | VoxelBuffer.ChannelId | — |
SetEraserValuevoid
public void SetEraserValue(ulong v);| Name | Type | Default |
|---|---|---|
v | ulong | — |
SetModevoid
public void SetMode(VoxelTool.ModeEnum m);| Name | Type | Default |
|---|---|---|
m | VoxelTool.ModeEnum | — |
SetRaycastNormalEnabledvoid
public void SetRaycastNormalEnabled(bool enabled);Sets whether VoxelTool.Raycast will compute hit normals. This is true by default.
| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetSdfScalevoid
public void SetSdfScale(float scale);| Name | Type | Default |
|---|---|---|
scale | float | — |
SetSdfStrengthvoid
public void SetSdfStrength(float strength);| Name | Type | Default |
|---|---|---|
strength | float | — |
SetTextureFalloffvoid
public void SetTextureFalloff(float falloff);| Name | Type | Default |
|---|---|---|
falloff | float | — |
SetTextureIndexvoid
public void SetTextureIndex(int index);| Name | Type | Default |
|---|---|---|
index | int | — |
SetTextureOpacityvoid
public void SetTextureOpacity(float opacity);| Name | Type | Default |
|---|---|---|
opacity | float | — |
SetValuevoid
public void SetValue(ulong v);| Name | Type | Default |
|---|---|---|
v | ulong | — |
SetVoxelvoid
public void SetVoxel(Vector3I pos, ulong v);Sets the raw integer value of a specific voxel on the current channel.
| Name | Type | Default |
|---|---|---|
pos | Vector3I | — |
v | ulong | — |
SetVoxelFvoid
public void SetVoxelF(Vector3I pos, float v);Sets the signed distance field (SDF) value a specific voxel. This should preferably be used on the SDF channel.
| Name | Type | Default |
|---|---|---|
pos | Vector3I | — |
v | float | — |
SetVoxelMetadatavoid
public void SetVoxelMetadata(Vector3I pos, Variant meta);Attaches arbitrary data on a specific voxel. Old data is replaced. Passing null will erase metadata.
If the underlying voxels can be saved, this metadata will also be saved, so make sure the data supports serialization (i.e you can't put nodes or arbitrary objects in it).
| Name | Type | Default |
|---|---|---|
pos | Vector3I | — |
meta | Variant | — |
SmoothSpherevoid
public void SmoothSphere(Vector3 sphereCenter, float sphereRadius, int blurRadius);Smoothens out terrain by performing box blur in a spherical area. Strength will be maximal at the center of the sphere, and decreases linearly to zero at the surface of the sphere. Voxels outside the sphere will not be affected.
sphere_center is position in the terrain that will be smoothed out.
sphere_radius radius of a sphere from the center where voxel values will be affected. Should be greater than zero.
blur_radius half the box blur length that will be sampled to calculate average voxel values. Higher values results in more aggressive smoothing. Should be at least 1.
Note 1: This is currently implemented only for terrain that uses SDF data (smooth voxels).
Note 2: Beware of using high sphere_radius and high blur_radius as the performance can drop quickly if this is called 60 times a second.
| Name | Type | Default |
|---|---|---|
sphereCenter | Vector3 | — |
sphereRadius | float | — |
blurRadius | int | — |
U16IndicesToVec4IVector4I
public static Vector4I U16IndicesToVec4I(int _UnnamedArg0);Decodes raw voxel integer data from the INDICES channel into a 4-integer vector.
| Name | Type | Default |
|---|---|---|
_UnnamedArg0 | int | — |
Vector4IU16WeightsToColorColor
public static Color U16WeightsToColor(int _UnnamedArg0);Decodes raw voxel integer data from the WEIGHTS channel into a normalized 4-float color.
| Name | Type | Default |
|---|---|---|
_UnnamedArg0 | int | — |
ColorVec4IToU16Indicesint
public static int Vec4IToU16Indices(Vector4I _UnnamedArg0);Encodes a 4-integer vector into 16-bit integer voxel data, for use in the INDICES channel.
| Name | Type | Default |
|---|---|---|
_UnnamedArg0 | Vector4I | — |
intVoxelTool.ModeEnum
| Value | Description |
|---|---|
Add = 0 | When editing |
Remove = 1 | When editing |
Set = 2 | Replace voxel values without any blending. Useful for blocky voxels. |
TexturePaint = 3 | When editing |