Table of Contents

Class VoxelVolumeBase

Namespace
VoxelPlayground.Engine
Assembly
VoxEngineCore.dll

Base class for voxel volumes, managing voxel data, coordinates, and basic operations.

public abstract class VoxelVolumeBase : MonoBehaviour
Inheritance
object
VoxelVolumeBase
Derived
Extension Methods

Fields

chunkCountX

Settings header.

[Header("--------------------Settings-----------------")]
public int chunkCountX

Field Value

int

chunkCountY

Number of chunks along the Y axis.

public int chunkCountY

Field Value

int

chunkCountZ

Number of chunks along the Z axis.

public int chunkCountZ

Field Value

int

cubeCountsPerAxisInAChunk

Number of voxels per axis in a single chunk.

public int3 cubeCountsPerAxisInAChunk

Field Value

int3

loadingState

The current loading state of the volume.

public VolumeLoadingState loadingState

Field Value

VolumeLoadingState

maxLength

The maximum length among the dimensions.

protected int maxLength

Field Value

int

numPointsX

Total number of points along the X axis.

[HideInInspector]
public int numPointsX

Field Value

int

numPointsY

Total number of points along the Y axis.

[HideInInspector]
public int numPointsY

Field Value

int

numPointsYMX

Precalculated value of numPointsY * numPointsX.

[HideInInspector]
public int numPointsYMX

Field Value

int

numPointsZ

Total number of points along the Z axis.

[HideInInspector]
public int numPointsZ

Field Value

int

originalSolidBlockCount

The original count of solid blocks when loaded.

public int originalSolidBlockCount

Field Value

int

pointData

The raw voxel data array.

protected NativeArray<PointDataV2> pointData

Field Value

NativeArray<PointDataV2>

pointDataPtr

Pointer to the raw voxel data.

protected PointDataV2* pointDataPtr

Field Value

PointDataV2*

solidBlockCount

The current count of solid blocks in the volume.

public int solidBlockCount

Field Value

int

Properties

trans

Cached Transform component.

public Transform trans { get; }

Property Value

Transform

Methods

CalculateAccurateBounds(float)

Calculates the accurate bounds of the solid voxels in world space.

public Bounds CalculateAccurateBounds(float extend = 0)

Parameters

extend float

Amount to extend the bounds.

Returns

Bounds

The calculated bounds.

CalculateRadii(float, out float)

Calculates the scaled radius.

public void CalculateRadii(float radius, out float scaledRadius)

Parameters

radius float

The original radius.

scaledRadius float

Output scaled radius.

CalculateRadiiSqr(float, out float, out float, out float)

Calculates squared radii for spherical operations, accounting for lossy scale.

public void CalculateRadiiSqr(float radius, out float scaledRadiusSqr, out float innerRadiusSqr, out float diff)

Parameters

radius float

The radius.

scaledRadiusSqr float

Output squared scaled radius.

innerRadiusSqr float

Output squared inner radius.

diff float

Output difference between squared radii.

ClampToLocalFull(Vector3)

Clamps a local position to be within the volume boundaries.

public Vector3 ClampToLocalFull(Vector3 local)

Parameters

local Vector3

The local position to clamp.

Returns

Vector3

The clamped local position.

ClosestPoint(Vector3, out Vector3, out Vector3[])

Finds the closest point on the surface of the voxel volume to the given world position.

public bool ClosestPoint(Vector3 worldPos, out Vector3 closest, out Vector3[] normals)

Parameters

worldPos Vector3

The query world position.

closest Vector3

The closest point found.

normals Vector3[]

The normals at the closest point.

Returns

bool

True if a closest point was found, false otherwise.

CoordFromIndex(int)

Calculates the local coordinates from a linear index.

public Vector3 CoordFromIndex(int voxelIndex)

Parameters

voxelIndex int

The linear index.

Returns

Vector3

The local coordinates.

FillVoxel(PointDataV2)

Fills the entire volume with the specified voxel data.

public void FillVoxel(PointDataV2 data)

Parameters

data PointDataV2

The voxel data to fill with.

FindAnyPointDataLocal(Vector3, out int, int)

Finds any solid voxel near the local position within a given size.

public Vector3 FindAnyPointDataLocal(Vector3 localPos, out int index, int size = 4)

Parameters

localPos Vector3

The center local position.

index int

The index of the found voxel.

size int

The search size.

Returns

Vector3

The position of the found solid voxel, or the original position if none found.

GetBounds_LocalSpace()

Gets the bounds of the volume in local space.

public Bounds GetBounds_LocalSpace()

Returns

Bounds

The local bounds.

GetBounds_WorldSpace()

Gets the bounds of the volume in world space.

public Bounds GetBounds_WorldSpace()

Returns

Bounds

The world bounds.

GetCenterPos_WorldSpace()

Gets the center position of the volume in world space.

public Vector3 GetCenterPos_WorldSpace()

Returns

Vector3

The world center position.

GetColor_world(Vector3)

Gets the color of the voxel at the specified world position.

public Color GetColor_world(Vector3 worldPos)

Parameters

worldPos Vector3

The world position.

Returns

Color

The color of the voxel.

GetData_local(int, int, int)

Gets the voxel data at the specified local coordinates (unsafe).

public PointDataV2 GetData_local(int lcoalx, int localy, int localz)

Parameters

lcoalx int

Local X coordinate.

localy int

Local Y coordinate.

localz int

Local Z coordinate.

Returns

PointDataV2

The voxel data.

GetData_local(int3)

Gets the voxel data at the specified local coordinates.

public PointDataV2 GetData_local(int3 localpos)

Parameters

localpos int3

The local coordinates.

Returns

PointDataV2

The voxel data.

GetData_world(Vector3)

Gets the voxel data at the specified world position.

public PointDataV2 GetData_world(Vector3 worldPos)

Parameters

worldPos Vector3

The world position.

Returns

PointDataV2

The voxel data.

GetPointDataArray()

Gets the native array of point data.

public NativeArray<PointDataV2> GetPointDataArray()

Returns

NativeArray<PointDataV2>

The native array of PointDataV2.

GetPointDataArrayPtr()

Gets the unsafe pointer to the point data array.

public PointDataV2* GetPointDataArrayPtr()

Returns

PointDataV2*

A pointer to PointDataV2.

GetTopMostSolidVoxel(Vector3)

Gets the top-most solid voxel starting from a given position and searching upwards.

public Vector3 GetTopMostSolidVoxel(Vector3 start)

Parameters

start Vector3

The starting world position.

Returns

Vector3

The world position of the top-most solid voxel.

GetTotalNumOfPoints()

Gets the total number of points in the volume.

public int GetTotalNumOfPoints()

Returns

int

The total number of points.

GetVoxelDataCenter_WorldSpace()

Calculates the center position of all solid voxels in world space.

public Vector3? GetVoxelDataCenter_WorldSpace()

Returns

Vector3?

The center position, or null if no solid voxels found.

IndexFromChunkCoord_boundaryCheck(int, int, int)

Calculates the linear index of a chunk with boundary checking.

public int IndexFromChunkCoord_boundaryCheck(int chunkX, int chunkY, int chunkZ)

Parameters

chunkX int

X coordinate of the chunk.

chunkY int

Y coordinate of the chunk.

chunkZ int

Z coordinate of the chunk.

Returns

int

The linear index, or -1 if out of bounds.

IndexFromChunkCoord(int, int, int)

Calculates the linear index of a chunk from its 3D coordinates.

public int IndexFromChunkCoord(int chunkX, int chunkY, int chunkZ)

Parameters

chunkX int

X coordinate of the chunk.

chunkY int

Y coordinate of the chunk.

chunkZ int

Z coordinate of the chunk.

Returns

int

The linear index.

IndexFromCoord_WithBoundaryCheck(int, int, int)

Gets the linear index from coordinates with boundary checking.

public int IndexFromCoord_WithBoundaryCheck(int x, int y, int z)

Parameters

x int

X coordinate.

y int

Y coordinate.

z int

Z coordinate.

Returns

int

The linear index, or -1 if out of bounds.

IndexFromCoord(int, int, int)

Gets the linear index from integer coordinates.

public int IndexFromCoord(int x, int y, int z)

Parameters

x int

X coordinate.

y int

Y coordinate.

z int

Z coordinate.

Returns

int

The linear index.

IndexFromCoord(int3)

Gets the linear index from integer local coordinates.

public int IndexFromCoord(int3 local)

Parameters

local int3

The local coordinates.

Returns

int

The linear index.

IndexFromCoord(Vector3)

Gets the linear index from a local position.

public int IndexFromCoord(Vector3 local)

Parameters

local Vector3

The local position.

Returns

int

The linear index.

IsInsideBoundaries_local_full(int, int, int)

Checks if integer local coordinates are inside the volume boundaries (full check).

public bool IsInsideBoundaries_local_full(int localx, int localy, int localz)

Parameters

localx int

Local X coordinate.

localy int

Local Y coordinate.

localz int

Local Z coordinate.

Returns

bool

True if inside, false otherwise.

IsInsideBoundaries_local_full(int3)

Checks if integer local coordinates are inside the volume boundaries (full check).

public bool IsInsideBoundaries_local_full(int3 local)

Parameters

local int3

The local coordinates.

Returns

bool

True if inside, false otherwise.

IsInsideBoundaries_local_full(Vector3)

Checks if a local position is inside the volume boundaries (full check).

public bool IsInsideBoundaries_local_full(Vector3 local)

Parameters

local Vector3

The local position.

Returns

bool

True if inside, false otherwise.

IsInsideBoundaries_local(int, int, int)

Checks if integer local coordinates are inside the volume boundaries.

public bool IsInsideBoundaries_local(int localx, int localy, int localz)

Parameters

localx int

Local X coordinate.

localy int

Local Y coordinate.

localz int

Local Z coordinate.

Returns

bool

True if inside, false otherwise.

IsInsideBoundaries_local(int3)

Checks if integer local coordinates are inside the volume boundaries.

public bool IsInsideBoundaries_local(int3 local)

Parameters

local int3

The local coordinates.

Returns

bool

True if inside, false otherwise.

IsInsideBoundaries_local(Vector3)

Checks if a local position is inside the volume boundaries.

public bool IsInsideBoundaries_local(Vector3 local)

Parameters

local Vector3

The local position.

Returns

bool

True if inside, false otherwise.

IsInsideBoundaries(Vector3)

Checks if a world position is inside the volume boundaries.

public bool IsInsideBoundaries(Vector3 worldPos)

Parameters

worldPos Vector3

The world position.

Returns

bool

True if inside, false otherwise.

IsInsideBounds_chunk(int, int, int)

Checks if chunk coordinates are within the valid range.

public bool IsInsideBounds_chunk(int chunkx, int chunky, int chunkz)

Parameters

chunkx int

Chunk X coordinate.

chunky int

Chunk Y coordinate.

chunkz int

Chunk Z coordinate.

Returns

bool

True if valid, false otherwise.

IsSurfaceVoxel(Vector3, bool)

Checks if a voxel at the given local position is a surface voxel (has at least one empty neighbor).

public bool IsSurfaceVoxel(Vector3 voxelLocalPos, bool checkAllDir = true)

Parameters

voxelLocalPos Vector3

The local position of the voxel.

checkAllDir bool

If true, checks all 6 directions; otherwise checks only +Y.

Returns

bool

True if it is a surface voxel, false otherwise.

IsVoxelSolid_WorldSpace(Vector3)

Checks if the voxel at the specified world position is solid.

public bool IsVoxelSolid_WorldSpace(Vector3 WorldPos)

Parameters

WorldPos Vector3

The world position.

Returns

bool

True if solid, false otherwise.

IsVoxelSolid(int, int, int)

Checks if the voxel at the specified local coordinates is solid.

public bool IsVoxelSolid(int lcoalx, int localy, int localz)

Parameters

lcoalx int

Local X coordinate.

localy int

Local Y coordinate.

localz int

Local Z coordinate.

Returns

bool

True if solid, false otherwise.

LoadDataOnly(IVoxelVolumeSaveData)

Loads only the data from the provided save data, without generating meshes immediately.

public virtual void LoadDataOnly(IVoxelVolumeSaveData cubeData)

Parameters

cubeData IVoxelVolumeSaveData

The save data interface.

LocalPosToChunkPos(Vector3)

Calculates the chunk coordinates from a local position.

public Vector3Int LocalPosToChunkPos(Vector3 localPos)

Parameters

localPos Vector3

The local position.

Returns

Vector3Int

The chunk coordinates.

LocalPosToChunkPos(Vector3Int)

Calculates the chunk coordinates from an integer local position.

public Vector3Int LocalPosToChunkPos(Vector3Int localPos)

Parameters

localPos Vector3Int

The integer local position.

Returns

Vector3Int

The chunk coordinates.

OnDataModified(Vector3)

Called when data is modified at a local position.

protected virtual void OnDataModified(Vector3 localPos)

Parameters

localPos Vector3

The local position where data was modified.

RaycastVoxels(Vector3, Vector3, float, out RaycastHitVoxel)

Performs a detailed raycast against the voxel volume.

public bool RaycastVoxels(Vector3 origin, Vector3 direction, float distance, out RaycastHitVoxel raycastHitVoxel)

Parameters

origin Vector3

The ray origin in world space.

direction Vector3

The ray direction in world space.

distance float

Maximum raycast distance.

raycastHitVoxel RaycastHitVoxel

Output detailed hit information.

Returns

bool

True if a voxel was hit, false otherwise.

RaycastVoxels(Vector3, Vector3, out Vector3, out Vector3, float)

Performs a raycast against the voxel volume.

public bool RaycastVoxels(Vector3 origin, Vector3 direction, out Vector3 hitPoint, out Vector3 hitNormal, float maxDistance = 10)

Parameters

origin Vector3

The ray origin in world space.

direction Vector3

The ray direction in world space.

hitPoint Vector3

Output hit point.

hitNormal Vector3

Output hit normal.

maxDistance float

Maximum raycast distance.

Returns

bool

True if a voxel was hit, false otherwise.

RaycastVoxels(Vector3, Vector3, out RaycastHitVoxel)

Performs a raycast against the voxel volume using the maximum length as distance.

public bool RaycastVoxels(Vector3 origin, Vector3 direction, out RaycastHitVoxel raycastHitVoxel)

Parameters

origin Vector3

The ray origin in world space.

direction Vector3

The ray direction in world space.

raycastHitVoxel RaycastHitVoxel

Output detailed hit information.

Returns

bool

True if a voxel was hit, false otherwise.

Reset()

Resets the volume, disposing of data and resetting state.

public virtual void Reset()

SampleMainPointData(Vector3, int)

Samples the main point data near a world position within a given size.

public PointDataV2 SampleMainPointData(Vector3 worldPos, int size = 4)

Parameters

worldPos Vector3

The world position to sample around.

size int

The search size.

Returns

PointDataV2

The point data of the found solid voxel.

SampleMainPointDataLocal(Vector3, out int, int)

Samples the main point data near a local position within a given size.

public PointDataV2 SampleMainPointDataLocal(Vector3 localPos, out int index, int size = 4)

Parameters

localPos Vector3

The local position to sample around.

index int

The index of the found voxel.

size int

The search size.

Returns

PointDataV2

The point data of the found solid voxel, or empty if none found.

SamplePoint(Vector3, int)

Samples a solid point position near a world position within a given size.

public Vector3 SamplePoint(Vector3 worldPos, int size = 4)

Parameters

worldPos Vector3

The world position to sample around.

size int

The search size.

Returns

Vector3

The world position of a nearby solid voxel, or the transformed result of FindAnyPointDataLocal.

SetCenterPos(Vector3)

Sets the position of the volume such that the center of the voxel grid aligns with the given world position.

public void SetCenterPos(Vector3 worldPos)

Parameters

worldPos Vector3

The target world position for the center.

SetData_local(int, int, int, ref PointDataV2)

Sets the voxel data at specific local coordinates with boundary checks.

public void SetData_local(int localx, int localy, int localz, ref PointDataV2 data)

Parameters

localx int

Local X coordinate.

localy int

Local Y coordinate.

localz int

Local Z coordinate.

data PointDataV2

The new voxel data.

SetData_local(int, ref PointDataV2)

Sets the voxel data at a specific index without boundary checks.

public void SetData_local(int index, ref PointDataV2 data)

Parameters

index int

The linear index.

data PointDataV2

The new voxel data.

SetData_local(Vector3Int, ref PointDataV2)

Sets the voxel data at a specific local position with boundary checks.

public void SetData_local(Vector3Int localpos, ref PointDataV2 data)

Parameters

localpos Vector3Int

Local position.

data PointDataV2

The new voxel data.

SetData_world(Vector3, ref PointDataV2)

Sets the voxel data at a specific world position.

public void SetData_world(Vector3 worldPos, ref PointDataV2 data)

Parameters

worldPos Vector3

The world position.

data PointDataV2

The new voxel data.

SetupMarchingCubeGrid()

Sets up grid dimensions based on chunk counts and size.

protected void SetupMarchingCubeGrid()

TransformBoundsToLocal(Bounds)

Transforms world bounds to local bounds.

public Bounds TransformBoundsToLocal(Bounds worldBounds)

Parameters

worldBounds Bounds

The world bounds.

Returns

Bounds

The local bounds.

WorldPosToLocalPos(Vector3)

Converts a world position to a local position relative to the volume.

public Vector3 WorldPosToLocalPos(Vector3 world)

Parameters

world Vector3

The world position.

Returns

Vector3

The local position.

WorldPosToLocalPosInt(Vector3)

Converts a world position to a rounded integer local position.

public Vector3Int WorldPosToLocalPosInt(Vector3 world)

Parameters

world Vector3

The world position.

Returns

Vector3Int

The integer local position.