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
-
objectVoxelVolumeBase
- 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
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
pointDataPtr
Pointer to the raw voxel data.
protected PointDataV2* pointDataPtr
Field Value
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
Methods
CalculateAccurateBounds(float)
Calculates the accurate bounds of the solid voxels in world space.
public Bounds CalculateAccurateBounds(float extend = 0)
Parameters
extendfloatAmount 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
radiusfloatThe original radius.
scaledRadiusfloatOutput 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
radiusfloatThe radius.
scaledRadiusSqrfloatOutput squared scaled radius.
innerRadiusSqrfloatOutput squared inner radius.
difffloatOutput difference between squared radii.
ClampToLocalFull(Vector3)
Clamps a local position to be within the volume boundaries.
public Vector3 ClampToLocalFull(Vector3 local)
Parameters
localVector3The 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
worldPosVector3The query world position.
closestVector3The closest point found.
normalsVector3[]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
voxelIndexintThe linear index.
Returns
- Vector3
The local coordinates.
FillVoxel(PointDataV2)
Fills the entire volume with the specified voxel data.
public void FillVoxel(PointDataV2 data)
Parameters
dataPointDataV2The 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
localPosVector3The center local position.
indexintThe index of the found voxel.
sizeintThe 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
worldPosVector3The 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
lcoalxintLocal X coordinate.
localyintLocal Y coordinate.
localzintLocal 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
localposint3The 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
worldPosVector3The 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
startVector3The 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
chunkXintX coordinate of the chunk.
chunkYintY coordinate of the chunk.
chunkZintZ 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
chunkXintX coordinate of the chunk.
chunkYintY coordinate of the chunk.
chunkZintZ 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
xintX coordinate.
yintY coordinate.
zintZ 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
xintX coordinate.
yintY coordinate.
zintZ coordinate.
Returns
- int
The linear index.
IndexFromCoord(int3)
Gets the linear index from integer local coordinates.
public int IndexFromCoord(int3 local)
Parameters
localint3The local coordinates.
Returns
- int
The linear index.
IndexFromCoord(Vector3)
Gets the linear index from a local position.
public int IndexFromCoord(Vector3 local)
Parameters
localVector3The 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
localxintLocal X coordinate.
localyintLocal Y coordinate.
localzintLocal 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
localint3The 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
localVector3The 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
localxintLocal X coordinate.
localyintLocal Y coordinate.
localzintLocal 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
localint3The 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
localVector3The 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
worldPosVector3The 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
chunkxintChunk X coordinate.
chunkyintChunk Y coordinate.
chunkzintChunk 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
voxelLocalPosVector3The local position of the voxel.
checkAllDirboolIf 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
WorldPosVector3The 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
lcoalxintLocal X coordinate.
localyintLocal Y coordinate.
localzintLocal 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
cubeDataIVoxelVolumeSaveDataThe save data interface.
LocalPosToChunkPos(Vector3)
Calculates the chunk coordinates from a local position.
public Vector3Int LocalPosToChunkPos(Vector3 localPos)
Parameters
localPosVector3The local position.
Returns
- Vector3Int
The chunk coordinates.
LocalPosToChunkPos(Vector3Int)
Calculates the chunk coordinates from an integer local position.
public Vector3Int LocalPosToChunkPos(Vector3Int localPos)
Parameters
localPosVector3IntThe 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
localPosVector3The 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
originVector3The ray origin in world space.
directionVector3The ray direction in world space.
distancefloatMaximum raycast distance.
raycastHitVoxelRaycastHitVoxelOutput 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
originVector3The ray origin in world space.
directionVector3The ray direction in world space.
hitPointVector3Output hit point.
hitNormalVector3Output hit normal.
maxDistancefloatMaximum 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
originVector3The ray origin in world space.
directionVector3The ray direction in world space.
raycastHitVoxelRaycastHitVoxelOutput 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
worldPosVector3The world position to sample around.
sizeintThe 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
localPosVector3The local position to sample around.
indexintThe index of the found voxel.
sizeintThe 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
worldPosVector3The world position to sample around.
sizeintThe 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
worldPosVector3The 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
localxintLocal X coordinate.
localyintLocal Y coordinate.
localzintLocal Z coordinate.
dataPointDataV2The 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
indexintThe linear index.
dataPointDataV2The 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
localposVector3IntLocal position.
dataPointDataV2The 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
worldPosVector3The world position.
dataPointDataV2The 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
worldBoundsBoundsThe 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
worldVector3The 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
worldVector3The world position.
Returns
- Vector3Int
The integer local position.